This repository was archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathendpoint.ts
70 lines (65 loc) · 2.14 KB
/
endpoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { failure } from 'io-ts/lib/PathReporter';
import {
DocumentedEndpointInstance,
MinimalEndpointInstance,
} from '../../endpoints';
import { GetEndpointSubscriber } from 'ts-endpoint-express';
import { RecordCodec, runtimeType } from 'ts-io-error/Codec';
import { IOError } from 'ts-io-error/lib';
import { Controller } from 'ts-endpoint-express/Controller';
import { Codec } from 'ts-io-error/lib/Codec';
import { Kind } from 'ts-endpoint-express/HKT';
import express from 'express';
import { EndpointInstance, InferEndpointParams } from 'ts-endpoint';
const toError = (e: unknown): IOError => {
// console.log(e);
if (Array.isArray(e) && e[0].context !== undefined) {
return {
name: 'Validation Error',
status: 400,
message: 'Error during validation',
details: {
kind: 'DecodingError',
errors: failure(e),
},
};
}
return {
name: 'EndpointError',
status: 500,
message: 'Unknown error',
details: {
kind: 'DecodingError',
errors: [],
},
};
};
export declare type UndefinedOrRuntime<N> = N extends RecordCodec<any, any>
? {
[k in keyof N['props']]: runtimeType<N['props'][k]>;
}
: undefined;
export declare type InferEndpointInstanceParams<EI> =
EI extends DocumentedEndpointInstance<infer E>
? InferEndpointParams<E>
: EI extends EndpointInstance<infer E>
? InferEndpointParams<E>
: never;
export const AddEndpoint = GetEndpointSubscriber(toError) as (
router: express.Router,
...m: express.RequestHandler[]
) => <E extends MinimalEndpointInstance>(
endpoint: E,
controller: Controller<
Kind<'IOError', NonNullable<E['Errors']>>,
UndefinedOrRuntime<InferEndpointInstanceParams<E>['params']>,
UndefinedOrRuntime<InferEndpointInstanceParams<E>['headers']>,
UndefinedOrRuntime<InferEndpointInstanceParams<E>['query']>,
InferEndpointInstanceParams<E>['body'] extends undefined
? undefined
: InferEndpointInstanceParams<E>['body'] extends Codec<any, any, any>
? runtimeType<InferEndpointInstanceParams<E>['body']>
: undefined,
runtimeType<InferEndpointInstanceParams<E>['output']>
>
) => void;