@every-app/sdk 0.1.13 → 0.1.14

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.
Files changed (32) hide show
  1. package/dist/shared/bypassGatewayLocalOnly.d.ts +2 -1
  2. package/dist/shared/bypassGatewayLocalOnly.d.ts.map +1 -1
  3. package/dist/shared/bypassGatewayLocalOnly.js +6 -2
  4. package/dist/tanstack/server/authenticateRequest.d.ts +2 -0
  5. package/dist/tanstack/server/authenticateRequest.d.ts.map +1 -1
  6. package/dist/tanstack/server/authenticateRequest.js +75 -3
  7. package/package.json +3 -3
  8. package/src/cloudflare/getLocalD1Url.ts +0 -64
  9. package/src/cloudflare/index.ts +0 -1
  10. package/src/cloudflare/lazyInit.ts +0 -67
  11. package/src/cloudflare/server/gateway.test.ts +0 -262
  12. package/src/cloudflare/server/gateway.ts +0 -114
  13. package/src/cloudflare/server/index.ts +0 -2
  14. package/src/core/authenticatedFetch.ts +0 -54
  15. package/src/core/index.ts +0 -12
  16. package/src/core/sessionManager.test.ts +0 -939
  17. package/src/core/sessionManager.ts +0 -492
  18. package/src/env.d.ts +0 -13
  19. package/src/shared/bypassGatewayLocalOnly.ts +0 -55
  20. package/src/shared/parseMessagePayload.ts +0 -22
  21. package/src/tanstack/EmbeddedAppProvider.tsx +0 -96
  22. package/src/tanstack/GatewayRequiredError.tsx +0 -150
  23. package/src/tanstack/_internal/useEveryAppSession.test.ts +0 -40
  24. package/src/tanstack/_internal/useEveryAppSession.tsx +0 -74
  25. package/src/tanstack/index.ts +0 -3
  26. package/src/tanstack/server/authConfig.ts +0 -19
  27. package/src/tanstack/server/authenticateRequest.test.ts +0 -482
  28. package/src/tanstack/server/authenticateRequest.ts +0 -143
  29. package/src/tanstack/server/index.ts +0 -3
  30. package/src/tanstack/server/types.ts +0 -4
  31. package/src/tanstack/useEveryAppRouter.tsx +0 -83
  32. package/src/tanstack/useSessionTokenClientMiddleware.ts +0 -43
@@ -3,7 +3,8 @@ export declare const BYPASS_GATEWAY_LOCAL_ONLY_USER_ID = "demo-local-user";
3
3
  export declare const BYPASS_GATEWAY_LOCAL_ONLY_EMAIL = "demo-local-user@local";
4
4
  export declare function isBypassGatewayLocalOnlyClient(): boolean;
5
5
  export declare function isBypassGatewayLocalOnlyServer(): boolean;
6
- export declare function createBypassGatewayLocalOnlySessionPayload(audience: string): {
6
+ export declare function createBypassGatewayLocalOnlySessionPayload(audience: string, orgId: string): {
7
+ orgId: string;
7
8
  sub: string;
8
9
  email: string;
9
10
  iss: string;
@@ -1 +1 @@
1
- {"version":3,"file":"bypassGatewayLocalOnly.d.ts","sourceRoot":"","sources":["../../src/shared/bypassGatewayLocalOnly.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,+BAA+B,8BAA8B,CAAC;AAC3E,eAAO,MAAM,iCAAiC,oBAAoB,CAAC;AACnE,eAAO,MAAM,+BAA+B,0BAA0B,CAAC;AAEvE,wBAAgB,8BAA8B,IAAI,OAAO,CAYxD;AAED,wBAAgB,8BAA8B,IAAI,OAAO,CAsBxD;AAED,wBAAgB,0CAA0C,CAAC,QAAQ,EAAE,MAAM;;;;;;;EAY1E"}
1
+ {"version":3,"file":"bypassGatewayLocalOnly.d.ts","sourceRoot":"","sources":["../../src/shared/bypassGatewayLocalOnly.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,+BAA+B,8BAA8B,CAAC;AAC3E,eAAO,MAAM,iCAAiC,oBAAoB,CAAC;AACnE,eAAO,MAAM,+BAA+B,0BAA0B,CAAC;AAEvE,wBAAgB,8BAA8B,IAAI,OAAO,CAYxD;AAED,wBAAgB,8BAA8B,IAAI,OAAO,CAsBxD;AAED,wBAAgB,0CAA0C,CACxD,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM;;;;;;;;EAkBd"}
@@ -27,10 +27,10 @@ export function isBypassGatewayLocalOnlyServer() {
27
27
  }
28
28
  return false;
29
29
  }
30
- export function createBypassGatewayLocalOnlySessionPayload(audience) {
30
+ export function createBypassGatewayLocalOnlySessionPayload(audience, orgId) {
31
31
  const issuedAt = Math.floor(Date.now() / 1000);
32
32
  const expiresAt = issuedAt + 60 * 60;
33
- return {
33
+ const payload = {
34
34
  sub: BYPASS_GATEWAY_LOCAL_ONLY_USER_ID,
35
35
  email: BYPASS_GATEWAY_LOCAL_ONLY_EMAIL,
36
36
  iss: "local",
@@ -38,4 +38,8 @@ export function createBypassGatewayLocalOnlySessionPayload(audience) {
38
38
  iat: issuedAt,
39
39
  exp: expiresAt,
40
40
  };
41
+ return {
42
+ ...payload,
43
+ orgId,
44
+ };
41
45
  }
@@ -16,6 +16,8 @@ interface SessionTokenPayload {
16
16
  iat: number;
17
17
  /** User email - used for user provisioning in apps */
18
18
  email?: string;
19
+ /** Organization ID used for org-bound runtime enforcement */
20
+ orgId?: string;
19
21
  }
20
22
  export declare function authenticateRequest(authConfig: AuthConfig, providedRequest?: Request): Promise<SessionTokenPayload | null>;
21
23
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"authenticateRequest.d.ts","sourceRoot":"","sources":["../../../src/tanstack/server/authenticateRequest.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAQ7C;;;GAGG;AACH,UAAU,mBAAmB;IAC3B,8BAA8B;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,6DAA6D;IAC7D,GAAG,EAAE,MAAM,CAAC;IACZ,2BAA2B;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,0BAA0B;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAsB,mBAAmB,CACvC,UAAU,EAAE,UAAU,EACtB,eAAe,CAAC,EAAE,OAAO,GACxB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAoDrC;AAyCD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAK3E"}
1
+ {"version":3,"file":"authenticateRequest.d.ts","sourceRoot":"","sources":["../../../src/tanstack/server/authenticateRequest.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAQ7C;;;GAGG;AACH,UAAU,mBAAmB;IAC3B,8BAA8B;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,6DAA6D;IAC7D,GAAG,EAAE,MAAM,CAAC;IACZ,2BAA2B;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,0BAA0B;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAYD,wBAAsB,mBAAmB,CACvC,UAAU,EAAE,UAAU,EACtB,eAAe,CAAC,EAAE,OAAO,GACxB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CA+FrC;AA8CD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAK3E"}
@@ -5,9 +5,10 @@ import { BYPASS_GATEWAY_LOCAL_ONLY_TOKEN, createBypassGatewayLocalOnlySessionPay
5
5
  export async function authenticateRequest(authConfig, providedRequest) {
6
6
  const request = providedRequest || getRequest();
7
7
  const authHeader = request.headers.get("authorization");
8
- const bypassGatewayLocalOnlyEnv = env.BYPASS_GATEWAY_LOCAL_ONLY;
8
+ const expectedOrganizationId = getOptionalEnvValue("EVERY_APP_ORG_ID");
9
+ const appTenancyMode = getAppTenancyMode();
9
10
  const isBypassGatewayLocalOnly = import.meta.env.PROD !== true &&
10
- (bypassGatewayLocalOnlyEnv === "true" ||
11
+ (getOptionalEnvValue("BYPASS_GATEWAY_LOCAL_ONLY") === "true" ||
11
12
  isBypassGatewayLocalOnlyServer() === true);
12
13
  if (!authHeader) {
13
14
  return null;
@@ -20,10 +21,37 @@ export async function authenticateRequest(authConfig, providedRequest) {
20
21
  if (token !== BYPASS_GATEWAY_LOCAL_ONLY_TOKEN) {
21
22
  return null;
22
23
  }
23
- return createBypassGatewayLocalOnlySessionPayload(authConfig.audience);
24
+ if (!expectedOrganizationId) {
25
+ console.error(JSON.stringify({
26
+ message: "BYPASS_GATEWAY_LOCAL_ONLY requires EVERY_APP_ORG_ID to be set.",
27
+ audience: authConfig.audience,
28
+ appTenancyMode,
29
+ }));
30
+ return null;
31
+ }
32
+ const session = createBypassGatewayLocalOnlySessionPayload(authConfig.audience, expectedOrganizationId);
33
+ if (!validateOrganizationBinding({
34
+ session,
35
+ source: "bypass",
36
+ audience: authConfig.audience,
37
+ expectedOrganizationId,
38
+ appTenancyMode,
39
+ })) {
40
+ return null;
41
+ }
42
+ return session;
24
43
  }
25
44
  try {
26
45
  const session = await verifySessionToken(token, authConfig);
46
+ if (!validateOrganizationBinding({
47
+ session,
48
+ source: "session",
49
+ audience: authConfig.audience,
50
+ expectedOrganizationId,
51
+ appTenancyMode,
52
+ })) {
53
+ return null;
54
+ }
27
55
  return session;
28
56
  }
29
57
  catch (error) {
@@ -39,10 +67,16 @@ export async function authenticateRequest(authConfig, providedRequest) {
39
67
  errorType: error instanceof Error ? error.constructor.name : "Unknown",
40
68
  issuer: authConfig.issuer,
41
69
  audience: authConfig.audience,
70
+ expectedOrganizationId,
71
+ appTenancyMode,
42
72
  }));
43
73
  return null;
44
74
  }
45
75
  }
76
+ function getAppTenancyMode() {
77
+ const mode = getOptionalEnvValue("APP_TENANCY_MODE")?.toLowerCase();
78
+ return mode === "multi" ? "multi" : "single";
79
+ }
46
80
  async function verifySessionToken(token, config) {
47
81
  const { issuer, audience } = config;
48
82
  if (!issuer) {
@@ -80,3 +114,41 @@ export function extractBearerToken(authHeader) {
80
114
  }
81
115
  return authHeader.substring(7);
82
116
  }
117
+ function getOptionalEnvValue(key) {
118
+ const value = env[key];
119
+ const trimmed = value?.trim();
120
+ return trimmed || null;
121
+ }
122
+ function validateOrganizationBinding({ session, source, audience, expectedOrganizationId, appTenancyMode, }) {
123
+ if (!session.orgId) {
124
+ console.error(JSON.stringify({
125
+ message: source === "bypass"
126
+ ? "Bypass mode requires organization claim"
127
+ : "Session token is missing orgId. SDK v0.2.0 requires org-bound session tokens. Redeploy the app with EVERY_APP_ORG_ID configured and request a fresh session token.",
128
+ audience,
129
+ appTenancyMode,
130
+ }));
131
+ return false;
132
+ }
133
+ if (appTenancyMode === "single" && !expectedOrganizationId) {
134
+ console.error(JSON.stringify({
135
+ message: "EVERY_APP_ORG_ID is required when APP_TENANCY_MODE=single. Configure EVERY_APP_ORG_ID in worker secrets (for example via `every app deploy`) and retry.",
136
+ audience,
137
+ appTenancyMode,
138
+ }));
139
+ return false;
140
+ }
141
+ if (appTenancyMode === "single" && session.orgId !== expectedOrganizationId) {
142
+ console.error(JSON.stringify({
143
+ message: source === "bypass"
144
+ ? "Bypass mode organization mismatch"
145
+ : "Session token organization mismatch",
146
+ tokenOrgId: session.orgId,
147
+ expectedOrganizationId,
148
+ audience,
149
+ appTenancyMode,
150
+ }));
151
+ return false;
152
+ }
153
+ return true;
154
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@every-app/sdk",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -27,11 +27,11 @@
27
27
  },
28
28
  "files": [
29
29
  "dist",
30
- "src"
30
+ "README.md"
31
31
  ],
32
32
  "scripts": {
33
- "postinstall": "tsc -p tsconfig.build.json",
34
33
  "build": "tsc -p tsconfig.build.json",
34
+ "prepack": "npm run build",
35
35
  "build:prepublish": "pnpm install --ignore-scripts && npm run types:check && npm run build",
36
36
  "prepublishOnly": "npm run build:prepublish",
37
37
  "types:check": "tsc --noEmit",
@@ -1,64 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { execSync } from "child_process";
4
- import { parse } from "jsonc-parser";
5
-
6
- function findSqliteFile(basePath: string): string | undefined {
7
- return fs
8
- .readdirSync(basePath, { encoding: "utf-8", recursive: true })
9
- .find((f) => f.endsWith(".sqlite"));
10
- }
11
-
12
- export function getLocalD1Url() {
13
- const basePath = path.resolve(".wrangler");
14
-
15
- // Check if .wrangler directory exists
16
- if (!fs.existsSync(basePath)) {
17
- console.error(
18
- "================================================================================",
19
- );
20
- console.error("WARNING: .wrangler directory not found");
21
- console.error("This is expected in CI/non-development environments.");
22
- console.error(
23
- "The local D1 database is only available after running 'wrangler dev' which you can trigger by running 'npm run dev'.",
24
- );
25
- console.error(
26
- "================================================================================",
27
- );
28
- return null;
29
- }
30
-
31
- let dbFile = findSqliteFile(basePath);
32
-
33
- if (!dbFile) {
34
- // Read wrangler.jsonc to get the database name
35
- const wranglerConfigPath = path.resolve("wrangler.jsonc");
36
- const wranglerConfig = parse(fs.readFileSync(wranglerConfigPath, "utf-8"));
37
-
38
- const databaseName = wranglerConfig.d1_databases?.[0]?.database_name;
39
-
40
- if (!databaseName) {
41
- throw new Error(
42
- "Could not find database_name in wrangler.jsonc d1_databases configuration",
43
- );
44
- }
45
-
46
- // Execute the command to initialize the local database
47
- console.log(`Initializing local D1 database: ${databaseName}...`);
48
- execSync(
49
- `npx wrangler d1 execute ${databaseName} --local --command "SELECT 1;"`,
50
- { stdio: "pipe" },
51
- );
52
-
53
- // Try to find the db file again after initialization
54
- dbFile = findSqliteFile(basePath);
55
-
56
- if (!dbFile) {
57
- throw new Error(
58
- `Failed to initialize local D1 database. The sqlite file was not created.`,
59
- );
60
- }
61
- }
62
-
63
- return path.resolve(basePath, dbFile);
64
- }
@@ -1 +0,0 @@
1
- export { lazyInitForWorkers } from "./lazyInit.js";
@@ -1,67 +0,0 @@
1
- /**
2
- * Wraps a factory function in a Proxy to defer initialization until first access.
3
- * This prevents async operations (Like creating Tanstack DB Collections) from running in Cloudflare Workers' global scope.
4
- *
5
- * @param factory - A function that creates and returns the resource.
6
- * Must be a callback to defer execution; passing the value directly
7
- * would evaluate it at module load time, triggering the Cloudflare error.
8
- * @returns A Proxy that lazily initializes the resource on first property access
9
- *
10
- * @example
11
- * ```ts
12
- * export const myCollection = lazyInitForWorkers(() =>
13
- * createCollection(queryCollectionOptions({
14
- * queryKey: ["myData"],
15
- * queryFn: async () => fetchData(),
16
- * // ... other options
17
- * }))
18
- * );
19
- * ```
20
- */
21
- export function lazyInitForWorkers<T extends object>(factory: () => T): T {
22
- // Closure: This variable is captured by getInstance() and the Proxy traps below.
23
- // It remains in memory as long as the returned Proxy is referenced, enabling singleton behavior.
24
- let instance: T | null = null;
25
-
26
- function getInstance() {
27
- if (!instance) {
28
- instance = factory();
29
- }
30
- return instance;
31
- }
32
-
33
- return new Proxy({} as T, {
34
- get(_, prop) {
35
- const inst = getInstance();
36
- const value = inst[prop as keyof T];
37
- // Bind methods to the instance to preserve `this` context
38
- return typeof value === "function" ? value.bind(inst) : value;
39
- },
40
- set(_, prop, value) {
41
- const inst = getInstance();
42
- (inst as any)[prop] = value;
43
- return true;
44
- },
45
- deleteProperty(_, prop) {
46
- const inst = getInstance();
47
- delete (inst as any)[prop];
48
- return true;
49
- },
50
- has(_, prop) {
51
- const inst = getInstance();
52
- return prop in inst;
53
- },
54
- ownKeys(_) {
55
- const inst = getInstance();
56
- return Reflect.ownKeys(inst);
57
- },
58
- getOwnPropertyDescriptor(_, prop) {
59
- const inst = getInstance();
60
- return Reflect.getOwnPropertyDescriptor(inst, prop);
61
- },
62
- getPrototypeOf(_) {
63
- const inst = getInstance();
64
- return Reflect.getPrototypeOf(inst);
65
- },
66
- });
67
- }
@@ -1,262 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import { fetchGateway, getGatewayUrl } from "./gateway";
3
-
4
- type TestFetcher = {
5
- fetch: ReturnType<typeof vi.fn>;
6
- };
7
-
8
- type TestEnv = {
9
- GATEWAY_URL?: string;
10
- EVERY_APP_GATEWAY?: TestFetcher;
11
- GATEWAY_APP_API_TOKEN?: string;
12
- APP_TOKEN?: string;
13
- };
14
-
15
- describe("gateway server helpers", () => {
16
- beforeEach(() => {
17
- vi.restoreAllMocks();
18
- });
19
-
20
- afterEach(() => {
21
- vi.restoreAllMocks();
22
- });
23
-
24
- it("returns gateway URL and throws when missing", () => {
25
- expect(getGatewayUrl({ GATEWAY_URL: "https://gateway.example.com" })).toBe(
26
- "https://gateway.example.com",
27
- );
28
-
29
- expect(() => getGatewayUrl({} as TestEnv)).toThrow(
30
- "GATEWAY_URL is required",
31
- );
32
- });
33
-
34
- it("fetches via HTTP when service binding is unavailable", async () => {
35
- const fetchMock = vi
36
- .spyOn(globalThis, "fetch")
37
- .mockResolvedValue(new Response("ok", { status: 200 }));
38
-
39
- const env: TestEnv = {
40
- GATEWAY_URL: "https://gateway.example.com",
41
- GATEWAY_APP_API_TOKEN: "eat_test_token",
42
- };
43
-
44
- await fetchGateway({
45
- env,
46
- url: "/api/ai/openai/v1/responses?stream=true",
47
- init: { method: "POST" },
48
- });
49
-
50
- expect(fetchMock).toHaveBeenCalledTimes(1);
51
- const [requestArg] = fetchMock.mock.calls[0];
52
- const request = requestArg as Request;
53
-
54
- expect(request.url).toBe(
55
- "https://gateway.example.com/api/ai/openai/v1/responses?stream=true",
56
- );
57
- });
58
-
59
- it("throws when absolute URL points to non-gateway origin", async () => {
60
- const fetchMock = vi
61
- .spyOn(globalThis, "fetch")
62
- .mockResolvedValue(new Response("ok", { status: 200 }));
63
-
64
- const env: TestEnv = {
65
- GATEWAY_URL: "https://gateway.example.com",
66
- GATEWAY_APP_API_TOKEN: "eat_test_token",
67
- };
68
-
69
- await expect(
70
- fetchGateway({
71
- env,
72
- url: "https://evil.example.com/api/ai/openai/v1/responses",
73
- init: { method: "POST" },
74
- }),
75
- ).rejects.toThrow(
76
- "Refusing to send gateway token to non-gateway origin: https://evil.example.com",
77
- );
78
-
79
- expect(fetchMock).not.toHaveBeenCalled();
80
- });
81
-
82
- it("allows absolute URL when it matches gateway origin", async () => {
83
- const fetchMock = vi
84
- .spyOn(globalThis, "fetch")
85
- .mockResolvedValue(new Response("ok", { status: 200 }));
86
-
87
- const env: TestEnv = {
88
- GATEWAY_URL: "https://gateway.example.com",
89
- GATEWAY_APP_API_TOKEN: "eat_test_token",
90
- };
91
-
92
- await fetchGateway({
93
- env,
94
- url: "https://gateway.example.com/api/ai/openai/v1/responses",
95
- init: { method: "POST" },
96
- });
97
-
98
- expect(fetchMock).toHaveBeenCalledTimes(1);
99
- const [requestArg] = fetchMock.mock.calls[0];
100
- const request = requestArg as Request;
101
- expect(request.url).toBe(
102
- "https://gateway.example.com/api/ai/openai/v1/responses",
103
- );
104
- });
105
-
106
- it("fetches via service binding when available in production", async () => {
107
- // Service binding is only used when import.meta.env.PROD is true
108
- const originalProd = import.meta.env.PROD;
109
- import.meta.env.PROD = true;
110
-
111
- try {
112
- const bindingFetch = vi
113
- .fn()
114
- .mockResolvedValue(new Response("ok", { status: 200 }));
115
-
116
- const env: TestEnv = {
117
- GATEWAY_URL: "https://gateway.example.com",
118
- EVERY_APP_GATEWAY: { fetch: bindingFetch },
119
- GATEWAY_APP_API_TOKEN: "eat_test_token",
120
- };
121
-
122
- await fetchGateway({
123
- env,
124
- url: "/api/ai/openai/v1/chat/completions",
125
- init: { method: "POST" },
126
- });
127
-
128
- expect(bindingFetch).toHaveBeenCalledTimes(1);
129
- const [requestArg] = bindingFetch.mock.calls[0];
130
- const request = requestArg as Request;
131
- expect(request.url).toBe(
132
- "http://localhost/api/ai/openai/v1/chat/completions",
133
- );
134
- } finally {
135
- import.meta.env.PROD = originalProd;
136
- }
137
- });
138
-
139
- it("skips service binding in development and uses HTTP fetch", async () => {
140
- const fetchMock = vi
141
- .spyOn(globalThis, "fetch")
142
- .mockResolvedValue(new Response("ok", { status: 200 }));
143
-
144
- const bindingFetch = vi.fn();
145
-
146
- const env: TestEnv = {
147
- GATEWAY_URL: "https://gateway.example.com",
148
- EVERY_APP_GATEWAY: { fetch: bindingFetch },
149
- GATEWAY_APP_API_TOKEN: "eat_test_token",
150
- };
151
-
152
- await fetchGateway({
153
- env,
154
- url: "/api/ai/openai/v1/chat/completions",
155
- init: { method: "POST" },
156
- });
157
-
158
- // In dev, should use HTTP fetch, not service binding
159
- expect(bindingFetch).not.toHaveBeenCalled();
160
- expect(fetchMock).toHaveBeenCalledTimes(1);
161
- });
162
-
163
- it("always injects app token and strips Authorization header", async () => {
164
- const fetchMock = vi
165
- .spyOn(globalThis, "fetch")
166
- .mockResolvedValue(new Response("ok", { status: 200 }));
167
-
168
- const env: TestEnv = {
169
- GATEWAY_URL: "https://gateway.example.com",
170
- GATEWAY_APP_API_TOKEN: "eat_my_token",
171
- };
172
-
173
- await fetchGateway({
174
- env,
175
- url: "/api/ai/openai/v1/responses",
176
- init: {
177
- method: "POST",
178
- headers: {
179
- authorization: "Bearer some-sdk-dummy-value",
180
- },
181
- },
182
- });
183
-
184
- expect(fetchMock).toHaveBeenCalledTimes(1);
185
- const [requestArg] = fetchMock.mock.calls[0];
186
- const request = requestArg as Request;
187
-
188
- expect(request.headers.get("x-every-app-token")).toBe("eat_my_token");
189
- expect(request.headers.get("authorization")).toBeNull();
190
- });
191
-
192
- it("throws when app token is missing", async () => {
193
- const env: TestEnv = {
194
- GATEWAY_URL: "https://gateway.example.com",
195
- };
196
-
197
- await expect(
198
- fetchGateway({
199
- env,
200
- url: "/api/ai/openai/v1/responses",
201
- }),
202
- ).rejects.toThrow("GATEWAY_APP_API_TOKEN is required");
203
- });
204
-
205
- it("supports legacy APP_TOKEN env variable", async () => {
206
- const fetchMock = vi
207
- .spyOn(globalThis, "fetch")
208
- .mockResolvedValue(new Response("ok", { status: 200 }));
209
-
210
- const env: TestEnv = {
211
- GATEWAY_URL: "https://gateway.example.com",
212
- APP_TOKEN: "eat_legacy_token",
213
- };
214
-
215
- await fetchGateway({
216
- env,
217
- url: "/api/ai/openai/v1/responses",
218
- init: { method: "POST" },
219
- });
220
-
221
- expect(fetchMock).toHaveBeenCalledTimes(1);
222
- const [requestArg] = fetchMock.mock.calls[0];
223
- const request = requestArg as Request;
224
-
225
- expect(request.headers.get("x-every-app-token")).toBe("eat_legacy_token");
226
- expect(request.headers.get("authorization")).toBeNull();
227
- });
228
-
229
- it("supports Request input and preserves method/body", async () => {
230
- const fetchMock = vi
231
- .spyOn(globalThis, "fetch")
232
- .mockResolvedValue(new Response("ok", { status: 200 }));
233
-
234
- const env: TestEnv = {
235
- GATEWAY_URL: "https://gateway.example.com",
236
- GATEWAY_APP_API_TOKEN: "eat_test_token",
237
- };
238
-
239
- const sourceRequest = new Request(
240
- "https://gateway.example.com/api/ai/openai/v1/responses",
241
- {
242
- method: "POST",
243
- body: JSON.stringify({ model: "gpt-5.2" }),
244
- headers: { "content-type": "application/json" },
245
- },
246
- );
247
-
248
- await fetchGateway({
249
- env,
250
- url: sourceRequest,
251
- });
252
-
253
- expect(fetchMock).toHaveBeenCalledTimes(1);
254
- const [requestArg] = fetchMock.mock.calls[0];
255
- const request = requestArg as Request;
256
- expect(request.url).toBe(
257
- "https://gateway.example.com/api/ai/openai/v1/responses",
258
- );
259
- expect(request.method).toBe("POST");
260
- expect(request.headers.get("content-type")).toBe("application/json");
261
- });
262
- });