1- import { walk } from 'https:// deno.land/[email protected] /fs/walk.ts' ; 1+ // deno-lint-ignore-file no-explicit-any
22import { 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
117export type ctx = Context < Record < string , any > , Record < string , any > > ;
128
139type 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
197199export { endpoints };
198- */
200+ */
0 commit comments