I'm creating a cache-proxy with Cloudflare Workers. I'm using a self made class instance to abstract Airtable queries, that require couple of secrets to be instantiated.
Now I must instantiate it inside fetch every time, as it's the only way I know to access Cloudflare env variables. Is there a way to globally access them so I can instantiate the class outside the fetch scope?
import { useAirtable } from 'painless-airtable';
import router from './router';
import type { Env } from './types';
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
// I'd like to extract this out of fetch
const { AIRTABLE_BASE: base, AIRTABLE_TOKEN: token } = env;
const airtable = useAirtable({ base, token });
return router.handle(request, { airtable, env, ctx });
},
};
ctx
to children. Looks weird and not reliable. Should not rely on the library for accessing Env globally. – Weikert