Skip to content

Commit dc13ad7

Browse files
committed
docs(repo): add example snippets
1 parent 5fb115f commit dc13ad7

File tree

13 files changed

+305
-0
lines changed

13 files changed

+305
-0
lines changed

packages/core/auth-js/src/GoTrueAdminApi.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ export default class GoTrueAdminApi {
4545
}
4646
protected fetch: Fetch
4747

48+
/**
49+
* Creates an admin API client that can be used to manage users and OAuth clients.
50+
*
51+
* @example
52+
* ```ts
53+
* import { GoTrueAdminApi } from '@supabase/auth-js'
54+
*
55+
* const admin = new GoTrueAdminApi({
56+
* url: 'https://xyzcompany.supabase.co/auth/v1',
57+
* headers: { Authorization: `Bearer ${process.env.SUPABASE_SERVICE_ROLE_KEY}` },
58+
* })
59+
* ```
60+
*/
4861
constructor({
4962
url = '',
5063
headers = {},

packages/core/auth-js/src/GoTrueClient.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ export default class GoTrueClient {
275275

276276
/**
277277
* Create a new client for use in the browser.
278+
*
279+
* @example
280+
* ```ts
281+
* import { GoTrueClient } from '@supabase/auth-js'
282+
*
283+
* const auth = new GoTrueClient({
284+
* url: 'https://xyzcompany.supabase.co/auth/v1',
285+
* headers: { apikey: 'public-anon-key' },
286+
* storageKey: 'supabase-auth',
287+
* })
288+
* ```
278289
*/
279290
constructor(options: GoTrueClientOptions) {
280291
const settings = { ...DEFAULT_OPTIONS, ...options }

packages/core/auth-js/src/lib/errors.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { WeakPasswordReasons } from './types'
22
import { ErrorCode } from './error-codes'
33

4+
/**
5+
* Base error thrown by Supabase Auth helpers.
6+
*
7+
* @example
8+
* ```ts
9+
* import { AuthError } from '@supabase/auth-js'
10+
*
11+
* throw new AuthError('Unexpected auth error', 500, 'unexpected')
12+
* ```
13+
*/
414
export class AuthError extends Error {
515
/**
616
* Error code associated with the error. Most errors coming from
@@ -27,6 +37,16 @@ export function isAuthError(error: unknown): error is AuthError {
2737
return typeof error === 'object' && error !== null && '__isAuthError' in error
2838
}
2939

40+
/**
41+
* Error returned directly from the GoTrue REST API.
42+
*
43+
* @example
44+
* ```ts
45+
* import { AuthApiError } from '@supabase/auth-js'
46+
*
47+
* throw new AuthApiError('Invalid credentials', 400, 'invalid_credentials')
48+
* ```
49+
*/
3050
export class AuthApiError extends AuthError {
3151
status: number
3252

@@ -42,6 +62,20 @@ export function isAuthApiError(error: unknown): error is AuthApiError {
4262
return isAuthError(error) && error.name === 'AuthApiError'
4363
}
4464

65+
/**
66+
* Wraps non-standard errors so callers can inspect the root cause.
67+
*
68+
* @example
69+
* ```ts
70+
* import { AuthUnknownError } from '@supabase/auth-js'
71+
*
72+
* try {
73+
* await someAuthCall()
74+
* } catch (err) {
75+
* throw new AuthUnknownError('Auth failed', err)
76+
* }
77+
* ```
78+
*/
4579
export class AuthUnknownError extends AuthError {
4680
originalError: unknown
4781

@@ -52,6 +86,16 @@ export class AuthUnknownError extends AuthError {
5286
}
5387
}
5488

89+
/**
90+
* Flexible error class used to create named auth errors at runtime.
91+
*
92+
* @example
93+
* ```ts
94+
* import { CustomAuthError } from '@supabase/auth-js'
95+
*
96+
* throw new CustomAuthError('My custom auth error', 'MyAuthError', 400, 'custom_code')
97+
* ```
98+
*/
5599
export class CustomAuthError extends AuthError {
56100
name: string
57101
status: number
@@ -63,6 +107,16 @@ export class CustomAuthError extends AuthError {
63107
}
64108
}
65109

110+
/**
111+
* Error thrown when an operation requires a session but none is present.
112+
*
113+
* @example
114+
* ```ts
115+
* import { AuthSessionMissingError } from '@supabase/auth-js'
116+
*
117+
* throw new AuthSessionMissingError()
118+
* ```
119+
*/
66120
export class AuthSessionMissingError extends CustomAuthError {
67121
constructor() {
68122
super('Auth session missing!', 'AuthSessionMissingError', 400, undefined)
@@ -73,18 +127,51 @@ export function isAuthSessionMissingError(error: any): error is AuthSessionMissi
73127
return isAuthError(error) && error.name === 'AuthSessionMissingError'
74128
}
75129

130+
/**
131+
* Error thrown when the token response is malformed.
132+
*
133+
* @example
134+
* ```ts
135+
* import { AuthInvalidTokenResponseError } from '@supabase/auth-js'
136+
*
137+
* throw new AuthInvalidTokenResponseError()
138+
* ```
139+
*/
76140
export class AuthInvalidTokenResponseError extends CustomAuthError {
77141
constructor() {
78142
super('Auth session or user missing', 'AuthInvalidTokenResponseError', 500, undefined)
79143
}
80144
}
81145

146+
/**
147+
* Error thrown when email/password credentials are invalid.
148+
*
149+
* @example
150+
* ```ts
151+
* import { AuthInvalidCredentialsError } from '@supabase/auth-js'
152+
*
153+
* throw new AuthInvalidCredentialsError('Email or password is incorrect')
154+
* ```
155+
*/
82156
export class AuthInvalidCredentialsError extends CustomAuthError {
83157
constructor(message: string) {
84158
super(message, 'AuthInvalidCredentialsError', 400, undefined)
85159
}
86160
}
87161

162+
/**
163+
* Error thrown when implicit grant redirects contain an error.
164+
*
165+
* @example
166+
* ```ts
167+
* import { AuthImplicitGrantRedirectError } from '@supabase/auth-js'
168+
*
169+
* throw new AuthImplicitGrantRedirectError('OAuth redirect failed', {
170+
* error: 'access_denied',
171+
* code: 'oauth_error',
172+
* })
173+
* ```
174+
*/
88175
export class AuthImplicitGrantRedirectError extends CustomAuthError {
89176
details: { error: string; code: string } | null = null
90177
constructor(message: string, details: { error: string; code: string } | null = null) {
@@ -108,6 +195,16 @@ export function isAuthImplicitGrantRedirectError(
108195
return isAuthError(error) && error.name === 'AuthImplicitGrantRedirectError'
109196
}
110197

198+
/**
199+
* Error thrown during PKCE code exchanges.
200+
*
201+
* @example
202+
* ```ts
203+
* import { AuthPKCEGrantCodeExchangeError } from '@supabase/auth-js'
204+
*
205+
* throw new AuthPKCEGrantCodeExchangeError('PKCE exchange failed')
206+
* ```
207+
*/
111208
export class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
112209
details: { error: string; code: string } | null = null
113210

@@ -126,6 +223,16 @@ export class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
126223
}
127224
}
128225

226+
/**
227+
* Error thrown when a transient fetch issue occurs.
228+
*
229+
* @example
230+
* ```ts
231+
* import { AuthRetryableFetchError } from '@supabase/auth-js'
232+
*
233+
* throw new AuthRetryableFetchError('Service temporarily unavailable', 503)
234+
* ```
235+
*/
129236
export class AuthRetryableFetchError extends CustomAuthError {
130237
constructor(message: string, status: number) {
131238
super(message, 'AuthRetryableFetchError', status, undefined)
@@ -141,6 +248,16 @@ export function isAuthRetryableFetchError(error: unknown): error is AuthRetryabl
141248
* weak. Inspect the reasons to identify what password strength rules are
142249
* inadequate.
143250
*/
251+
/**
252+
* Error thrown when a supplied password is considered weak.
253+
*
254+
* @example
255+
* ```ts
256+
* import { AuthWeakPasswordError } from '@supabase/auth-js'
257+
*
258+
* throw new AuthWeakPasswordError('Password too short', 400, ['min_length'])
259+
* ```
260+
*/
144261
export class AuthWeakPasswordError extends CustomAuthError {
145262
/**
146263
* Reasons why the password is deemed weak.
@@ -158,6 +275,16 @@ export function isAuthWeakPasswordError(error: unknown): error is AuthWeakPasswo
158275
return isAuthError(error) && error.name === 'AuthWeakPasswordError'
159276
}
160277

278+
/**
279+
* Error thrown when a JWT cannot be verified or parsed.
280+
*
281+
* @example
282+
* ```ts
283+
* import { AuthInvalidJwtError } from '@supabase/auth-js'
284+
*
285+
* throw new AuthInvalidJwtError('Token signature is invalid')
286+
* ```
287+
*/
161288
export class AuthInvalidJwtError extends CustomAuthError {
162289
constructor(message: string) {
163290
super(message, 'AuthInvalidJwtError', 400, 'invalid_jwt')

packages/core/auth-js/src/lib/locks.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ export const internals = {
1919
* An error thrown when a lock cannot be acquired after some amount of time.
2020
*
2121
* Use the {@link #isAcquireTimeout} property instead of checking with `instanceof`.
22+
*
23+
* @example
24+
* ```ts
25+
* import { LockAcquireTimeoutError } from '@supabase/auth-js'
26+
*
27+
* class CustomLockError extends LockAcquireTimeoutError {
28+
* constructor() {
29+
* super('Lock timed out')
30+
* }
31+
* }
32+
* ```
2233
*/
2334
export abstract class LockAcquireTimeoutError extends Error {
2435
public readonly isAcquireTimeout = true
@@ -28,7 +39,27 @@ export abstract class LockAcquireTimeoutError extends Error {
2839
}
2940
}
3041

42+
/**
43+
* Error thrown when the browser Navigator Lock API fails to acquire a lock.
44+
*
45+
* @example
46+
* ```ts
47+
* import { NavigatorLockAcquireTimeoutError } from '@supabase/auth-js'
48+
*
49+
* throw new NavigatorLockAcquireTimeoutError('Lock timed out')
50+
* ```
51+
*/
3152
export class NavigatorLockAcquireTimeoutError extends LockAcquireTimeoutError {}
53+
/**
54+
* Error thrown when the process-level lock helper cannot acquire a lock.
55+
*
56+
* @example
57+
* ```ts
58+
* import { ProcessLockAcquireTimeoutError } from '@supabase/auth-js'
59+
*
60+
* throw new ProcessLockAcquireTimeoutError('Lock timed out')
61+
* ```
62+
*/
3263
export class ProcessLockAcquireTimeoutError extends LockAcquireTimeoutError {}
3364

3465
/**

packages/core/functions-js/src/types.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ export interface FunctionsResponseFailure {
1515
}
1616
export type FunctionsResponse<T> = FunctionsResponseSuccess<T> | FunctionsResponseFailure
1717

18+
/**
19+
* Base error for Supabase Edge Function invocations.
20+
*
21+
* @example
22+
* ```ts
23+
* import { FunctionsError } from '@supabase/functions-js'
24+
*
25+
* throw new FunctionsError('Unexpected error invoking function', 'FunctionsError', {
26+
* requestId: 'abc123',
27+
* })
28+
* ```
29+
*/
1830
export class FunctionsError extends Error {
1931
context: any
2032
constructor(message: string, name = 'FunctionsError', context?: any) {
@@ -24,18 +36,48 @@ export class FunctionsError extends Error {
2436
}
2537
}
2638

39+
/**
40+
* Error thrown when the network request to an Edge Function fails.
41+
*
42+
* @example
43+
* ```ts
44+
* import { FunctionsFetchError } from '@supabase/functions-js'
45+
*
46+
* throw new FunctionsFetchError({ requestId: 'abc123' })
47+
* ```
48+
*/
2749
export class FunctionsFetchError extends FunctionsError {
2850
constructor(context: any) {
2951
super('Failed to send a request to the Edge Function', 'FunctionsFetchError', context)
3052
}
3153
}
3254

55+
/**
56+
* Error thrown when the Supabase relay cannot reach the Edge Function.
57+
*
58+
* @example
59+
* ```ts
60+
* import { FunctionsRelayError } from '@supabase/functions-js'
61+
*
62+
* throw new FunctionsRelayError({ region: 'us-east-1' })
63+
* ```
64+
*/
3365
export class FunctionsRelayError extends FunctionsError {
3466
constructor(context: any) {
3567
super('Relay Error invoking the Edge Function', 'FunctionsRelayError', context)
3668
}
3769
}
3870

71+
/**
72+
* Error thrown when the Edge Function returns a non-2xx status code.
73+
*
74+
* @example
75+
* ```ts
76+
* import { FunctionsHttpError } from '@supabase/functions-js'
77+
*
78+
* throw new FunctionsHttpError({ status: 500 })
79+
* ```
80+
*/
3981
export class FunctionsHttpError extends FunctionsError {
4082
constructor(context: any) {
4183
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context)

packages/core/postgrest-js/src/PostgrestBuilder.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ export default abstract class PostgrestBuilder<
2828
protected fetch: Fetch
2929
protected isMaybeSingle: boolean
3030

31+
/**
32+
* @example
33+
* ```ts
34+
* import PostgrestQueryBuilder from '@supabase/postgrest-js'
35+
*
36+
* const builder = new PostgrestQueryBuilder(
37+
* new URL('https://xyzcompany.supabase.co/rest/v1/users'),
38+
* { headers: new Headers({ apikey: 'public-anon-key' }) }
39+
* )
40+
* ```
41+
*/
3142
constructor(builder: {
3243
method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
3344
url: URL

packages/core/postgrest-js/src/PostgrestClient.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export default class PostgrestClient<
4848
* @param options.headers - Custom headers
4949
* @param options.schema - Postgres schema to switch to
5050
* @param options.fetch - Custom fetch
51+
* @example
52+
* ```ts
53+
* import PostgrestClient from '@supabase/postgrest-js'
54+
*
55+
* const postgrest = new PostgrestClient('https://xyzcompany.supabase.co/rest/v1', {
56+
* headers: { apikey: 'public-anon-key' },
57+
* schema: 'public',
58+
* })
59+
* ```
5160
*/
5261
constructor(
5362
url: string,

0 commit comments

Comments
 (0)