-
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.
feat(controller): adding CRUD base, error-handling, pipe decorator
- Loading branch information
1 parent
244ab24
commit fb1cfab
Showing
21 changed files
with
1,028 additions
and
673 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 |
---|---|---|
|
@@ -103,5 +103,6 @@ dist | |
# TernJS port file | ||
.tern-port | ||
.idea | ||
.vscode | ||
lib | ||
docs |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"module": "commonjs", | ||
"sourceMap": true, | ||
"declaration": true, | ||
"removeComments": false, | ||
"noImplicitAny": false, | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true | ||
}, | ||
"include": ["./**/*"], | ||
"exclude": ["node_modules", "**/*.spec.ts"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Model, model, Document } from 'm16' | ||
import { Body, Delete, Get, Param, Patch, Post, Put } from '../decorators' | ||
|
||
export abstract class Crud<T extends Document = any, CreateDTO = Partial<T>, UpdateDTO = Partial<T>> { | ||
Model: Model<T> | ||
constructor(public Document: new (data: any) => T) { | ||
this.Model = model(Document) | ||
} | ||
|
||
@Get() | ||
list() { | ||
return this.Model.find() | ||
} | ||
|
||
@Post() | ||
create(@Body() body: CreateDTO) { | ||
const document = new this.Document(body) | ||
return document.save() | ||
} | ||
|
||
@Patch(':id') | ||
update(@Param('id') id: number | string, @Body() body: Partial<T>) { | ||
const document = new this.Document(body) | ||
console.log(id) | ||
console.log(document) | ||
return this.Model.findOneAndUpdate({ _id: id }, { $set: document }) | ||
} | ||
|
||
@Put(':id') | ||
replace(@Param('id') id: number | string, @Body() body: UpdateDTO) { | ||
const document = new this.Document(body) | ||
return this.Model.findOneAndReplace({ _id: id }, document) | ||
} | ||
|
||
@Delete(':id') | ||
remove(@Param('id') id: number | string) { | ||
return this.Model.findOneAndDelete({ _id: id }) | ||
} | ||
} |
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 @@ | ||
export * from './crud' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { HttpStatus } from '../enums' | ||
import { HttpError } from './errors/http-error' | ||
|
||
export function defaultErrorHandler(error, request, reply) { | ||
if (error instanceof HttpError && typeof error.getResponse === 'function') { | ||
const data = error.getResponse({ request, reply }) | ||
return reply.code(data.code).send(data) | ||
} | ||
return reply.code(HttpStatus.INTERNAL_SERVER_ERROR).send({ message: 'Internal Error Server' }) | ||
} |
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,8 @@ | ||
import { HttpStatus } from './../../enums/http-status.enum' | ||
import { HttpError } from './http-error' | ||
|
||
export class BadRequestError extends HttpError { | ||
constructor(message: string, error = 'Bad Request', code = HttpStatus.BAD_REQUEST) { | ||
super({ code, error, message }) | ||
} | ||
} |
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,45 @@ | ||
import { HttpStatus } from './../../enums/http-status.enum' | ||
|
||
interface IHttpError { | ||
error?: string | ||
code?: number | ||
message?: string | Record<string, any> | any | ||
} | ||
|
||
interface IHttpErrorStatics { | ||
timestamp?: string | ||
path?: string | ||
} | ||
|
||
interface IGetResponseParams { | ||
reply?: any | ||
request?: any | ||
} | ||
|
||
export abstract class HttpError extends Error { | ||
public readonly code | ||
public readonly error | ||
|
||
constructor( | ||
{ error, code, message }: IHttpError = { | ||
code: HttpStatus.INTERNAL_SERVER_ERROR, | ||
error: 'Internal Server Error', | ||
message: '', | ||
}, | ||
) { | ||
super() | ||
this.code = code | ||
this.error = error | ||
this.message = message | ||
} | ||
|
||
getResponse({ request }: IGetResponseParams = {}): IHttpError & IHttpErrorStatics { | ||
return { | ||
code: this.code, | ||
error: this.error, | ||
['message']: this.message || undefined, | ||
timestamp: new Date().toISOString(), | ||
['path']: request ? request.url : undefined, | ||
} | ||
} | ||
} |
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,8 @@ | ||
import { HttpStatus } from './../../enums/http-status.enum' | ||
import { HttpError } from './http-error' | ||
|
||
export class InternalServerError extends HttpError { | ||
constructor(message: string, error = 'Internal Server Error', code = HttpStatus.INTERNAL_SERVER_ERROR) { | ||
super({ code, error, message }) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './app' | ||
export * from './decorators' | ||
export * from './crud' |
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.