diff --git a/types/roads-server/index.d.ts b/types/roads-server/index.d.ts new file mode 100644 index 0000000000..9543d760f1 --- /dev/null +++ b/types/roads-server/index.d.ts @@ -0,0 +1,42 @@ +// 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 + */ + 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" +}