@supacloud/js 0.19.3 → 0.19.5

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
@@ -47,6 +47,40 @@ const finalState = await task.wait();
47
47
  console.log(finalState.status);
48
48
  ```
49
49
 
50
+ ## SupAuth OAuth refresh with `supabase-js`
51
+
52
+ SupAuth OAuth public clients can require `client_id` on refresh-token requests.
53
+ Keep using `@supabase/supabase-js` for session storage, locking, and automatic
54
+ refresh, and provide the SupaCloud transport adapter when creating the client:
55
+
56
+ ```ts
57
+ import { createClient } from "@supabase/supabase-js";
58
+ import { createSupaCloudOAuthFetch } from "@supacloud/js";
59
+
60
+ const supabase = createClient("https://auth.example.com", "anon-key", {
61
+ global: {
62
+ fetch: createSupaCloudOAuthFetch({
63
+ clientId: "public-oauth-client-id",
64
+ tokenEndpoint: "https://auth.example.com/auth/v1/oauth/token",
65
+ }),
66
+ },
67
+ });
68
+ ```
69
+
70
+ For a standard single-project Supabase app without SupAuth, omit `clientId` (or
71
+ omit the adapter entirely). With no `clientId`, the returned transport is a
72
+ transparent pass-through, so the regular `/auth/v1/token` refresh flow remains
73
+ unchanged. This allows shared application setup to enable SupAuth by
74
+ environment configuration without maintaining a second session implementation.
75
+
76
+ The adapter only transforms `POST /auth/v1/token?grant_type=refresh_token`:
77
+ it sends the same refresh token as an OAuth form request, moves
78
+ `grant_type=refresh_token` into the form body, and adds `client_id` when the
79
+ request does not already contain one. Rewritten refresh requests reject HTTP
80
+ redirects instead of forwarding the refresh token to a second endpoint. All
81
+ other Supabase Auth, Database, Storage, Realtime, and Functions requests pass
82
+ through unchanged. Never pass a client secret to browser code.
83
+
50
84
  ## SupAuth Management
51
85
 
52
86
  `supacloud.supauth` is a management-plane helper for provisioning and verifying a SupAuth/SupaOAuth runtime on SupaCloud. It is intended for trusted server-side tools, CI jobs, or admin backends that can call the SupaCloud Management API.
@@ -0,0 +1,25 @@
1
+ export type SupaCloudOAuthFetchOptions = {
2
+ /**
3
+ * Public OAuth client identifier registered with SupAuth. Leave empty for a
4
+ * standard single-project Supabase deployment; the transport then becomes a
5
+ * transparent pass-through.
6
+ */
7
+ clientId?: string | null;
8
+ /**
9
+ * OAuth token endpoint. When omitted, `/auth/v1/oauth/token` is derived from
10
+ * the Supabase Auth refresh URL origin.
11
+ */
12
+ tokenEndpoint?: string;
13
+ /** Injectable transport for tests or runtimes with a custom fetch. */
14
+ fetch?: typeof fetch;
15
+ };
16
+ /**
17
+ * Adapts Supabase Auth's built-in refresh request to SupAuth's OAuth token
18
+ * contract while leaving every other supabase-js request untouched.
19
+ *
20
+ * Usage:
21
+ * createClient(authUrl, anonKey, {
22
+ * global: { fetch: createSupaCloudOAuthFetch({ clientId }) },
23
+ * });
24
+ */
25
+ export declare function createSupaCloudOAuthFetch(options?: SupaCloudOAuthFetchOptions): typeof fetch;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { SupabaseClient } from "@supabase/supabase-js";
2
+ export { createSupaCloudOAuthFetch, type SupaCloudOAuthFetchOptions, } from "./auth-fetch.js";
2
3
  export type SupaCloudTaskStatus = "pending" | "leased" | "running" | "retry_scheduled" | "succeeded" | "failed" | "dead_lettered" | "cancelled" | "queued" | "processing" | "completed";
3
4
  export type SupaCloudTaskLogEntry = {
4
5
  timestamp: string;
@@ -449,4 +450,3 @@ export declare function createSupaCloudClient<TClient extends SupabaseClient = S
449
450
  invokeBackground: (functionName: string, submitOptions?: SupaCloudTaskSubmitOptions) => Promise<SupaCloudTaskReceipt>;
450
451
  };
451
452
  };
452
- export {};
package/dist/index.js CHANGED
@@ -1,3 +1,83 @@
1
+ // src/auth-fetch.ts
2
+ function requestMethod(input, init) {
3
+ return (init?.method || (input instanceof Request ? input.method : "GET")).toUpperCase();
4
+ }
5
+ function isRefreshTokenRequest(url, method) {
6
+ return method === "POST" && url.pathname.endsWith("/auth/v1/token") && url.searchParams.get("grant_type") === "refresh_token";
7
+ }
8
+ function parseRefreshBody(body, contentType) {
9
+ if (!body)
10
+ return null;
11
+ if (contentType.toLowerCase().includes("application/json") || body.trimStart().startsWith("{")) {
12
+ let value;
13
+ try {
14
+ value = JSON.parse(body);
15
+ } catch {
16
+ return null;
17
+ }
18
+ if (!value || typeof value !== "object" || Array.isArray(value))
19
+ return null;
20
+ const record = value;
21
+ const refreshToken = typeof record.refresh_token === "string" ? record.refresh_token.trim() : "";
22
+ if (!refreshToken)
23
+ return null;
24
+ const params2 = new URLSearchParams;
25
+ for (const [key, item] of Object.entries(record)) {
26
+ if (typeof item === "string")
27
+ params2.set(key, item);
28
+ }
29
+ return params2;
30
+ }
31
+ const params = new URLSearchParams(body);
32
+ return params.get("refresh_token")?.trim() ? params : null;
33
+ }
34
+ function replacementRequest(request, tokenEndpoint, body) {
35
+ const headers = new Headers(request.headers);
36
+ headers.set("content-type", "application/x-www-form-urlencoded");
37
+ headers.delete("content-length");
38
+ headers.delete("authorization");
39
+ headers.delete("cookie");
40
+ headers.delete("proxy-authorization");
41
+ return new Request(tokenEndpoint, {
42
+ method: "POST",
43
+ headers,
44
+ body,
45
+ cache: request.cache,
46
+ credentials: request.credentials,
47
+ integrity: request.integrity,
48
+ keepalive: request.keepalive,
49
+ mode: request.mode,
50
+ redirect: "error",
51
+ referrer: request.referrer,
52
+ referrerPolicy: request.referrerPolicy,
53
+ signal: request.signal
54
+ });
55
+ }
56
+ function createSupaCloudOAuthFetch(options = {}) {
57
+ const fetchImpl = options.fetch || globalThis.fetch.bind(globalThis);
58
+ const clientId = options.clientId?.trim();
59
+ if (!clientId)
60
+ return fetchImpl;
61
+ return async (input, init) => {
62
+ const url = new URL(input instanceof Request ? input.url : input.toString());
63
+ if (!isRefreshTokenRequest(url, requestMethod(input, init))) {
64
+ return fetchImpl(input, init);
65
+ }
66
+ const request = new Request(input, init);
67
+ const body = parseRefreshBody(await request.clone().text(), request.headers.get("content-type") || "");
68
+ if (!body)
69
+ return fetchImpl(input, init);
70
+ body.set("grant_type", "refresh_token");
71
+ if (!body.get("client_id"))
72
+ body.set("client_id", clientId);
73
+ const endpoint = options.tokenEndpoint?.trim() || new URL(url.pathname.replace(/\/auth\/v1\/token$/, "/auth/v1/oauth/token"), url.origin).toString();
74
+ if (new URL(endpoint).origin !== url.origin) {
75
+ throw new Error("SupaCloud OAuth tokenEndpoint must use the Supabase Auth origin");
76
+ }
77
+ return fetchImpl(replacementRequest(request, endpoint, body));
78
+ };
79
+ }
80
+
1
81
  // src/index.ts
2
82
  class SupaCloudTaskSubmitError extends Error {
3
83
  responseBody;
@@ -656,6 +736,7 @@ function createSupaCloudClient(options) {
656
736
  };
657
737
  }
658
738
  export {
739
+ createSupaCloudOAuthFetch,
659
740
  createSupaCloudClient,
660
741
  SupaCloudTaskSubmitError,
661
742
  SupaCloudApiError
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/js",
3
- "version": "0.19.3",
3
+ "version": "0.19.5",
4
4
  "description": "Platform SDK for SupaCloud, built on top of supabase-js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -23,6 +23,7 @@
23
23
  "build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
24
24
  "clean": "rm -rf dist",
25
25
  "prepublishOnly": "bun run build",
26
+ "typecheck:consumer": "tsc -p tsconfig.consumer.json --noEmit",
26
27
  "typecheck": "tsc -p tsconfig.json --noEmit",
27
28
  "typecheck:test": "tsc -p tsconfig.test.json --noEmit"
28
29
  },
@@ -40,11 +41,11 @@
40
41
  "directory": "packages/supacloud-js"
41
42
  },
42
43
  "peerDependencies": {
43
- "@supabase/supabase-js": "^2.103.0"
44
+ "@supabase/supabase-js": "^2.110.7"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@types/bun": "^1.3.14",
47
- "@supabase/supabase-js": "^2.108.2",
48
+ "@supabase/supabase-js": "^2.110.7",
48
49
  "typescript": "^7.0.2"
49
50
  }
50
51
  }