@supatype/client 0.3.6 → 0.7.0
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 +58 -31
- package/.turbo/turbo-typecheck.log +2 -2
- package/LICENSE +1 -1
- package/dist/auth-redirect.d.ts +48 -0
- package/dist/auth-redirect.d.ts.map +1 -0
- package/dist/auth-redirect.js +70 -0
- package/dist/auth-redirect.js.map +1 -0
- package/dist/auth.d.ts +78 -4
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +373 -274
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +16 -0
- package/dist/bindings.d.ts.map +1 -0
- package/dist/bindings.js +13 -0
- package/dist/bindings.js.map +1 -0
- package/dist/caller-types.d.ts +18 -0
- package/dist/caller-types.d.ts.map +1 -0
- package/dist/caller-types.js +22 -0
- package/dist/caller-types.js.map +1 -0
- package/dist/draft-create.d.ts +12 -0
- package/dist/draft-create.d.ts.map +1 -0
- package/dist/draft-create.js +47 -0
- package/dist/draft-create.js.map +1 -0
- package/dist/draft-models-registry.d.ts +27 -0
- package/dist/draft-models-registry.d.ts.map +1 -0
- package/dist/draft-models-registry.js +61 -0
- package/dist/draft-models-registry.js.map +1 -0
- 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 +42 -34
- package/dist/error-codes-doc.js.map +1 -1
- package/dist/errors.d.ts +102 -60
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +69 -28
- package/dist/errors.js.map +1 -1
- package/dist/exact-response.d.ts.map +1 -1
- package/dist/exact-response.js +2 -1
- package/dist/exact-response.js.map +1 -1
- package/dist/exact-values.d.ts +2 -0
- package/dist/exact-values.d.ts.map +1 -1
- package/dist/exact-values.js +12 -10
- package/dist/exact-values.js.map +1 -1
- package/dist/fetch-with-retry.d.ts +21 -6
- package/dist/fetch-with-retry.d.ts.map +1 -1
- package/dist/fetch-with-retry.js +67 -53
- package/dist/fetch-with-retry.js.map +1 -1
- package/dist/field-kinds-registry.d.ts +11 -3
- package/dist/field-kinds-registry.d.ts.map +1 -1
- package/dist/field-kinds-registry.js +10 -6
- package/dist/field-kinds-registry.js.map +1 -1
- package/dist/filters.d.ts +51 -0
- package/dist/filters.d.ts.map +1 -0
- package/dist/filters.js +117 -0
- package/dist/filters.js.map +1 -0
- package/dist/generated-client.d.ts +7 -0
- package/dist/generated-client.d.ts.map +1 -0
- package/dist/generated-client.js +7 -0
- package/dist/generated-client.js.map +1 -0
- package/dist/graphql-document.d.ts +39 -0
- package/dist/graphql-document.d.ts.map +1 -0
- package/dist/graphql-document.js +228 -0
- package/dist/graphql-document.js.map +1 -0
- package/dist/graphql-json.d.ts +56 -0
- package/dist/graphql-json.d.ts.map +1 -0
- package/dist/graphql-json.js +220 -0
- package/dist/graphql-json.js.map +1 -0
- package/dist/graphql-transport.d.ts +28 -0
- package/dist/graphql-transport.d.ts.map +1 -0
- package/dist/graphql-transport.js +77 -0
- package/dist/graphql-transport.js.map +1 -0
- package/dist/index.d.ts +103 -40
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +153 -157
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +61 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +83 -0
- package/dist/json.js.map +1 -0
- package/dist/jwt.d.ts +16 -0
- package/dist/jwt.d.ts.map +1 -0
- package/dist/jwt.js +37 -0
- package/dist/jwt.js.map +1 -0
- package/dist/live-preview.d.ts +23 -0
- package/dist/live-preview.d.ts.map +1 -0
- package/dist/live-preview.js +28 -0
- package/dist/live-preview.js.map +1 -0
- package/dist/mutation-request.d.ts +17 -0
- package/dist/mutation-request.d.ts.map +1 -0
- package/dist/mutation-request.js +41 -0
- package/dist/mutation-request.js.map +1 -0
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +8 -6
- package/dist/preview.js.map +1 -1
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js +19 -13
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +108 -29
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +294 -224
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +20 -2
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +105 -12
- package/dist/realtime.js.map +1 -1
- package/dist/select-types.d.ts +99 -0
- package/dist/select-types.d.ts.map +1 -0
- package/dist/select-types.js +2 -0
- package/dist/select-types.js.map +1 -0
- package/dist/storage.d.ts +81 -11
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +129 -37
- package/dist/storage.js.map +1 -1
- package/dist/table-subscription.d.ts +69 -0
- package/dist/table-subscription.d.ts.map +1 -0
- package/dist/table-subscription.js +93 -0
- package/dist/table-subscription.js.map +1 -0
- package/dist/types.d.ts +84 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -1
- package/dist/types.js.map +1 -1
- package/package.json +6 -2
- package/src/auth-redirect.ts +90 -0
- package/src/auth.ts +421 -280
- package/src/bindings.ts +22 -0
- package/src/caller-types.ts +27 -0
- package/src/draft-create.ts +51 -0
- package/src/draft-models-registry.ts +81 -0
- package/src/error-codes-doc.ts +44 -35
- package/src/errors.ts +72 -28
- package/src/exact-response.ts +2 -1
- package/src/exact-values.ts +16 -9
- package/src/fetch-with-retry.ts +80 -70
- package/src/field-kinds-registry.ts +22 -9
- package/src/filters.ts +133 -0
- package/src/generated-client.ts +6 -0
- package/src/graphql-document.ts +234 -0
- package/src/graphql-json.ts +243 -0
- package/src/graphql-transport.ts +97 -0
- package/src/index.ts +272 -201
- package/src/json.ts +117 -0
- package/src/jwt.ts +37 -0
- package/src/live-preview.ts +37 -0
- package/src/mutation-request.ts +66 -0
- package/src/preview.ts +9 -11
- package/src/query-cache.ts +19 -16
- package/src/query.ts +389 -270
- package/src/realtime.ts +116 -13
- package/src/select-types.ts +184 -0
- package/src/storage.ts +214 -43
- package/src/table-subscription.ts +160 -0
- package/src/types.ts +90 -5
- package/tests/auth-error-codes.test.ts +53 -0
- package/tests/auth-errors.test.ts +84 -0
- package/tests/auth-events.test.ts +136 -0
- package/tests/auth-url-session.test.ts +204 -0
- package/tests/auth.test.ts +122 -26
- package/tests/count.test.ts +60 -0
- package/tests/data-plane-auth.test.ts +21 -46
- package/tests/draft-insert.integration.test.ts +118 -0
- package/tests/draft-insert.test.ts +225 -0
- package/tests/draft-profile.test.ts +55 -84
- package/tests/draft-update.integration.test.ts +122 -0
- package/tests/draft-update.test.ts +174 -0
- package/tests/dynamic-headers.test.ts +45 -0
- package/tests/entry-points.test.ts +42 -0
- package/tests/exact-response.test.ts +4 -4
- package/tests/exact-values.test.ts +14 -25
- package/tests/field-kinds-registry.test.ts +3 -3
- package/tests/filters.test.ts +146 -0
- package/tests/graphql-json.test.ts +199 -0
- package/tests/helpers/mock-websocket.ts +44 -11
- package/tests/helpers/oauth-binding.ts +92 -0
- package/tests/helpers/signup-binding.ts +73 -0
- package/tests/helpers/socket-swap.ts +139 -0
- package/tests/mfa-totp-integration.test.ts +13 -20
- package/tests/postgrest-error-codes.test.ts +85 -0
- package/tests/query-cache.test.ts +116 -117
- package/tests/query.test.ts +29 -31
- package/tests/rate-limit-integration.test.ts +4 -12
- package/tests/realtime-exact-values.test.ts +2 -4
- package/tests/realtime-presence.test.ts +75 -0
- package/tests/realtime-session-auth.test.ts +2 -4
- package/tests/realtime-swap-status.test.ts +62 -0
- package/tests/retry-integration.test.ts +3 -8
- package/tests/retry-policy.test.ts +166 -0
- package/tests/rpc-resend.test.ts +24 -0
- package/tests/server-bodies.test.ts +139 -0
- package/tests/serverless.test.ts +2 -2
- package/tests/signed-url-absolute.test.ts +2 -5
- package/tests/storage-object-shape.test.ts +101 -0
- package/tests/storage-quota-integration.test.ts +3 -2
- package/tests/storage-reference-url.test.ts +51 -0
- package/tests/storage-upload-browser.test.ts +71 -0
- package/tests/storage-upload-limit-integration.test.ts +12 -22
- package/tests/subscribe-frame.test.ts +5 -11
- package/tests/support/fetch-mock.ts +64 -0
- package/tests/support/storage-object.ts +37 -0
- package/tests/table-subscribe.test.ts +13 -13
- package/tests/table-subscription.test.ts +105 -0
- package/tests/table-transport.test.ts +94 -0
- package/tests/tsconfig.json +1 -1
- package/tests/types.test.ts +153 -1
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
package/src/bindings.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@supatype/client/bindings`: what the React, Vue, Svelte and Solid bindings share.
|
|
3
|
+
*
|
|
4
|
+
* Kept off the main entry on purpose. These are the bindings' building blocks, not API an app is
|
|
5
|
+
* meant to call, and everything on the main entry is a promise to every app. An app subscribes
|
|
6
|
+
* through `from(table).subscribe()` or its framework's binding instead.
|
|
7
|
+
*/
|
|
8
|
+
export { watchTable, subscribeToTable, subscriptionViewOf, applyRowChange } from "./table-subscription.js"
|
|
9
|
+
export type {
|
|
10
|
+
SubscriptionState,
|
|
11
|
+
SubscriptionView,
|
|
12
|
+
TableWatchOptions,
|
|
13
|
+
TableWatchHandlers,
|
|
14
|
+
TableSubscribeOptions,
|
|
15
|
+
TableSubscription,
|
|
16
|
+
} from "./table-subscription.js"
|
|
17
|
+
export { startOAuthRedirect } from "./auth-redirect.js"
|
|
18
|
+
export { isClientFor, isRowsOf, isCallerValue } from "./caller-types.js"
|
|
19
|
+
export { runMutation } from "./mutation-request.js"
|
|
20
|
+
export type { MutationOperation, MutationOutcome } from "./mutation-request.js"
|
|
21
|
+
export { livePreviewMessage, previewMessageOf, originOf } from "./live-preview.js"
|
|
22
|
+
export type { LivePreviewMessage } from "./live-preview.js"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AnyClient, SupatypeClient } from "./index.js"
|
|
2
|
+
import type { AnyDatabase } from "./types.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where a framework binding takes its caller at their word.
|
|
6
|
+
*
|
|
7
|
+
* The database, row and response types a caller names (`useSupatype<Db>()`, `useQuery<Db, "posts",
|
|
8
|
+
* Post>`, `createFunction<Order>`) describe what the server sends, and nothing in the browser can
|
|
9
|
+
* check them against it. These are the only places the bindings turn a value into such a type, each
|
|
10
|
+
* checking what can be checked, so a cast is never scattered through the hooks, composables, stores
|
|
11
|
+
* and primitives.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** The client in context, as the caller's database. Boundary: the database type is the caller's. */
|
|
15
|
+
export function isClientFor<TDatabase extends AnyDatabase>(_client: AnyClient): _client is SupatypeClient<TDatabase> {
|
|
16
|
+
return true
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Rows read through the untyped client, as the caller's row type: checked to be a list of objects. */
|
|
20
|
+
export function isRowsOf<TRow>(rows: unknown): rows is TRow[] {
|
|
21
|
+
return Array.isArray(rows) && rows.every((row) => typeof row === "object" && row !== null && !Array.isArray(row))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A function's answer or a preview's data, as the caller's type. Boundary: any JSON value can be one. */
|
|
25
|
+
export function isCallerValue<T>(_value: unknown): _value is T {
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure half of creating records on a drafts model: which function to call, what it answered,
|
|
3
|
+
* and the rows read back in the order they were sent.
|
|
4
|
+
*
|
|
5
|
+
* One record goes through `supatype.create_record`, several through `supatype.create_records`,
|
|
6
|
+
* which runs `create_record` for each inside one call, so a refused record takes the ones before it
|
|
7
|
+
* with it. One call per row would not be all or nothing, and PostgREST cannot batch an RPC.
|
|
8
|
+
*/
|
|
9
|
+
import { isJsonObject } from "./json.js"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The column the draft read-back carries a new record's key under, to put the rows in the order
|
|
13
|
+
* they were sent, whatever the insert's select names. Removed before the caller sees a row.
|
|
14
|
+
*/
|
|
15
|
+
const ORDER_KEY = "__supatype_created_key"
|
|
16
|
+
|
|
17
|
+
/** The function a write of `body` creates its records through, and the `data` it sends. */
|
|
18
|
+
export function createCall(body: unknown): { fn: "create_record" | "create_records"; data: unknown } {
|
|
19
|
+
if (Array.isArray(body) && body.length > 1) return { fn: "create_records", data: body }
|
|
20
|
+
return { fn: "create_record", data: Array.isArray(body) ? body[0] : body }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The new keys an answer names: `create_record`'s one object, or `create_records`' array of them. */
|
|
24
|
+
export function createdKeys(answer: unknown): string[] | null {
|
|
25
|
+
const answers = Array.isArray(answer) ? answer : [answer]
|
|
26
|
+
const keys = answers.map((one) => (isJsonObject(one) ? one["record_id"] : undefined))
|
|
27
|
+
return keys.every((key): key is string => typeof key === "string") ? keys : null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The select the read-back sends: the insert's own, plus the key to order several rows by. */
|
|
31
|
+
export function readBackColumns(select: string | null, primaryKey: string, count: number): string | undefined {
|
|
32
|
+
if (count < 2) return select ?? undefined
|
|
33
|
+
return `${select ?? "*"},${ORDER_KEY}:${primaryKey}`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The rows read back, in the order their records were sent, without the ordering key. */
|
|
37
|
+
export function inCreatedOrder(
|
|
38
|
+
rows: readonly Record<string, unknown>[],
|
|
39
|
+
keys: readonly string[],
|
|
40
|
+
): Record<string, unknown>[] {
|
|
41
|
+
if (keys.length < 2) return [...rows]
|
|
42
|
+
const byKey = new Map<string, Record<string, unknown>>()
|
|
43
|
+
for (const row of rows) {
|
|
44
|
+
const { [ORDER_KEY]: key, ...rest } = row
|
|
45
|
+
byKey.set(String(key), rest)
|
|
46
|
+
}
|
|
47
|
+
return keys.flatMap((key) => {
|
|
48
|
+
const row = byKey.get(key)
|
|
49
|
+
return row === undefined ? [] : [row]
|
|
50
|
+
})
|
|
51
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which tables belong to models that keep drafts, told to the client by generated code.
|
|
3
|
+
*
|
|
4
|
+
* `supatype push` writes `supatype/generated/client.ts`, which calls {@link registerDraftModels} at import
|
|
5
|
+
* time, as it calls `registerFieldKinds`. The client reads it to send `insert` on such a table
|
|
6
|
+
* through `supatype.create_record`, or `supatype.create_records` for several rows, rather than a
|
|
7
|
+
* plain `POST`.
|
|
8
|
+
*
|
|
9
|
+
* # Why an insert needs to know
|
|
10
|
+
*
|
|
11
|
+
* A new record on a drafts model is unpublished. When the model's read rule hides unpublished rows
|
|
12
|
+
* (`published_at <= now()`), Postgres refuses a plain insert that asks for the row back: it checks
|
|
13
|
+
* the returned row against the read rule as the row is inserted, before anything has made it visible
|
|
14
|
+
* to its author. `create_record` inserts without reading back and writes version 1, after which the
|
|
15
|
+
* record is visible to whoever created it. Studio has always created records that way; this lets an
|
|
16
|
+
* app's `insert` do the same without knowing it has to.
|
|
17
|
+
*
|
|
18
|
+
* Nothing registered means nothing is routed: a client built from `@supatype/client` directly, with
|
|
19
|
+
* no generated file, inserts as it always has.
|
|
20
|
+
*/
|
|
21
|
+
import { isJsonObject } from "./json.js"
|
|
22
|
+
|
|
23
|
+
/** What the client needs of one drafts model to create a record and read it back. */
|
|
24
|
+
export type DraftModel = {
|
|
25
|
+
/** The column the table is keyed by, which the new record is read back through. */
|
|
26
|
+
readonly primaryKey: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The shape the generated file passes. Versioned so an older client declines a newer one. */
|
|
30
|
+
export type DraftModelsDocument = {
|
|
31
|
+
readonly version: 1
|
|
32
|
+
readonly tables: Readonly<Record<string, DraftModel>>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A document from a generator this client predates. Accepted so generated code compiles, then ignored. */
|
|
36
|
+
export type UnrecognisedDraftModelsDocument = {
|
|
37
|
+
readonly version: number
|
|
38
|
+
readonly tables: unknown
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const SUPPORTED_VERSION = 1
|
|
42
|
+
|
|
43
|
+
// Module level, as the field kinds are: the schema does not change between the clients a server
|
|
44
|
+
// builds per request.
|
|
45
|
+
let registered: ReadonlyMap<string, DraftModel> | null = null
|
|
46
|
+
|
|
47
|
+
/** One table's entry, or null when it is not a usable one. */
|
|
48
|
+
function draftModelOf(entry: unknown): DraftModel | null {
|
|
49
|
+
if (!isJsonObject(entry)) return null
|
|
50
|
+
const primaryKey = entry["primaryKey"]
|
|
51
|
+
return typeof primaryKey === "string" && primaryKey !== "" ? { primaryKey } : null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Record which tables keep drafts. Called by generated code, not by hand.
|
|
56
|
+
*
|
|
57
|
+
* Checked as JSON whatever the type says, since plain JavaScript calls this too, and a version this
|
|
58
|
+
* client does not know is ignored rather than read by the wrong rules.
|
|
59
|
+
*/
|
|
60
|
+
export function registerDraftModels(document: DraftModelsDocument | UnrecognisedDraftModelsDocument): void {
|
|
61
|
+
if (!isJsonObject(document) || document["version"] !== SUPPORTED_VERSION) return
|
|
62
|
+
const declared = document["tables"]
|
|
63
|
+
if (!isJsonObject(declared)) return
|
|
64
|
+
|
|
65
|
+
const tables = new Map<string, DraftModel>()
|
|
66
|
+
for (const [table, entry] of Object.entries(declared)) {
|
|
67
|
+
const model = draftModelOf(entry)
|
|
68
|
+
if (model !== null) tables.set(table, model)
|
|
69
|
+
}
|
|
70
|
+
registered = tables
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The drafts model behind `table`, or undefined when the table keeps no drafts. */
|
|
74
|
+
export function registeredDraftModel(table: string): DraftModel | undefined {
|
|
75
|
+
return registered?.get(table)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Forget what was registered. For tests, and for a process that switches projects. */
|
|
79
|
+
export function forgetRegisteredDraftModels(): void {
|
|
80
|
+
registered = null
|
|
81
|
+
}
|
package/src/error-codes-doc.ts
CHANGED
|
@@ -8,8 +8,10 @@
|
|
|
8
8
|
* on the documentation page.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import { AuthErrorCodes } from "./errors.js"
|
|
12
|
+
|
|
11
13
|
export interface ErrorCodeEntry {
|
|
12
|
-
/** Machine-readable error code (e.g. "
|
|
14
|
+
/** Machine-readable error code, as the error carries it (e.g. "invalid_credentials"). */
|
|
13
15
|
code: string
|
|
14
16
|
/** Human-readable short description. */
|
|
15
17
|
description: string
|
|
@@ -24,91 +26,91 @@ export interface ErrorCodeEntry {
|
|
|
24
26
|
export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
25
27
|
// ── Auth errors ──────────────────────────────────────────────────────────
|
|
26
28
|
{
|
|
27
|
-
code:
|
|
29
|
+
code: AuthErrorCodes.INVALID_CREDENTIALS,
|
|
28
30
|
description: "The email/password combination is incorrect.",
|
|
29
31
|
suggestion: "Check the email and password. If forgotten, use resetPasswordForEmail().",
|
|
30
32
|
httpStatus: 400,
|
|
31
33
|
category: "auth",
|
|
32
34
|
},
|
|
33
35
|
{
|
|
34
|
-
code:
|
|
36
|
+
code: AuthErrorCodes.USER_NOT_FOUND,
|
|
35
37
|
description: "No user exists with the provided identifier.",
|
|
36
38
|
suggestion: "Verify the email address. The user may need to sign up first.",
|
|
37
39
|
httpStatus: 404,
|
|
38
40
|
category: "auth",
|
|
39
41
|
},
|
|
40
42
|
{
|
|
41
|
-
code:
|
|
43
|
+
code: AuthErrorCodes.EMAIL_NOT_CONFIRMED,
|
|
42
44
|
description: "The user's email has not been confirmed yet.",
|
|
43
45
|
suggestion: "Check inbox for the confirmation email. Use auth.resend({ type: 'signup' }) to re-send.",
|
|
44
46
|
httpStatus: 403,
|
|
45
47
|
category: "auth",
|
|
46
48
|
},
|
|
47
49
|
{
|
|
48
|
-
code:
|
|
50
|
+
code: AuthErrorCodes.PHONE_NOT_CONFIRMED,
|
|
49
51
|
description: "The user's phone number has not been verified.",
|
|
50
52
|
suggestion: "Complete the phone OTP verification flow.",
|
|
51
53
|
httpStatus: 403,
|
|
52
54
|
category: "auth",
|
|
53
55
|
},
|
|
54
56
|
{
|
|
55
|
-
code:
|
|
57
|
+
code: AuthErrorCodes.SESSION_EXPIRED,
|
|
56
58
|
description: "The session has expired and the refresh token is no longer valid.",
|
|
57
59
|
suggestion: "Sign the user in again. Implement automatic token refresh with onAuthStateChange.",
|
|
58
60
|
httpStatus: 401,
|
|
59
61
|
category: "auth",
|
|
60
62
|
},
|
|
61
63
|
{
|
|
62
|
-
code:
|
|
64
|
+
code: AuthErrorCodes.INVALID_TOKEN,
|
|
63
65
|
description: "The provided JWT is malformed, expired, or signed with a different secret.",
|
|
64
66
|
suggestion: "Ensure the token is from the correct project. Check expiry. Re-authenticate.",
|
|
65
67
|
httpStatus: 401,
|
|
66
68
|
category: "auth",
|
|
67
69
|
},
|
|
68
70
|
{
|
|
69
|
-
code:
|
|
71
|
+
code: AuthErrorCodes.MFA_REQUIRED,
|
|
70
72
|
description: "Multi-factor authentication is required to complete this action.",
|
|
71
73
|
suggestion: "Call mfa.challenge() and mfa.verify() with the user's TOTP code.",
|
|
72
74
|
httpStatus: 403,
|
|
73
75
|
category: "auth",
|
|
74
76
|
},
|
|
75
77
|
{
|
|
76
|
-
code:
|
|
78
|
+
code: AuthErrorCodes.MFA_CHALLENGE_EXPIRED,
|
|
77
79
|
description: "The MFA challenge has expired.",
|
|
78
80
|
suggestion: "Create a new challenge with mfa.challenge() and try again.",
|
|
79
81
|
httpStatus: 400,
|
|
80
82
|
category: "auth",
|
|
81
83
|
},
|
|
82
84
|
{
|
|
83
|
-
code:
|
|
85
|
+
code: AuthErrorCodes.MFA_VERIFICATION_FAILED,
|
|
84
86
|
description: "The MFA verification code is incorrect.",
|
|
85
87
|
suggestion: "Ensure the TOTP code is current (codes rotate every 30 seconds).",
|
|
86
88
|
httpStatus: 400,
|
|
87
89
|
category: "auth",
|
|
88
90
|
},
|
|
89
91
|
{
|
|
90
|
-
code:
|
|
92
|
+
code: AuthErrorCodes.WEAK_PASSWORD,
|
|
91
93
|
description: "The password does not meet the minimum strength requirements.",
|
|
92
94
|
suggestion: "Use a password with at least 8 characters including mixed case and numbers.",
|
|
93
95
|
httpStatus: 422,
|
|
94
96
|
category: "auth",
|
|
95
97
|
},
|
|
96
98
|
{
|
|
97
|
-
code:
|
|
99
|
+
code: AuthErrorCodes.USER_ALREADY_EXISTS,
|
|
98
100
|
description: "A user with this email or phone already exists.",
|
|
99
101
|
suggestion: "Use signInWithPassword() instead, or try a different email.",
|
|
100
102
|
httpStatus: 409,
|
|
101
103
|
category: "auth",
|
|
102
104
|
},
|
|
103
105
|
{
|
|
104
|
-
code:
|
|
106
|
+
code: AuthErrorCodes.OAUTH_CALLBACK_ERROR,
|
|
105
107
|
description: "The OAuth provider returned an error during the callback.",
|
|
106
108
|
suggestion: "Check the provider configuration (client ID, secret, redirect URL).",
|
|
107
109
|
httpStatus: 400,
|
|
108
110
|
category: "auth",
|
|
109
111
|
},
|
|
110
112
|
{
|
|
111
|
-
code:
|
|
113
|
+
code: AuthErrorCodes.MAGIC_LINK_EXPIRED,
|
|
112
114
|
description: "The magic link has expired (default: 10 minutes).",
|
|
113
115
|
suggestion: "Request a new magic link with signInWithOtp({ email }).",
|
|
114
116
|
httpStatus: 400,
|
|
@@ -118,15 +120,22 @@ export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
|
118
120
|
// ── PostgREST errors ─────────────────────────────────────────────────────
|
|
119
121
|
{
|
|
120
122
|
code: "PGRST301",
|
|
121
|
-
description: "JWT
|
|
122
|
-
suggestion: "
|
|
123
|
+
description: "The JWT could not be decoded or verified: a bad signature, a malformed token, or an empty one.",
|
|
124
|
+
suggestion: "Check that the apikey and Authorization headers carry a token signed with this project's JWT secret.",
|
|
125
|
+
httpStatus: 401,
|
|
126
|
+
category: "postgrest",
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
code: "PGRST303",
|
|
130
|
+
description: "The JWT's claims were rejected. An expired token is the usual cause.",
|
|
131
|
+
suggestion: "Refresh the user's session or sign in again.",
|
|
123
132
|
httpStatus: 401,
|
|
124
133
|
category: "postgrest",
|
|
125
134
|
},
|
|
126
135
|
{
|
|
127
|
-
code: "
|
|
128
|
-
description: "Permission denied.
|
|
129
|
-
suggestion: "Review your access rules in the schema.
|
|
136
|
+
code: "42501",
|
|
137
|
+
description: "Permission denied. A row-level security policy or a missing grant blocked the operation.",
|
|
138
|
+
suggestion: "Review your access rules in the schema. A request without a signed-in user is answered with 401.",
|
|
130
139
|
httpStatus: 403,
|
|
131
140
|
category: "postgrest",
|
|
132
141
|
},
|
|
@@ -139,55 +148,55 @@ export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
|
139
148
|
},
|
|
140
149
|
{
|
|
141
150
|
code: "PGRST204",
|
|
142
|
-
description: "
|
|
143
|
-
suggestion: "
|
|
144
|
-
httpStatus:
|
|
151
|
+
description: "The request named a column that is not in PostgREST's schema cache.",
|
|
152
|
+
suggestion: "Check the column names you write against the current schema, and push the schema if it changed.",
|
|
153
|
+
httpStatus: 400,
|
|
154
|
+
category: "postgrest",
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
code: "PGRST205",
|
|
158
|
+
description: "Table does not exist. The schema may need to be pushed.",
|
|
159
|
+
suggestion: "Run `npx supatype push` to apply your schema to the database.",
|
|
160
|
+
httpStatus: 404,
|
|
145
161
|
category: "postgrest",
|
|
146
162
|
},
|
|
147
163
|
{
|
|
148
|
-
code: "
|
|
164
|
+
code: "23503",
|
|
149
165
|
description: "Foreign key constraint violation. The referenced record does not exist.",
|
|
150
166
|
suggestion: "Ensure the related record exists before creating/updating. Check relation fields.",
|
|
151
167
|
httpStatus: 409,
|
|
152
168
|
category: "postgrest",
|
|
153
169
|
},
|
|
154
170
|
{
|
|
155
|
-
code: "
|
|
171
|
+
code: "23505",
|
|
156
172
|
description: "Unique constraint violation. A record with this value already exists.",
|
|
157
173
|
suggestion: "Use .upsert() for insert-or-update, or check for existing records first.",
|
|
158
174
|
httpStatus: 409,
|
|
159
175
|
category: "postgrest",
|
|
160
176
|
},
|
|
161
177
|
{
|
|
162
|
-
code: "
|
|
178
|
+
code: "23502",
|
|
163
179
|
description: "Not-null constraint violation. A required field is missing.",
|
|
164
180
|
suggestion: "Include all required fields in the insert/update payload.",
|
|
165
181
|
httpStatus: 400,
|
|
166
182
|
category: "postgrest",
|
|
167
183
|
},
|
|
168
184
|
{
|
|
169
|
-
code: "
|
|
185
|
+
code: "23514",
|
|
170
186
|
description: "Check constraint violation. A field value does not satisfy the constraint.",
|
|
171
187
|
suggestion: "Ensure enum values match declared options. Check field validation rules.",
|
|
172
188
|
httpStatus: 400,
|
|
173
189
|
category: "postgrest",
|
|
174
190
|
},
|
|
175
191
|
{
|
|
176
|
-
code: "
|
|
192
|
+
code: "22P02",
|
|
177
193
|
description: "Invalid input syntax. The value cannot be cast to the expected type.",
|
|
178
194
|
suggestion: "Check that field values match the expected types (e.g., UUID format, number format).",
|
|
179
195
|
httpStatus: 400,
|
|
180
196
|
category: "postgrest",
|
|
181
197
|
},
|
|
182
198
|
{
|
|
183
|
-
code: "
|
|
184
|
-
description: "Table does not exist. The schema may need to be pushed.",
|
|
185
|
-
suggestion: "Run `npx supatype push` to apply your schema to the database.",
|
|
186
|
-
httpStatus: 404,
|
|
187
|
-
category: "postgrest",
|
|
188
|
-
},
|
|
189
|
-
{
|
|
190
|
-
code: "PGRST42703",
|
|
199
|
+
code: "42703",
|
|
191
200
|
description: "Column does not exist. The field may have been renamed or removed.",
|
|
192
201
|
suggestion: "Check your query column names against the current schema.",
|
|
193
202
|
httpStatus: 400,
|
package/src/errors.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { SupatypeError as SupatypeErrorShape } from "./types.js"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Typed error hierarchy for the Supatype SDK.
|
|
3
5
|
* Every error has a machine-readable `code` field and optional `helpUrl`.
|
|
@@ -120,20 +122,37 @@ export class RateLimitError extends SupatypeError {
|
|
|
120
122
|
|
|
121
123
|
// ── Common error codes ────────────────────────────────────────────
|
|
122
124
|
|
|
125
|
+
/**
|
|
126
|
+
* The `code` an auth error carries, as the auth server sends it in `error_code`.
|
|
127
|
+
*
|
|
128
|
+
* These were `AUTH_INVALID_CREDENTIALS` and the like, which the server never sends, so comparing
|
|
129
|
+
* an error's code with them never matched. The names are unchanged; the values are the server's.
|
|
130
|
+
*/
|
|
123
131
|
export const AuthErrorCodes = {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
/** Wrong email or password */
|
|
133
|
+
INVALID_CREDENTIALS: "invalid_credentials",
|
|
134
|
+
USER_NOT_FOUND: "user_not_found",
|
|
135
|
+
EMAIL_NOT_CONFIRMED: "email_not_confirmed",
|
|
136
|
+
PHONE_NOT_CONFIRMED: "phone_not_confirmed",
|
|
137
|
+
SESSION_EXPIRED: "session_expired",
|
|
138
|
+
/** The access token could not be verified */
|
|
139
|
+
INVALID_TOKEN: "bad_jwt",
|
|
140
|
+
/** The request needs a session at a higher assurance level: complete an MFA challenge first */
|
|
141
|
+
MFA_REQUIRED: "insufficient_aal",
|
|
142
|
+
MFA_CHALLENGE_EXPIRED: "mfa_challenge_expired",
|
|
143
|
+
MFA_VERIFICATION_FAILED: "mfa_verification_failed",
|
|
144
|
+
WEAK_PASSWORD: "weak_password",
|
|
145
|
+
/** Sign-up with an email that is already registered sends EMAIL_EXISTS */
|
|
146
|
+
USER_ALREADY_EXISTS: "user_already_exists",
|
|
147
|
+
EMAIL_EXISTS: "email_exists",
|
|
148
|
+
OAUTH_CALLBACK_ERROR: "bad_oauth_callback",
|
|
149
|
+
/** An emailed link or one-time code that has expired or was already used */
|
|
150
|
+
MAGIC_LINK_EXPIRED: "otp_expired",
|
|
151
|
+
OTP_EXPIRED: "otp_expired",
|
|
152
|
+
SIGNUP_DISABLED: "signup_disabled",
|
|
153
|
+
SAME_PASSWORD: "same_password",
|
|
154
|
+
OVER_EMAIL_SEND_RATE_LIMIT: "over_email_send_rate_limit",
|
|
155
|
+
OVER_REQUEST_RATE_LIMIT: "over_request_rate_limit",
|
|
137
156
|
} as const
|
|
138
157
|
|
|
139
158
|
export const StorageErrorCodes = {
|
|
@@ -145,31 +164,39 @@ export const StorageErrorCodes = {
|
|
|
145
164
|
PRESIGNED_URL_EXPIRED: "STORAGE_PRESIGNED_URL_EXPIRED",
|
|
146
165
|
} as const
|
|
147
166
|
|
|
167
|
+
/**
|
|
168
|
+
* The `code` a REST error carries, as PostgREST 14 sends it.
|
|
169
|
+
*
|
|
170
|
+
* PostgREST's own errors are `PGRST` codes. A database error is passed through under its Postgres
|
|
171
|
+
* SQLSTATE, with no prefix: a unique violation is `23505`, never `PGRST23505`.
|
|
172
|
+
*/
|
|
148
173
|
export const PostgrestErrorCodes = {
|
|
149
|
-
/**
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
|
|
153
|
-
/**
|
|
174
|
+
/** The token could not be decoded or verified: a bad signature, a malformed or empty token */
|
|
175
|
+
JWT_INVALID: "PGRST301",
|
|
176
|
+
/** The token's claims were rejected, an expired token among them (`PGRST301` before PostgREST 13) */
|
|
177
|
+
JWT_EXPIRED: "PGRST303",
|
|
178
|
+
/** Row-level security or a missing grant refused the operation */
|
|
179
|
+
PERMISSION_DENIED: "42501",
|
|
180
|
+
/** `.single()` found no row */
|
|
154
181
|
NOT_FOUND: "PGRST116",
|
|
155
|
-
/**
|
|
182
|
+
/** `.single()` found more than one row */
|
|
156
183
|
MULTIPLE_ROWS: "PGRST116",
|
|
157
|
-
/**
|
|
184
|
+
/** The request named a column the schema cache does not have */
|
|
158
185
|
SCHEMA_CACHE_MISS: "PGRST204",
|
|
159
186
|
/** Foreign key violation */
|
|
160
|
-
FK_VIOLATION: "
|
|
187
|
+
FK_VIOLATION: "23503",
|
|
161
188
|
/** Unique constraint violation */
|
|
162
|
-
UNIQUE_VIOLATION: "
|
|
189
|
+
UNIQUE_VIOLATION: "23505",
|
|
163
190
|
/** Not-null constraint violation */
|
|
164
|
-
NOT_NULL_VIOLATION: "
|
|
191
|
+
NOT_NULL_VIOLATION: "23502",
|
|
165
192
|
/** Check constraint violation */
|
|
166
|
-
CHECK_VIOLATION: "
|
|
193
|
+
CHECK_VIOLATION: "23514",
|
|
167
194
|
/** Invalid input syntax */
|
|
168
|
-
INVALID_INPUT: "
|
|
169
|
-
/**
|
|
170
|
-
UNDEFINED_TABLE: "
|
|
195
|
+
INVALID_INPUT: "22P02",
|
|
196
|
+
/** The table is not in the schema cache (the SQLSTATE `42P01` before PostgREST 13) */
|
|
197
|
+
UNDEFINED_TABLE: "PGRST205",
|
|
171
198
|
/** Undefined column */
|
|
172
|
-
UNDEFINED_COLUMN: "
|
|
199
|
+
UNDEFINED_COLUMN: "42703",
|
|
173
200
|
} as const
|
|
174
201
|
|
|
175
202
|
export const RealtimeErrorCodes = {
|
|
@@ -199,3 +226,20 @@ export const ErrorCodes = {
|
|
|
199
226
|
Realtime: RealtimeErrorCodes,
|
|
200
227
|
Network: NetworkErrorCodes,
|
|
201
228
|
} as const
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A request that never got an answer, as a result rather than a throw.
|
|
232
|
+
*
|
|
233
|
+
* The client's fetch throws typed errors (a timeout, a rate limit after the last retry), and their
|
|
234
|
+
* code and status are what lets a caller tell "try again later" from "the server is gone".
|
|
235
|
+
*/
|
|
236
|
+
export function transportFailure(e: unknown): SupatypeErrorShape {
|
|
237
|
+
if (e instanceof SupatypeError) {
|
|
238
|
+
return {
|
|
239
|
+
message: e.message,
|
|
240
|
+
code: e.code,
|
|
241
|
+
...(e.statusCode !== undefined && { status: e.statusCode }),
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return { message: e instanceof Error ? e.message : "Network error" }
|
|
245
|
+
}
|
package/src/exact-response.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
type ExactFields,
|
|
13
13
|
} from "./exact-values.js"
|
|
14
14
|
import { fieldKindsAreRegistered, registeredFieldsFor } from "./field-kinds-registry.js"
|
|
15
|
+
import { GENERATED_CLIENT_IMPORT } from "./generated-client.js"
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Either the parsed rows, or the reason they could not be read without corrupting something.
|
|
@@ -60,7 +61,7 @@ function describe(table: string, columns: readonly string[]): string {
|
|
|
60
61
|
`The ${which} ${named} on ${table} returned a value that JavaScript cannot hold exactly, and ` +
|
|
61
62
|
`this project's generated types have not been loaded, so Supatype does not know which ` +
|
|
62
63
|
`columns to keep exact. Run \`supatype push\` and import the client from the generated file:` +
|
|
63
|
-
`\n\n import { createClient } from "
|
|
64
|
+
`\n\n import { createClient } from "${GENERATED_CLIENT_IMPORT}"\n\n` +
|
|
64
65
|
`Reading it as a number would have silently changed the value, so it has not guessed.`
|
|
65
66
|
)
|
|
66
67
|
}
|
package/src/exact-values.ts
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
* module exists to remove.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
+
import { isJsonObject } from "./json.js"
|
|
19
|
+
|
|
18
20
|
/** The two shapes a column can need. `numeric` covers both `decimal` and `money`. */
|
|
19
21
|
export type ExactKind = "bigint" | "numeric"
|
|
20
22
|
|
|
@@ -44,7 +46,7 @@ function forEachKeyedNumber(body: string, visit: (found: KeyedNumber) => void):
|
|
|
44
46
|
let pendingKey: string | null = null
|
|
45
47
|
|
|
46
48
|
while (i < body.length) {
|
|
47
|
-
const ch = body
|
|
49
|
+
const ch = body.charAt(i)
|
|
48
50
|
|
|
49
51
|
if (ch === QUOTE) {
|
|
50
52
|
const end = endOfString(body, i)
|
|
@@ -68,7 +70,7 @@ function forEachKeyedNumber(body: string, visit: (found: KeyedNumber) => void):
|
|
|
68
70
|
|
|
69
71
|
if (ch === ":") {
|
|
70
72
|
i += 1
|
|
71
|
-
while (i < body.length && WHITESPACE.has(body
|
|
73
|
+
while (i < body.length && WHITESPACE.has(body.charAt(i))) i += 1
|
|
72
74
|
const key = pendingKey
|
|
73
75
|
pendingKey = null
|
|
74
76
|
if (key !== null) {
|
|
@@ -161,9 +163,14 @@ export function stringifyWithBigInts(value: unknown): string {
|
|
|
161
163
|
}
|
|
162
164
|
|
|
163
165
|
function coerceRow(row: unknown, fields: ExactFields): unknown {
|
|
164
|
-
|
|
166
|
+
return isJsonObject(row) ? coerceRecordKinds(row, fields) : row
|
|
167
|
+
}
|
|
165
168
|
|
|
166
|
-
|
|
169
|
+
/** One row with its bigint columns made native bigints: the same object when none needed it. */
|
|
170
|
+
export function coerceRecordKinds(
|
|
171
|
+
source: Record<string, unknown>,
|
|
172
|
+
fields: ExactFields,
|
|
173
|
+
): Record<string, unknown> {
|
|
167
174
|
let copy: Record<string, unknown> | null = null
|
|
168
175
|
|
|
169
176
|
for (const [name, kind] of Object.entries(fields)) {
|
|
@@ -174,7 +181,7 @@ function coerceRow(row: unknown, fields: ExactFields): unknown {
|
|
|
174
181
|
copy[name] = BigInt(current)
|
|
175
182
|
}
|
|
176
183
|
|
|
177
|
-
return copy ??
|
|
184
|
+
return copy ?? source
|
|
178
185
|
}
|
|
179
186
|
|
|
180
187
|
/** Whether parsing this literal as a double changes the value it names. */
|
|
@@ -221,7 +228,7 @@ function firstMeaningfulChar(body: string): string | undefined {
|
|
|
221
228
|
|
|
222
229
|
function nextMeaningfulChar(body: string, from: number): string | undefined {
|
|
223
230
|
for (let i = from; i < body.length; i += 1) {
|
|
224
|
-
const ch = body
|
|
231
|
+
const ch = body.charAt(i)
|
|
225
232
|
if (!WHITESPACE.has(ch)) return ch
|
|
226
233
|
}
|
|
227
234
|
return undefined
|
|
@@ -257,19 +264,19 @@ function endOfNumber(body: string, from: number): number {
|
|
|
257
264
|
let i = from
|
|
258
265
|
if (body[i] === "-") i += 1
|
|
259
266
|
const digitsStart = i
|
|
260
|
-
while (i < body.length && isDigit(body
|
|
267
|
+
while (i < body.length && isDigit(body.charAt(i))) i += 1
|
|
261
268
|
if (i === digitsStart) return from
|
|
262
269
|
|
|
263
270
|
if (body[i] === ".") {
|
|
264
271
|
i += 1
|
|
265
|
-
while (i < body.length && isDigit(body
|
|
272
|
+
while (i < body.length && isDigit(body.charAt(i))) i += 1
|
|
266
273
|
}
|
|
267
274
|
|
|
268
275
|
if (body[i] === "e" || body[i] === "E") {
|
|
269
276
|
let j = i + 1
|
|
270
277
|
if (body[j] === "+" || body[j] === "-") j += 1
|
|
271
278
|
const expStart = j
|
|
272
|
-
while (j < body.length && isDigit(body
|
|
279
|
+
while (j < body.length && isDigit(body.charAt(j))) j += 1
|
|
273
280
|
if (j > expStart) i = j
|
|
274
281
|
}
|
|
275
282
|
|