Skip to content

Commit c6299f6

Browse files
static import from array test
1 parent 2d8923d commit c6299f6

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

db/mongodb.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ if (!MONGODB_URL && typeof MONGODB_URL !== 'string') {
1616
}
1717

1818
let client: mongoose.Mongoose | null = null;
19+
let startTime: number | null = null;
1920
let state = 'not-started';
2021

2122
async function connect() {
2223
state = 'connecting';
2324
console.log('Connecting to MongoDB...');
25+
startTime = Date.now();
2426
client = await mongoose.connect((MONGODB_URL as string) || '');
2527

2628
if (!client) {
@@ -41,7 +43,7 @@ const stateMiddleware: Middleware = (ctx, next) => {
4143
status: 500,
4244
},
4345
connecting: {
44-
body: 'Database connection in progress',
46+
body: `Database connection in progress ${startTime ? `${Math.floor((Date.now() - startTime) / 1000)} seconds passed)` : ''}`,
4547
status: 500,
4648
},
4749
failed: {

endpoints.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { walk } from 'https://deno.land/[email protected]/fs/walk.ts';
1+
// deno-lint-ignore-file no-explicit-any
22
import { Context } from 'https://deno.land/x/[email protected]/mod.ts';
33

4-
54
// All endpoint file paths should be written here
6-
const paths = [
7-
'./api/Users/whoAmI.ts',
8-
]
5+
const endpointExports = [await import('./api/Users/whoAmI.ts')];
96

10-
// deno-lint-ignore no-explicit-any
117
export type ctx = Context<Record<string, any>, Record<string, any>>;
128

139
type AllMethods =
@@ -37,22 +33,28 @@ const endpoints: Array<{
3733
* { pattern: new URLPattern({ pathname: "/api/whoAmI" }), handler: handler }
3834
* IF MATCH -> handler(req)
3935
*/
40-
for (const path of paths) {
41-
42-
let pattern, GET, POST, PUT, DELETE, PATCH;
36+
for (const _export of endpointExports) {
37+
let pattern: URLPattern | null = null;
38+
let GET: ((ctx: ctx) => Response | Promise<Response>) | null = null;
39+
let POST: ((ctx: ctx) => Response | Promise<Response>) | null = null;
40+
let PUT: ((ctx: ctx) => Response | Promise<Response>) | null = null;
41+
let DELETE: ((ctx: ctx) => Response | Promise<Response>) | null = null;
42+
let PATCH: ((ctx: ctx) => Response | Promise<Response>) | null = null;
4343
try {
44-
({ pattern, GET, POST, PUT, DELETE, PATCH } = await import(path));
44+
pattern = _export.pattern;
45+
GET = (_export as any).GET ? (_export as any).GET : null;
46+
POST = (_export as any).POST ? (_export as any).POST : null;
47+
PUT = (_export as any).PUT ? (_export as any).PUT : null;
48+
DELETE = (_export as any).DELETE ? (_export as any).DELETE : null;
49+
PATCH = (_export as any).PATCH ? (_export as any).PATCH : null;
4550
} catch (error) {
46-
console.error(
47-
`Failed to import ${path}`,
48-
error
49-
);
51+
console.error(`Failed to import `, _export, error);
5052
continue;
5153
}
5254
const method: AllMethods | '' = '';
5355
const availableMethods: Array<AllMethods> = [];
5456
const methods: {
55-
[key in AllMethods]?: (ctx: ctx) => Response | Promise<Response>;
57+
[key in AllMethods]?: (((ctx: ctx) => Response | Promise<Response>) | null);
5658
} = { GET, POST, PUT, DELETE, PATCH };
5759
for (const [method, handler] of Object.entries(methods) as [
5860
AllMethods,
@@ -195,4 +197,4 @@ for await (const walkEntry of walk(cwd)) {
195197
}
196198
197199
export { endpoints };
198-
*/
200+
*/

0 commit comments

Comments
 (0)