Skip to content

Commit

Permalink
fix(katzencore): Imports of Storage Drivers are resolvable again
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlkatze committed Jul 23, 2024
1 parent af653c9 commit 1eecd3d
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/runtime/storage/StorageManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageManagementDriver> => {
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`)
}
}

Expand All @@ -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
Expand Down

0 comments on commit 1eecd3d

Please sign in to comment.