22import { Context } from 'https://deno.land/x/[email protected] /mod.ts' ; 33
44// All endpoint file paths should be written here
5- const endpointExports = [ await import ( './ api/Users/whoAmI.ts' ) ] ;
5+ const endpointExports = [ await import ( "@ api/Users/whoAmI.ts" ) ] ;
66
77export type ctx = Context < Record < string , any > , Record < string , any > > ;
88
@@ -89,112 +89,4 @@ for (const _export of endpointExports) {
8989 }
9090}
9191
92- export { endpoints } ;
93-
94- /*
95- // deno-lint-ignore no-explicit-any
96- export type ctx = Context<Record<string, any>, Record<string, any>>;
97-
98- type AllMethods =
99- | 'GET'
100- | 'POST'
101- | 'PUT'
102- | 'DELETE'
103- | 'PATCH'
104- | 'OPTIONS'
105- | 'HEAD'
106- | 'CONNECT'
107- | 'TRACE';
108-
109- const cwd = Deno.cwd();
110- const endpoints: Array<{
111- endpoint: {
112- pattern: URLPattern;
113- method: AllMethods;
114- };
115- handler: (ctx: ctx) => Response | Promise<Response>;
116- }> = [];
117-
118- /*
119- * Loop through all files in the current directory and subdirectories
120- * to find the handler for the requested URL (just checks api folder)
121- *
122- * api\Users\whoAmI.ts -> ./api/Users/whoAmI.ts
123- * { pattern: new URLPattern({ pathname: "/api/whoAmI" }), handler: handler }
124- * IF MATCH -> handler(req)
125- *
126- for await (const walkEntry of walk(cwd)) {
127- const type = walkEntry.isSymlink
128- ? 'symlink'
129- : walkEntry.isFile
130- ? 'file'
131- : 'directory';
132-
133- console.log(type, walkEntry.path);
134-
135- if (type !== 'file') {
136- continue;
137- }
138-
139- const path = `.${walkEntry.path.substring(cwd.length).replaceAll('\\', '/')}`;
140- if (!path.startsWith('./api')) {
141- continue;
142- }
143-
144- // ./api/Users/whoAmI.ts -> Users/whoAmI
145- // const afterAPI = path.substring(6);
146-
147- const actualPath = Deno.build.os === 'windows'
148- ? path
149- : walkEntry.path;
150-
151- let pattern, GET, POST, PUT, DELETE, PATCH;
152- try {
153- ({ pattern, GET, POST, PUT, DELETE, PATCH } = await import(actualPath));
154- } catch (error) {
155- console.error(
156- `Failed to import ${actualPath}`,
157- error
158- );
159- continue;
160- }
161- const method: AllMethods | '' = '';
162- const availableMethods: Array<AllMethods> = [];
163- const methods: {
164- [key in AllMethods]?: (ctx: ctx) => Response | Promise<Response>;
165- } = { GET, POST, PUT, DELETE, PATCH };
166- for (const [method, handler] of Object.entries(methods) as [
167- AllMethods,
168- (ctx: ctx) => Response | Promise<Response>
169- ][]) {
170- if (typeof handler === 'function') {
171- availableMethods.push(method);
172- endpoints.push({
173- endpoint: {
174- pattern: pattern,
175- method,
176- },
177- handler,
178- });
179- }
180- }
181-
182- endpoints.push({
183- endpoint: {
184- pattern: pattern,
185- method: 'OPTIONS',
186- },
187- handler: (ctx) => {
188- ctx.response.headers.set('Allow', availableMethods.join(', '));
189- ctx.response.body = "Look header's Allow property :)";
190- ctx.response.status = 200;
191- return new Response(null, { status: 200 });
192- },
193- });
194- if (!method) {
195- continue;
196- }
197- }
198-
199- export { endpoints };
200- */
92+ export { endpoints } ;
0 commit comments