@supatype/client 0.1.0-alpha.10
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/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test.log +23 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/dist/auth.d.ts +279 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +848 -0
- package/dist/auth.js.map +1 -0
- package/dist/error-codes-doc.d.ts +31 -0
- package/dist/error-codes-doc.d.ts.map +1 -0
- package/dist/error-codes-doc.js +294 -0
- package/dist/error-codes-doc.js.map +1 -0
- package/dist/errors.d.ts +197 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +162 -0
- package/dist/errors.js.map +1 -0
- package/dist/fetch-with-retry.d.ts +49 -0
- package/dist/fetch-with-retry.d.ts.map +1 -0
- package/dist/fetch-with-retry.js +150 -0
- package/dist/fetch-with-retry.js.map +1 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +285 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +55 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +264 -0
- package/dist/query.js.map +1 -0
- package/dist/realtime.d.ts +69 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +279 -0
- package/dist/realtime.js.map +1 -0
- package/dist/retry.d.ts +24 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +25 -0
- package/dist/retry.js.map +1 -0
- package/dist/serverless-docs.d.ts +38 -0
- package/dist/serverless-docs.d.ts.map +1 -0
- package/dist/serverless-docs.js +83 -0
- package/dist/serverless-docs.js.map +1 -0
- package/dist/storage.d.ts +102 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +169 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +26 -0
- package/src/auth.ts +1024 -0
- package/src/error-codes-doc.ts +315 -0
- package/src/errors.ts +201 -0
- package/src/fetch-with-retry.ts +200 -0
- package/src/index.ts +518 -0
- package/src/query.ts +331 -0
- package/src/realtime.ts +418 -0
- package/src/retry.ts +35 -0
- package/src/serverless-docs.ts +107 -0
- package/src/storage.ts +234 -0
- package/src/types.ts +264 -0
- package/tests/auth.test.ts +351 -0
- package/tests/mfa-totp-integration.test.ts +359 -0
- package/tests/query.test.ts +286 -0
- package/tests/rate-limit-integration.test.ts +185 -0
- package/tests/realtime-multi-tenant-integration.test.ts +372 -0
- package/tests/retry-integration.test.ts +212 -0
- package/tests/serverless.test.ts +115 -0
- package/tests/storage-quota-integration.test.ts +189 -0
- package/tests/storage-upload-limit-integration.test.ts +185 -0
- package/tests/types.test.ts +137 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
2
|
+
import { AuthClient } from "../src/auth.js"
|
|
3
|
+
|
|
4
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
const GOTRUE_URL = "http://localhost:9999"
|
|
7
|
+
const HEADERS = { apikey: "test-anon-key", "Content-Type": "application/json" }
|
|
8
|
+
|
|
9
|
+
const RAW_USER = {
|
|
10
|
+
id: "user-1",
|
|
11
|
+
email: "test@example.com",
|
|
12
|
+
created_at: "2024-01-01T00:00:00Z",
|
|
13
|
+
updated_at: "2024-01-01T00:00:00Z",
|
|
14
|
+
app_metadata: { provider: "email" },
|
|
15
|
+
user_metadata: { name: "Test" },
|
|
16
|
+
role: "authenticated",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const TOKEN_RESPONSE = {
|
|
20
|
+
access_token: "access-token-123",
|
|
21
|
+
refresh_token: "refresh-token-abc",
|
|
22
|
+
token_type: "bearer",
|
|
23
|
+
expires_in: 3600,
|
|
24
|
+
user: RAW_USER,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function mockFetch(body: unknown, ok = true, status?: number): ReturnType<typeof vi.fn> {
|
|
28
|
+
return vi.fn().mockResolvedValue({
|
|
29
|
+
ok,
|
|
30
|
+
status: status ?? (ok ? 200 : 400),
|
|
31
|
+
json: vi.fn().mockResolvedValue(body),
|
|
32
|
+
headers: { get: () => null },
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function freshClient(): AuthClient {
|
|
37
|
+
return new AuthClient(GOTRUE_URL, HEADERS)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
describe("AuthClient.signUp()", () => {
|
|
43
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
44
|
+
|
|
45
|
+
it("sends POST to /signup", async () => {
|
|
46
|
+
const fetch = mockFetch(TOKEN_RESPONSE)
|
|
47
|
+
vi.stubGlobal("fetch", fetch)
|
|
48
|
+
|
|
49
|
+
const { error } = await freshClient().signUp({ email: "a@b.com", password: "secret" })
|
|
50
|
+
|
|
51
|
+
expect(error).toBeNull()
|
|
52
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
53
|
+
`${GOTRUE_URL}/signup`,
|
|
54
|
+
expect.objectContaining({ method: "POST" }),
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("includes optional user metadata in request body", async () => {
|
|
59
|
+
const fetch = mockFetch(TOKEN_RESPONSE)
|
|
60
|
+
vi.stubGlobal("fetch", fetch)
|
|
61
|
+
|
|
62
|
+
await freshClient().signUp({ email: "a@b.com", password: "s", options: { data: { name: "Alice" } } })
|
|
63
|
+
|
|
64
|
+
const [, opts] = fetch.mock.calls[0] as [string, RequestInit]
|
|
65
|
+
const body = JSON.parse(opts.body as string) as Record<string, unknown>
|
|
66
|
+
expect(body["data"]).toEqual({ name: "Alice" })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it("returns error on 400", async () => {
|
|
70
|
+
vi.stubGlobal("fetch", mockFetch({ error_description: "Email taken" }, false, 400))
|
|
71
|
+
|
|
72
|
+
const { data, error } = await freshClient().signUp({ email: "a@b.com", password: "s" })
|
|
73
|
+
|
|
74
|
+
expect(data.session).toBeNull()
|
|
75
|
+
expect(error?.message).toBe("Email taken")
|
|
76
|
+
expect(error?.status).toBe(400)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it("parses session and user from response", async () => {
|
|
80
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
81
|
+
|
|
82
|
+
const { data, error } = await freshClient().signUp({ email: "a@b.com", password: "s" })
|
|
83
|
+
|
|
84
|
+
expect(error).toBeNull()
|
|
85
|
+
expect(data.session?.accessToken).toBe("access-token-123")
|
|
86
|
+
expect(data.session?.refreshToken).toBe("refresh-token-abc")
|
|
87
|
+
expect(data.user?.id).toBe("user-1")
|
|
88
|
+
expect(data.user?.email).toBe("test@example.com")
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
describe("AuthClient.signInAnonymously()", () => {
|
|
93
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
94
|
+
|
|
95
|
+
const ANON_USER = {
|
|
96
|
+
...RAW_USER,
|
|
97
|
+
email: undefined,
|
|
98
|
+
is_anonymous: true,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const ANON_TOKEN_RESPONSE = {
|
|
102
|
+
...TOKEN_RESPONSE,
|
|
103
|
+
user: ANON_USER,
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
it("sends POST to /signup with no email or password", async () => {
|
|
107
|
+
const fetch = mockFetch(ANON_TOKEN_RESPONSE)
|
|
108
|
+
vi.stubGlobal("fetch", fetch)
|
|
109
|
+
|
|
110
|
+
const { error } = await freshClient().signInAnonymously()
|
|
111
|
+
|
|
112
|
+
expect(error).toBeNull()
|
|
113
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
114
|
+
`${GOTRUE_URL}/signup`,
|
|
115
|
+
expect.objectContaining({ method: "POST" }),
|
|
116
|
+
)
|
|
117
|
+
const [, opts] = fetch.mock.calls[0] as [string, RequestInit]
|
|
118
|
+
const body = JSON.parse(opts.body as string) as Record<string, unknown>
|
|
119
|
+
expect(body["email"]).toBeUndefined()
|
|
120
|
+
expect(body["password"]).toBeUndefined()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it("includes optional user metadata and captcha token", async () => {
|
|
124
|
+
const fetch = mockFetch(ANON_TOKEN_RESPONSE)
|
|
125
|
+
vi.stubGlobal("fetch", fetch)
|
|
126
|
+
|
|
127
|
+
await freshClient().signInAnonymously({
|
|
128
|
+
options: { data: { guest: true }, captchaToken: "captcha-123" },
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
const [, opts] = fetch.mock.calls[0] as [string, RequestInit]
|
|
132
|
+
const body = JSON.parse(opts.body as string) as Record<string, unknown>
|
|
133
|
+
expect(body["data"]).toEqual({ guest: true })
|
|
134
|
+
expect(body["gotrue_meta_security"]).toEqual({ captcha_token: "captcha-123" })
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it("parses isAnonymous from response", async () => {
|
|
138
|
+
vi.stubGlobal("fetch", mockFetch(ANON_TOKEN_RESPONSE))
|
|
139
|
+
|
|
140
|
+
const { data, error } = await freshClient().signInAnonymously()
|
|
141
|
+
|
|
142
|
+
expect(error).toBeNull()
|
|
143
|
+
expect(data.user?.isAnonymous).toBe(true)
|
|
144
|
+
expect(data.session?.accessToken).toBe("access-token-123")
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it("returns error when anonymous sign-ins are disabled", async () => {
|
|
148
|
+
vi.stubGlobal(
|
|
149
|
+
"fetch",
|
|
150
|
+
mockFetch({ msg: "Anonymous sign-ins are disabled" }, false, 422),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
const { data, error } = await freshClient().signInAnonymously()
|
|
154
|
+
|
|
155
|
+
expect(data.session).toBeNull()
|
|
156
|
+
expect(error?.message).toBe("Anonymous sign-ins are disabled")
|
|
157
|
+
expect(error?.status).toBe(422)
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
describe("AuthClient.signInWithPassword()", () => {
|
|
162
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
163
|
+
|
|
164
|
+
it("sends POST to /token?grant_type=password", async () => {
|
|
165
|
+
const fetch = mockFetch(TOKEN_RESPONSE)
|
|
166
|
+
vi.stubGlobal("fetch", fetch)
|
|
167
|
+
|
|
168
|
+
await freshClient().signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
169
|
+
|
|
170
|
+
const [url] = fetch.mock.calls[0] as [string, unknown]
|
|
171
|
+
expect(url).toBe(`${GOTRUE_URL}/token?grant_type=password`)
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it("returns error on invalid credentials", async () => {
|
|
175
|
+
vi.stubGlobal("fetch", mockFetch({ error_description: "Invalid login credentials" }, false, 400))
|
|
176
|
+
|
|
177
|
+
const { error } = await freshClient().signInWithPassword({ email: "a@b.com", password: "wrong" })
|
|
178
|
+
|
|
179
|
+
expect(error?.message).toBe("Invalid login credentials")
|
|
180
|
+
expect(error?.status).toBe(400)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it("updates internal session state", async () => {
|
|
184
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
185
|
+
|
|
186
|
+
const client = freshClient()
|
|
187
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
188
|
+
const { data } = await client.getSession()
|
|
189
|
+
|
|
190
|
+
expect(data.session?.accessToken).toBe("access-token-123")
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
describe("AuthClient.signInWithOAuth()", () => {
|
|
195
|
+
it("returns redirect URL without making a fetch call", async () => {
|
|
196
|
+
const fetchSpy = vi.fn()
|
|
197
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
198
|
+
|
|
199
|
+
const { data, error } = await freshClient().signInWithOAuth({ provider: "google" })
|
|
200
|
+
|
|
201
|
+
expect(fetchSpy).not.toHaveBeenCalled()
|
|
202
|
+
expect(error).toBeNull()
|
|
203
|
+
expect(data.url).toContain("authorize")
|
|
204
|
+
expect(data.url).toContain("provider=google")
|
|
205
|
+
expect(data.provider).toBe("google")
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it("appends redirect_to param when provided", async () => {
|
|
209
|
+
vi.stubGlobal("fetch", vi.fn())
|
|
210
|
+
|
|
211
|
+
const { data } = await freshClient().signInWithOAuth({
|
|
212
|
+
provider: "github",
|
|
213
|
+
options: { redirectTo: "http://localhost:3000/auth/callback" },
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
expect(data.url).toContain("redirect_to=")
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
describe("AuthClient.signOut()", () => {
|
|
221
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
222
|
+
|
|
223
|
+
it("calls /logout when a session exists", async () => {
|
|
224
|
+
const client = freshClient()
|
|
225
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
226
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
227
|
+
|
|
228
|
+
const logoutFetch = vi.fn().mockResolvedValue({ ok: true, status: 204 })
|
|
229
|
+
vi.stubGlobal("fetch", logoutFetch)
|
|
230
|
+
await client.signOut()
|
|
231
|
+
|
|
232
|
+
const [url, opts] = logoutFetch.mock.calls[0] as [string, RequestInit]
|
|
233
|
+
expect(url).toBe(`${GOTRUE_URL}/logout`)
|
|
234
|
+
expect(opts.method).toBe("POST")
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it("clears session after sign out", async () => {
|
|
238
|
+
const client = freshClient()
|
|
239
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
240
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
241
|
+
|
|
242
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true, status: 204 }))
|
|
243
|
+
await client.signOut()
|
|
244
|
+
|
|
245
|
+
const { data } = await client.getSession()
|
|
246
|
+
expect(data.session).toBeNull()
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
it("does not call /logout when no session exists", async () => {
|
|
250
|
+
const fetchSpy = vi.fn()
|
|
251
|
+
vi.stubGlobal("fetch", fetchSpy)
|
|
252
|
+
|
|
253
|
+
await freshClient().signOut()
|
|
254
|
+
|
|
255
|
+
expect(fetchSpy).not.toHaveBeenCalled()
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
describe("AuthClient.onAuthStateChange()", () => {
|
|
260
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
261
|
+
|
|
262
|
+
it("emits SIGNED_OUT immediately when not signed in", () => {
|
|
263
|
+
const events: string[] = []
|
|
264
|
+
const client = freshClient()
|
|
265
|
+
const { data: { subscription } } = client.onAuthStateChange((event) => events.push(event))
|
|
266
|
+
|
|
267
|
+
expect(events).toContain("SIGNED_OUT")
|
|
268
|
+
subscription.unsubscribe()
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it("emits SIGNED_IN after successful sign in", async () => {
|
|
272
|
+
const events: string[] = []
|
|
273
|
+
const client = freshClient()
|
|
274
|
+
const { data: { subscription } } = client.onAuthStateChange((event) => events.push(event))
|
|
275
|
+
|
|
276
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
277
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
278
|
+
|
|
279
|
+
expect(events).toContain("SIGNED_IN")
|
|
280
|
+
subscription.unsubscribe()
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it("emits SIGNED_OUT after sign out", async () => {
|
|
284
|
+
const events: string[] = []
|
|
285
|
+
const client = freshClient()
|
|
286
|
+
|
|
287
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
288
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
289
|
+
|
|
290
|
+
const { data: { subscription } } = client.onAuthStateChange((event) => events.push(event))
|
|
291
|
+
events.length = 0 // clear SIGNED_IN from subscription creation
|
|
292
|
+
|
|
293
|
+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok: true, status: 204 }))
|
|
294
|
+
await client.signOut()
|
|
295
|
+
|
|
296
|
+
expect(events).toContain("SIGNED_OUT")
|
|
297
|
+
subscription.unsubscribe()
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it("unsubscribe stops future notifications", async () => {
|
|
301
|
+
let callCount = 0
|
|
302
|
+
const client = freshClient()
|
|
303
|
+
const { data: { subscription } } = client.onAuthStateChange(() => callCount++)
|
|
304
|
+
const countAtUnsubscribe = callCount
|
|
305
|
+
subscription.unsubscribe()
|
|
306
|
+
|
|
307
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
308
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
309
|
+
|
|
310
|
+
expect(callCount).toBe(countAtUnsubscribe) // no new calls
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
it("multiple listeners each receive events", async () => {
|
|
314
|
+
const events1: string[] = []
|
|
315
|
+
const events2: string[] = []
|
|
316
|
+
const client = freshClient()
|
|
317
|
+
|
|
318
|
+
const { data: { subscription: s1 } } = client.onAuthStateChange((e) => events1.push(e))
|
|
319
|
+
const { data: { subscription: s2 } } = client.onAuthStateChange((e) => events2.push(e))
|
|
320
|
+
|
|
321
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
322
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
323
|
+
|
|
324
|
+
expect(events1).toContain("SIGNED_IN")
|
|
325
|
+
expect(events2).toContain("SIGNED_IN")
|
|
326
|
+
s1.unsubscribe()
|
|
327
|
+
s2.unsubscribe()
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
describe("AuthClient.getSession() / getUser()", () => {
|
|
332
|
+
it("getSession returns null when not authenticated", async () => {
|
|
333
|
+
const { data } = await freshClient().getSession()
|
|
334
|
+
expect(data.session).toBeNull()
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
it("getUser fetches /user with Authorization header when session exists", async () => {
|
|
338
|
+
const client = freshClient()
|
|
339
|
+
vi.stubGlobal("fetch", mockFetch(TOKEN_RESPONSE))
|
|
340
|
+
await client.signInWithPassword({ email: "a@b.com", password: "pass" })
|
|
341
|
+
|
|
342
|
+
const userFetch = mockFetch(RAW_USER)
|
|
343
|
+
vi.stubGlobal("fetch", userFetch)
|
|
344
|
+
const { data, error } = await client.getUser()
|
|
345
|
+
|
|
346
|
+
expect(error).toBeNull()
|
|
347
|
+
expect(data.user?.id).toBe("user-1")
|
|
348
|
+
const [, opts] = userFetch.mock.calls[0] as [string, RequestInit]
|
|
349
|
+
expect((opts.headers as Record<string, string>)["Authorization"]).toContain("Bearer access-token-123")
|
|
350
|
+
})
|
|
351
|
+
})
|