@japikey/cloudflare 0.2.2 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,7 +24,7 @@ import {
24
24
  createApiKeyRouter,
25
25
  createJWKSRouter,
26
26
  isJWKSPath,
27
- type CreateApiKeyRouterOptions,
27
+ type ApiKeyRouterOptions,
28
28
  type CreateApiKeyData,
29
29
  } from '@japikey/cloudflare';
30
30
 
@@ -75,7 +75,7 @@ export default {
75
75
  const db = new D1Driver(env.DB);
76
76
  await db.ensureTable();
77
77
 
78
- const options: CreateApiKeyRouterOptions<Env> = {
78
+ const options: ApiKeyRouterOptions<Env> = {
79
79
  getUserId,
80
80
  parseCreateApiKeyRequest,
81
81
  issuer: new URL('https://example.com/'),
@@ -91,7 +91,11 @@ export default {
91
91
  // Handle JWKS routes
92
92
  const baseIssuer = new URL('https://example.com/');
93
93
  if (isJWKSPath(request, baseIssuer)) {
94
- const jwksRouter = createJWKSRouter(baseIssuer, db);
94
+ const jwksRouter = createJWKSRouter({
95
+ baseIssuer,
96
+ db,
97
+ maxAgeSeconds: 300,
98
+ });
95
99
  return jwksRouter.fetch(request, env);
96
100
  }
97
101
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { CreateApiKeyRouterOptions, CreateApiKeyData } from './router.ts';
1
+ import type { ApiKeyRouterOptions, CreateApiKeyData, JwksRouterOptions } from './router.ts';
2
2
  import { createApiKeyRouter, createJWKSRouter, isJWKSPath } from './router.ts';
3
3
  import D1Driver from './d1.ts';
4
- export { createApiKeyRouter, createJWKSRouter, isJWKSPath, D1Driver, type CreateApiKeyRouterOptions, type CreateApiKeyData, };
4
+ export { createApiKeyRouter, createJWKSRouter, isJWKSPath, D1Driver, type ApiKeyRouterOptions, type CreateApiKeyData, type JwksRouterOptions, };
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,QAAQ,MAAM,SAAS,CAAC;AAE/B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,KAAK,yBAAyB,EAC9B,KAAK,gBAAgB,GACtB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC/E,OAAO,QAAQ,MAAM,SAAS,CAAC;AAE/B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,GACvB,CAAC"}
package/dist/router.d.ts CHANGED
@@ -1,19 +1,27 @@
1
1
  import type { DatabaseDriver } from '@japikey/japikey';
2
- import type { ExportedHandler } from '@cloudflare/workers-types';
2
+ import type { Request as WorkerRequest, ExportedHandlerFetchHandler, IncomingRequestCfProperties } from '@cloudflare/workers-types';
3
3
  export type CreateApiKeyData = {
4
4
  expiresAt: Date;
5
5
  claims: Record<string, unknown>;
6
6
  databaseMetadata: Record<string, unknown>;
7
7
  };
8
- export type CreateApiKeyRouterOptions<Env> = {
9
- getUserId: (request: Request, env: Env) => Promise<string>;
10
- parseCreateApiKeyRequest: (request: Request, env: Env) => Promise<CreateApiKeyData>;
8
+ export type ApiKeyRouterOptions<Env> = {
9
+ getUserId: (request: WorkerRequest<unknown, IncomingRequestCfProperties>, env: Env) => Promise<string>;
10
+ parseCreateApiKeyRequest: (request: WorkerRequest<unknown, IncomingRequestCfProperties>, env: Env) => Promise<CreateApiKeyData>;
11
11
  issuer: URL;
12
12
  aud: string;
13
13
  db: DatabaseDriver;
14
14
  routePrefix: string;
15
15
  };
16
- export declare function createJWKSRouter<Env>(baseIssuer: URL, db: DatabaseDriver): ExportedHandler<Env>;
17
- export declare function isJWKSPath(request: Request, baseIssuer: URL): boolean;
18
- export declare function createApiKeyRouter<Env>(options: CreateApiKeyRouterOptions<Env>): ExportedHandler<Env>;
16
+ export type JwksRouterOptions = {
17
+ baseIssuer: URL;
18
+ db: DatabaseDriver;
19
+ maxAgeSeconds?: number;
20
+ };
21
+ export type SimpleHandler<Env> = {
22
+ fetch: ExportedHandlerFetchHandler<Env>;
23
+ };
24
+ export declare function createJWKSRouter<Env>(options: JwksRouterOptions): SimpleHandler<Env>;
25
+ export declare function isJWKSPath<C>(request: WorkerRequest<unknown, IncomingRequestCfProperties>, baseIssuer: URL): boolean;
26
+ export declare function createApiKeyRouter<Env>(options: ApiKeyRouterOptions<Env>): SimpleHandler<Env>;
19
27
  //# sourceMappingURL=router.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAiB,MAAM,kBAAkB,CAAC;AAGtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAGjE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC3C,CAAC;AAEF,MAAM,MAAM,yBAAyB,CAAC,GAAG,IAAI;IAC3C,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,KAAK,O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AAE,aAAa,CAAC,OAAO,EAAE,2BAA2B,CAAC,EAC5D,GAAG,EAAE,GAAG,KACL,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC/B,MAAM,EAAE,GAAG,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,EAAE,EAAE,cAAc,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,EAAE,GAAG,CAAC;IAChB,EAAE,EAAE,cAAc,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,aAAa,CAAC,GAAG,IAAI;IAC/B,KAAK,EAAE,2BAA2B,CAAC,GAAG,CAAC,CAAC;CACzC,CAAC;AA2EF,wBAAgB,gBAAgB,CAAC,GAAG,EAClC,OAAO,EAAE,iBAAiB,GACzB,aAAa,CAAC,GAAG,CAAC,CASpB;AAED,wBAAgB,UAAU,CAAC,CAAC,EAC1B,OAAO,EAAE,aAAa,CAAC,OAAO,EAAE,2BAA2B,CAAC,EAC5D,UAAU,EAAE,GAAG,GACd,OAAO,CAOT;AA6HD,wBAAgB,kBAAkB,CAAC,GAAG,EACpC,OAAO,EAAE,mBAAmB,CAAC,GAAG,CAAC,GAChC,aAAa,CAAC,GAAG,CAAC,CAqCpB"}
package/dist/router.js CHANGED
@@ -25,18 +25,21 @@ function wrapError(fn) {
25
25
  }
26
26
  };
27
27
  }
28
- async function handleJWKSRequest(kid, db) {
29
- const row = await db.getApiKey(kid);
28
+ async function handleJWKSRequest(kid, options) {
29
+ const row = await options.db.getApiKey(kid);
30
30
  if (!row || row.revoked) {
31
31
  throw new NotFoundError('API key not found');
32
32
  }
33
33
  const jwks = {
34
34
  keys: [row.jwk],
35
35
  };
36
+ const headers = {
37
+ 'Content-Type': 'application/json',
38
+ };
39
+ const maxAge = Math.max(options.maxAgeSeconds ?? 0, 0); // Negative values are undefined per mdn - clamp to 0
40
+ headers['Cache-Control'] = `max-age=${maxAge}`;
36
41
  return new Response(JSON.stringify(jwks), {
37
- headers: {
38
- 'Content-Type': 'application/json',
39
- },
42
+ headers,
40
43
  });
41
44
  }
42
45
  function getWellKnownKid(request, baseIssuer) {
@@ -60,11 +63,11 @@ function getWellKnownKid(request, baseIssuer) {
60
63
  }
61
64
  return kid;
62
65
  }
63
- export function createJWKSRouter(baseIssuer, db) {
66
+ export function createJWKSRouter(options) {
64
67
  return {
65
- fetch: wrapError(async function fetch(request, env) {
66
- const kid = getWellKnownKid(request, baseIssuer);
67
- return handleJWKSRequest(kid, db);
68
+ fetch: wrapError(async function fetch(request) {
69
+ const kid = getWellKnownKid(request, options.baseIssuer);
70
+ return handleJWKSRequest(kid, options);
68
71
  }),
69
72
  };
70
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@japikey/cloudflare",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "description": "Cloudflare worker-based endpoints for japikey",
5
5
  "engines": {
6
6
  "node": ">=22.5.0"
@@ -44,11 +44,10 @@
44
44
  "format:check": "prettier --check src/**/*.ts"
45
45
  },
46
46
  "dependencies": {
47
- "@japikey/japikey": "0.2.2",
47
+ "@japikey/japikey": "0.3.1",
48
48
  "path-to-regexp": "^8.2.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@cloudflare/workers-types": "^4.20250806.0",
52
51
  "@types/node": "^24.1.0",
53
52
  "@typescript-eslint/eslint-plugin": "^8.38.0",
54
53
  "@typescript-eslint/parser": "^8.38.0",
@@ -60,5 +59,8 @@
60
59
  "prettier": "^3.6.2",
61
60
  "rimraf": "^6.0.1",
62
61
  "vitest": "^3.2.4"
62
+ },
63
+ "peerDependencies": {
64
+ "@cloudflare/workers-types": "^4.20250810.0"
63
65
  }
64
66
  }