-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(nest): add cli to deps * docs(readme): update * docs(readme): update badges * chore(deps): @nestjs/swagger and class-transformer * feat(common): app api, res model, decorators and errorfilter * feat(swagger): setup swagger api docs * feat(api): controllers, endpoints and dto definition * chore(eslint)L setup linter * feat: lint script * feat(eslint): config * style: apply fixes from lint * test: fix pipeline * fix: swagger setup * fix(birthdaycontroller): class serializer without options throws * refactor(folder-structure): move model dependent modules into models folder * refactor: import paths
- Loading branch information
Showing
47 changed files
with
3,634 additions
and
2,251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import globals from "globals"; | ||
import pluginJs from "@eslint/js"; | ||
import tseslint from "typescript-eslint"; | ||
|
||
|
||
export default [ | ||
{languageOptions: { globals: globals.browser }, rules: { "no-unused-vars": "error" },}, | ||
pluginJs.configs.recommended, | ||
...tseslint.configs.recommended, | ||
]; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get('/') | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
getHello(): string { | ||
return 'Hello World!'; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Type, applyDecorators } from '@nestjs/common'; | ||
import { | ||
ApiCreatedResponse, | ||
ApiOkResponse, | ||
getSchemaPath, | ||
} from '@nestjs/swagger'; | ||
import { ReS } from '../res.model'; | ||
|
||
export const ApiReS = <TModel extends Type<any>>( | ||
model: TModel, | ||
description: string, | ||
status?: number, | ||
) => { | ||
if (status === 201) { | ||
return applyDecorators( | ||
ApiCreatedResponse({ | ||
description, | ||
schema: { | ||
allOf: [ | ||
{ $ref: getSchemaPath(ReS) }, | ||
{ | ||
properties: { | ||
data: { | ||
$ref: getSchemaPath(model), | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
return applyDecorators( | ||
ApiOkResponse({ | ||
description, | ||
schema: { | ||
allOf: [ | ||
{ $ref: getSchemaPath(ReS) }, | ||
{ | ||
properties: { | ||
data: { | ||
$ref: getSchemaPath(model), | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
ExceptionFilter, | ||
Catch, | ||
HttpException, | ||
ArgumentsHost, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { ReE } from '../res.model'; | ||
|
||
@Catch() | ||
export class ErrorFilter implements ExceptionFilter { | ||
catch(error: Error, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse(); | ||
const statusCode: number = | ||
error instanceof HttpException | ||
? error.getStatus() | ||
: HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
let message; | ||
console.error(error); | ||
if (error instanceof HttpException) { | ||
const errorMessage = (error.getResponse() as any)?.message; | ||
message = Array.isArray(errorMessage) ? errorMessage : [errorMessage]; | ||
} else { | ||
message = [error.message]; | ||
} | ||
|
||
const name = | ||
error instanceof HttpException | ||
? (error.getResponse() as any)?.error || error.name | ||
: error.name; | ||
|
||
const responseWithStatusCode = response.status(statusCode); | ||
const errorPayload = ReE.FromData(statusCode, name, message); | ||
// watch out for fastify | ||
if (typeof responseWithStatusCode.json === 'undefined') { | ||
responseWithStatusCode.send(errorPayload); | ||
} else { | ||
responseWithStatusCode.json(errorPayload); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class ReturnHelper { | ||
@ApiProperty({ example: true, description: 'success indicator' }) | ||
public success = true; | ||
} | ||
|
||
export class ReS<T> extends ReturnHelper { | ||
static FromData<T>(arg0: T): ReS<T> { | ||
const ret = new ReS<T>(); | ||
ret.success = true; | ||
ret.data = arg0; | ||
return ret; | ||
} | ||
|
||
public data: T; | ||
} | ||
|
||
export class ReE extends ReturnHelper { | ||
static FromData(statusCode: number, error: string, message: string[]): ReE { | ||
const ret = new ReE(); | ||
ret.success = false; | ||
ret.message = message; | ||
ret.error = error; | ||
ret.statusCode = statusCode; | ||
return ret; | ||
} | ||
@ApiProperty({ description: 'statusCode' }) | ||
public statusCode: number; | ||
@ApiProperty({ description: 'detail message' }) | ||
public message: string[]; | ||
@ApiProperty({ description: 'error description' }) | ||
public error: string; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.