@supatype/client 0.9.0 → 0.9.2
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 +1 -1
- package/.turbo/turbo-test.log +76 -68
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +82 -0
- package/dist/auth.d.ts +50 -6
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +117 -32
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +3 -0
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +2 -0
- package/dist/bindings.js.map +1 -1
- package/dist/error-codes-doc.d.ts +1 -1
- package/dist/error-codes-doc.d.ts.map +1 -1
- package/dist/error-codes-doc.js +7 -0
- package/dist/error-codes-doc.js.map +1 -1
- package/dist/errors.d.ts +81 -21
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +133 -46
- package/dist/errors.js.map +1 -1
- package/dist/graphql-transport.d.ts +8 -1
- package/dist/graphql-transport.d.ts.map +1 -1
- package/dist/graphql-transport.js +5 -5
- package/dist/graphql-transport.js.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +52 -37
- package/dist/index.js.map +1 -1
- package/dist/latest-rows.d.ts +44 -0
- package/dist/latest-rows.d.ts.map +1 -0
- package/dist/latest-rows.js +58 -0
- package/dist/latest-rows.js.map +1 -0
- package/dist/mutation-request.d.ts +2 -1
- package/dist/mutation-request.d.ts.map +1 -1
- package/dist/mutation-request.js +2 -1
- package/dist/mutation-request.js.map +1 -1
- package/dist/page-env-script.d.ts +12 -0
- package/dist/page-env-script.d.ts.map +1 -0
- package/dist/page-env-script.js +30 -0
- package/dist/page-env-script.js.map +1 -0
- package/dist/preview.d.ts +2 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +7 -3
- package/dist/preview.js.map +1 -1
- package/dist/query.d.ts +18 -5
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +33 -16
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +22 -8
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +121 -41
- package/dist/realtime.js.map +1 -1
- package/dist/runtime-env.d.ts +59 -0
- package/dist/runtime-env.d.ts.map +1 -0
- package/dist/runtime-env.js +202 -0
- package/dist/runtime-env.js.map +1 -0
- package/dist/setup-fault.d.ts +13 -0
- package/dist/setup-fault.d.ts.map +1 -0
- package/dist/setup-fault.js +17 -0
- package/dist/setup-fault.js.map +1 -0
- package/dist/storage.d.ts +10 -5
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +22 -13
- package/dist/storage.js.map +1 -1
- package/dist/table-subscription.d.ts +5 -3
- package/dist/table-subscription.d.ts.map +1 -1
- package/dist/table-subscription.js +8 -5
- package/dist/table-subscription.js.map +1 -1
- package/dist/thrown-error.d.ts +16 -0
- package/dist/thrown-error.d.ts.map +1 -0
- package/dist/thrown-error.js +31 -0
- package/dist/thrown-error.js.map +1 -0
- package/dist/types.d.ts +64 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -1
- package/src/auth.ts +121 -36
- package/src/bindings.ts +3 -0
- package/src/error-codes-doc.ts +9 -1
- package/src/errors.ts +155 -57
- package/src/graphql-transport.ts +5 -6
- package/src/index.ts +63 -45
- package/src/latest-rows.ts +95 -0
- package/src/mutation-request.ts +3 -2
- package/src/page-env-script.ts +27 -0
- package/src/preview.ts +8 -4
- package/src/query.ts +38 -19
- package/src/realtime.ts +146 -44
- package/src/runtime-env.ts +262 -0
- package/src/setup-fault.ts +18 -0
- package/src/storage.ts +26 -16
- package/src/table-subscription.ts +8 -6
- package/src/thrown-error.ts +31 -0
- package/src/types.ts +70 -14
- package/tests/auth-errors.test.ts +3 -3
- package/tests/auth-events.test.ts +3 -7
- package/tests/auth-sso.test.ts +291 -0
- package/tests/auth-url-session.test.ts +6 -25
- package/tests/entry-points.test.ts +9 -0
- package/tests/error-family.test.ts +79 -0
- package/tests/examples-runtime-env.test.ts +135 -0
- package/tests/helpers/auth-unmount-binding.ts +63 -0
- package/tests/helpers/mock-websocket.ts +22 -0
- package/tests/helpers/oauth-binding.ts +4 -3
- package/tests/helpers/reactive-options-binding.ts +99 -0
- package/tests/helpers/stale-read-binding.ts +113 -0
- package/tests/helpers/thrown-error-binding.ts +127 -0
- package/tests/postgrest-error-codes.test.ts +4 -4
- package/tests/query.test.ts +39 -2
- package/tests/rate-limit-integration.test.ts +1 -1
- package/tests/realtime-multi-tenant-integration.test.ts +0 -1
- package/tests/realtime-refusal-error.test.ts +79 -0
- package/tests/realtime-rows-after-broadcast.test.ts +205 -0
- package/tests/realtime-session-auth.test.ts +0 -1
- package/tests/realtime-shared-channel.test.ts +9 -27
- package/tests/result-error-class.test.ts +107 -0
- package/tests/retry-policy.test.ts +1 -1
- package/tests/runtime-env-bundle.test.ts +121 -0
- package/tests/runtime-env-expo.test.ts +58 -0
- package/tests/runtime-env-page-script.test.ts +174 -0
- package/tests/runtime-env.test.ts +259 -0
- package/tests/server-bodies.test.ts +3 -3
- package/tests/subscribe-frame.test.ts +48 -0
- package/tests/support/browser.ts +28 -0
- package/tests/support/jwt.ts +5 -0
- package/tests/thrown-error.test.ts +59 -0
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SupatypeError } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* A request a binding made that threw instead of answering `{ data, error }`, as the binding's
|
|
4
|
+
* `error`, reported once with `console.error`.
|
|
5
|
+
*
|
|
6
|
+
* The client answers a refusal or a failed network call with `{ data, error }`, but some faults are
|
|
7
|
+
* thrown on purpose: a client with no API address or anon key throws from its first request. A hook
|
|
8
|
+
* that only awaited the request left its `loading` set for good and the rejection unhandled, so
|
|
9
|
+
* every binding catches it and holds this as its `error` instead.
|
|
10
|
+
*
|
|
11
|
+
* The console line is the point: the error is usually a fault in the app's setup, and an app that
|
|
12
|
+
* never reads `error` would otherwise show an empty page with no word of why. `where` names the
|
|
13
|
+
* hook, so the line says which one.
|
|
14
|
+
*/
|
|
15
|
+
export function reportThrown(where, thrown) {
|
|
16
|
+
console.error(`[supatype] ${where}: the request threw.`, thrown);
|
|
17
|
+
return thrownAsError(thrown);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The thrown value as a binding's `error`.
|
|
21
|
+
*
|
|
22
|
+
* One of the SDK's own errors is kept as it is, so `error instanceof` still names it, a setup fault
|
|
23
|
+
* included. Anything else becomes a `SupatypeError` with its message, holding it as `cause`.
|
|
24
|
+
*/
|
|
25
|
+
function thrownAsError(thrown) {
|
|
26
|
+
if (thrown instanceof SupatypeError)
|
|
27
|
+
return thrown;
|
|
28
|
+
const message = thrown instanceof Error ? thrown.message : typeof thrown === "string" ? thrown : "Unknown error";
|
|
29
|
+
return new SupatypeError({ message }, { cause: thrown });
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=thrown-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thrown-error.js","sourceRoot":"","sources":["../src/thrown-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa,EAAE,MAAe;IACzD,OAAO,CAAC,KAAK,CAAC,cAAc,KAAK,sBAAsB,EAAE,MAAM,CAAC,CAAA;IAChE,OAAO,aAAa,CAAC,MAAM,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,MAAe;IACpC,IAAI,MAAM,YAAY,aAAa;QAAE,OAAO,MAAM,CAAA;IAClD,MAAM,OAAO,GAAG,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,CAAA;IAChH,OAAO,IAAI,aAAa,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAA;AAC1D,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TableRelationship } from "./select-types.js";
|
|
2
|
+
import type { SupatypeError } from "./errors.js";
|
|
2
3
|
export interface UserIdentity {
|
|
3
4
|
id: string;
|
|
4
5
|
userId: string;
|
|
@@ -49,6 +50,50 @@ export type AuthChangeEvent = "SIGNED_IN" | "SIGNED_OUT" | "TOKEN_REFRESHED" | "
|
|
|
49
50
|
* React Native helpers should pass `pkce` (recommended default for mobile).
|
|
50
51
|
*/
|
|
51
52
|
export type AuthFlowType = "pkce" | "implicit";
|
|
53
|
+
/** How `signInWithSSO` starts and ends. */
|
|
54
|
+
export interface SignInWithSSOOptions {
|
|
55
|
+
/** Where the browser lands once signed in; honoured on the site URL's host or the redirect allow list. */
|
|
56
|
+
redirectTo?: string | undefined;
|
|
57
|
+
/** The CAPTCHA token, when the project requires one. */
|
|
58
|
+
captchaToken?: string | undefined;
|
|
59
|
+
/** Return the identity provider's URL and leave the browser where it is. */
|
|
60
|
+
skipBrowserRedirect?: boolean | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Default: `pkce`, so the session comes back as a `?code=` only this browser can exchange, never
|
|
63
|
+
* as tokens in the URL. `implicit` returns tokens in the fragment. (`signInWithOAuth` defaults to
|
|
64
|
+
* `implicit`.) A sign-in the identity provider starts always comes back implicit, and the client
|
|
65
|
+
* accepts it either way.
|
|
66
|
+
*/
|
|
67
|
+
flowType?: AuthFlowType | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Studio's own sign-in. The auth server then returns the browser to its Studio address and
|
|
70
|
+
* ignores `redirectTo`, as `studio=1` does for `signInWithOAuth`. Apps leave this unset.
|
|
71
|
+
*/
|
|
72
|
+
studio?: boolean | undefined;
|
|
73
|
+
}
|
|
74
|
+
/** Sign in through the SAML identity provider registered for the user's email domain. */
|
|
75
|
+
export interface SignInWithSSODomain {
|
|
76
|
+
/** The domain of the user's email address: `acme.com`. */
|
|
77
|
+
domain: string;
|
|
78
|
+
providerId?: never;
|
|
79
|
+
options?: SignInWithSSOOptions | undefined;
|
|
80
|
+
}
|
|
81
|
+
/** Sign in through one SAML identity provider, named by its ID. */
|
|
82
|
+
export interface SignInWithSSOProvider {
|
|
83
|
+
/** The provider's UUID, as the admin API reports it. */
|
|
84
|
+
providerId: string;
|
|
85
|
+
domain?: never;
|
|
86
|
+
options?: SignInWithSSOOptions | undefined;
|
|
87
|
+
}
|
|
88
|
+
/** What `signInWithSSO` takes: an email domain or a provider's ID, never both. */
|
|
89
|
+
export type SignInWithSSOCredentials = SignInWithSSODomain | SignInWithSSOProvider;
|
|
90
|
+
/** What `signInWithSSO` answers: the identity provider's URL, or why there is none. */
|
|
91
|
+
export interface SignInWithSSOResult {
|
|
92
|
+
data: {
|
|
93
|
+
url: string;
|
|
94
|
+
};
|
|
95
|
+
error: SupatypeError | null;
|
|
96
|
+
}
|
|
52
97
|
export interface AuthMFAEnrollResponse {
|
|
53
98
|
/** The MFA factor ID */
|
|
54
99
|
id: string;
|
|
@@ -94,7 +139,10 @@ export interface AuthMFAListFactorsResponse {
|
|
|
94
139
|
phone: Factor[];
|
|
95
140
|
}
|
|
96
141
|
export type OtpType = "signup" | "magiclink" | "recovery" | "invite" | "email_change" | "sms" | "phone_change" | "email";
|
|
97
|
-
|
|
142
|
+
/** The class every result error is; its fields are `SupatypeErrorFields`. */
|
|
143
|
+
export type { SupatypeError };
|
|
144
|
+
/** The fields a `SupatypeError` carries, as given to its constructor and as its JSON. */
|
|
145
|
+
export interface SupatypeErrorFields {
|
|
98
146
|
message: string;
|
|
99
147
|
status?: number | undefined;
|
|
100
148
|
code?: string | undefined;
|
|
@@ -120,15 +168,8 @@ export interface SupatypeError {
|
|
|
120
168
|
*/
|
|
121
169
|
details?: string | undefined;
|
|
122
170
|
}
|
|
123
|
-
/**
|
|
124
|
-
|
|
125
|
-
*
|
|
126
|
-
* `provider` names the OAuth provider this browser started the sign-in with, when the client
|
|
127
|
-
* remembers one, so an app showing one button per provider can put the message by the right one.
|
|
128
|
-
*/
|
|
129
|
-
export interface AuthRedirectError extends SupatypeError {
|
|
130
|
-
provider?: string | undefined;
|
|
131
|
-
}
|
|
171
|
+
/** A sign-in refused in the redirect URL: the class, with the provider it was started with. */
|
|
172
|
+
export type { AuthRedirectError } from "./errors.js";
|
|
132
173
|
/**
|
|
133
174
|
* A column in the response that carries a read restriction.
|
|
134
175
|
*
|
|
@@ -340,10 +381,19 @@ export interface SelectQueryOptions {
|
|
|
340
381
|
profile?: string | undefined;
|
|
341
382
|
}
|
|
342
383
|
export interface SupatypeClientConfig {
|
|
343
|
-
/**
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Base URL of the Supatype gateway (e.g. http://localhost:18473). Left out, or empty as a bundler
|
|
386
|
+
* leaves an unset name, it is read from `window.__SUPATYPE_ENV__` (set by
|
|
387
|
+
* `<script src="/supatype-env.js">`) when the first request is made. It is never guessed from the
|
|
388
|
+
* page's own address: with neither, that request throws `MissingClientValuesError`.
|
|
389
|
+
*/
|
|
390
|
+
url?: string | undefined;
|
|
391
|
+
/**
|
|
392
|
+
* Anon JWT key. Left out, or empty, it is read from `window.__SUPATYPE_ENV__` when the first
|
|
393
|
+
* request is made; with neither, that request throws `MissingClientValuesError` naming the
|
|
394
|
+
* variables to set. `createClient` itself never throws for either.
|
|
395
|
+
*/
|
|
396
|
+
anonKey?: string | undefined;
|
|
347
397
|
/**
|
|
348
398
|
* Service role key: bypasses row-level security.
|
|
349
399
|
* Only set this in trusted server-side or developer-tool contexts.
|
|
@@ -439,5 +489,4 @@ export interface SupatypeClientConfig {
|
|
|
439
489
|
*/
|
|
440
490
|
headers?: Record<string, string> | (() => Record<string, string>) | undefined;
|
|
441
491
|
}
|
|
442
|
-
export {};
|
|
443
492
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAIhD,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAClC;AAED,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,UAAU,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;IACzC,MAAM,EAAE,UAAU,GAAG,YAAY,CAAA;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC3B;AAED,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,IAAI,CAAC,E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MG;QACH,mBAAmB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;QACzC,oFAAoF;QACpF,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC/B;;;;WAIG;QACH,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QACjC;;;WAGG;QACH,OAAO,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;QACjC;;;;;WAKG;QACH,kBAAkB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;KACzC,GAAG,SAAS,CAAA;IACb;;;;;;;;OAQG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC3B;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,kBAAkB,EAAE,UAAU,GAAG,SAAS,CAAA;IAC9D;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IACpC;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAChC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAA;CAC9E"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAgbA;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAA;AAEnC;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,UAAU,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supatype/client",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Supatype client SDK: typed REST, auth, storage, realtime",
|
|
6
6
|
"type": "module",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/node": "^22",
|
|
24
24
|
"typescript": "^5",
|
|
25
|
+
"vite": "^6",
|
|
25
26
|
"vitest": "^4.0.18"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
package/src/auth.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AuthChangeEvent,
|
|
3
3
|
AuthFlowType,
|
|
4
|
-
AuthRedirectError,
|
|
5
4
|
AuthMFAChallengeResponse,
|
|
6
5
|
AuthMFAEnrollResponse,
|
|
7
6
|
AuthMFAListFactorsResponse,
|
|
@@ -9,7 +8,9 @@ import type {
|
|
|
9
8
|
Factor,
|
|
10
9
|
OtpType,
|
|
11
10
|
Session,
|
|
12
|
-
|
|
11
|
+
SignInWithSSOCredentials,
|
|
12
|
+
SignInWithSSOOptions,
|
|
13
|
+
SignInWithSSOResult,
|
|
13
14
|
User,
|
|
14
15
|
UserIdentity,
|
|
15
16
|
} from "./types.js"
|
|
@@ -38,6 +39,9 @@ import {
|
|
|
38
39
|
type Guard,
|
|
39
40
|
} from "./json.js"
|
|
40
41
|
import { decodeJwtPayload } from "./jwt.js"
|
|
42
|
+
import { rethrowSetupFault } from "./setup-fault.js"
|
|
43
|
+
import { settle, type Deferred } from "./runtime-env.js"
|
|
44
|
+
import { AuthRedirectError, SupatypeError } from "./errors.js"
|
|
41
45
|
|
|
42
46
|
export type AuthListener = (event: AuthChangeEvent, session: Session | null) => void
|
|
43
47
|
|
|
@@ -63,6 +67,25 @@ const DEFAULT_COOKIE_PREFIX = "st"
|
|
|
63
67
|
/** Storage key suffixes for the state an OAuth redirect has to survive. */
|
|
64
68
|
const VERIFIER_SUFFIX = "-code-verifier"
|
|
65
69
|
const PROVIDER_SUFFIX = "-oauth-provider"
|
|
70
|
+
/**
|
|
71
|
+
* What a SAML sign-in is remembered as: the `provider` of an `AuthRedirectError` the auth server
|
|
72
|
+
* sent back in the URL for a sign-in `signInWithSSO` started.
|
|
73
|
+
*/
|
|
74
|
+
export const SSO_PROVIDER = "sso"
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The domain `signInWithSSO` looks a provider up by, from what a person typed: the part after the
|
|
78
|
+
* last `@` of a work email, or the whole of a bare domain, trimmed and lower-cased. Null when there
|
|
79
|
+
* is nothing to look up, so a form can refuse it before asking the server.
|
|
80
|
+
*
|
|
81
|
+
* One implementation for `<SSOForm>` and Studio's sign-in, which each had their own and disagreed:
|
|
82
|
+
* Studio found no domain in a bare one, and the form sent a domain typed after `@ ` with its space.
|
|
83
|
+
*/
|
|
84
|
+
export function ssoDomain(input: string): string | null {
|
|
85
|
+
const value = input.trim().toLowerCase()
|
|
86
|
+
const domain = value.slice(value.lastIndexOf("@") + 1).trim()
|
|
87
|
+
return domain === "" ? null : domain
|
|
88
|
+
}
|
|
66
89
|
const DEBUG_AUTH = typeof process !== "undefined" && process.env["NEXT_PUBLIC_SUPATYPE_DEBUG_AUTH"] === "1"
|
|
67
90
|
|
|
68
91
|
/**
|
|
@@ -82,7 +105,7 @@ function authErrorFromBody(body: Record<string, unknown>, status: number): Supat
|
|
|
82
105
|
const message =
|
|
83
106
|
text("msg") ?? text("message") ?? text("error_description") ?? text("error") ??
|
|
84
107
|
`Auth request failed (HTTP ${status})`
|
|
85
|
-
return { message, status, ...(code !== undefined && { code }) }
|
|
108
|
+
return new SupatypeError({ message, status, ...(code !== undefined && { code }) })
|
|
86
109
|
}
|
|
87
110
|
|
|
88
111
|
/** The assurance levels the client reports; the server's unused `aal3` reads as unknown. */
|
|
@@ -135,8 +158,8 @@ function snakeCaseUser(user: Record<string, unknown>): Record<string, unknown> {
|
|
|
135
158
|
}
|
|
136
159
|
|
|
137
160
|
export class AuthClient {
|
|
138
|
-
private readonly
|
|
139
|
-
private readonly
|
|
161
|
+
private readonly urlSource: Deferred<string>
|
|
162
|
+
private readonly baseHeadersSource: Deferred<Record<string, string>>
|
|
140
163
|
private currentSession: Session | null = null
|
|
141
164
|
private readonly listeners = new Map<string, AuthListener>()
|
|
142
165
|
private listenerIdCounter = 0
|
|
@@ -163,9 +186,13 @@ export class AuthClient {
|
|
|
163
186
|
/** In-memory PKCE code_verifier for the in-flight OAuth / OTP redirect. */
|
|
164
187
|
private codeVerifier: string | null = null
|
|
165
188
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
189
|
+
/**
|
|
190
|
+
* `url` and `baseHeaders` may be functions, read for each request. `createClient` passes
|
|
191
|
+
* functions, so a client made before the page has said where the API is fails only at a request.
|
|
192
|
+
*/
|
|
193
|
+
constructor(url: Deferred<string>, baseHeaders: Deferred<Record<string, string>>, opts: AuthClientOptions = {}) {
|
|
194
|
+
this.urlSource = url
|
|
195
|
+
this.baseHeadersSource = baseHeaders
|
|
169
196
|
this.persistSession = opts.persistSession ?? true
|
|
170
197
|
this.autoRefreshToken = opts.autoRefreshToken ?? true
|
|
171
198
|
this.refreshExpiredToken = opts.refreshExpiredToken ?? true
|
|
@@ -189,6 +216,16 @@ export class AuthClient {
|
|
|
189
216
|
}
|
|
190
217
|
}
|
|
191
218
|
|
|
219
|
+
/** The auth address, read now. */
|
|
220
|
+
private get url(): string {
|
|
221
|
+
return settle(this.urlSource)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The anon headers every auth request starts from, read now. */
|
|
225
|
+
private get baseHeaders(): Record<string, string> {
|
|
226
|
+
return settle(this.baseHeadersSource)
|
|
227
|
+
}
|
|
228
|
+
|
|
192
229
|
/** The session the client starts with: the one passed in, else the persisted one. */
|
|
193
230
|
private loadInitialSession(opts: AuthClientOptions): Promise<void> {
|
|
194
231
|
if (opts.initialSession !== undefined) {
|
|
@@ -242,12 +279,12 @@ export class AuthClient {
|
|
|
242
279
|
const provider = await this.takeFlowItem(PROVIDER_SUFFIX)
|
|
243
280
|
this.currentSession = null
|
|
244
281
|
const { error } = await this.getSessionFromUrl(href).catch((err: unknown) => ({
|
|
245
|
-
error: { message: err instanceof Error ? err.message : "Could not finish the sign-in" },
|
|
282
|
+
error: new SupatypeError({ message: err instanceof Error ? err.message : "Could not finish the sign-in" }, { cause: err }),
|
|
246
283
|
}))
|
|
247
284
|
cleanBrowserUrl(href)
|
|
248
285
|
if (error === null) return { error: null }
|
|
249
286
|
this._setSession(null)
|
|
250
|
-
return { error: { ...error, ...(provider !== null && { provider }) } }
|
|
287
|
+
return { error: new AuthRedirectError({ ...error.toJSON(), ...(provider !== null && { provider }) }, { cause: error }) }
|
|
251
288
|
}
|
|
252
289
|
|
|
253
290
|
/** A URL with tokens or a refusal, or with a `code` for a PKCE flow this client started. */
|
|
@@ -474,7 +511,7 @@ export class AuthClient {
|
|
|
474
511
|
// navigating there shows as raw text; asking first lets the caller show a message instead.
|
|
475
512
|
if (await this.providerDisabled(opts.provider)) {
|
|
476
513
|
const message = `Sign-in with ${opts.provider} is not enabled for this project.`
|
|
477
|
-
return { data: { url: "", provider: opts.provider }, error: { message, status: 400, code: "provider_disabled" } }
|
|
514
|
+
return { data: { url: "", provider: opts.provider }, error: new SupatypeError({ message, status: 400, code: "provider_disabled" }) }
|
|
478
515
|
}
|
|
479
516
|
await this.writeFlowItem(PROVIDER_SUFFIX, opts.provider)
|
|
480
517
|
const url = this.endpoint("/authorize", opts.options?.redirectTo)
|
|
@@ -487,13 +524,54 @@ export class AuthClient {
|
|
|
487
524
|
}
|
|
488
525
|
const flowType = opts.options?.flowType ?? "implicit"
|
|
489
526
|
if (flowType === "pkce") {
|
|
490
|
-
await this.beginPkce()
|
|
491
|
-
url.searchParams.set("code_challenge", createCodeChallengeS256(this.codeVerifier!))
|
|
492
|
-
url.searchParams.set("code_challenge_method", PKCE_METHOD_S256)
|
|
527
|
+
for (const [key, value] of Object.entries(await this.beginPkce())) url.searchParams.set(key, value)
|
|
493
528
|
}
|
|
494
529
|
return { data: { url: url.toString(), provider: opts.provider }, error: null }
|
|
495
530
|
}
|
|
496
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Sign in with SAML single sign-on, through the identity provider registered for the user's email
|
|
534
|
+
* domain, or one named by its ID.
|
|
535
|
+
*
|
|
536
|
+
* Asks `POST /sso` for the identity provider's URL and sends the browser there, unless
|
|
537
|
+
* `skipBrowserRedirect` is set or there is no browser. The identity provider returns the user to
|
|
538
|
+
* the auth server, which sends them on to `redirectTo`; the client finishes the sign-in on start,
|
|
539
|
+
* as it does for OAuth. A refusal (no provider for the domain, a disabled one) is returned and the
|
|
540
|
+
* page stays put.
|
|
541
|
+
*
|
|
542
|
+
* PKCE by default (`flowType`), unlike `signInWithOAuth`: the sign-in comes back as a `?code=`
|
|
543
|
+
* this browser exchanges with its stored verifier.
|
|
544
|
+
*/
|
|
545
|
+
async signInWithSSO(params: SignInWithSSOCredentials): Promise<SignInWithSSOResult> {
|
|
546
|
+
const res = await this.send(`${this.url}/sso`, {
|
|
547
|
+
method: "POST",
|
|
548
|
+
headers: this.baseHeaders,
|
|
549
|
+
body: JSON.stringify(await this.ssoRequestBody(params)),
|
|
550
|
+
})
|
|
551
|
+
if (!res.ok) return { data: { url: "" }, error: await this._parseError(res) }
|
|
552
|
+
const url = (await readJsonObject(res))["url"]
|
|
553
|
+
if (typeof url !== "string" || url === "") {
|
|
554
|
+
const message = "The auth server answered /sso without a sign-in URL"
|
|
555
|
+
return { data: { url: "" }, error: new SupatypeError({ message, status: res.status }) }
|
|
556
|
+
}
|
|
557
|
+
await this.writeFlowItem(PROVIDER_SUFFIX, SSO_PROVIDER)
|
|
558
|
+
if (params.options?.skipBrowserRedirect !== true && typeof window !== "undefined") window.location.assign(url)
|
|
559
|
+
return { data: { url }, error: null }
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** The `/sso` body: the domain or the provider's ID, the caller's options, and any PKCE challenge. */
|
|
563
|
+
private async ssoRequestBody(params: SignInWithSSOCredentials): Promise<Record<string, unknown>> {
|
|
564
|
+
const opts: SignInWithSSOOptions = params.options ?? {}
|
|
565
|
+
const body: Record<string, unknown> = params.domain !== undefined ? { domain: params.domain } : { provider_id: params.providerId }
|
|
566
|
+
// A fetch cannot follow the server's 303 to another origin, so ask for the URL as JSON.
|
|
567
|
+
body["skip_http_redirect"] = true
|
|
568
|
+
if (opts.redirectTo !== undefined) body["redirect_to"] = opts.redirectTo
|
|
569
|
+
if (opts.captchaToken !== undefined) body["supatype_meta_security"] = { captcha_token: opts.captchaToken }
|
|
570
|
+
if (opts.studio === true) body["studio"] = true
|
|
571
|
+
const flowType = opts.flowType ?? "pkce"
|
|
572
|
+
return flowType === "pkce" ? { ...body, ...(await this.beginPkce()) } : body
|
|
573
|
+
}
|
|
574
|
+
|
|
497
575
|
/**
|
|
498
576
|
* True only when the server's `/settings` list the provider as off. A provider they do not list
|
|
499
577
|
* (a `custom:` one) or settings that cannot be read leave the decision to `/authorize`. Looked up
|
|
@@ -552,8 +630,8 @@ export class AuthClient {
|
|
|
552
630
|
|
|
553
631
|
/**
|
|
554
632
|
* Exchange a PKCE auth code for a session (`POST /token?grant_type=pkce`).
|
|
555
|
-
* Requires a prior
|
|
556
|
-
* so the matching `code_verifier` is available in memory / storage.
|
|
633
|
+
* Requires a prior PKCE sign-in in this browser (`signInWithSSO`, or `signInWithOAuth` or OTP with
|
|
634
|
+
* `flowType: "pkce"`), so the matching `code_verifier` is available in memory / storage.
|
|
557
635
|
*/
|
|
558
636
|
async exchangeCodeForSession(authCode: string): Promise<{
|
|
559
637
|
data: { session: Session | null; user: User | null }
|
|
@@ -563,10 +641,10 @@ export class AuthClient {
|
|
|
563
641
|
if (verifier === null) {
|
|
564
642
|
return {
|
|
565
643
|
data: { session: null, user: null },
|
|
566
|
-
error: {
|
|
567
|
-
message: "Missing PKCE code_verifier. Start OAuth with flowType: \"pkce\" first.",
|
|
644
|
+
error: new SupatypeError({
|
|
645
|
+
message: "Missing PKCE code_verifier. Start the sign-in (SSO, or OAuth with flowType: \"pkce\") in this browser first.",
|
|
568
646
|
status: 400,
|
|
569
|
-
},
|
|
647
|
+
}),
|
|
570
648
|
}
|
|
571
649
|
}
|
|
572
650
|
const res = await this.send(`${this.url}/token?grant_type=pkce`, {
|
|
@@ -593,7 +671,7 @@ export class AuthClient {
|
|
|
593
671
|
if (params === null) {
|
|
594
672
|
return {
|
|
595
673
|
data: { session: null, user: null },
|
|
596
|
-
error: { message: `Invalid redirect URL: ${url}`, status: 400 },
|
|
674
|
+
error: new SupatypeError({ message: `Invalid redirect URL: ${url}`, status: 400 }),
|
|
597
675
|
}
|
|
598
676
|
}
|
|
599
677
|
const { query, fragment: hashParams } = params
|
|
@@ -633,10 +711,10 @@ export class AuthClient {
|
|
|
633
711
|
|
|
634
712
|
return {
|
|
635
713
|
data: { session: null, user: null },
|
|
636
|
-
error: {
|
|
714
|
+
error: new SupatypeError({
|
|
637
715
|
message: "No auth code or tokens found in redirect URL",
|
|
638
716
|
status: 400,
|
|
639
|
-
},
|
|
717
|
+
}),
|
|
640
718
|
}
|
|
641
719
|
}
|
|
642
720
|
|
|
@@ -741,11 +819,11 @@ export class AuthClient {
|
|
|
741
819
|
error: SupatypeError | null
|
|
742
820
|
}> {
|
|
743
821
|
if (this.currentSession === null) {
|
|
744
|
-
return { data: { session: null }, error: { message: "No active session" } }
|
|
822
|
+
return { data: { session: null }, error: new SupatypeError({ message: "No active session" }) }
|
|
745
823
|
}
|
|
746
824
|
if (!this.currentSession.refreshToken?.trim()) {
|
|
747
825
|
this._setSession(null)
|
|
748
|
-
return { data: { session: null }, error: { message: "No refresh token" } }
|
|
826
|
+
return { data: { session: null }, error: new SupatypeError({ message: "No refresh token" }) }
|
|
749
827
|
}
|
|
750
828
|
try {
|
|
751
829
|
const res = await this.send(`${this.url}/token?grant_type=refresh_token`, {
|
|
@@ -760,9 +838,11 @@ export class AuthClient {
|
|
|
760
838
|
}
|
|
761
839
|
return await this._parseAuthResponse(res)
|
|
762
840
|
} catch (err) {
|
|
841
|
+
// Not a refused refresh: nothing was sent, and the session may be perfectly good.
|
|
842
|
+
rethrowSetupFault(err)
|
|
763
843
|
const message = err instanceof Error ? err.message : "Refresh failed"
|
|
764
844
|
this._setSession(null)
|
|
765
|
-
return { data: { session: null }, error: { message } }
|
|
845
|
+
return { data: { session: null }, error: new SupatypeError({ message }, { cause: err }) }
|
|
766
846
|
}
|
|
767
847
|
}
|
|
768
848
|
|
|
@@ -785,7 +865,7 @@ export class AuthClient {
|
|
|
785
865
|
data?: Record<string, unknown> | undefined
|
|
786
866
|
}): Promise<{ data: { user: User | null }; error: SupatypeError | null }> {
|
|
787
867
|
if (this.currentSession === null) {
|
|
788
|
-
return { data: { user: null }, error: { message: "Not authenticated" } }
|
|
868
|
+
return { data: { user: null }, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
789
869
|
}
|
|
790
870
|
const res = await this.send(`${this.url}/user`, {
|
|
791
871
|
method: "PUT",
|
|
@@ -926,7 +1006,7 @@ export class AuthClient {
|
|
|
926
1006
|
error: SupatypeError | null
|
|
927
1007
|
}> => {
|
|
928
1008
|
if (this.currentSession === null) {
|
|
929
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1009
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
930
1010
|
}
|
|
931
1011
|
const body: Record<string, unknown> = {
|
|
932
1012
|
factor_type: params.factorType,
|
|
@@ -975,7 +1055,7 @@ export class AuthClient {
|
|
|
975
1055
|
error: SupatypeError | null
|
|
976
1056
|
}> => {
|
|
977
1057
|
if (this.currentSession === null) {
|
|
978
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1058
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
979
1059
|
}
|
|
980
1060
|
const body: Record<string, unknown> = {}
|
|
981
1061
|
if (params.channel !== undefined) body["channel"] = params.channel
|
|
@@ -1019,7 +1099,7 @@ export class AuthClient {
|
|
|
1019
1099
|
if (this.currentSession === null) {
|
|
1020
1100
|
return {
|
|
1021
1101
|
data: { session: null, user: null },
|
|
1022
|
-
error: { message: "Not authenticated" },
|
|
1102
|
+
error: new SupatypeError({ message: "Not authenticated" }),
|
|
1023
1103
|
}
|
|
1024
1104
|
}
|
|
1025
1105
|
const res = await this.send(
|
|
@@ -1079,7 +1159,7 @@ export class AuthClient {
|
|
|
1079
1159
|
error: SupatypeError | null
|
|
1080
1160
|
}> => {
|
|
1081
1161
|
if (this.currentSession === null) {
|
|
1082
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1162
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
1083
1163
|
}
|
|
1084
1164
|
const res = await this.send(
|
|
1085
1165
|
`${this.url}/factors/${params.factorId}`,
|
|
@@ -1107,7 +1187,7 @@ export class AuthClient {
|
|
|
1107
1187
|
}> => {
|
|
1108
1188
|
const userResult = await this.getUser()
|
|
1109
1189
|
if (userResult.error !== null || userResult.data.user === null) {
|
|
1110
|
-
return { data: null, error: userResult.error ?? { message: "Could not get user" } }
|
|
1190
|
+
return { data: null, error: userResult.error ?? new SupatypeError({ message: "Could not get user" }) }
|
|
1111
1191
|
}
|
|
1112
1192
|
const allFactors: Factor[] = userResult.data.user.factors ?? []
|
|
1113
1193
|
const verifiedFactors = allFactors.filter((f) => f.status === "verified")
|
|
@@ -1134,16 +1214,16 @@ export class AuthClient {
|
|
|
1134
1214
|
error: SupatypeError | null
|
|
1135
1215
|
}> => {
|
|
1136
1216
|
if (this.currentSession === null) {
|
|
1137
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1217
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
1138
1218
|
}
|
|
1139
1219
|
// Decode the JWT payload to read the aal and amr claims
|
|
1140
1220
|
const parts = this.currentSession.accessToken.split(".")
|
|
1141
1221
|
if (parts.length !== 3) {
|
|
1142
|
-
return { data: null, error: { message: "Invalid access token format" } }
|
|
1222
|
+
return { data: null, error: new SupatypeError({ message: "Invalid access token format" }) }
|
|
1143
1223
|
}
|
|
1144
1224
|
const payload = decodeJwtPayload(parts[1]!)
|
|
1145
1225
|
if (payload === null) {
|
|
1146
|
-
return { data: null, error: { message: "Failed to decode access token" } }
|
|
1226
|
+
return { data: null, error: new SupatypeError({ message: "Failed to decode access token" }) }
|
|
1147
1227
|
}
|
|
1148
1228
|
const { currentLevel, methods } = assuranceClaims(payload)
|
|
1149
1229
|
try {
|
|
@@ -1154,7 +1234,7 @@ export class AuthClient {
|
|
|
1154
1234
|
const nextLevel = hasVerifiedFactors ? "aal2" : "aal1"
|
|
1155
1235
|
return { data: { currentLevel, nextLevel, currentAuthenticationMethods: methods }, error: null }
|
|
1156
1236
|
} catch {
|
|
1157
|
-
return { data: null, error: { message: "Failed to decode access token" } }
|
|
1237
|
+
return { data: null, error: new SupatypeError({ message: "Failed to decode access token" }) }
|
|
1158
1238
|
}
|
|
1159
1239
|
},
|
|
1160
1240
|
}
|
|
@@ -1234,10 +1314,15 @@ export class AuthClient {
|
|
|
1234
1314
|
return value
|
|
1235
1315
|
}
|
|
1236
1316
|
|
|
1237
|
-
|
|
1317
|
+
/**
|
|
1318
|
+
* Start a PKCE flow: a fresh verifier, kept for the redirect, and the challenge parameters the
|
|
1319
|
+
* auth server takes with it (as `/authorize` query parameters or `/sso` body fields).
|
|
1320
|
+
*/
|
|
1321
|
+
private async beginPkce(): Promise<{ code_challenge: string; code_challenge_method: string }> {
|
|
1238
1322
|
const verifier = generateCodeVerifier()
|
|
1239
1323
|
this.codeVerifier = verifier
|
|
1240
1324
|
await this.writeFlowItem(VERIFIER_SUFFIX, verifier)
|
|
1325
|
+
return { code_challenge: createCodeChallengeS256(verifier), code_challenge_method: PKCE_METHOD_S256 }
|
|
1241
1326
|
}
|
|
1242
1327
|
|
|
1243
1328
|
private async readCodeVerifier(): Promise<string | null> {
|
package/src/bindings.ts
CHANGED
|
@@ -17,6 +17,9 @@ export type {
|
|
|
17
17
|
export { startOAuthRedirect } from "./auth-redirect.js"
|
|
18
18
|
export { isClientFor, isRowsOf, isCallerValue } from "./caller-types.js"
|
|
19
19
|
export { runMutation } from "./mutation-request.js"
|
|
20
|
+
export { reportThrown } from "./thrown-error.js"
|
|
21
|
+
export { latestRowsReader, withPage } from "./latest-rows.js"
|
|
22
|
+
export type { RowsReadOptions, RowsView } from "./latest-rows.js"
|
|
20
23
|
export type { MutationOperation, MutationOutcome, MutationRequest, MutationSelect } from "./mutation-request.js"
|
|
21
24
|
export { livePreviewMessage, previewMessageOf, originOf } from "./live-preview.js"
|
|
22
25
|
export type { LivePreviewMessage } from "./live-preview.js"
|
package/src/error-codes-doc.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface ErrorCodeEntry {
|
|
|
20
20
|
/** HTTP status code typically associated with this error. */
|
|
21
21
|
httpStatus?: number | undefined
|
|
22
22
|
/** Category grouping. */
|
|
23
|
-
category: "auth" | "postgrest" | "storage" | "realtime" | "network"
|
|
23
|
+
category: "auth" | "postgrest" | "storage" | "realtime" | "network" | "setup"
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
@@ -305,6 +305,14 @@ export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
|
305
305
|
httpStatus: 429,
|
|
306
306
|
category: "network",
|
|
307
307
|
},
|
|
308
|
+
// ── Setup errors ─────────────────────────────────────────────────────────
|
|
309
|
+
{
|
|
310
|
+
code: "MISSING_CLIENT_VALUES",
|
|
311
|
+
description: "A request was made by a client with no API address or no anon key, so nothing was sent.",
|
|
312
|
+
suggestion:
|
|
313
|
+
"Pass url and anonKey to createClient, set them at build time, or load /supatype-env.js before the app. The error names each value and every name it can be set under.",
|
|
314
|
+
category: "setup",
|
|
315
|
+
},
|
|
308
316
|
] as const
|
|
309
317
|
|
|
310
318
|
/**
|