Trying to get access to bindings in a function that's called by hono

I wanted to know if the below was bad practice
export type Bindings = {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
INNGEST_WORKFLOW_PERSISTANCE_KV: KVNamespace;
// DATABASE: D1Database;

//
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
// MY_DURABLE_OBJECT: DurableObjectNamespace;
//
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket;
//
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
// MY_SERVICE: Fetcher;
//
// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
// MY_QUEUE: Queue;
// AI: any;
// VECTOR_INDEX: VectorizeIndex;
};

declare global {
export interface BINDINGS extends Bindings {}
// eslint-disable-next-line no-var
var BINDINGS: BINDINGS;
}

const app = new Hono<{ Bindings: Bindings }>();

app.use('*', async (c, next) => {
try {
console.log('hello');
globalThis.BINDINGS = c.env;
await next();
} catch (e) {
console.log(e);
}
});

app.get('/', (c) => c.text('Hello Hono!'));
export type Bindings = {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
INNGEST_WORKFLOW_PERSISTANCE_KV: KVNamespace;
// DATABASE: D1Database;

//
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
// MY_DURABLE_OBJECT: DurableObjectNamespace;
//
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket;
//
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
// MY_SERVICE: Fetcher;
//
// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
// MY_QUEUE: Queue;
// AI: any;
// VECTOR_INDEX: VectorizeIndex;
};

declare global {
export interface BINDINGS extends Bindings {}
// eslint-disable-next-line no-var
var BINDINGS: BINDINGS;
}

const app = new Hono<{ Bindings: Bindings }>();

app.use('*', async (c, next) => {
try {
console.log('hello');
globalThis.BINDINGS = c.env;
await next();
} catch (e) {
console.log(e);
}
});

app.get('/', (c) => c.text('Hello Hono!'));
2 Replies
Shravan
ShravanOP14mo ago
I need access to kv in a function called by inngest. I'm not sure if using globalThis to access the bindings is a good idea due to cloudflare's reuse of isolates. Is there a better way to do this?
app.all(
'/api/inngest',
serve({
client: inngestClient,
functions: inngestFunctionsList,
})
);
app.all(
'/api/inngest',
serve({
client: inngestClient,
functions: inngestFunctionsList,
})
);
James
James14mo ago
Personally I tend to pass around the context object into my functions You can type that as the same generic you pass into Hono Context<{Bindings: …}>
Want results from more Discord servers?
Add your server