Skip to content

Commit 2d8923d

Browse files
static import test
1 parent 69a9629 commit 2d8923d

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

endpoints.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { walk } from 'https://deno.land/[email protected]/fs/walk.ts';
22
import { Context } from 'https://deno.land/x/[email protected]/mod.ts';
33

4+
5+
// All endpoint file paths should be written here
6+
const paths = [
7+
'./api/Users/whoAmI.ts',
8+
]
9+
410
// deno-lint-ignore no-explicit-any
511
export type ctx = Context<Record<string, any>, Record<string, any>>;
612

@@ -15,7 +21,6 @@ type AllMethods =
1521
| 'CONNECT'
1622
| 'TRACE';
1723

18-
const cwd = Deno.cwd();
1924
const endpoints: Array<{
2025
endpoint: {
2126
pattern: URLPattern;
@@ -32,6 +37,90 @@ const endpoints: Array<{
3237
* { pattern: new URLPattern({ pathname: "/api/whoAmI" }), handler: handler }
3338
* IF MATCH -> handler(req)
3439
*/
40+
for (const path of paths) {
41+
42+
let pattern, GET, POST, PUT, DELETE, PATCH;
43+
try {
44+
({ pattern, GET, POST, PUT, DELETE, PATCH } = await import(path));
45+
} catch (error) {
46+
console.error(
47+
`Failed to import ${path}`,
48+
error
49+
);
50+
continue;
51+
}
52+
const method: AllMethods | '' = '';
53+
const availableMethods: Array<AllMethods> = [];
54+
const methods: {
55+
[key in AllMethods]?: (ctx: ctx) => Response | Promise<Response>;
56+
} = { GET, POST, PUT, DELETE, PATCH };
57+
for (const [method, handler] of Object.entries(methods) as [
58+
AllMethods,
59+
(ctx: ctx) => Response | Promise<Response>
60+
][]) {
61+
if (typeof handler === 'function') {
62+
availableMethods.push(method);
63+
endpoints.push({
64+
endpoint: {
65+
pattern: pattern,
66+
method,
67+
},
68+
handler,
69+
});
70+
}
71+
}
72+
73+
endpoints.push({
74+
endpoint: {
75+
pattern: pattern,
76+
method: 'OPTIONS',
77+
},
78+
handler: (ctx) => {
79+
ctx.response.headers.set('Allow', availableMethods.join(', '));
80+
ctx.response.body = "Look header's Allow property :)";
81+
ctx.response.status = 200;
82+
return new Response(null, { status: 200 });
83+
},
84+
});
85+
if (!method) {
86+
continue;
87+
}
88+
}
89+
90+
export { endpoints };
91+
92+
/*
93+
// deno-lint-ignore no-explicit-any
94+
export type ctx = Context<Record<string, any>, Record<string, any>>;
95+
96+
type AllMethods =
97+
| 'GET'
98+
| 'POST'
99+
| 'PUT'
100+
| 'DELETE'
101+
| 'PATCH'
102+
| 'OPTIONS'
103+
| 'HEAD'
104+
| 'CONNECT'
105+
| 'TRACE';
106+
107+
const cwd = Deno.cwd();
108+
const endpoints: Array<{
109+
endpoint: {
110+
pattern: URLPattern;
111+
method: AllMethods;
112+
};
113+
handler: (ctx: ctx) => Response | Promise<Response>;
114+
}> = [];
115+
116+
/*
117+
* Loop through all files in the current directory and subdirectories
118+
* to find the handler for the requested URL (just checks api folder)
119+
*
120+
* api\Users\whoAmI.ts -> ./api/Users/whoAmI.ts
121+
* { pattern: new URLPattern({ pathname: "/api/whoAmI" }), handler: handler }
122+
* IF MATCH -> handler(req)
123+
*
35124
for await (const walkEntry of walk(cwd)) {
36125
const type = walkEntry.isSymlink
37126
? 'symlink'
@@ -106,3 +195,4 @@ for await (const walkEntry of walk(cwd)) {
106195
}
107196
108197
export { endpoints };
198+
*/

0 commit comments

Comments
 (0)