diff --git a/package-lock.json b/package-lock.json index 24f21cc..986b442 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@shelepuginivan/lunatic", - "version": "1.2.0", + "version": "1.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@shelepuginivan/lunatic", - "version": "1.2.0", + "version": "1.3.0", "license": "MIT", "devDependencies": { "@jest/globals": "^29.6.1", diff --git a/package.json b/package.json index 76d1766..7e5077c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shelepuginivan/lunatic", - "version": "1.2.0", + "version": "1.3.0", "description": "Node.js backend framework", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/router.ts b/src/router.ts index 5aa6301..210df10 100644 --- a/src/router.ts +++ b/src/router.ts @@ -14,62 +14,82 @@ export class Router { this.middlewares = []; } - public use(handler: RequestHandler | Router): this - public use(path: string, handler: RequestHandler | Router): this + public use(handler: RequestHandler): this + public use(router: Router): this + public use(path: string, handler: RequestHandler): this + public use(path: string, router: Router): this public use(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router) { return this.addMiddleware('*', arg1, arg2); } - public get(handler: RequestHandler | Router): this - public get(path: string, handler: RequestHandler | Router): this + public get(handler: RequestHandler): this + public get(router: Router): this + public get(path: string, handler: RequestHandler): this + public get(path: string, router: Router): this public get(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('GET', arg1, arg2); } - public head(handler: RequestHandler | Router): this - public head(path: string, handler: RequestHandler | Router): this + public head(handler: RequestHandler): this + public head(router: Router): this + public head(path: string, handler: RequestHandler): this + public head(path: string, router: Router): this public head(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('HEAD', arg1, arg2); } - public post(handler: RequestHandler | Router): this - public post(path: string, handler: RequestHandler | Router): this + public post(handler: RequestHandler): this + public post(router: Router): this + public post(path: string, handler: RequestHandler): this + public post(path: string, router: Router): this public post(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('POST', arg1, arg2); } - public put(handler: RequestHandler | Router): this - public put(path: string, handler: RequestHandler | Router): this + public put(handler: RequestHandler): this + public put(router: Router): this + public put(path: string, handler: RequestHandler): this + public put(path: string, router: Router): this public put(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('PUT', arg1, arg2); } - public delete(handler: RequestHandler | Router): this - public delete(path: string, handler: RequestHandler | Router): this + public delete(handler: RequestHandler): this + public delete(router: Router): this + public delete(path: string, handler: RequestHandler): this + public delete(path: string, router: Router): this public delete(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('DELETE', arg1, arg2); } - public connect(handler: RequestHandler | Router): this - public connect(path: string, handler: RequestHandler | Router): this + public connect(handler: RequestHandler): this + public connect(router: Router): this + public connect(path: string, handler: RequestHandler): this + public connect(path: string, router: Router): this public connect(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('CONNECT', arg1, arg2); } - public options(handler: RequestHandler | Router): this - public options(path: string, handler: RequestHandler | Router): this + public options(handler: RequestHandler): this + public options(router: Router): this + public options(path: string, handler: RequestHandler): this + public options(path: string, router: Router): this public options(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('OPTIONS', arg1, arg2); } - public trace(handler: RequestHandler | Router): this - public trace(path: string, handler: RequestHandler | Router): this + public trace(handler: RequestHandler): this + public trace(router: Router): this + public trace(path: string, handler: RequestHandler): this + public trace(path: string, router: Router): this public trace(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('TRACE', arg1, arg2); } - public patch(handler: RequestHandler | Router): this - public patch(path: string, handler: RequestHandler | Router): this + public patch(handler: RequestHandler): this + public patch(router: Router): this + public patch(path: string, handler: RequestHandler): this + public patch(path: string, router: Router): this public patch(arg1: string | RequestHandler | Router, arg2?: RequestHandler | Router): this { return this.addMiddleware('PATCH', arg1, arg2); } @@ -85,7 +105,7 @@ export class Router { const { method, path, handler } = this.middlewares[i]; i++; - const isRouter = handler instanceof Router + const isRouter = handler instanceof Router; if (method !== '*' && req.method !== method) { return nextHandler(); diff --git a/src/server.ts b/src/server.ts index ffec674..e140fc5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,8 +8,8 @@ import { RenderFunction } from './types/render-function'; export class LunaticServer extends Router { public renderFunction: RenderFunction; + public readonly httpServer: Server; private readonly enabledFeatures: Set; - private readonly httpServer: Server; constructor(httpServer?: Server) { super(); @@ -90,9 +90,16 @@ export class LunaticServer extends Router { } } - public listen(port: number): Server { + public listen( + port?: number, + hostname?: string, + backlog?: number, + ): Promise { this.httpServer.on('request', this.callback); - return this.httpServer.listen(port); + + return new Promise((resolve: () => void) => { + this.httpServer.listen(port, hostname, backlog, resolve); + }); } public renderer(renderFunction: RenderFunction): this { diff --git a/tests/server.listen.spec.ts b/tests/server.listen.spec.ts index 9bdb56e..05149a0 100644 --- a/tests/server.listen.spec.ts +++ b/tests/server.listen.spec.ts @@ -10,9 +10,8 @@ describe('LunaticServer.listen()', () => { }); it('Should start server', (done) => { - const server = app.listen(5124); - - expect(server.listening).toBe(true); - server.close(done); + app.listen(5124) + .then(() => expect(app.httpServer.listening).toBe(true)) + .then(() => app.httpServer.close(done)); }); }); diff --git a/tests/server.spec.ts b/tests/server.spec.ts index 84e305c..6bbfb2a 100644 --- a/tests/server.spec.ts +++ b/tests/server.spec.ts @@ -8,9 +8,8 @@ describe('LunaticServer', () => { const server = new Server(); const app = new LunaticServer(server); - app.listen(8000); - - expect(server.listening).toBe(true); - server.close(done); + app.listen(8000, undefined, undefined) + .then(() => expect(server.listening).toBe(true)) + .then(() => server.close(done)) }); });