import type { Cache, CacheStorage } from '@cloudflare/workers-types'; import { createBindingProxy } from './proxy'; /** * Whether the bindings proxy is enabled and currently active. * * The proxy is enabled by default in development mode, but can be disabled by setting * `DISABLE_BINDINGS_PROXY` to `true`. * * Alternatively, it can be enabled in other environments by setting `ENABLE_BINDINGS_PROXY` to * `true`. * */ export const isProxyEnabled = () => typeof process !== 'undefined' && (process?.env?.ENABLE_BINDINGS_PROXY || (!process?.env?.DISABLE_BINDINGS_PROXY && process?.env?.NODE_ENV === 'development')); /** * Interfaces with a binding from the environment. * * @example * ```ts * const value = await binding('MY_KV').get('key'); * ``` * * @example * By default, `process.env` is used in production, however, a custom fallback can be provided. * ```ts * const MY_KV = binding('MY_KV', { fallback: platform.env }); * ``` * * @param id Binding ID. * @param opts Binding options, such as a custom fallback. * @returns Binding value. */ export const binding = (id: string, opts?: BindingOpts): T => { if (isProxyEnabled()) { return new Proxy( {}, { get: (_, prop) => createBindingProxy(id, { proxyType: 'binding' })[prop as keyof T], }, ) as T; } return (opts?.fallback ?? process?.env)?.[id] as T; }; type DeriveCacheReturnType = T extends 'default' | undefined ? Cache : Promise; /** * Interfaces with the Cloudflare Cache API. * * By default, the `default` cache is used, however, a custom cache can be provided by passing a * cache name as the first argument. * * @example * ```ts * const value = await cacheApi().put(..., ...); * ``` * * @example * ```ts * const value = await cacheApi('custom').put(..., ...); * ``` * * @param cacheName Name of the cache to open, or `undefined` to open the default cache. * @returns Cache instance. */ export const cacheApi = ( cacheName?: T, ): DeriveCacheReturnType => { if (isProxyEnabled()) { return new Proxy( {}, { get: (_, prop: keyof Cache) => createBindingProxy(cacheName ?? 'default', { proxyType: 'caches' })[prop], }, ) as DeriveCacheReturnType; } const cachesInstance = caches as unknown as CacheStorage; return ( cacheName === 'default' || cacheName === undefined ? cachesInstance.default : cachesInstance.open(cacheName) ) as DeriveCacheReturnType; }; type BindingOpts = { fallback: Record; };