![Page 1: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/1.jpg)
Please write the title in all capital letters
MAKE NODEJS APIs GREAT WITH TYPESCRIPT
![Page 2: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/2.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"ABOUT ME
I really like my work, software engineering never makes me bored, always keeps in learning and improving mood.
http://bit.ly/2FAm3Lr
http://bit.ly/2FAm3Lr
![Page 3: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/3.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"NODEJS REPUTATION
NodeJS
FRONT-END DEVELOPERS
![Page 4: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/4.jpg)
4
Put images in the grey dotted box "unsupported placeholder" - behind the orange box (left side stays white)
Write the quote in all capital letters
What’s wrong with NodeJS APIs?
● Dynamic VS strong typing
● Pure OOP model of JS
● No of access modifiers
● Lack of documentation
● No hypermedia in RESTful APIs
● Design and structure
![Page 5: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/5.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"TYPESCRIPT FOR A RESQUE
TYPE SYSTEM INTERFACES
DECORATORS ACCESS MODIFIERS
GENERICS ABSTRACT CLASSES
![Page 6: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/6.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"TYPES TRIPLET
TYPE ALIASESpublic port: number;
INTERFACESprotected server: http.Server;
CLASSESpublic article: Article;
![Page 7: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/7.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"LET’S COMPARE
CLASSES● Reference types only
● Can be extended
● Signature and implementation
TYPE ALISES● Primitive types (number, string,
boolean) and reference types (Object).
● Can’t be extended
INTERFACES● Reference types only
● Can be extended
● Signature not implementation
![Page 8: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/8.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
INTERFACES
interface BaseArticle { SKU: string, name: string, type: string, price: Price}
export default BaseArticle;
interface FashionArticle extends BaseArticle { size: Sizes, color: Colors}
export default FashionArticle;
import { Document } from "mongoose";
interface FashionArticleModel extends FashionArticle, Document {};export default FashionArticleModel;
![Page 9: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/9.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"USING INTERFACES AND GENERICS
import { Schema, Model, model} from "mongoose";import FashionArticleModel from "../interfaces/FashionArticleModel";
const ArticleSchema: Schema = new Schema({ ….});
const ArticleModel: Model<FashionArticleModel> = model<FashionArticleModel>("Article", ArticleSchema);
export {ArticleModel};
![Page 10: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/10.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"MORE INTERFACES
export class Server {
protected app: express.Application;
protected server: http.Server;
private db: mongoose.Connection;
private routes: express.Router[] = [];
}
![Page 11: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/11.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"ACCESS MODIFIERS
PUBLICDefault modifier and can be omitted (better not).
PRIVATERestics members visibility to current class only.
PROTECTEDVisible in derived classes.
![Page 12: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/12.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
IN PRACTICE
class Server { protected app: express.Application; protected server: http.Server; public port: number;
constructor(port: number = 3000) { this.app = express(); this.port = port; this.app.set("port", port); this.app.listen(this.port); }}
import * as io from "socket.io";
class SocketServer extends Server { private socketServer: io.Server;
constructor(public port: number) { super(port); this.socketServer = io(this.server); …. }}
![Page 13: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/13.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
DECORATORS
CLASSES METHODS
PARAMETERS FIELDS
![Page 14: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/14.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: TRY-CATCH WRAPPER
class TestClass {
public doSomething(str: string): boolean { return str.length > 0; }
}
var safeTest: TestClass = new TestClass();safeTest.doSomething(null);safeTest.doSomething("Hello from IJS and API conference");
@safe
![Page 15: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/15.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: TRY-CATCH WRAPPER
function safe(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> {
let originalMethod = descriptor.value; descriptor.value = function () { try { originalMethod.apply(this, arguments); } catch(ex) { console.error(ex); } };
return descriptor;}
![Page 16: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/16.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: DECLARATIVE ROUTES
var express = require('express');var router = express.Router();
router.get('/', function(req, res, next) { res.send('respond with a resource');});
router.get('/:id', function(req, res, next) { res.send('respond with a resource');});
Typical ExpressJS route:
![Page 17: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/17.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: DECLARATIVE ROUTES@RouteHandler("/sample-route")class SampleRoute { public router: Router; constructor(public app: Server) {}
@Get() public resources(req: Request, res: Response) { res.json([]); }
@Get("/:id") public resources(req: Request, res: Response) { res.json({}); }}
![Page 18: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/18.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: REQUEST VALIDATION
@Validate({ param: "name", validate: "required"})public createArticle(request: Request, response: Response): void {
// create an article}
![Page 19: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/19.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: REQUEST VALIDATION
export function Validate(params: Array<any>): any { return (target: Object, propertyKey: string): TypedPropertyDescriptor<any> => { const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey); const originalMethod = descriptor.value;
descriptor.value = function () { const body = arguments[0].body; const response = arguments[1]; // do some work }; return descriptor; };}
![Page 20: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/20.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: HATEOAS
Hypermedia allows to decouple client and server to a large extent and allow them to evolve independently.
![Page 21: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/21.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
EXAMPLE: HYPERMEDIA
DEMO
![Page 22: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/22.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"DOCUMENTATION
TSOA
![Page 23: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/23.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"AUTO SWAGGER DOCUMENTATION WITH DECORATORS
@Route("Users")class ArticlesService {
@Post() public createArticle(@Body() requestBody: FashionArticle): Promise<FashionArticle> {
// create an article }}
export default ArticlesService;
TSOA
![Page 24: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/24.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"AUTO SWAGGER DOCS WITH DECORATORS
![Page 25: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/25.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"UNIT AND INTEGRATION TESTS
SUPERTEST
WRITE YOUR TESTS IN TYPESCRIPT
![Page 26: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/26.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"DEBUG DIRECTLY IN TS
![Page 27: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/27.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
DRAWBACKS
Additional build step.
Lack of definition files for npm modules.
Source and compiled code.
![Page 28: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/28.jpg)
Please write the title in all capital lettersMAKE NODEJS APIs GREAT WITH TYPESCRIPT
![Page 29: MAKE NODEJS APIs GREAT WITH TYPESCRIPT · Please write the title in all capital letters Put images in the grey dotted box "unsupported ABOUT ME placeholder" I really like my work,](https://reader034.vdocument.in/reader034/viewer/2022050717/5e163e3651d94d6f8a25136d/html5/thumbnails/29.jpg)
Please write the title in all capital letters
Put images in the grey dotted box "unsupported placeholder"
THANKS FOR ATTENTION
SlidesNodeJS TypeScript starter http://bit.ly/2FW64qnTSOA auto swagger documentation http://bit.ly/2FW64qn