@supabase/server 0.1.0-alpha.1 → 0.1.1-rc.25
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/adapters/hono/index.cjs +27 -1
- package/dist/adapters/hono/index.d.cts +27 -1
- package/dist/adapters/hono/index.d.mts +27 -1
- package/dist/adapters/hono/index.mjs +27 -1
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.mts +2 -2
- package/dist/core/index.mjs +1 -1
- package/dist/create-admin-client-Cp7FxI6O.d.cts +271 -0
- package/dist/create-admin-client-ZTnl1zMe.d.mts +271 -0
- package/dist/{create-supabase-context-BrSIe29v.mjs → create-supabase-context-CmWaH3s6.mjs} +21 -1
- package/dist/{create-supabase-context-DNWor6i_.cjs → create-supabase-context-DcVorGKG.cjs} +21 -1
- package/dist/index.cjs +45 -2
- package/dist/index.d.cts +48 -5
- package/dist/index.d.mts +48 -5
- package/dist/index.mjs +45 -2
- package/dist/types-ClmJ8pi8.d.mts +227 -0
- package/dist/types-CnKoFCMX.d.cts +227 -0
- package/dist/{verify-auth-DxUT0XoT.mjs → verify-auth-2S7zFfR-.mjs} +217 -0
- package/dist/{verify-auth-6a1UPrFz.cjs → verify-auth-DvRVnjdq.cjs} +217 -0
- package/dist/wrappers/index.cjs +19 -0
- package/dist/wrappers/index.d.cts +19 -0
- package/dist/wrappers/index.d.mts +19 -0
- package/dist/wrappers/index.mjs +19 -0
- package/package.json +1 -1
- package/dist/create-admin-client-BZ_3qcxI.d.cts +0 -60
- package/dist/create-admin-client-CSX-Q_Fv.d.mts +0 -60
- package/dist/types-BLM5-qA8.d.mts +0 -59
- package/dist/types-DNh3Z1O1.d.cts +0 -59
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { i as Credentials, n as AllowWithKey, r as AuthResult, s as SupabaseEnv } from "./types-ClmJ8pi8.mjs";
|
|
2
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
3
|
+
|
|
4
|
+
//#region src/errors.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Thrown when a required environment variable is missing or malformed.
|
|
7
|
+
*
|
|
8
|
+
* Has a fixed `status` of `500` since environment errors are server-side
|
|
9
|
+
* configuration issues, not client errors.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { EnvError } from '@supabase/server'
|
|
14
|
+
*
|
|
15
|
+
* try {
|
|
16
|
+
* const client = createAdminClient()
|
|
17
|
+
* } catch (e) {
|
|
18
|
+
* if (e instanceof EnvError) {
|
|
19
|
+
* console.error(`Config issue [${e.code}]: ${e.message}`)
|
|
20
|
+
* // → "Config issue [MISSING_SUPABASE_URL]: SUPABASE_URL is required but not set"
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare class EnvError extends Error {
|
|
26
|
+
/** Always `500` — environment errors are server-side issues. */
|
|
27
|
+
readonly status = 500;
|
|
28
|
+
/**
|
|
29
|
+
* Machine-readable error code.
|
|
30
|
+
*
|
|
31
|
+
* Known codes:
|
|
32
|
+
* - `"MISSING_SUPABASE_URL"` — `SUPABASE_URL` not set
|
|
33
|
+
* - `"MISSING_PUBLISHABLE_KEY"` — No publishable key found
|
|
34
|
+
* - `"MISSING_SECRET_KEY"` — No secret key found
|
|
35
|
+
* - `"ENV_ERROR"` — Generic environment error
|
|
36
|
+
*/
|
|
37
|
+
readonly code: string;
|
|
38
|
+
constructor(message: string, code?: string);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Thrown when authentication or authorization fails.
|
|
42
|
+
*
|
|
43
|
+
* Carries an HTTP `status` code suitable for returning directly in a response
|
|
44
|
+
* (typically `401` for invalid credentials, `500` for server-side auth failures).
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { AuthError, createSupabaseContext } from '@supabase/server'
|
|
49
|
+
*
|
|
50
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
51
|
+
* if (error) {
|
|
52
|
+
* // error is an AuthError
|
|
53
|
+
* return Response.json(
|
|
54
|
+
* { error: error.message, code: error.code },
|
|
55
|
+
* { status: error.status },
|
|
56
|
+
* )
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare class AuthError extends Error {
|
|
61
|
+
/**
|
|
62
|
+
* HTTP status code.
|
|
63
|
+
*
|
|
64
|
+
* - `401` — Invalid or missing credentials
|
|
65
|
+
* - `500` — Server-side auth failure (e.g., missing JWKS, env misconfiguration)
|
|
66
|
+
*/
|
|
67
|
+
readonly status: number;
|
|
68
|
+
/**
|
|
69
|
+
* Machine-readable error code.
|
|
70
|
+
*
|
|
71
|
+
* Known codes:
|
|
72
|
+
* - `"INVALID_CREDENTIALS"` — No credential matched any allowed auth mode
|
|
73
|
+
* - `"CLIENT_ERROR"` — Failed to create a Supabase client after auth succeeded
|
|
74
|
+
* - `"AUTH_ERROR"` — Generic authentication error
|
|
75
|
+
* - Any `EnvError` code (propagated when env resolution fails during auth)
|
|
76
|
+
*/
|
|
77
|
+
readonly code: string;
|
|
78
|
+
constructor(message: string, code?: string, status?: number);
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/core/resolve-env.d.ts
|
|
82
|
+
/**
|
|
83
|
+
* Resolves Supabase environment configuration from runtime environment variables.
|
|
84
|
+
*
|
|
85
|
+
* Reads `SUPABASE_URL`, keys (`SUPABASE_PUBLISHABLE_KEYS` / `SUPABASE_SECRET_KEYS`),
|
|
86
|
+
* and `SUPABASE_JWKS`. Works across Deno, Node.js, and Bun. For Cloudflare Workers,
|
|
87
|
+
* use `overrides` or enable node-compat.
|
|
88
|
+
*
|
|
89
|
+
* @param overrides - Partial values that take precedence over env vars.
|
|
90
|
+
* @returns `{ data: SupabaseEnv, error: null }` on success, `{ data: null, error: EnvError }` on failure.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const { data: env, error } = resolveEnv()
|
|
95
|
+
* if (error) throw error
|
|
96
|
+
*
|
|
97
|
+
* // Override for tests
|
|
98
|
+
* const { data: env } = resolveEnv({ url: 'http://localhost:54321' })
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
declare function resolveEnv(overrides?: Partial<SupabaseEnv>): {
|
|
102
|
+
data: SupabaseEnv;
|
|
103
|
+
error: null;
|
|
104
|
+
} | {
|
|
105
|
+
data: null;
|
|
106
|
+
error: EnvError;
|
|
107
|
+
};
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/core/extract-credentials.d.ts
|
|
110
|
+
/**
|
|
111
|
+
* Extracts authentication credentials from an incoming HTTP request.
|
|
112
|
+
*
|
|
113
|
+
* Reads two headers:
|
|
114
|
+
* - `Authorization: Bearer <token>` → extracted as `token`
|
|
115
|
+
* - `apikey: <key>` → extracted as `apikey`
|
|
116
|
+
*
|
|
117
|
+
* This is a pure extraction step — no validation or verification is performed.
|
|
118
|
+
* Pass the result to {@link verifyCredentials} to validate against allowed auth modes.
|
|
119
|
+
*
|
|
120
|
+
* @param request - The incoming HTTP request.
|
|
121
|
+
* @returns The extracted {@link Credentials}. Fields are `null` when the corresponding header is absent.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* import { extractCredentials } from '@supabase/server/core'
|
|
126
|
+
*
|
|
127
|
+
* const creds = extractCredentials(request)
|
|
128
|
+
* console.log(creds.token) // "eyJhbGci..." or null
|
|
129
|
+
* console.log(creds.apikey) // "sb-abc123-publishable-..." or null
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function extractCredentials(request: Request): Credentials;
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/core/verify-credentials.d.ts
|
|
135
|
+
/**
|
|
136
|
+
* Options for {@link verifyCredentials}.
|
|
137
|
+
*/
|
|
138
|
+
interface VerifyCredentialsOptions {
|
|
139
|
+
/**
|
|
140
|
+
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
141
|
+
*
|
|
142
|
+
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
143
|
+
*/
|
|
144
|
+
allow: AllowWithKey | AllowWithKey[];
|
|
145
|
+
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
146
|
+
env?: Partial<SupabaseEnv>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Verifies pre-extracted credentials against one or more allowed auth modes.
|
|
150
|
+
*
|
|
151
|
+
* Tries each mode in order — first match wins. Use {@link verifyAuth} to extract
|
|
152
|
+
* and verify in a single call.
|
|
153
|
+
*
|
|
154
|
+
* @param credentials - The credentials to verify (from {@link extractCredentials}).
|
|
155
|
+
* @param options - Allowed auth modes and optional env overrides.
|
|
156
|
+
* @returns `{ data: AuthResult, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* const credentials = extractCredentials(request)
|
|
161
|
+
* const { data: auth, error } = await verifyCredentials(credentials, {
|
|
162
|
+
* allow: ['user', 'public'],
|
|
163
|
+
* })
|
|
164
|
+
* if (error) {
|
|
165
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
166
|
+
* }
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
declare function verifyCredentials(credentials: Credentials, options: VerifyCredentialsOptions): Promise<{
|
|
170
|
+
data: AuthResult;
|
|
171
|
+
error: null;
|
|
172
|
+
} | {
|
|
173
|
+
data: null;
|
|
174
|
+
error: AuthError;
|
|
175
|
+
}>;
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/core/verify-auth.d.ts
|
|
178
|
+
/**
|
|
179
|
+
* Options for {@link verifyAuth}.
|
|
180
|
+
*/
|
|
181
|
+
interface VerifyAuthOptions {
|
|
182
|
+
/**
|
|
183
|
+
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
184
|
+
*
|
|
185
|
+
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
186
|
+
*/
|
|
187
|
+
allow: AllowWithKey | AllowWithKey[];
|
|
188
|
+
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
189
|
+
env?: Partial<SupabaseEnv>;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Extracts credentials from a request and verifies them in a single step.
|
|
193
|
+
*
|
|
194
|
+
* This is a convenience function that combines {@link extractCredentials} and
|
|
195
|
+
* {@link verifyCredentials}. Use it when you want the full auth flow without
|
|
196
|
+
* needing to inspect the raw credentials.
|
|
197
|
+
*
|
|
198
|
+
* @param request - The incoming HTTP request.
|
|
199
|
+
* @param options - Auth modes to accept and optional environment overrides.
|
|
200
|
+
*
|
|
201
|
+
* @returns A result tuple: `{ data, error }`.
|
|
202
|
+
* - On success: `{ data: AuthResult, error: null }`
|
|
203
|
+
* - On failure: `{ data: null, error: AuthError }`
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* import { verifyAuth } from '@supabase/server/core'
|
|
208
|
+
*
|
|
209
|
+
* const { data: auth, error } = await verifyAuth(request, {
|
|
210
|
+
* allow: 'user',
|
|
211
|
+
* })
|
|
212
|
+
*
|
|
213
|
+
* if (error) {
|
|
214
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
215
|
+
* }
|
|
216
|
+
*
|
|
217
|
+
* console.log(auth.userClaims!.id) // "d0f1a2b3-..."
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function verifyAuth(request: Request, options: VerifyAuthOptions): Promise<{
|
|
221
|
+
data: AuthResult;
|
|
222
|
+
error: null;
|
|
223
|
+
} | {
|
|
224
|
+
data: null;
|
|
225
|
+
error: AuthError;
|
|
226
|
+
}>;
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/core/create-context-client.d.ts
|
|
229
|
+
/**
|
|
230
|
+
* Creates a Supabase client scoped to the caller's context.
|
|
231
|
+
*
|
|
232
|
+
* Configured with a publishable key and (optionally) the caller's JWT,
|
|
233
|
+
* so Row-Level Security policies apply. Session persistence is disabled
|
|
234
|
+
* (stateless, one client per request).
|
|
235
|
+
*
|
|
236
|
+
* @param token - The caller's JWT, or `null` for anonymous access.
|
|
237
|
+
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
238
|
+
* @param keyName - Name of the publishable key to use. Falls back to `"default"`, then first available.
|
|
239
|
+
* @returns A configured {@link SupabaseClient} with RLS enforced.
|
|
240
|
+
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified publishable key is not found.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* const { data: auth } = await verifyAuth(request, { allow: 'user' })
|
|
245
|
+
* const supabase = createContextClient(auth.token)
|
|
246
|
+
* const { data } = await supabase.rpc('get_my_items')
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
declare function createContextClient<Database = unknown>(token?: string | null, env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/core/create-admin-client.d.ts
|
|
252
|
+
/**
|
|
253
|
+
* Creates an admin Supabase client that bypasses Row-Level Security.
|
|
254
|
+
*
|
|
255
|
+
* Uses a secret key for authentication, giving full access to all data.
|
|
256
|
+
* Session persistence is disabled (stateless, one client per request).
|
|
257
|
+
*
|
|
258
|
+
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
259
|
+
* @param keyName - Name of the secret key to use. Falls back to `"default"`, then first available.
|
|
260
|
+
* @returns A configured {@link SupabaseClient} with admin (service-role) privileges.
|
|
261
|
+
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified secret key is not found.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* const supabaseAdmin = createAdminClient()
|
|
266
|
+
* const { data } = await supabaseAdmin.from('audit_log').insert({ action: 'user_login' })
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
declare function createAdminClient<Database = unknown>(env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
270
|
+
//#endregion
|
|
271
|
+
export { extractCredentials as a, EnvError as c, verifyCredentials as i, createContextClient as n, resolveEnv as o, verifyAuth as r, AuthError as s, createAdminClient as t };
|
|
@@ -1,6 +1,26 @@
|
|
|
1
|
-
import { a as createAdminClient, c as EnvError, i as createContextClient, s as AuthError, t as verifyAuth } from "./verify-auth-
|
|
1
|
+
import { a as createAdminClient, c as EnvError, i as createContextClient, s as AuthError, t as verifyAuth } from "./verify-auth-2S7zFfR-.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/create-supabase-context.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates a {@link SupabaseContext} directly from a request.
|
|
6
|
+
*
|
|
7
|
+
* Use this when you need the context without the full {@link withSupabase} wrapper —
|
|
8
|
+
* e.g., inside framework route handlers or custom middleware. Returns a result tuple
|
|
9
|
+
* instead of producing a `Response`.
|
|
10
|
+
*
|
|
11
|
+
* @param request - The incoming HTTP request.
|
|
12
|
+
* @param options - Auth modes, environment overrides. The `cors` option is ignored here.
|
|
13
|
+
* @returns `{ data: SupabaseContext, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
18
|
+
* if (error) {
|
|
19
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
20
|
+
* }
|
|
21
|
+
* const { data } = await ctx.supabase.rpc('get_my_items')
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
4
24
|
async function createSupabaseContext(request, options) {
|
|
5
25
|
const { data: auth, error } = await verifyAuth(request, {
|
|
6
26
|
allow: options?.allow ?? "user",
|
|
@@ -1,6 +1,26 @@
|
|
|
1
|
-
const require_verify_auth = require('./verify-auth-
|
|
1
|
+
const require_verify_auth = require('./verify-auth-DvRVnjdq.cjs');
|
|
2
2
|
|
|
3
3
|
//#region src/create-supabase-context.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates a {@link SupabaseContext} directly from a request.
|
|
6
|
+
*
|
|
7
|
+
* Use this when you need the context without the full {@link withSupabase} wrapper —
|
|
8
|
+
* e.g., inside framework route handlers or custom middleware. Returns a result tuple
|
|
9
|
+
* instead of producing a `Response`.
|
|
10
|
+
*
|
|
11
|
+
* @param request - The incoming HTTP request.
|
|
12
|
+
* @param options - Auth modes, environment overrides. The `cors` option is ignored here.
|
|
13
|
+
* @returns `{ data: SupabaseContext, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
18
|
+
* if (error) {
|
|
19
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
20
|
+
* }
|
|
21
|
+
* const { data } = await ctx.supabase.rpc('get_my_items')
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
4
24
|
async function createSupabaseContext(request, options) {
|
|
5
25
|
const { data: auth, error } = await require_verify_auth.verifyAuth(request, {
|
|
6
26
|
allow: options?.allow ?? "user",
|
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_verify_auth = require('./verify-auth-
|
|
3
|
-
const require_create_supabase_context = require('./create-supabase-context-
|
|
2
|
+
const require_verify_auth = require('./verify-auth-DvRVnjdq.cjs');
|
|
3
|
+
const require_create_supabase_context = require('./create-supabase-context-DcVorGKG.cjs');
|
|
4
4
|
let _supabase_supabase_js_cors = require("@supabase/supabase-js/cors");
|
|
5
5
|
|
|
6
6
|
//#region src/cors.ts
|
|
7
|
+
/**
|
|
8
|
+
* Builds the CORS headers object based on the given configuration.
|
|
9
|
+
*
|
|
10
|
+
* @param config - The CORS configuration.
|
|
11
|
+
* @returns A headers record to include in the response. Empty object if CORS is disabled.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
7
15
|
function buildCorsHeaders(config) {
|
|
8
16
|
if (config === false) return {};
|
|
9
17
|
if (typeof config === "object") return config;
|
|
10
18
|
return _supabase_supabase_js_cors.corsHeaders;
|
|
11
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns a new `Response` with CORS headers appended.
|
|
22
|
+
*
|
|
23
|
+
* Creates a clone of the original response and sets each CORS header on it.
|
|
24
|
+
* If CORS is disabled (`config === false`), returns the original response unchanged.
|
|
25
|
+
*
|
|
26
|
+
* @param response - The original response to augment.
|
|
27
|
+
* @param config - The CORS configuration.
|
|
28
|
+
* @returns A new `Response` with CORS headers set, or the original response if CORS is disabled.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
12
32
|
function addCorsHeaders(response, config) {
|
|
13
33
|
if (config === false) return response;
|
|
14
34
|
const corsHeaders = buildCorsHeaders(config);
|
|
@@ -19,6 +39,29 @@ function addCorsHeaders(response, config) {
|
|
|
19
39
|
|
|
20
40
|
//#endregion
|
|
21
41
|
//#region src/with-supabase.ts
|
|
42
|
+
/**
|
|
43
|
+
* Wraps a request handler with Supabase auth, client creation, and CORS handling.
|
|
44
|
+
*
|
|
45
|
+
* Built for the Web API `Request`/`Response` standard that all modern runtimes
|
|
46
|
+
* implement natively. Handles CORS preflight, credential verification,
|
|
47
|
+
* context creation, and error responses. Your handler only runs on successful auth.
|
|
48
|
+
*
|
|
49
|
+
* @param config - Auth modes, CORS, and environment overrides. See {@link WithSupabaseConfig}.
|
|
50
|
+
* @param handler - Receives the `Request` and a fully-initialized {@link SupabaseContext}.
|
|
51
|
+
* @returns A `(req: Request) => Promise<Response>` fetch handler.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { withSupabase } from '@supabase/server'
|
|
56
|
+
*
|
|
57
|
+
* export default {
|
|
58
|
+
* fetch: withSupabase({ allow: 'user' }, async (req, ctx) => {
|
|
59
|
+
* const { data } = await ctx.supabase.rpc('get_my_profile')
|
|
60
|
+
* return Response.json(data)
|
|
61
|
+
* }),
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
22
65
|
function withSupabase(config, handler) {
|
|
23
66
|
return async (req) => {
|
|
24
67
|
if (config.cors !== false && req.method === "OPTIONS") return new Response(null, {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,55 @@
|
|
|
1
|
-
import { a as JWTClaims, c as UserClaims, i as Credentials, l as WithSupabaseConfig, n as AllowWithKey, o as SupabaseContext, r as AuthResult, s as SupabaseEnv, t as Allow } from "./types-
|
|
2
|
-
import { a as extractCredentials, c as EnvError, i as verifyCredentials, n as createContextClient, o as resolveEnv, r as verifyAuth, s as AuthError, t as createAdminClient } from "./create-admin-client-
|
|
1
|
+
import { a as JWTClaims, c as UserClaims, i as Credentials, l as WithSupabaseConfig, n as AllowWithKey, o as SupabaseContext, r as AuthResult, s as SupabaseEnv, t as Allow } from "./types-CnKoFCMX.cjs";
|
|
2
|
+
import { a as extractCredentials, c as EnvError, i as verifyCredentials, n as createContextClient, o as resolveEnv, r as verifyAuth, s as AuthError, t as createAdminClient } from "./create-admin-client-Cp7FxI6O.cjs";
|
|
3
3
|
|
|
4
4
|
//#region src/with-supabase.d.ts
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Wraps a request handler with Supabase auth, client creation, and CORS handling.
|
|
7
|
+
*
|
|
8
|
+
* Built for the Web API `Request`/`Response` standard that all modern runtimes
|
|
9
|
+
* implement natively. Handles CORS preflight, credential verification,
|
|
10
|
+
* context creation, and error responses. Your handler only runs on successful auth.
|
|
11
|
+
*
|
|
12
|
+
* @param config - Auth modes, CORS, and environment overrides. See {@link WithSupabaseConfig}.
|
|
13
|
+
* @param handler - Receives the `Request` and a fully-initialized {@link SupabaseContext}.
|
|
14
|
+
* @returns A `(req: Request) => Promise<Response>` fetch handler.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { withSupabase } from '@supabase/server'
|
|
19
|
+
*
|
|
20
|
+
* export default {
|
|
21
|
+
* fetch: withSupabase({ allow: 'user' }, async (req, ctx) => {
|
|
22
|
+
* const { data } = await ctx.supabase.rpc('get_my_profile')
|
|
23
|
+
* return Response.json(data)
|
|
24
|
+
* }),
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function withSupabase<Database = unknown>(config: WithSupabaseConfig, handler: (req: Request, ctx: SupabaseContext<Database>) => Promise<Response>): (req: Request) => Promise<Response>;
|
|
6
29
|
//#endregion
|
|
7
30
|
//#region src/create-supabase-context.d.ts
|
|
8
|
-
|
|
9
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Creates a {@link SupabaseContext} directly from a request.
|
|
33
|
+
*
|
|
34
|
+
* Use this when you need the context without the full {@link withSupabase} wrapper —
|
|
35
|
+
* e.g., inside framework route handlers or custom middleware. Returns a result tuple
|
|
36
|
+
* instead of producing a `Response`.
|
|
37
|
+
*
|
|
38
|
+
* @param request - The incoming HTTP request.
|
|
39
|
+
* @param options - Auth modes, environment overrides. The `cors` option is ignored here.
|
|
40
|
+
* @returns `{ data: SupabaseContext, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
45
|
+
* if (error) {
|
|
46
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
47
|
+
* }
|
|
48
|
+
* const { data } = await ctx.supabase.rpc('get_my_items')
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function createSupabaseContext<Database = unknown>(request: Request, options?: WithSupabaseConfig): Promise<{
|
|
52
|
+
data: SupabaseContext<Database>;
|
|
10
53
|
error: null;
|
|
11
54
|
} | {
|
|
12
55
|
data: null;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,55 @@
|
|
|
1
|
-
import { a as JWTClaims, c as UserClaims, i as Credentials, l as WithSupabaseConfig, n as AllowWithKey, o as SupabaseContext, r as AuthResult, s as SupabaseEnv, t as Allow } from "./types-
|
|
2
|
-
import { a as extractCredentials, c as EnvError, i as verifyCredentials, n as createContextClient, o as resolveEnv, r as verifyAuth, s as AuthError, t as createAdminClient } from "./create-admin-client-
|
|
1
|
+
import { a as JWTClaims, c as UserClaims, i as Credentials, l as WithSupabaseConfig, n as AllowWithKey, o as SupabaseContext, r as AuthResult, s as SupabaseEnv, t as Allow } from "./types-ClmJ8pi8.mjs";
|
|
2
|
+
import { a as extractCredentials, c as EnvError, i as verifyCredentials, n as createContextClient, o as resolveEnv, r as verifyAuth, s as AuthError, t as createAdminClient } from "./create-admin-client-ZTnl1zMe.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/with-supabase.d.ts
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Wraps a request handler with Supabase auth, client creation, and CORS handling.
|
|
7
|
+
*
|
|
8
|
+
* Built for the Web API `Request`/`Response` standard that all modern runtimes
|
|
9
|
+
* implement natively. Handles CORS preflight, credential verification,
|
|
10
|
+
* context creation, and error responses. Your handler only runs on successful auth.
|
|
11
|
+
*
|
|
12
|
+
* @param config - Auth modes, CORS, and environment overrides. See {@link WithSupabaseConfig}.
|
|
13
|
+
* @param handler - Receives the `Request` and a fully-initialized {@link SupabaseContext}.
|
|
14
|
+
* @returns A `(req: Request) => Promise<Response>` fetch handler.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { withSupabase } from '@supabase/server'
|
|
19
|
+
*
|
|
20
|
+
* export default {
|
|
21
|
+
* fetch: withSupabase({ allow: 'user' }, async (req, ctx) => {
|
|
22
|
+
* const { data } = await ctx.supabase.rpc('get_my_profile')
|
|
23
|
+
* return Response.json(data)
|
|
24
|
+
* }),
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function withSupabase<Database = unknown>(config: WithSupabaseConfig, handler: (req: Request, ctx: SupabaseContext<Database>) => Promise<Response>): (req: Request) => Promise<Response>;
|
|
6
29
|
//#endregion
|
|
7
30
|
//#region src/create-supabase-context.d.ts
|
|
8
|
-
|
|
9
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Creates a {@link SupabaseContext} directly from a request.
|
|
33
|
+
*
|
|
34
|
+
* Use this when you need the context without the full {@link withSupabase} wrapper —
|
|
35
|
+
* e.g., inside framework route handlers or custom middleware. Returns a result tuple
|
|
36
|
+
* instead of producing a `Response`.
|
|
37
|
+
*
|
|
38
|
+
* @param request - The incoming HTTP request.
|
|
39
|
+
* @param options - Auth modes, environment overrides. The `cors` option is ignored here.
|
|
40
|
+
* @returns `{ data: SupabaseContext, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
45
|
+
* if (error) {
|
|
46
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
47
|
+
* }
|
|
48
|
+
* const { data } = await ctx.supabase.rpc('get_my_items')
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function createSupabaseContext<Database = unknown>(request: Request, options?: WithSupabaseConfig): Promise<{
|
|
52
|
+
data: SupabaseContext<Database>;
|
|
10
53
|
error: null;
|
|
11
54
|
} | {
|
|
12
55
|
data: null;
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
|
-
import { a as createAdminClient, c as EnvError, i as createContextClient, n as verifyCredentials, o as resolveEnv, r as extractCredentials, s as AuthError, t as verifyAuth } from "./verify-auth-
|
|
2
|
-
import { t as createSupabaseContext } from "./create-supabase-context-
|
|
1
|
+
import { a as createAdminClient, c as EnvError, i as createContextClient, n as verifyCredentials, o as resolveEnv, r as extractCredentials, s as AuthError, t as verifyAuth } from "./verify-auth-2S7zFfR-.mjs";
|
|
2
|
+
import { t as createSupabaseContext } from "./create-supabase-context-CmWaH3s6.mjs";
|
|
3
3
|
import { corsHeaders } from "@supabase/supabase-js/cors";
|
|
4
4
|
|
|
5
5
|
//#region src/cors.ts
|
|
6
|
+
/**
|
|
7
|
+
* Builds the CORS headers object based on the given configuration.
|
|
8
|
+
*
|
|
9
|
+
* @param config - The CORS configuration.
|
|
10
|
+
* @returns A headers record to include in the response. Empty object if CORS is disabled.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
6
14
|
function buildCorsHeaders(config) {
|
|
7
15
|
if (config === false) return {};
|
|
8
16
|
if (typeof config === "object") return config;
|
|
9
17
|
return corsHeaders;
|
|
10
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns a new `Response` with CORS headers appended.
|
|
21
|
+
*
|
|
22
|
+
* Creates a clone of the original response and sets each CORS header on it.
|
|
23
|
+
* If CORS is disabled (`config === false`), returns the original response unchanged.
|
|
24
|
+
*
|
|
25
|
+
* @param response - The original response to augment.
|
|
26
|
+
* @param config - The CORS configuration.
|
|
27
|
+
* @returns A new `Response` with CORS headers set, or the original response if CORS is disabled.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
11
31
|
function addCorsHeaders(response, config) {
|
|
12
32
|
if (config === false) return response;
|
|
13
33
|
const corsHeaders = buildCorsHeaders(config);
|
|
@@ -18,6 +38,29 @@ function addCorsHeaders(response, config) {
|
|
|
18
38
|
|
|
19
39
|
//#endregion
|
|
20
40
|
//#region src/with-supabase.ts
|
|
41
|
+
/**
|
|
42
|
+
* Wraps a request handler with Supabase auth, client creation, and CORS handling.
|
|
43
|
+
*
|
|
44
|
+
* Built for the Web API `Request`/`Response` standard that all modern runtimes
|
|
45
|
+
* implement natively. Handles CORS preflight, credential verification,
|
|
46
|
+
* context creation, and error responses. Your handler only runs on successful auth.
|
|
47
|
+
*
|
|
48
|
+
* @param config - Auth modes, CORS, and environment overrides. See {@link WithSupabaseConfig}.
|
|
49
|
+
* @param handler - Receives the `Request` and a fully-initialized {@link SupabaseContext}.
|
|
50
|
+
* @returns A `(req: Request) => Promise<Response>` fetch handler.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { withSupabase } from '@supabase/server'
|
|
55
|
+
*
|
|
56
|
+
* export default {
|
|
57
|
+
* fetch: withSupabase({ allow: 'user' }, async (req, ctx) => {
|
|
58
|
+
* const { data } = await ctx.supabase.rpc('get_my_profile')
|
|
59
|
+
* return Response.json(data)
|
|
60
|
+
* }),
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
21
64
|
function withSupabase(config, handler) {
|
|
22
65
|
return async (req) => {
|
|
23
66
|
if (config.cors !== false && req.method === "OPTIONS") return new Response(null, {
|