-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
1,355 additions
and
1,344 deletions.
There are no files selected for viewing
17 changes: 5 additions & 12 deletions
17
docs/docs/snippets/middlewares/custom-endpoint-decorator-status.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
19 changes: 19 additions & 0 deletions
19
packages/common/src/mvc/components/statusAndHeadersMiddleware.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as Express from "express"; | ||
|
||
export function statusAndHeadersMiddleware(request: Express.Request, response: Express.Response, next: any) { | ||
const { | ||
statusCode, | ||
response: {headers = {}} | ||
} = request.ctx.endpoint; | ||
|
||
if (response.statusCode === 200) { | ||
// apply status only if the isn't already modified | ||
response.status(statusCode); | ||
} | ||
|
||
// apply headers | ||
Object.entries(headers).forEach(([key, schema]) => { | ||
schema.value !== undefined && response.set(key, String(schema.value)); | ||
}); | ||
next(); | ||
} |
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 @@ | ||
import {DecoratorParameters, getDecoratorType, Type} from "@tsed/core"; | ||
import {EndpointMetadata} from "../../models/EndpointMetadata"; | ||
import {EndpointRegistry} from "../../registries/EndpointRegistry"; | ||
|
||
/** | ||
* | ||
* @param fn | ||
* @decorator | ||
*/ | ||
export function EndpointFn(fn: (endpoint: EndpointMetadata, parameters: DecoratorParameters) => void) { | ||
return <T>(target: Type<any>, property: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void => { | ||
if (getDecoratorType([target, property, descriptor]) === "method") { | ||
fn(EndpointRegistry.get(target, property!), [target, property, descriptor]); | ||
|
||
return descriptor; | ||
} | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,31 +1,45 @@ | ||
import {getDecoratorType, Type} from "@tsed/core"; | ||
import {EndpointRegistry} from "../../registries/EndpointRegistry"; | ||
import {cleanObject, deepMerge} from "@tsed/core"; | ||
import {IResponseOptions} from "../../interfaces/IResponseOptions"; | ||
import {EndpointFn} from "./endpointFn"; | ||
|
||
const isSuccessStatus = (code: number | undefined) => code && 200 <= code && code < 300; | ||
|
||
/** | ||
* Define the returned type for the serialization. | ||
* | ||
* ```typescript | ||
* @Controller('/') | ||
* export class Ctrl { | ||
* | ||
* @Get('/') | ||
* @ReturnType(User) | ||
* get(): Promise<User> { } | ||
* @ReturnType(200, {type: User, collectionType: Map}) | ||
* get(): Promise<Map<User>> { } | ||
* } | ||
* | ||
* ``` | ||
* | ||
* @returns {Function} | ||
* @param type | ||
* @param response | ||
* @decorator | ||
* @endpoint | ||
*/ | ||
export function ReturnType(type: Type<any> | any): Function { | ||
return <T>(target: Type<any>, targetKey?: string, descriptor?: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void => { | ||
if (getDecoratorType([target, targetKey, descriptor]) === "method") { | ||
EndpointRegistry.get(target, targetKey!).type = type; | ||
export function ReturnType(response: Partial<IResponseOptions> = {}): Function { | ||
return EndpointFn(endpoint => { | ||
const {responses, statusCode} = endpoint; | ||
const code = response.code || statusCode; // implicit | ||
|
||
return descriptor; | ||
if (isSuccessStatus(response.code)) { | ||
const {response} = endpoint; | ||
responses.delete(statusCode); | ||
endpoint.statusCode = code; | ||
endpoint.responses.set(code, response); | ||
} | ||
}; | ||
|
||
response = { | ||
code, | ||
description: "", | ||
...deepMerge(endpoint.get(code), cleanObject(response)) | ||
}; | ||
|
||
endpoint.responses.set(response.code!, response as IResponseOptions); | ||
}); | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
...corators/params/utils/mapParamsOptions.ts → .../mvc/decorators/utils/mapParamsOptions.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
7 changes: 7 additions & 0 deletions
7
packages/common/src/mvc/decorators/utils/mapReturnedResponse.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function mapReturnedResponse({use, collection, ...options}: any): any { | ||
return { | ||
...options, | ||
type: options.type || use, | ||
collectionType: options.collectionType || collection | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import {IResponseHeaders} from "./IResponseHeaders"; | ||
|
||
/** | ||
* Interface can be implemented to customize the error sent to the client. | ||
*/ | ||
export interface IResponseError extends Error { | ||
errors?: any[]; | ||
origin?: Error; | ||
headers?: IResponseHeaders; | ||
headers?: {}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.