(moduleId: string)
| 22 | * @returns Imported resource module with handler |
| 23 | */ |
| 24 | export async function importResourceModule(moduleId: string): Promise<ImportedResourceModule> { |
| 25 | const cached = moduleCache.get(moduleId); |
| 26 | if (cached) { |
| 27 | return cached; |
| 28 | } |
| 29 | |
| 30 | const packageRoot = getPackageRoot(); |
| 31 | const modulePath = path.join(packageRoot, 'build', `${moduleId}.js`); |
| 32 | const moduleUrl = pathToFileURL(modulePath).href; |
| 33 | |
| 34 | let mod: Record<string, unknown>; |
| 35 | try { |
| 36 | mod = (await import(moduleUrl)) as Record<string, unknown>; |
| 37 | } catch (err) { |
| 38 | throw new Error(`Failed to import resource module '${moduleId}': ${err}`); |
| 39 | } |
| 40 | |
| 41 | if (typeof mod.handler !== 'function') { |
| 42 | throw new Error( |
| 43 | `Resource module '${moduleId}' does not export the required shape. ` + |
| 44 | `Expected a named export: export const handler = ...`, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | const result: ImportedResourceModule = { |
| 49 | handler: mod.handler as ImportedResourceModule['handler'], |
| 50 | }; |
| 51 | |
| 52 | moduleCache.set(moduleId, result); |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Reset module cache (for tests). |
no test coverage detected