Skip to content
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
],
rules: {
'no-console': 'error',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-inferrable-types': 'error',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
15 changes: 15 additions & 0 deletions lib/rest/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ExecutionContext, createParamDecorator } from '@nestjs/common';

export const Req = createParamDecorator(
(data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.intent.req();
},
);

// export const Res = createParamDecorator(
// (data: string, ctx: ExecutionContext) => {
// const request = ctx.switchToHttp().getRequest();
// return request.intent.res();
// },
// );
15 changes: 0 additions & 15 deletions lib/rest/decoratorts.ts

This file was deleted.

3 changes: 2 additions & 1 deletion lib/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './restController';
export * from './restServer';
export * from './interceptors/timeout';
export * from './interfaces';
export * from './decoratorts';
export * from './decorators';
export { Res } from '@nestjs/common';
export { Response } from 'express';
export * from './request';
20 changes: 10 additions & 10 deletions lib/rest/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ export class Request {
constructor(private request: ERequest) {
this.$payload = {};
this.$headers = {};
this.initiate();
this.initiate(request);
this.id = ulid();
this.$query = request.query;
this.$body = request.body;
this.$pathParams = request.params;
this.$headers = request.headers;
}

private initiate() {
this.$query = this.request.query;
this.$body = this.request.body;
this.$pathParams = this.request.params;
this.$headers = this.request.headers;
private initiate(request: ERequest) {
this.$query = request.query;
this.$body = request.body;
this.$pathParams = request.params;
this.$headers = request.headers;
this.$payload = { ...this.$query, ...this.$pathParams, ...this.$body };
}

Expand Down Expand Up @@ -151,22 +155,18 @@ export class Request {
}

only(...keys: string[]): Record<string, any> {
console.log(keys);
return {};
}

except(...keys: string[]): Record<string, any> {
console.log(keys);
return {};
}

is(pathPattern: string): boolean {
console.log(pathPattern);
return false;
}

has(...keys: string[]): boolean {
console.log('kjeys ===> ', keys);
for (const key of keys) {
if (!(key in this.$payload)) return false;
}
Expand Down
15 changes: 13 additions & 2 deletions lib/rest/restServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export class RestServer {
*/

static async make(module: any, options?: ServerOptions): Promise<void> {
const app = await NestFactory.create<NestExpressApplication>(module);
const app = await NestFactory.create<NestExpressApplication>(module, {
bodyParser: true,
});

if (options?.addValidationContainer) {
useContainer(app.select(module), { fallbackOnErrors: true });
Expand All @@ -25,6 +27,13 @@ export class RestServer {
app.enable(corsRule);
}

/**
* Explicitly enable body parsers, so that they are available in all of the middlewares.
*/
app.useBodyParser('json');
app.useBodyParser('raw');
app.useBodyParser('urlencoded');

app.use(requestMiddleware);

this.configureErrorReporter(config.get('app.sentry'));
Expand All @@ -34,7 +43,9 @@ export class RestServer {
app.useGlobalFilters(options.exceptionFilter(httpAdapter));
}

options?.globalPrefix && app.setGlobalPrefix(options.globalPrefix);
if (options?.globalPrefix) {
app.setGlobalPrefix(options.globalPrefix);
}

await app.listen(options?.port || config.get<number>('app.port'));
}
Expand Down
7 changes: 4 additions & 3 deletions lib/validator/validationGuard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Request as ERequest } from 'express';
import { Request } from '../rest';

@Injectable()
export class IntentValidationGuard implements CanActivate {
constructor(private reflector: Reflector) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest() as Request;
const iRequest = request['intent'].req();
const expressRequest = context.switchToHttp().getRequest() as ERequest;
const request = expressRequest['intent'].req() as Request;
const schema = this.reflector.get('dtoSchema', context.getHandler());
iRequest.addDto(await iRequest.validate(schema));
request.addDto(await request.validate(schema));
return true;
}
}