Skip to content

Commit 11a863b

Browse files
thinkclayclaude
andauthored
fix(http): send Supabase apikey header when anonKey is configured (#6)
Supabase Kong (the gateway in front of /functions/v1/*) rejects every request that lacks an `apikey` header, even when Authorization carries a valid Bearer JWT. The SDK previously only emitted Authorization, so any caller whose baseUrl pointed at a Supabase Edge Function got back a 400 with `{"message":"No API key found in request"}`. Adds a typed `anonKey` field to ScaffaldConfig and has HttpClient emit `apikey: <anonKey>` on every request when it's set. Public-endpoint callers can pass anonKey alone (no Authorization) — useful for unauthenticated routes like /v1/auth/magic-link. Co-authored-by: Claude Opus 4.7 <[email protected]>
1 parent 0e8e5ba commit 11a863b

4 files changed

Lines changed: 62 additions & 4 deletions

File tree

‎src/__tests__/http-client.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,50 @@ describe('HTTP Client', () => {
4141
expect(jobs).toBeDefined()
4242
})
4343

44+
it('should accept anonKey alone (for public endpoints behind Supabase Kong)', async () => {
45+
const clientWithAnonKey = new Scaffald({
46+
anonKey: 'public_anon_key_123',
47+
})
48+
const jobs = await clientWithAnonKey.jobs.list()
49+
expect(jobs).toBeDefined()
50+
})
51+
52+
it('should send apikey header when anonKey is configured', async () => {
53+
let capturedApikey: string | null = null
54+
server.use(
55+
http.get('https://api.scaffald.com/v1/jobs', ({ request }) => {
56+
capturedApikey = request.headers.get('apikey')
57+
return HttpResponse.json({ data: [] })
58+
})
59+
)
60+
61+
const clientWithAnonKey = new Scaffald({
62+
supabaseToken: 'jwt_token',
63+
anonKey: 'public_anon_key_xyz',
64+
})
65+
await clientWithAnonKey.jobs.list()
66+
67+
expect(capturedApikey).toBe('public_anon_key_xyz')
68+
})
69+
70+
it('should not send apikey header when anonKey is omitted', async () => {
71+
let capturedApikey: string | null | undefined = undefined
72+
server.use(
73+
http.get('https://api.scaffald.com/v1/jobs', ({ request }) => {
74+
capturedApikey = request.headers.get('apikey')
75+
return HttpResponse.json({ data: [] })
76+
})
77+
)
78+
79+
const clientNoAnon = new Scaffald({ apiKey: 'sk_test_123' })
80+
await clientNoAnon.jobs.list()
81+
82+
expect(capturedApikey).toBeNull()
83+
})
84+
4485
it('should throw error when no auth is provided', () => {
4586
expect(() => new Scaffald({})).toThrow(
46-
'Either apiKey, accessToken, or supabaseToken must be provided'
87+
'Either apiKey, accessToken, supabaseToken, or anonKey must be provided'
4788
)
4889
})
4990
})

‎src/client.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ export class Scaffald {
141141
public readonly analytics: Analytics
142142

143143
constructor(config: ScaffaldConfig) {
144-
if (!config.apiKey && !config.accessToken && !config.supabaseToken) {
145-
throw new Error('Either apiKey, accessToken, or supabaseToken must be provided')
144+
if (!config.apiKey && !config.accessToken && !config.supabaseToken && !config.anonKey) {
145+
throw new Error('Either apiKey, accessToken, supabaseToken, or anonKey must be provided')
146146
}
147147

148148
const internalConfig: ScaffaldConfigInternal = {

‎src/config.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ export interface ScaffaldConfig {
2020
*/
2121
supabaseToken?: string
2222

23+
/**
24+
* Supabase anon key. When set, the SDK sends it as an `apikey` request header
25+
* on every call. Required whenever the API base sits behind Supabase's Kong
26+
* gateway (e.g. `<project>.supabase.co/functions/v1/api`), which rejects
27+
* requests without `apikey` even when Authorization carries a valid JWT.
28+
* Safe to ship in client bundles — it's the public anon key, not a secret.
29+
*/
30+
anonKey?: string
31+
2332
/**
2433
* Base URL for the Scaffald API
2534
* @default 'https://api.scaffald.com'
@@ -51,10 +60,11 @@ export interface ScaffaldConfig {
5160
}
5261

5362
export interface ScaffaldConfigInternal
54-
extends Required<Omit<ScaffaldConfig, 'apiKey' | 'accessToken' | 'supabaseToken'>> {
63+
extends Required<Omit<ScaffaldConfig, 'apiKey' | 'accessToken' | 'supabaseToken' | 'anonKey'>> {
5564
apiKey?: string
5665
accessToken?: string
5766
supabaseToken?: string
67+
anonKey?: string
5868
}
5969

6070
export const DEFAULT_CONFIG: Omit<ScaffaldConfigInternal, 'apiKey' | 'accessToken'> = {

‎src/http/client.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export class HttpClient {
6060
requestHeaders['Authorization'] = `Bearer ${this.config.supabaseToken}`
6161
}
6262

63+
// Supabase Kong (the gateway in front of /functions/v1/*) rejects any
64+
// request without an `apikey` header — even when Authorization carries a
65+
// valid Bearer JWT. Attach it whenever the caller configured an anonKey.
66+
if (this.config.anonKey) {
67+
requestHeaders['apikey'] = this.config.anonKey
68+
}
69+
6370
if (method === 'POST' && idempotencyKey) {
6471
requestHeaders['Idempotency-Key'] = idempotencyKey
6572
}

0 commit comments

Comments
 (0)