Skip to content

Commit a66387e

Browse files
authored
docs(repo): add example snippets and descriptions (#1860)
1 parent 5fb115f commit a66387e

File tree

21 files changed

+570
-7
lines changed

21 files changed

+570
-7
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: 43 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
/**
@@ -55,6 +86,12 @@ export class ProcessLockAcquireTimeoutError extends LockAcquireTimeoutError {}
5586
* will time out after so many milliseconds. An error is
5687
* a timeout if it has `isAcquireTimeout` set to true.
5788
* @param fn The operation to run once the lock is acquired.
89+
* @example
90+
* ```ts
91+
* await navigatorLock('sync-user', 1000, async () => {
92+
* await refreshSession()
93+
* })
94+
* ```
5895
*/
5996
export async function navigatorLock<R>(
6097
name: string,
@@ -167,6 +204,12 @@ const PROCESS_LOCKS: { [name: string]: Promise<any> } = {}
167204
* will time out after so many milliseconds. An error is
168205
* a timeout if it has `isAcquireTimeout` set to true.
169206
* @param fn The operation to run once the lock is acquired.
207+
* @example
208+
* ```ts
209+
* await processLock('migrate', 5000, async () => {
210+
* await runMigration()
211+
* })
212+
* ```
170213
*/
171214
export async function processLock<R>(
172215
name: string,

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@ import {
99
FunctionsResponse,
1010
} from './types'
1111

12+
/**
13+
* Client for invoking Supabase Edge Functions.
14+
*/
1215
export class FunctionsClient {
1316
protected url: string
1417
protected headers: Record<string, string>
1518
protected region: FunctionRegion
1619
protected fetch: Fetch
1720

21+
/**
22+
* Creates a new Functions client bound to an Edge Functions URL.
23+
*
24+
* @example
25+
* ```ts
26+
* import { FunctionsClient, FunctionRegion } from '@supabase/functions-js'
27+
*
28+
* const functions = new FunctionsClient('https://xyzcompany.supabase.co/functions/v1', {
29+
* headers: { apikey: 'public-anon-key' },
30+
* region: FunctionRegion.UsEast1,
31+
* })
32+
* ```
33+
*/
1834
constructor(
1935
url: string,
2036
{
@@ -36,6 +52,10 @@ export class FunctionsClient {
3652
/**
3753
* Updates the authorization header
3854
* @param token - the new jwt token sent in the authorisation header
55+
* @example
56+
* ```ts
57+
* functions.setAuth(session.access_token)
58+
* ```
3959
*/
4060
setAuth(token: string) {
4161
this.headers.Authorization = `Bearer ${token}`
@@ -45,6 +65,12 @@ export class FunctionsClient {
4565
* Invokes a function
4666
* @param functionName - The name of the Function to invoke.
4767
* @param options - Options for invoking the Function.
68+
* @example
69+
* ```ts
70+
* const { data, error } = await functions.invoke('hello-world', {
71+
* body: { name: 'Ada' },
72+
* })
73+
* ```
4874
*/
4975
async invoke<T = any>(
5076
functionName: string,

0 commit comments

Comments
 (0)