-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.ts
43 lines (39 loc) · 1.37 KB
/
handler.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
import type { OpenAPIComponentObject } from '@asteasolutions/zod-to-openapi/dist/openapi-registry.ts';
import type { Handler } from '@chubbyts/chubbyts-http-types/dist/handler';
import type { Response } from '@chubbyts/chubbyts-http-types/dist/message';
import type { ResponseFactory } from '@chubbyts/chubbyts-http-types/dist/message-factory';
export const createPingHandler = (responseFactory: ResponseFactory): Handler => {
return async (): Promise<Response> => {
const response = responseFactory(200);
response.body.end(JSON.stringify({ datetime: new Date().toISOString() }));
return {
...response,
headers: {
...response.headers,
'content-type': ['application/json'],
'cache-control': ['no-cache, no-store, must-revalidate'],
pragma: ['no-cache'],
expires: ['0'],
},
};
};
};
export const createOpenApiHandler = (
openApiObject: OpenAPIComponentObject,
responseFactory: ResponseFactory,
): Handler => {
return async (): Promise<Response> => {
const response = responseFactory(200);
response.body.end(JSON.stringify(openApiObject));
return {
...response,
headers: {
...response.headers,
'content-type': ['application/json'],
'cache-control': ['no-cache, no-store, must-revalidate'],
pragma: ['no-cache'],
expires: ['0'],
},
};
};
};