Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(common): Fix responses typings when a response is sent
Closes: 742
- Loading branch information
Showing
36 changed files
with
858 additions
and
699 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
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 was deleted.
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 |
---|---|---|
@@ -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.
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,17 +1,26 @@ | ||
import {IMetadataType} from "@tsed/core"; | ||
import {IResponseHeaders} from "./IResponseHeaders"; | ||
|
||
declare global { | ||
namespace TsED { | ||
interface ResponseOptions {} | ||
interface ResponseHeader { | ||
value?: string | number; | ||
} | ||
|
||
interface ResponseHeaders { | ||
[key: string]: ResponseHeader; | ||
} | ||
|
||
interface ResponseOptions extends IMetadataType { | ||
code?: number; | ||
headers?: { | ||
[key: string]: ResponseHeader; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
export interface IResponseOptions extends IMetadataType, TsED.ResponseOptions { | ||
code?: number; | ||
headers?: { | ||
[exampleName: string]: IResponseHeaders; | ||
}; | ||
export interface IResponseOptions extends TsED.ResponseOptions {} | ||
|
||
[key: string]: any; | ||
} | ||
export interface IResponseHeaders extends TsED.ResponseHeaders {} | ||
|
||
export interface IResponseHeader extends TsED.ResponseHeader {} |
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.