From 1eecd3d14182bc6cb57dfe3d113a1acca537f8a0 Mon Sep 17 00:00:00 2001 From: maxlkatze Date: Tue, 23 Jul 2024 09:09:22 +0200 Subject: [PATCH] fix(katzencore): Imports of Storage Drivers are resolvable again --- src/runtime/storage/StorageManagement.ts | 56 +++++++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/src/runtime/storage/StorageManagement.ts b/src/runtime/storage/StorageManagement.ts index 545047d..a621091 100644 --- a/src/runtime/storage/StorageManagement.ts +++ b/src/runtime/storage/StorageManagement.ts @@ -8,24 +8,66 @@ interface StorageManagementDriver extends Storage { interface ExtendedRuntimeConfig extends RuntimeConfig { storageKey: string storage: { - type: string - options: object + type: 'azure-app-configuration' | 'cloudflare-kv-binding' | 'fs' | 'github' | 'mongodb' | 'netlify-blobs' | 'planetscale' | 'redis' | 'vercel-kv' + options: unknown } } +interface DynamicModuleImport { + default: (opts: unknown) => Driver +} + +interface DynamicNitroPackImport { + (opts: unknown): Driver +} + export const useContentStorage = async (_runtimeConfig: RuntimeConfig): Promise => { const runtimeConfig = _runtimeConfig as ExtendedRuntimeConfig + let module: unknown + switch (runtimeConfig.storage.type) { + case 'azure-app-configuration': + module = await import('unstorage/drivers/azure-app-configuration') + break + case 'cloudflare-kv-binding': + module = await import('unstorage/drivers/cloudflare-kv-binding') + break + case 'fs': + module = await import('unstorage/drivers/fs') + break + case 'github': + module = await import('unstorage/drivers/github') + break + case 'mongodb': + module = await import('unstorage/drivers/mongodb') + break + case 'netlify-blobs': + module = await import('unstorage/drivers/netlify-blobs') + break + case 'planetscale': + module = await import('unstorage/drivers/planetscale') + break + case 'redis': + module = await import('unstorage/drivers/redis') + break + case 'vercel-kv': + module = await import('unstorage/drivers/vercel-kv') + break + default: + throw new Error(`Driver ${runtimeConfig.storage.type} not found`) + } + let driver: Driver try { - driver = (await import(`unstorage/drivers/${runtimeConfig.storage.type}`))(runtimeConfig.storage.options) + const nitroPackImport = module as DynamicNitroPackImport + driver = nitroPackImport(runtimeConfig.storage.options) as Driver } catch (e) { try { - driver = (await import(`unstorage/drivers/${runtimeConfig.storage.type}`)).default(JSON.parse(JSON.stringify(runtimeConfig.storage.options))) + const moduleImport = module as DynamicModuleImport + driver = moduleImport.default(runtimeConfig.storage.options) as Driver } catch (e) { - console.error(e) - throw new Error(`Driver ${runtimeConfig.storage.type} not found`) + throw new Error(`Driver ${runtimeConfig.storage.type} could not be imported`) } } @@ -34,7 +76,7 @@ export const useContentStorage = async (_runtimeConfig: RuntimeConfig): Promise< }) as StorageManagementDriver // Add custom method to publish content storage.publishContent = async (content) => { - // TODO + // TODO GITHUB ETC console.log('Publishing content', content) } return storage