From 2d6b9e356260aa945f92e4f7b508021cf741889b Mon Sep 17 00:00:00 2001 From: dominuskernel Date: Tue, 27 Feb 2018 12:23:31 +0100 Subject: [PATCH 1/3] Add types definitions for roads-server --- types/roads-server/index.d.ts | 43 ++++++++++++++++++++++++ types/roads-server/roads-server-tests.ts | 25 ++++++++++++++ types/roads-server/tsconfig.json | 24 +++++++++++++ types/roads-server/tslint.json | 3 ++ 4 files changed, 95 insertions(+) create mode 100644 types/roads-server/index.d.ts create mode 100644 types/roads-server/roads-server-tests.ts create mode 100644 types/roads-server/tsconfig.json create mode 100644 types/roads-server/tslint.json diff --git a/types/roads-server/index.d.ts b/types/roads-server/index.d.ts new file mode 100644 index 0000000000..e624b2828e --- /dev/null +++ b/types/roads-server/index.d.ts @@ -0,0 +1,43 @@ +// Type definitions for roads-server 1.0 +// Project: https://github.com/Dashron/roads-server +// Definitions by: Francisco Jesus +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.6 +/// + +import { Road } from "roads"; + +/** + * Roads-Server is a simple webserver for setting up and using the roads framework on an HTTP or HTTP 2 server + */ +export class Server { + /** + * + * @param Roads The Road that handles all the routes + * @param error_handler An overwrite to the standard error handler. Accepts a single parameter (the error) and should return a Roads.Response object + */ + constructor(Roads: Road, error_handler: (error: any) => any); + + /** + * Start the http server. Accepts the same parameters as HttpServer.listen + * @param port Port which listen the server + * @param handler Execution tasks after start the server + */ + listen(port: number, handler: () => any): any; +} + +export class Http2Server { + /** + * + * @param Roads The Road that handles all the routes + * @param error_handler An overwrite to the standard error handler. Accepts a single parameter (the error) and should return a Roads.Response object + */ + constructor(Roads: Road); + + /** + * Start the http server. Accepts the same parameters as HttpServer.listen + * @param port Port which listen the server + * @param handler Execution tasks after start the server + */ + listen(port: number, handler: () => any): any; +} diff --git a/types/roads-server/roads-server-tests.ts b/types/roads-server/roads-server-tests.ts new file mode 100644 index 0000000000..bff1852145 --- /dev/null +++ b/types/roads-server/roads-server-tests.ts @@ -0,0 +1,25 @@ +import { Road, Response } from "roads"; +import { Server, Http2Server } from "roads-server"; + +const road = new Road(); +const server = new Server(road, (error: any) => { + switch (error.code) { + case 404: + return new Response("Not Found", 404); + case 405: + return new Response("Not Allowed", 405); + default: + case 500: + return new Response(error.message, 500); + } +}); + +server.listen(3000, () => { + console.log("Serve is running in the port 3000"); +}); + +const server2 = new Http2Server(road); + +server2.listen(3000, () => { + console.log("Serve is running in the port 3000"); +}); diff --git a/types/roads-server/tsconfig.json b/types/roads-server/tsconfig.json new file mode 100644 index 0000000000..bbe5268bef --- /dev/null +++ b/types/roads-server/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "roads-server-tests.ts" + ] +} diff --git a/types/roads-server/tslint.json b/types/roads-server/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/roads-server/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} From c3a16f1266abacf43031a1a0c5675d14a600e820 Mon Sep 17 00:00:00 2001 From: dominuskernel Date: Thu, 1 Mar 2018 11:16:37 +0100 Subject: [PATCH 2/3] turn optional the error_handler argument --- types/roads-server/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/roads-server/index.d.ts b/types/roads-server/index.d.ts index e624b2828e..f9fb7d1384 100644 --- a/types/roads-server/index.d.ts +++ b/types/roads-server/index.d.ts @@ -16,7 +16,7 @@ export class Server { * @param Roads The Road that handles all the routes * @param error_handler An overwrite to the standard error handler. Accepts a single parameter (the error) and should return a Roads.Response object */ - constructor(Roads: Road, error_handler: (error: any) => any); + constructor(Roads: Road, error_handler?: (error: any) => any); /** * Start the http server. Accepts the same parameters as HttpServer.listen From fbe268136f5130633608f58cd41f6f249e51ae69 Mon Sep 17 00:00:00 2001 From: dominuskernel Date: Thu, 1 Mar 2018 11:22:46 +0100 Subject: [PATCH 3/3] remove comment in Http2Server --- types/roads-server/index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/roads-server/index.d.ts b/types/roads-server/index.d.ts index f9fb7d1384..9543d760f1 100644 --- a/types/roads-server/index.d.ts +++ b/types/roads-server/index.d.ts @@ -30,7 +30,6 @@ export class Http2Server { /** * * @param Roads The Road that handles all the routes - * @param error_handler An overwrite to the standard error handler. Accepts a single parameter (the error) and should return a Roads.Response object */ constructor(Roads: Road);