JavaDad
JavaDad
Explore posts from servers
CDCloudflare Developers
Created by JavaDad on 4/15/2024 in #workers-help
Can not enable Log Push
No description
3 replies
HHono
Created by JavaDad on 4/10/2024 in #help
Typing Middleware JWT and others
Currently I am trying to type the c.get('jwtPayload') that is documented here. I have tried using the method described here in a global.d.ts file but this does not seem to take any affect, the jwtPayload get result is still typed as any. Currently I have removed the global.d.ts file and set types on the Hono instance instead so its more explicit. This is still not working for the jwtPayload (and other values I use in middelware like the oauth middleware):
// controller.ts
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();

app.use("/*", async (c, next) => {
const jwtMiddleware = jwt({
secret: c.env.JWT_SECRET,
});
return jwtMiddleware(c, next);
});

app.use("/*", async (c, next) => {
const jwtPayload = c.get("jwtPayload") as JWTPayload; // without cast is any
if (jwtPayload.role < Role.MANAGER) {
throw error(401, "Unauthorized");
}
});

app.get("/:id", async ({ get, env, req, json }) => {
const { id } = get("jwtPayload") as JWTPayload; // without cast is any
// More code below
// ...
});

// ===========================================

// auth.ts
app.post("/auth", async(c) => {
// Verification of google oauth omitted
// ...

// Once user is authed
const newUserToken = await sign({
id: user.id,
email: user.email,
name: user.name,
profilePicture: user.profilePicture,
role: user.role,
},
env.JWT_SECRET,
);

// Code that returns JWT to client to be included in Authorization header on subsequent requests
// ...
});
// controller.ts
const app = new Hono<{ Bindings: Bindings; Variables: Variables }>();

app.use("/*", async (c, next) => {
const jwtMiddleware = jwt({
secret: c.env.JWT_SECRET,
});
return jwtMiddleware(c, next);
});

app.use("/*", async (c, next) => {
const jwtPayload = c.get("jwtPayload") as JWTPayload; // without cast is any
if (jwtPayload.role < Role.MANAGER) {
throw error(401, "Unauthorized");
}
});

app.get("/:id", async ({ get, env, req, json }) => {
const { id } = get("jwtPayload") as JWTPayload; // without cast is any
// More code below
// ...
});

// ===========================================

// auth.ts
app.post("/auth", async(c) => {
// Verification of google oauth omitted
// ...

// Once user is authed
const newUserToken = await sign({
id: user.id,
email: user.email,
name: user.name,
profilePicture: user.profilePicture,
role: user.role,
},
env.JWT_SECRET,
);

// Code that returns JWT to client to be included in Authorization header on subsequent requests
// ...
});
23 replies
DTDrizzle Team
Created by JavaDad on 12/22/2023 in #help
How to select part of a string in Drizzle?
I have dates in ISO format (Stored as Strings) in my SQLite database. I need to be able to find a match only querying the year-month-day. Attached are two different ways I have tried this and neither seem to work.
const timers = await db.select().from(timerSchema).where(sql`to_tsvector ('simple', '${timerSchema date}') @@ to_tsquery('simple', '${queryDayString}'))`;
const timers = await db.select().from(timerSchema).where(sql`to_tsvector ('simple', '${timerSchema date}') @@ to_tsquery('simple', '${queryDayString}'))`;
const timers = await db.select().from(timerSchema).where(sql`timer_date like '%${queryDayString}%'`);
const timers = await db.select().from(timerSchema).where(sql`timer_date like '%${queryDayString}%'`);
4 replies
CDCloudflare Developers
Created by JavaDad on 4/28/2023 in #pages-help
Cloudflare Pages Function CORS error
4 replies
CDCloudflare Developers
Created by JavaDad on 3/1/2023 in #workers-help
Can't Work with Queues and D1 in the same project
Queues will not work when running wrangler dev as they are only supported with the --local command. Unfortunately, D1 does not seem to work correctly locally when using workers-qb as the following error always is thrown when running workers-qb locally but not when running remote.
Error: D1_ERROR
at D1Database._send (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:148:13)
at D1PreparedStatement.all (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:242:7)
... 3 lines matching cause stack trace ...
at Server.<anonymous> (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+http-server@2.12.0/node_modules/@miniflare/http-server/src/index.ts:307:20) {
[cause]: Error: Error: RangeError: Too many parameter values were provided
at D1Database._send (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:149:16)
at D1PreparedStatement.all (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:242:7)
at RegisterHandler (/Users/brayden/code/dd/rollcall-cloudflare/src/handlers/registerHandler.ts:29:21)
at Object.handle (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/itty-router@3.0.11/node_modules/itty-router/dist/itty-router.mjs:1:638)
at ServiceWorkerGlobalScope.[kDispatchFetch] (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+core@2.12.0/node_modules/@miniflare/core/src/standards/event.ts:385:13)
Error: D1_ERROR
at D1Database._send (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:148:13)
at D1PreparedStatement.all (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:242:7)
... 3 lines matching cause stack trace ...
at Server.<anonymous> (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+http-server@2.12.0/node_modules/@miniflare/http-server/src/index.ts:307:20) {
[cause]: Error: Error: RangeError: Too many parameter values were provided
at D1Database._send (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:149:16)
at D1PreparedStatement.all (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+d1@2.12.0/node_modules/@miniflare/d1/src/d1js.ts:242:7)
at RegisterHandler (/Users/brayden/code/dd/rollcall-cloudflare/src/handlers/registerHandler.ts:29:21)
at Object.handle (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/itty-router@3.0.11/node_modules/itty-router/dist/itty-router.mjs:1:638)
at ServiceWorkerGlobalScope.[kDispatchFetch] (/Users/brayden/code/dd/rollcall-cloudflare/node_modules/.pnpm/@miniflare+core@2.12.0/node_modules/@miniflare/core/src/standards/event.ts:385:13)
3 replies
CDCloudflare Developers
Created by JavaDad on 1/27/2023 in #workers-help
How to bind to services
Hello, I am new to workers and trying to understand service binding. In the wrangler init TypeScript project there is an environment interface:
export interface Env {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace;
//
// 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;
}
export interface Env {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace;
//
// 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;
}
What I am trying to understand is where the values KVNamespace or R2Bucket are coming from. In my wranlger.toml I have a binding = "DB" (in this case D1) but I don't know how to use env.DB in the code since TypeScript says DB does not exist on type Env. I understand it needs to be added into the interface but not sure how to reference it there.
8 replies