Skip to content
This repository was archived by the owner on Jun 9, 2020. It is now read-only.

Commit e483725

Browse files
author
Christoph Bühler
committed
feat: let returntype define it's value type
1 parent fa6b58d commit e483725

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

src/ReturnTypeHandler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { ReturnType } from './routes/ReturnType';
99
* This handler is registered within the giuseppe core and holds all registered return type handlers. All those
1010
* {@link ReturnType} are than checked when a return value is given to the handler. If no matching handler is found
1111
* the default handler is taken. If even that isn't found, an error is thrown.
12-
*
12+
*
1313
* @export
1414
* @class ReturnTypeHandler
1515
*/
1616
export class ReturnTypeHandler {
17-
private returnTypes: { [type: string]: ReturnType<any> } = {};
17+
private returnTypes: { [type: string]: ReturnType<any, any> } = {};
1818

19-
constructor(types: ReturnType<any>[]) {
19+
constructor(types: ReturnType<any, any>[]) {
2020
for (const type of types) {
2121
this.returnTypes[type.type] = type;
2222
}
@@ -26,8 +26,8 @@ export class ReturnTypeHandler {
2626
* Handles the response for a given value. Searches in the handlers for the correct return type handler. If no matching
2727
* handler is found, the `default` handler is used. And if no default is registered (or was overwritten with undefined)
2828
* an error is thrown.
29-
*
30-
* @param {*} value
29+
*
30+
* @param {*} value
3131
* @param {Response} response
3232
* @throws {NoReturnValueHandlerFoundError} One handler must be there to handle the value.
3333
* @memberof ReturnTypeHandler

src/routes/ReturnType.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,47 @@
22
* Return type handler. This element registers a hander for specific return types. When an object is received from
33
* a route function, it's constructor name is checked against all various registered return type handler. When no one
44
* is found, giuseppe throws an error, otherwise the registered handler (or the default) will be used.
5-
*
5+
*
66
* @export
77
* @interface ReturnType
8-
* @template T
8+
* @template TValue Type for the incomming value
9+
* @template TResult Type for the returned value of the `getValue` function
910
*/
10-
export interface ReturnType<T> {
11+
export interface ReturnType<TValue, TResult = string> {
1112
/**
1213
* The name of the type that is handled. This can be 'String', 'Number', or anything else.
1314
* One special name is possible: 'default'. The default is used whenever no other types that matches are found.
14-
*
15+
*
1516
* @type {string}
1617
* @memberof ReturnType
1718
*/
1819
type: string;
1920

2021
/**
2122
* Returns the headers for the given value. The function must return something. Even if it's an empty header hash.
22-
*
23-
* @param {T} [value]
24-
* @returns {{ [field: string]: string }}
23+
*
24+
* @param {TValue} [value]
25+
* @returns {{ [field: string]: string }}
2526
* @memberof ReturnType
2627
*/
27-
getHeaders(value?: T): { [field: string]: string };
28+
getHeaders(value?: TValue): { [field: string]: string };
2829

2930
/**
3031
* Get the return http status code for the given value.
31-
*
32-
* @param {T} [value]
33-
* @returns {number}
32+
*
33+
* @param {TValue} [value]
34+
* @returns {number}
3435
* @memberof ReturnType
3536
*/
36-
getStatus(value?: T): number;
37+
getStatus(value?: TValue): number;
3738

3839
/**
3940
* Get the stringified value for a given value. This value is sent by express with the set headers and
4041
* status codes. This function is only called if an actual value exists.
41-
*
42-
* @param {T} value
43-
* @returns {string}
42+
*
43+
* @param {TValue} value
44+
* @returns {string}
4445
* @memberof ReturnType
4546
*/
46-
getValue(value: T): string;
47+
getValue(value: TValue): TResult;
4748
}

0 commit comments

Comments
 (0)