import { inspect } from "node:util"; export type BindingTypeMap = { D1Database: D1Database; KVNamespace: KVNamespace; DurableObjectNamespace: DurableObjectNamespace; R2Bucket: R2Bucket; }; export type GetBindingType = keyof BindingTypeMap; export type BindingMap = { key: string; value: BindingTypeMap[T] }; export function getBindings(env: any, type: T): BindingMap[] { const bindings: BindingMap[] = []; for (const key in env) { try { if ( (env[key] as any).constructor.name === type || String(env[key]) === `[object ${type}]` || inspect(env[key]).includes(type) ) { bindings.push({ key, value: env[key] as BindingTypeMap[T], }); } } catch (e) {} } return bindings; } export function getBinding(env: any, type: T): BindingMap { const bindings = getBindings(env, type); if (bindings.length === 0) { throw new Error(`No ${type} found in bindings`); } return bindings[0] as BindingMap; }