Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Release v1.3.0 #1

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
62 changes: 41 additions & 21 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down
13 changes: 10 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationFeature>;
private readonly httpServer: Server;

constructor(httpServer?: Server) {
super();
Expand Down Expand Up @@ -90,9 +90,16 @@ export class LunaticServer extends Router {
}
}

public listen(port: number): Server {
public listen(
port?: number,
hostname?: string,
backlog?: number,
): Promise<void> {
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 {
Expand Down
7 changes: 3 additions & 4 deletions tests/server.listen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
7 changes: 3 additions & 4 deletions tests/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
});
});