@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.
- package/dist/shared/bypassGatewayLocalOnly.d.ts +2 -1
- package/dist/shared/bypassGatewayLocalOnly.d.ts.map +1 -1
- package/dist/shared/bypassGatewayLocalOnly.js +6 -2
- package/dist/tanstack/server/authenticateRequest.d.ts +2 -0
- package/dist/tanstack/server/authenticateRequest.d.ts.map +1 -1
- package/dist/tanstack/server/authenticateRequest.js +75 -3
- package/package.json +3 -3
- package/src/cloudflare/getLocalD1Url.ts +0 -64
- package/src/cloudflare/index.ts +0 -1
- package/src/cloudflare/lazyInit.ts +0 -67
- package/src/cloudflare/server/gateway.test.ts +0 -262
- package/src/cloudflare/server/gateway.ts +0 -114
- package/src/cloudflare/server/index.ts +0 -2
- package/src/core/authenticatedFetch.ts +0 -54
- package/src/core/index.ts +0 -12
- package/src/core/sessionManager.test.ts +0 -939
- package/src/core/sessionManager.ts +0 -492
- package/src/env.d.ts +0 -13
- package/src/shared/bypassGatewayLocalOnly.ts +0 -55
- package/src/shared/parseMessagePayload.ts +0 -22
- package/src/tanstack/EmbeddedAppProvider.tsx +0 -96
- package/src/tanstack/GatewayRequiredError.tsx +0 -150
- package/src/tanstack/_internal/useEveryAppSession.test.ts +0 -40
- package/src/tanstack/_internal/useEveryAppSession.tsx +0 -74
- package/src/tanstack/index.ts +0 -3
- package/src/tanstack/server/authConfig.ts +0 -19
- package/src/tanstack/server/authenticateRequest.test.ts +0 -482
- package/src/tanstack/server/authenticateRequest.ts +0 -143
- package/src/tanstack/server/index.ts +0 -3
- package/src/tanstack/server/types.ts +0 -4
- package/src/tanstack/useEveryAppRouter.tsx +0 -83
- package/src/tanstack/useSessionTokenClientMiddleware.ts +0 -43
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
const SERVICE_BINDING_ORIGIN = "http://localhost";
|
|
2
|
-
const APP_TOKEN_HEADER = "x-every-app-token";
|
|
3
|
-
|
|
4
|
-
interface GatewayFetcher {
|
|
5
|
-
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface GatewayEnv {
|
|
9
|
-
GATEWAY_URL?: string;
|
|
10
|
-
EVERY_APP_GATEWAY?: GatewayFetcher;
|
|
11
|
-
GATEWAY_APP_API_TOKEN?: string;
|
|
12
|
-
APP_TOKEN?: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface FetchGatewayOptions {
|
|
16
|
-
env: GatewayEnv;
|
|
17
|
-
/** The URL, path, or Request to send to the gateway. Typically the full URL
|
|
18
|
-
* passed by a provider SDK's custom `fetch` override. */
|
|
19
|
-
url: string | URL | Request;
|
|
20
|
-
/** Standard `RequestInit` (method, headers, body, etc.) from the provider SDK. */
|
|
21
|
-
init?: RequestInit;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function getGatewayUrl(env: GatewayEnv): string {
|
|
25
|
-
const gatewayUrl = env.GATEWAY_URL?.trim();
|
|
26
|
-
if (!gatewayUrl) {
|
|
27
|
-
throw new Error("GATEWAY_URL is required");
|
|
28
|
-
}
|
|
29
|
-
return gatewayUrl;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Fetch from the gateway proxy, authenticating with the app token.
|
|
34
|
-
*
|
|
35
|
-
* - Strips any existing `Authorization` header (the gateway only accepts
|
|
36
|
-
* app token auth via `x-every-app-token`).
|
|
37
|
-
* - Requires `GATEWAY_APP_API_TOKEN` (or legacy `APP_TOKEN`) in the env.
|
|
38
|
-
* - Routes via service binding in production, falls back to HTTP in dev.
|
|
39
|
-
*/
|
|
40
|
-
export async function fetchGateway({
|
|
41
|
-
env,
|
|
42
|
-
url,
|
|
43
|
-
init,
|
|
44
|
-
}: FetchGatewayOptions): Promise<Response> {
|
|
45
|
-
const gatewayBaseUrl = getGatewayUrl(env);
|
|
46
|
-
const resolvedRequest = toRequest(url, init, gatewayBaseUrl);
|
|
47
|
-
const authenticatedRequest = applyAppTokenAuth(resolvedRequest, env);
|
|
48
|
-
|
|
49
|
-
// Use service binding in production for zero-latency internal routing.
|
|
50
|
-
// In local dev wrangler still exposes the binding object, but the target
|
|
51
|
-
// service usually isn't running locally, so we skip it and use HTTP fetch.
|
|
52
|
-
if (import.meta.env.PROD && env.EVERY_APP_GATEWAY) {
|
|
53
|
-
const url = new URL(authenticatedRequest.url);
|
|
54
|
-
const bindingUrl = `${SERVICE_BINDING_ORIGIN}${url.pathname}${url.search}`;
|
|
55
|
-
const bindingRequest = new Request(bindingUrl, authenticatedRequest);
|
|
56
|
-
return env.EVERY_APP_GATEWAY.fetch(bindingRequest);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// HTTP fetch – used in local dev, or as a fallback when no binding exists
|
|
60
|
-
return fetch(authenticatedRequest);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function applyAppTokenAuth(request: Request, env: GatewayEnv): Request {
|
|
64
|
-
const gatewayOrigin = new URL(getGatewayUrl(env)).origin;
|
|
65
|
-
const requestOrigin = new URL(request.url).origin;
|
|
66
|
-
if (requestOrigin !== gatewayOrigin) {
|
|
67
|
-
throw new Error(
|
|
68
|
-
`Refusing to send gateway token to non-gateway origin: ${requestOrigin}`,
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const appToken = getGatewayAppApiToken(env);
|
|
73
|
-
if (!appToken) {
|
|
74
|
-
throw new Error(
|
|
75
|
-
"GATEWAY_APP_API_TOKEN is required. Run `npx everyapp app deploy` to provision one.",
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const headers = new Headers(request.headers);
|
|
80
|
-
headers.delete("authorization");
|
|
81
|
-
headers.set(APP_TOKEN_HEADER, appToken);
|
|
82
|
-
return new Request(request, { headers });
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function getGatewayAppApiToken(env: GatewayEnv): string | null {
|
|
86
|
-
const configuredToken = env.GATEWAY_APP_API_TOKEN?.trim();
|
|
87
|
-
if (configuredToken) {
|
|
88
|
-
return configuredToken;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const legacyToken = env.APP_TOKEN?.trim();
|
|
92
|
-
return legacyToken || null;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function toRequest(
|
|
96
|
-
input: RequestInfo | URL,
|
|
97
|
-
init?: RequestInit,
|
|
98
|
-
baseUrl?: string,
|
|
99
|
-
): Request {
|
|
100
|
-
if (input instanceof Request) {
|
|
101
|
-
return init ? new Request(input, init) : input;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (input instanceof URL) {
|
|
105
|
-
return new Request(input.toString(), init);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (typeof input === "string" && baseUrl && !/^https?:\/\//i.test(input)) {
|
|
109
|
-
const normalizedPath = input.startsWith("/") ? input : `/${input}`;
|
|
110
|
-
return new Request(new URL(normalizedPath, baseUrl).toString(), init);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return new Request(input, init);
|
|
114
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BYPASS_GATEWAY_LOCAL_ONLY_TOKEN,
|
|
3
|
-
isBypassGatewayLocalOnlyClient,
|
|
4
|
-
} from "../shared/bypassGatewayLocalOnly.js";
|
|
5
|
-
|
|
6
|
-
interface SessionManager {
|
|
7
|
-
getToken(): Promise<string>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface WindowWithSessionManager extends Window {
|
|
11
|
-
__embeddedSessionManager?: SessionManager;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Gets the current session token from the embedded session manager
|
|
16
|
-
*/
|
|
17
|
-
export async function getSessionToken(): Promise<string> {
|
|
18
|
-
if (isBypassGatewayLocalOnlyClient()) {
|
|
19
|
-
return BYPASS_GATEWAY_LOCAL_ONLY_TOKEN;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const windowWithSession = window as WindowWithSessionManager;
|
|
23
|
-
const sessionManager = windowWithSession.__embeddedSessionManager;
|
|
24
|
-
|
|
25
|
-
if (!sessionManager) {
|
|
26
|
-
throw new Error("Session manager not available");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const token = await sessionManager.getToken();
|
|
30
|
-
|
|
31
|
-
if (!token) {
|
|
32
|
-
throw new Error("No token available");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return token;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Performs a fetch request with the authorization header automatically added
|
|
40
|
-
*/
|
|
41
|
-
export async function authenticatedFetch(
|
|
42
|
-
input: RequestInfo | URL,
|
|
43
|
-
init?: RequestInit,
|
|
44
|
-
): Promise<Response> {
|
|
45
|
-
const token = await getSessionToken();
|
|
46
|
-
|
|
47
|
-
const headers = new Headers(init?.headers);
|
|
48
|
-
headers.set("Authorization", `Bearer ${token}`);
|
|
49
|
-
|
|
50
|
-
return fetch(input, {
|
|
51
|
-
...init,
|
|
52
|
-
headers,
|
|
53
|
-
});
|
|
54
|
-
}
|
package/src/core/index.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
SessionManager,
|
|
3
|
-
isRunningInIframe,
|
|
4
|
-
isRunningInReactNativeWebView,
|
|
5
|
-
detectEnvironment,
|
|
6
|
-
} from "./sessionManager.js";
|
|
7
|
-
export type {
|
|
8
|
-
SessionManagerConfig,
|
|
9
|
-
EmbeddedEnvironment,
|
|
10
|
-
} from "./sessionManager.js";
|
|
11
|
-
|
|
12
|
-
export { authenticatedFetch, getSessionToken } from "./authenticatedFetch.js";
|