MAKE NODEJS APIs GREAT WITH TYPESCRIPT

Similar documents
An Introduction to TypeScript. Personal Info

Nick Senger & Jesse van den Kieboom

Online Multimedia Winter semester 2015/16

Chapter 4 Defining Classes I

Introduction to Programming Using Java (98-388)

Backend Development. SWE 432, Fall Web Application Development

ANGULAR 2.X,4.X + TYPESRCIPT by Sindhu

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

TypeScript. Types. CS144: Web Applications

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Express.JS. Prof. Cesare Pautasso Modularity

Advanced React JS + Redux Development

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

Angular 2 Programming

Introduction to TypeScript

Client Side JavaScript and AJAX

Full Stack boot camp

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

#06 RPC & REST CLIENT/SERVER COMPUTING AND WEB TECHNOLOGIES

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

TypeScript coding JavaScript without the pain

Persistence. SWE 432, Fall 2017 Design and Implementation of Software for the Web

And Even More and More C++ Fundamentals of Computer Science

CS193X: Web Programming Fundamentals

Topic 12: Connecting Express and Mongo

Web API Best Practices

Lab 1 - Introduction to Angular

Think like an Elm developer

Tuesday, January 13, Backend III: Node.js with Databases

NODE.JS SERVER SIDE JAVASCRIPT. Introduc)on Node.js

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Data Types and the main Function

Setting up an Angular 4 MEAN Stack (Tutorial)

pistahx type safe, design first, haxe web api by Emmanuel BOTROS YOUSSEF / mebyz

Stencil: The Time for Vanilla Web Components has Arrived

Sessions. Mendel Rosenblum. CS142 Lecture Notes - Sessions

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Advance Mobile& Web Application development using Angular and Native Script

MongoDB. Robert M. Vunabandi

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Introduction to Express.js. CSC309 Feb. 6, 2015 Surya Nallu

Advanced Systems Programming

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Today s Agenda. Quick Review

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

The Object clone() Method

Node.js. Node.js Overview. CS144: Web Applications

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Cross-Platform Mobile-Entwicklung mit dem Ionic Framework

Extra Notes - Data Stores & APIs - using MongoDB and native driver

Lecture 10 Declarations and Scope

Ten interesting features of Google s Angular Project

Vue.js Developer friendly, Fast and Versatile

Introduction to Java

TYPESCRIPT. Presented by Clarke Bowers

Certified Core Java Developer VS-1036

Angular 4 Syllabus. Module 1: Introduction. Module 2: AngularJS to Angular 4. Module 3: Introduction to Typescript

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

welcome to BOILERCAMP HOW TO WEB DEV

Angular 2 and TypeScript Web Application Development

END-TO-END JAVASCRIPT WEB APPS

INF5750. Introduction to JavaScript and Node.js

API-First: An Agile Approach to Data Management ERIK HENNUM

Simple REST-APIs with Dropwizard and Swagger. Bernd Schönbach LeanIX GmbH

Implicit Evaluation of auto Variables and Arguments

BEAAquaLogic. Service Bus. Interoperability With EJB Transport

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Web Application Development

Java Swing Introduction

Module 6 Node.js and Socket.IO

Top Down Design vs. Modularization

WEBASSETS & DUKPY FREE YOURSELF FROM NODEJS. Alessandro amol

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

ADF Mobile Code Corner

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

One language to rule them all: TypeScript. Gil Fink CEO and Senior Consultant, sparxys

Review: C++ Basic Concepts. Dr. Yingwu Zhu

STL in Action: Helper Algorithms

Deep Dive on How ArcGIS API for JavaScript Widgets Were Built

Front End. Presentation Layer. UI (User Interface) User <==> Data access layer

Lab 1 - Introduction to TypeScript

Ticket Machine Project(s)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Object Oriented Programming in C#

We are assuming you have node installed!

LEARN WITH INTRODUCTION TO TYPESCRIPT

Angular 4 Training Course Content

Introduction to Programming (Java) 2/12

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

MEAN February. techdt.la

Lecture 1 - Introduction (Class Notes)

Java Object Oriented Design. CSC207 Fall 2014

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Object-Oriented Programming

Advanced Express Web Application Development

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Microservices with Node.js

Chapter 1 - Consuming REST Web Services in Angular

Transcription:

MAKE NODEJS APIs GREAT WITH TYPESCRIPT

ABOUT ME I really like my work, software engineering never makes me bored, always keeps in learning and improving mood. dmytro.zharkov@gmail.com http://bit.ly/2fam3lr http://bit.ly/2fam3lr

NODEJS REPUTATION FRONT-END DEVELOPERS NodeJS

What s wrong with NodeJS APIs? Dynamic VS strong typing Pure OOP model of JS No of access modifiers No hypermedia in RESTful APIs Lack of documentation Design and structure 4

TYPESCRIPT FOR A RESQUE TYPE SYSTEM INTERFACES DECORATORS ACCESS MODIFIERS GENERICS ABSTRACT CLASSES

TYPES TRIPLET TYPE ALIASES public port: number; INTERFACES protected server: http.server; CLASSES public article: Article;

LET S COMPARE 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 CLASSES Reference types only Can be extended Signature and implementation

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;

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;

MORE INTERFACES export class Server { protected app: express.application; protected server: http.server; private db: mongoose.connection; private routes: express.router[] = [];

ACCESS MODIFIERS PUBLIC Default modifier and can be omitted (better not). PRIVATE Restics members visibility to current class only. PROTECTED Visible in derived classes.

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);.

DECORATORS CLASSES METHODS PARAMETERS FIELDS

EXAMPLE: TRY-CATCH WRAPPER class TestClass { @safe 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");

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;

EXAMPLE: DECLARATIVE ROUTES Typical ExpressJS route: 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'); );

@RouteHandler("/sample-route") class SampleRoute { public router: Router; constructor(public app: Server) { EXAMPLE: DECLARATIVE ROUTES @Get() public resources(req: Request, res: Response) { res.json([]); @Get("/:id") public resources(req: Request, res: Response) { res.json({);

EXAMPLE: REQUEST VALIDATION @Validate({ param: "name", validate: "required" ) public createarticle(request: Request, response: Response): void { // create an article

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; ;

EXAMPLE: HATEOAS Hypermedia allows to decouple client and server to a large extent and allow them to evolve independently.

EXAMPLE: HYPERMEDIA DEMO

TSOA DOCUMENTATION

AUTO SWAGGER DOCUMENTATION WITH DECORATORS TSOA @Route("Users") class ArticlesService { @Post() public createarticle(@body() requestbody: FashionArticle): Promise<FashionArticle> { // create an article export default ArticlesService;

AUTO SWAGGER DOCS WITH DECORATORS

UNIT AND INTEGRATION TESTS WRITE YOUR TESTS IN TYPESCRIPT SUPERTEST

DEBUG DIRECTLY IN TS

Additional build step. DRAWBACKS Lack of definition files for npm modules. Source and compiled code.

MAKE NODEJS APIs GREAT WITH TYPESCRIPT

THANKS FOR ATTENTION Slides NodeJS TypeScript starter TSOA auto swagger documentation http://bit.ly/2fw64qn http://bit.ly/2fw64qn