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
ShravanOP13mo ago
I need access to kv in a function that's called by hono. its an inngest server
app.all(
'/api/inngest',
serve({
client: inngestClient,
functions: inngestFunctionsList,
})
);
app.all(
'/api/inngest',
serve({
client: inngestClient,
functions: inngestFunctionsList,
})
);
I'm not sure how else to gett access to KV. Is it bad practice to bind it to globalThis?
Cyb3r-Jak3
Cyb3r-Jak313mo ago
Use c.env to get your binding. https://hono.dev/api/context#env
Context - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute@Edge, Deno, Bun, Vercel, Lagon, Node.js, and others. Fast, but not only fast.
Want results from more Discord servers?
Add your server