(opts: BootstrapOptions = {})
| 22 | } |
| 23 | |
| 24 | export async function bootstrapLoader(opts: BootstrapOptions = {}): Promise<void> { |
| 25 | const base = opts.baseUrl ?? '/api/widget-modules' |
| 26 | const importer = opts.importer ?? ((url: string) => import(/* @vite-ignore */ url)) |
| 27 | const hostVersion = opts.hostSdkVersion ?? SDK_VERSION |
| 28 | |
| 29 | const res = await fetch(base, { credentials: 'include' }) |
| 30 | if (!res.ok) { |
| 31 | throw new Error(`bootstrapLoader: list failed ${res.status}`) |
| 32 | } |
| 33 | const body = (await res.json()) as { data: ListEntry[] } |
| 34 | const modules = body.data |
| 35 | |
| 36 | await Promise.allSettled( |
| 37 | modules.map(async (entry) => { |
| 38 | try { |
| 39 | if (!isCompatible(hostVersion, entry.manifest.sdkVersion)) { |
| 40 | throw new Error( |
| 41 | `sdk version mismatch: host ${hostVersion} does not satisfy manifest range ${entry.manifest.sdkVersion}` |
| 42 | ) |
| 43 | } |
| 44 | const url = `${base}/${entry.id}/${entryAssetPath(entry.entry_path)}` |
| 45 | const mod = await importer(url) |
| 46 | if (!mod.default || mod.default.__brand !== 'WidgetModule') { |
| 47 | throw new Error(`module ${entry.id} did not export a WidgetModule via default`) |
| 48 | } |
| 49 | registryActions.register(entry.id, mod.default, entry.manifest) |
| 50 | } catch (err) { |
| 51 | registryActions.recordLoadFailure(entry.id, err instanceof Error ? err : new Error(String(err))) |
| 52 | } |
| 53 | }) |
| 54 | ) |
| 55 | } |
no test coverage detected