From 4e8416e99bed74ac413c88884d477b09dc0ebae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Poisson?= Date: Tue, 6 Jul 2021 17:29:05 +0200 Subject: [PATCH] feat: jsdoc type generation --- angular-app/angular.json | 3 +++ angular-app/package.json | 8 +++++--- angular-app/src/app/app.component.ts | 12 ++++++------ backend/.gitignore | 1 + backend/package.json | 9 ++++++++- backend/{ => src}/index.js | 6 +++++- backend/src/mongodb.js | 24 +++++++++++++++++++----- backend/tsconfig.json | 11 +++++++++++ backend/warp.config.js | 10 +++++++--- 9 files changed, 65 insertions(+), 19 deletions(-) rename backend/{ => src}/index.js (56%) create mode 100644 backend/tsconfig.json diff --git a/angular-app/angular.json b/angular-app/angular.json index 6f967d5..1b637ea 100644 --- a/angular-app/angular.json +++ b/angular-app/angular.json @@ -1,5 +1,8 @@ { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "cli": { + "analytics": false + }, "version": 1, "newProjectRoot": "projects", "projects": { diff --git a/angular-app/package.json b/angular-app/package.json index 042b1c0..3217528 100644 --- a/angular-app/package.json +++ b/angular-app/package.json @@ -4,13 +4,15 @@ "scripts": { "postinstall": "cd ../backend/ && npm install", "ng": "ng", + "prestart": "warp project select && cd ../backend/ && npm run ts-build", + "start:ts-watch": "cd ../backend/ && npm run ts-watch", "start:client": "ng serve", "start:server": "warp dev ../backend/", "start": "run-p start:*", + "ts-build": "cd ../backend/ && npm run ts-build", "build:client": "ng build", "build:server": "warp build ../backend/", "build": "run-s build:server build:client", - "predeploy": "warp project select", "deploy": "warp deploy ./ ../backend/", "test": "ng test", "lint": "ng lint", @@ -26,7 +28,7 @@ "@angular/platform-browser": "~11.2.11", "@angular/platform-browser-dynamic": "~11.2.11", "@angular/router": "~11.2.11", - "@warpjs/engine": "^4.0.9", + "@warpjs/engine": "^4.0.11", "rxjs": "~6.6.0", "tslib": "^2.3.0", "zone.js": "~0.11.3" @@ -51,6 +53,6 @@ "ts-node": "~8.3.0", "tslint": "~6.1.0", "typescript": "~4.1.5", - "warp": "^4.0.9" + "warp": "^4.0.11" } } diff --git a/angular-app/src/app/app.component.ts b/angular-app/src/app/app.component.ts index bd0d1f5..343fdf5 100644 --- a/angular-app/src/app/app.component.ts +++ b/angular-app/src/app/app.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit } from '@angular/core'; - import Backend from 'backend'; -const { hello, fetchMovies } = new Backend() as any; + +const backend = new Backend(); @Component({ selector: 'app-root', @@ -14,13 +14,13 @@ export class AppComponent implements OnInit { movies = {}; ngOnInit() { - hello().then((msg: string) => { + backend.hello().then((msg: string) => { this.message = msg; }); // fetchMovies from mongodb - fetchMovies('Star Trek').then( - (data: any) => (this.movies = JSON.stringify(data, null, 2)) - ); + backend.fetchMovies('Star Trek').then((data) => { + this.movies = JSON.stringify(data, null, 2); + }); } } diff --git a/backend/.gitignore b/backend/.gitignore index 75498dc..39be5ca 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -2,6 +2,7 @@ # dependencies node_modules/ +dist/ .warp diff --git a/backend/package.json b/backend/package.json index 28799f3..733e1b5 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,9 +1,16 @@ { "name": "backend", "version": "1.0.0", - "main": "index.js", + "main": "src/index.js", "license": "MIT", + "scripts": { + "ts-watch": "tsc --watch", + "ts-build": "tsc" + }, "dependencies": { "mongodb": "^3.6.6" + }, + "devDependencies": { + "typescript": "^4.3.5" } } diff --git a/backend/index.js b/backend/src/index.js similarity index 56% rename from backend/index.js rename to backend/src/index.js index ecac1b1..d4b5fbd 100644 --- a/backend/index.js +++ b/backend/src/index.js @@ -1,5 +1,9 @@ -const { fetchMovies } = require('./src/mongodb'); +const { fetchMovies } = require("./mongodb"); +/** + * + * @returns {Promise} return a hello from your backend module + */ const hello = () => { return `Hello from ScaleDynamics Platform, MongoDB, Angular and Node.js ${process.version} !`; }; diff --git a/backend/src/mongodb.js b/backend/src/mongodb.js index 55c6f71..ac776cb 100644 --- a/backend/src/mongodb.js +++ b/backend/src/mongodb.js @@ -1,7 +1,16 @@ -const { MongoClient } = require('mongodb'); +const { MongoClient } = require("mongodb"); + +/** + * A movie + * @typedef {Object} Movie + * @property {string} title - Title of the movie + * @property {string} year - Year of release of the movie + * @property {string} plot - Plot of the movie + * @property {string} poster - Link to the poster of the movie + */ // connection URI -const URI = 'mongodb+srv://test:test@movies-scqxj.gcp.mongodb.net/'; +const URI = "mongodb+srv://test:test@movies-scqxj.gcp.mongodb.net/"; // create & connect a new MongoDB client const connection = MongoClient(URI, { @@ -9,6 +18,11 @@ const connection = MongoClient(URI, { useUnifiedTopology: true, }).connect(); +/** + * + * @param {string} search the name of the movies you seek + * @returns {Promise} return an array of movies + */ const fetchMovies = async (search) => { // await database connection const client = await connection.catch((error) => { @@ -16,9 +30,9 @@ const fetchMovies = async (search) => { }); // request database return client - .db('sample_mflix') - .collection('movies') - .find({ poster: { $exists: true }, title: { $regex: search, $options: 'i' } }) + .db("sample_mflix") + .collection("movies") + .find({ poster: { $exists: true }, title: { $regex: search, $options: "i" } }) .project({ _id: 0, title: 1, year: 1, plot: 1, poster: 1 }) .sort({ year: -1 }) .limit(50) diff --git a/backend/tsconfig.json b/backend/tsconfig.json new file mode 100644 index 0000000..622b4e6 --- /dev/null +++ b/backend/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "strict": true, + "allowJs": true, + "declaration": true, + "emitDeclarationOnly": true, + "outDir": "dist", + "moduleResolution": "node" + }, + "include": ["src/**/*.js"] +} diff --git a/backend/warp.config.js b/backend/warp.config.js index f395142..3fe4382 100644 --- a/backend/warp.config.js +++ b/backend/warp.config.js @@ -1,7 +1,11 @@ module.exports = { output: { - format: 'node-module', - projectPath: '../angular-app', - name: 'backend', + format: "node-module", + projectPath: "../angular-app", + name: "backend", + }, + expose: { + source: "src/index.js", + type: "dist/index.d.ts", }, };