diff --git a/core/nano-server/src/nano-server.ts b/core/nano-server/src/nano-server.ts index 42a4cda6c..9f42e434a 100644 --- a/core/nano-server/src/nano-server.ts +++ b/core/nano-server/src/nano-server.ts @@ -52,13 +52,13 @@ export class AlwatrNanoServer { * const nanoServer = new AlwatrNanoServer(); * * nanoServer.route('GET', '/', async (connection) => { - * connection.reply({ + * return { * ok: true, * data: { * app: 'Alwatr Nanoservice Starter Kit', * message: 'Hello ;)', * }, - * }); + * }; * }); * ``` */ @@ -134,20 +134,22 @@ export class AlwatrNanoServer { * * ```ts * nanoServer.route('GET', '/', async (connection) => { - * connection.reply({ + * return { * ok: true, * data: { * app: 'Alwatr Nanoservice Starter Kit', * message: 'Hello ;)', * }, * }); - * }); + * }; * ``` */ route, TData = Record>( method: 'ALL' | Methods, route: 'all' | `/${string}`, - middleware: (connection: AlwatrConnection) => AlwatrServiceResponse | null, + middleware: ( + connection: AlwatrConnection + ) => AlwatrServiceResponse | Promise | null> | null, ): void { this._logger.logMethodArgs('route', {method, route}); @@ -170,13 +172,13 @@ export class AlwatrNanoServer { * Example: * ```ts * nanoServer.route('GET', '/', async (connection) => { - * connection.reply({ + * return { * ok: true, * data: { * app: 'Alwatr Nanoservice Starter Kit', * message: 'Hello ;)', * }, - * }); + * }; * }); * ``` */ @@ -334,7 +336,7 @@ export class AlwatrNanoServer { method: connection.method, route, }); - connection.reply({ + this.reply(serverResponse, { ok: false, statusCode: 500, errorCode: 'http_server_middleware_error', @@ -348,7 +350,7 @@ export class AlwatrNanoServer { } protected _notFoundListener = (connection: AlwatrConnection): void => { - connection.reply({ + this.reply(connection.serverResponse, { ok: false, statusCode: 404, errorCode: 'not_found', @@ -430,10 +432,9 @@ export class AlwatrConnection { * Example: * ```ts * const bodyData = await connection.requireJsonBody(); - * if (bodyData == null) return; * ``` */ - async requireJsonBody(): Promise { + async requireJsonBody(): Promise { // if request content type is json if (this.incomingMessage.headers['content-type'] !== 'application/json') { throw new Error('require_body_json'); @@ -526,11 +527,10 @@ export class AlwatrConnection { * Example: * ```ts * const params = connection.requireQueryParams<{id: string}>({id: 'string'}); - * if (params == null) return; * console.log(params.id); * ``` */ - requireQueryParams(params: Record): T | null { + requireQueryParams(params: Record): T { const parsedParams: Record = {}; for (const paramName in params) {