-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.ts
171 lines (158 loc) · 4.53 KB
/
endpoints.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// deno-lint-ignore-file no-explicit-any
import { z } from 'npm:zod';
import { Context, MiddlewareHandler } from 'hono';
import { describeRoute, DescribeRouteOptions } from 'npm:hono-openapi';
import * as v from 'npm:valibot';
import { createRoute } from 'npm:@hono/zod-openapi';
import { OpenAPIDoc } from '@/lib/openAPI.types.ts';
async function loadEndpoints() {
return [
await import('./api/Users/whoAmI.ts'),
await import('./api/Users/getUserInfo.ts'),
await import('./api/Users/register.ts'),
];
}
export type Ctx = Context<any>;
type AllMethods =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'PATCH'
| 'OPTIONS'
| 'HEAD'
| 'CONNECT'
| 'TRACE';
type StatusCode = 200 | 201 | 202 | 204 | 400 | 401 | 403 | 404 | 500 | any;
type MiddlewareDataArray = Array<{
middleware: string;
base: any;
user: any;
}>;
type ValidationType = {
query?:
| {
[key: string]: z.ZodType<any, any, any> | string | undefined;
}
| undefined;
body?: z.infer<any> | undefined;
};
function getDataFromMiddleware(
middlewareDatas: MiddlewareDataArray,
middleware: string
): { isFound: boolean; [key: string]: any } {
const middlewareData = middlewareDatas.find(
(data) => data.middleware === middleware
);
if (!middlewareData) {
return {
isFound: false,
user: null,
};
}
return {
isFound: true,
...middlewareData.user,
};
}
const endpoints: Array<{
endpoint: {
path: string;
middlewares: string[] | undefined;
validation: ValidationType;
method: AllMethods;
openAPI: OpenAPIDoc | null;
};
handler: (
ctx: Ctx,
middlewareDatas: MiddlewareDataArray
) => Response | Promise<Response>;
}> = [];
async function initializeEndpoints() {
const endpointExports = await loadEndpoints();
for (const _export of endpointExports) {
let path: string | null = null;
let middlewares: string[] | undefined = [];
let validation: ValidationType = { query: {}, body: undefined };
let openAPI: OpenAPIDoc | null = null;
let GET: ((ctx: Ctx) => Response | Promise<Response>) | null = null;
let POST: ((ctx: Ctx) => Response | Promise<Response>) | null = null;
let PUT: ((ctx: Ctx) => Response | Promise<Response>) | null = null;
let DELETE: ((ctx: Ctx) => Response | Promise<Response>) | null = null;
let PATCH: ((ctx: Ctx) => Response | Promise<Response>) | null = null;
try {
path = _export.path;
middlewares = (_export as any).middlewares;
validation = (_export as any).validation;
openAPI = (_export as any).openAPI ? (_export as any).openAPI : null;
GET = (_export as any).GET ? (_export as any).GET : null;
POST = (_export as any).POST ? (_export as any).POST : null;
PUT = (_export as any).PUT ? (_export as any).PUT : null;
DELETE = (_export as any).DELETE ? (_export as any).DELETE : null;
PATCH = (_export as any).PATCH ? (_export as any).PATCH : null;
} catch (error) {
console.error(`Failed to import `, _export, error);
continue;
}
const method: AllMethods | '' = '';
const availableMethods: Array<AllMethods> = [];
const methods: {
[key in AllMethods]?: ((ctx: Ctx) => Response | Promise<Response>) | null;
} = { GET, POST, PUT, DELETE, PATCH };
for (const [method, handler] of Object.entries(methods) as [
AllMethods,
(ctx: Ctx) => Response | Promise<Response>
][]) {
if (typeof handler === 'function') {
availableMethods.push(method);
endpoints.push({
endpoint: {
openAPI,
path,
middlewares,
validation,
method,
},
handler,
});
}
}
endpoints.push({
endpoint: {
openAPI: {
description: `Get the available methods for this endpoint`,
tags: openAPI?.tags,
responses: {
'200': {
type: 'plain/text',
zodSchema: z.string(),
},
},
},
path,
middlewares: [],
validation,
method: 'OPTIONS',
},
handler: async (ctx: Ctx) => {
await ctx.res.headers.append('Allow', availableMethods.join(', '));
await ctx.status(200);
return await ctx.text("Look header's Allow property :)");
},
});
if (!method) {
continue;
}
}
}
async function getEndpoints() {
await initializeEndpoints();
return endpoints;
}
export {
type MiddlewareDataArray,
getDataFromMiddleware,
type ValidationType,
getEndpoints,
type StatusCode
};