terrascale 0.3.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/LICENSE +202 -0
- package/README.md +143 -0
- package/package.json +159 -0
- package/sdk-current-contract.json +27 -0
- package/sdk-route-manifest.json +67 -0
- package/src/admin.js +9 -0
- package/src/better-auth.js +14 -0
- package/src/config.js +121 -0
- package/src/database-codec.js +845 -0
- package/src/database-types.js +237 -0
- package/src/database-view.js +422 -0
- package/src/database.js +420 -0
- package/src/discovery.js +374 -0
- package/src/http.js +887 -0
- package/src/index.js +79 -0
- package/src/local/authentication.js +47 -0
- package/src/local/better-auth.js +517 -0
- package/src/local/cli.js +51 -0
- package/src/local/context.js +23 -0
- package/src/local/environment.js +109 -0
- package/src/local/index.js +204 -0
- package/src/local/router.js +999 -0
- package/src/local/server.js +664 -0
- package/src/local/store.js +530 -0
- package/src/local/test-environment.js +74 -0
- package/src/management-contracts.js +72 -0
- package/src/management.js +12 -0
- package/src/native-origin.js +75 -0
- package/src/postgres.js +494 -0
- package/src/react/core.js +743 -0
- package/src/react/index.js +99 -0
- package/src/result.js +251 -0
- package/src/schema.js +366 -0
- package/src/sql.js +996 -0
- package/src/svelte/index.js +129 -0
- package/src/tanstack/index.js +511 -0
- package/src/ts-auth-discovery.js +190 -0
- package/src/ts-auth.js +3497 -0
- package/types/admin.d.ts +6 -0
- package/types/better-auth.d.ts +8 -0
- package/types/config.d.ts +58 -0
- package/types/database-codec.d.ts +111 -0
- package/types/database-types.d.ts +213 -0
- package/types/database-view.d.ts +183 -0
- package/types/database.d.ts +98 -0
- package/types/discovery.d.ts +114 -0
- package/types/http.d.ts +46 -0
- package/types/index.d.ts +52 -0
- package/types/local/authentication.d.ts +11 -0
- package/types/local/better-auth.d.ts +33 -0
- package/types/local/cli.d.ts +2 -0
- package/types/local/context.d.ts +14 -0
- package/types/local/environment.d.ts +23 -0
- package/types/local/index.d.ts +94 -0
- package/types/local/router.d.ts +66 -0
- package/types/local/server.d.ts +54 -0
- package/types/local/store.d.ts +106 -0
- package/types/local/test-environment.d.ts +25 -0
- package/types/management-contracts.d.ts +44 -0
- package/types/management.d.ts +6 -0
- package/types/native-origin.d.ts +23 -0
- package/types/postgres.d.ts +123 -0
- package/types/react/core.d.ts +366 -0
- package/types/react/index.d.ts +54 -0
- package/types/result.d.ts +161 -0
- package/types/schema.d.ts +145 -0
- package/types/sql.d.ts +288 -0
- package/types/svelte/index.d.ts +81 -0
- package/types/tanstack/index.d.ts +165 -0
- package/types/ts-auth-discovery.d.ts +11 -0
- package/types/ts-auth.d.ts +1826 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {TerraScaleBlockedCapability} capability
|
|
3
|
+
* @returns {never}
|
|
4
|
+
*/
|
|
5
|
+
export function requireTerraScaleCapability(capability: TerraScaleBlockedCapability): never;
|
|
6
|
+
/** Required capabilities whose producer/runtime admission is still open. */
|
|
7
|
+
export const terraScaleReleaseBlockers: Readonly<{
|
|
8
|
+
management: Readonly<{
|
|
9
|
+
code: "management_contract_unavailable";
|
|
10
|
+
owner: "C06/C07 regional management service";
|
|
11
|
+
required: readonly string[];
|
|
12
|
+
}>;
|
|
13
|
+
betterAuth: Readonly<{
|
|
14
|
+
code: "security_authority_unavailable";
|
|
15
|
+
owner: "native single-writer security admission and C06/C07";
|
|
16
|
+
required: readonly string[];
|
|
17
|
+
}>;
|
|
18
|
+
qualification: Readonly<{
|
|
19
|
+
code: "runtime_qualification_unavailable";
|
|
20
|
+
owner: "real producer allocation and deployed customer application";
|
|
21
|
+
required: readonly string[];
|
|
22
|
+
}>;
|
|
23
|
+
}>;
|
|
24
|
+
/** @typedef {keyof typeof terraScaleReleaseBlockers} TerraScaleBlockedCapability */
|
|
25
|
+
export class TerraScaleCapabilityUnavailableError extends Error {
|
|
26
|
+
/** @param {TerraScaleBlockedCapability} capability */
|
|
27
|
+
constructor(capability: TerraScaleBlockedCapability);
|
|
28
|
+
/**
|
|
29
|
+
* @readonly
|
|
30
|
+
* @type {string}
|
|
31
|
+
*/
|
|
32
|
+
readonly code: string;
|
|
33
|
+
/**
|
|
34
|
+
* @readonly
|
|
35
|
+
* @type {TerraScaleBlockedCapability}
|
|
36
|
+
*/
|
|
37
|
+
readonly capability: TerraScaleBlockedCapability;
|
|
38
|
+
/**
|
|
39
|
+
* @readonly
|
|
40
|
+
* @type {readonly string[]}
|
|
41
|
+
*/
|
|
42
|
+
readonly required: readonly string[];
|
|
43
|
+
}
|
|
44
|
+
export type TerraScaleBlockedCapability = keyof typeof terraScaleReleaseBlockers;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Required public management support remains closed until C06/C07 route admission.
|
|
3
|
+
* @returns {never}
|
|
4
|
+
*/
|
|
5
|
+
export function requireTerraScaleManagement(): never;
|
|
6
|
+
export { terraScaleReleaseBlockers, TerraScaleCapabilityUnavailableError } from "./management-contracts.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {unknown} value
|
|
3
|
+
* @returns {string}
|
|
4
|
+
*/
|
|
5
|
+
export function requireNativeRegion(value: unknown): string;
|
|
6
|
+
/**
|
|
7
|
+
* @param {unknown} code
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
export function nativeRegionalOrigin(code: unknown): string;
|
|
11
|
+
/**
|
|
12
|
+
* Hosted identity refuses database API origins, without using native admission.
|
|
13
|
+
* @param {string} hostname
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
export function isDataApiHostname(hostname: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} raw
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export function nativeServiceOrigin(raw: string): string;
|
|
22
|
+
/** Internal admission policy shared by native clients and qualification configuration. */
|
|
23
|
+
export const terraScaleServiceDomain: "terrascale.tech";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validate and clone a builder descriptor without converting or interpolating values.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} statement
|
|
5
|
+
* @param {readonly unknown[]} [parameters]
|
|
6
|
+
* @returns {PostgresBoundQuery}
|
|
7
|
+
*/
|
|
8
|
+
export function postgresQuery(statement: string, parameters?: readonly unknown[]): PostgresBoundQuery;
|
|
9
|
+
/** @typedef {bigint | string} PostgresParameter */
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {{
|
|
12
|
+
* readonly statement: string;
|
|
13
|
+
* readonly parameters: readonly unknown[];
|
|
14
|
+
* }} PostgresQuery
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {{
|
|
18
|
+
* readonly statement: string;
|
|
19
|
+
* readonly parameters: readonly PostgresParameter[];
|
|
20
|
+
* }} PostgresBoundQuery
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {{
|
|
24
|
+
* readonly host: string;
|
|
25
|
+
* readonly port: number;
|
|
26
|
+
* readonly tlsServerName: string;
|
|
27
|
+
* readonly caCertificatePem: string;
|
|
28
|
+
* readonly database: string;
|
|
29
|
+
* readonly keyId: string;
|
|
30
|
+
* readonly password: string;
|
|
31
|
+
* readonly connectTimeoutMs?: number;
|
|
32
|
+
* }} PostgresConfig
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {{
|
|
36
|
+
* readonly name: string;
|
|
37
|
+
* readonly oid: 20 | 25;
|
|
38
|
+
* }} PostgresColumn
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* @typedef {{
|
|
42
|
+
* readonly command: string;
|
|
43
|
+
* readonly affectedRows: number;
|
|
44
|
+
* readonly columns: readonly PostgresColumn[];
|
|
45
|
+
* readonly rows: readonly (readonly PostgresParameter[])[];
|
|
46
|
+
* }} PostgresResult
|
|
47
|
+
*/
|
|
48
|
+
/** Diagnostics retain SQLSTATE, but never driver messages, SQL or passwords. */
|
|
49
|
+
export class PostgresError extends Error {
|
|
50
|
+
/**
|
|
51
|
+
* @param {"configuration" | "query" | "connection" | "database" | "protocol"} kind
|
|
52
|
+
* @param {string} [sqlState]
|
|
53
|
+
*/
|
|
54
|
+
constructor(kind: "configuration" | "query" | "connection" | "database" | "protocol", sqlState?: string);
|
|
55
|
+
/**
|
|
56
|
+
* @override
|
|
57
|
+
* @readonly
|
|
58
|
+
*/
|
|
59
|
+
override readonly name: "PostgresError";
|
|
60
|
+
/**
|
|
61
|
+
* @readonly
|
|
62
|
+
* @type {"configuration" | "query" | "connection" | "database" | "protocol"}
|
|
63
|
+
*/
|
|
64
|
+
readonly kind: "configuration" | "query" | "connection" | "database" | "protocol";
|
|
65
|
+
/**
|
|
66
|
+
* @readonly
|
|
67
|
+
* @type {string | undefined}
|
|
68
|
+
*/
|
|
69
|
+
readonly sqlState: string | undefined;
|
|
70
|
+
}
|
|
71
|
+
/** One driver connection; no automatic reconnect, mutation retry or callback replay. */
|
|
72
|
+
export class PostgresClient {
|
|
73
|
+
/**
|
|
74
|
+
* @param {PostgresConfig} input
|
|
75
|
+
* @returns {Promise<PostgresClient>}
|
|
76
|
+
*/
|
|
77
|
+
static connect(input: PostgresConfig): Promise<PostgresClient>;
|
|
78
|
+
/**
|
|
79
|
+
* @private
|
|
80
|
+
* @param {DriverClient} driver
|
|
81
|
+
* @param {typeof DatabaseError} databaseError
|
|
82
|
+
*/
|
|
83
|
+
private constructor();
|
|
84
|
+
/**
|
|
85
|
+
* @param {PostgresQuery} input
|
|
86
|
+
* @returns {Promise<PostgresResult>}
|
|
87
|
+
*/
|
|
88
|
+
query(input: PostgresQuery): Promise<PostgresResult>;
|
|
89
|
+
/**
|
|
90
|
+
* @returns {Promise<void>}
|
|
91
|
+
*/
|
|
92
|
+
close(): Promise<void>;
|
|
93
|
+
#private;
|
|
94
|
+
}
|
|
95
|
+
export type PostgresParameter = bigint | string;
|
|
96
|
+
export type PostgresQuery = {
|
|
97
|
+
readonly statement: string;
|
|
98
|
+
readonly parameters: readonly unknown[];
|
|
99
|
+
};
|
|
100
|
+
export type PostgresBoundQuery = {
|
|
101
|
+
readonly statement: string;
|
|
102
|
+
readonly parameters: readonly PostgresParameter[];
|
|
103
|
+
};
|
|
104
|
+
export type PostgresConfig = {
|
|
105
|
+
readonly host: string;
|
|
106
|
+
readonly port: number;
|
|
107
|
+
readonly tlsServerName: string;
|
|
108
|
+
readonly caCertificatePem: string;
|
|
109
|
+
readonly database: string;
|
|
110
|
+
readonly keyId: string;
|
|
111
|
+
readonly password: string;
|
|
112
|
+
readonly connectTimeoutMs?: number;
|
|
113
|
+
};
|
|
114
|
+
export type PostgresColumn = {
|
|
115
|
+
readonly name: string;
|
|
116
|
+
readonly oid: 20 | 25;
|
|
117
|
+
};
|
|
118
|
+
export type PostgresResult = {
|
|
119
|
+
readonly command: string;
|
|
120
|
+
readonly affectedRows: number;
|
|
121
|
+
readonly columns: readonly PostgresColumn[];
|
|
122
|
+
readonly rows: readonly (readonly PostgresParameter[])[];
|
|
123
|
+
};
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template TValue
|
|
3
|
+
* @typedef {{ readonly kind: "snapshot" | "data"; readonly value: TValue }
|
|
4
|
+
* | { readonly kind: "reset"; readonly reset: ReactiveReset<TValue> }
|
|
5
|
+
* | { readonly kind: "error"; readonly error: unknown }} ReactiveHttpEvent
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Typed boundary for the owning HTTP contract. Implementations perform the
|
|
9
|
+
* documented request and polling protocol; this adapter only maps events to
|
|
10
|
+
* the hook's observer and propagates AbortSignal.
|
|
11
|
+
*
|
|
12
|
+
* @template TRequest, TValue
|
|
13
|
+
* @typedef {{
|
|
14
|
+
* readonly subscribe: (
|
|
15
|
+
* request: TRequest,
|
|
16
|
+
* observer: { readonly event: (event: ReactiveHttpEvent<TValue>) => void },
|
|
17
|
+
* context: ReactiveSubscriptionContext,
|
|
18
|
+
* ) => ReactiveDisposer | void | Promise<ReactiveDisposer | void>;
|
|
19
|
+
* }} ReactiveHttpTransport
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* @template TRequest, TValue
|
|
23
|
+
* @param {ReactiveHttpTransport<TRequest, TValue>} transport
|
|
24
|
+
* @param {TRequest} request
|
|
25
|
+
* @param {string | readonly unknown[]} [key]
|
|
26
|
+
* @returns {ReactiveQuerySource<TValue>}
|
|
27
|
+
*/
|
|
28
|
+
export function createReactiveHttpQuery<TRequest, TValue>(transport: ReactiveHttpTransport<TRequest, TValue>, request: TRequest, key?: string | readonly unknown[]): ReactiveQuerySource<TValue>;
|
|
29
|
+
/** @typedef {import("../database-view.js").DatabaseQueryCallbacks} DatabaseQueryCallbacks */
|
|
30
|
+
/** @typedef {import("../database-view.js").DatabaseQueryOptions} DatabaseQueryOptions */
|
|
31
|
+
/** @typedef {import("../database-view.js").DatabaseQuerySource} DatabaseQuerySource */
|
|
32
|
+
/** @typedef {import("../database-view.js").DatabaseViewBinding} DatabaseViewBinding */
|
|
33
|
+
/** @typedef {import("../database-view.js").DatabaseViewPosition} DatabaseViewPosition */
|
|
34
|
+
/** @typedef {import("../database-view.js").DatabaseViewSnapshot} DatabaseViewSnapshot */
|
|
35
|
+
/**
|
|
36
|
+
* A subscription cleanup returned by a typed reactive transport.
|
|
37
|
+
*
|
|
38
|
+
* @typedef {(() => void) | { readonly dispose: () => void } | { readonly unsubscribe: () => void }} ReactiveDisposer
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* @typedef {{
|
|
42
|
+
* readonly signal: AbortSignal;
|
|
43
|
+
* }} ReactiveSubscriptionContext
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* @template TValue
|
|
47
|
+
* @typedef {{
|
|
48
|
+
* readonly next: (value: TValue) => void;
|
|
49
|
+
* readonly error: (error: unknown) => void;
|
|
50
|
+
* readonly reset: (reset: ReactiveReset<TValue>) => void;
|
|
51
|
+
* }} ReactiveQueryCallbacks
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* The operation owned by the public reactive contract. It may use HTTP
|
|
55
|
+
* long-polling or WebSocket delivery; the hook does not know or care which.
|
|
56
|
+
*
|
|
57
|
+
* @template TValue
|
|
58
|
+
* @typedef {{
|
|
59
|
+
* readonly key?: string | readonly unknown[];
|
|
60
|
+
* readonly subscribe: (
|
|
61
|
+
* observer: ReactiveQueryCallbacks<TValue>,
|
|
62
|
+
* context: ReactiveSubscriptionContext,
|
|
63
|
+
* ) => ReactiveDisposer | void | Promise<ReactiveDisposer | void>;
|
|
64
|
+
* }} ReactiveQuerySource
|
|
65
|
+
*/
|
|
66
|
+
/**
|
|
67
|
+
* @typedef {"checkpoint-expired"
|
|
68
|
+
* | "retention-advanced"
|
|
69
|
+
* | "generation-changed"
|
|
70
|
+
* | "checkpoint-not-current"
|
|
71
|
+
* | "view-diverged"
|
|
72
|
+
* | "oversized"
|
|
73
|
+
* | "invalid-operations"
|
|
74
|
+
* | "invalid-checkpoint"
|
|
75
|
+
* | "backpressure"
|
|
76
|
+
* | (string & {})} ReactiveResetReason
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* A server-authorized reset. The checkpoint remains opaque to the hook.
|
|
80
|
+
*
|
|
81
|
+
* @template TValue
|
|
82
|
+
* @typedef {{
|
|
83
|
+
* readonly reason: ReactiveResetReason;
|
|
84
|
+
* readonly data?: TValue;
|
|
85
|
+
* readonly checkpoint?: string | Uint8Array;
|
|
86
|
+
* readonly code?: string;
|
|
87
|
+
* readonly message?: string;
|
|
88
|
+
* }} ReactiveReset
|
|
89
|
+
*/
|
|
90
|
+
/** @typedef {"subscription" | "transport" | "remote" | "cancelled" | "mutation"} ReactiveErrorKind */
|
|
91
|
+
/**
|
|
92
|
+
* @typedef {{
|
|
93
|
+
* readonly kind: ReactiveErrorKind;
|
|
94
|
+
* readonly code: string;
|
|
95
|
+
* readonly message: string;
|
|
96
|
+
* readonly cause?: unknown;
|
|
97
|
+
* readonly terraScale?: TerraScaleError;
|
|
98
|
+
* readonly database?: Extract<DatabaseResult<never>, { readonly ok: false }>["error"];
|
|
99
|
+
* }} ReactiveError
|
|
100
|
+
*/
|
|
101
|
+
/** @typedef {"pending" | "success" | "error" | "reset"} ReactiveQueryStatus */
|
|
102
|
+
/**
|
|
103
|
+
* @template TValue
|
|
104
|
+
* @typedef {{
|
|
105
|
+
* readonly status: ReactiveQueryStatus;
|
|
106
|
+
* readonly data: TValue | undefined;
|
|
107
|
+
* readonly error: ReactiveError | undefined;
|
|
108
|
+
* readonly reset: ReactiveReset<TValue> | undefined;
|
|
109
|
+
* readonly isLoading: boolean;
|
|
110
|
+
* readonly isFetching: boolean;
|
|
111
|
+
* readonly isStale: boolean;
|
|
112
|
+
* }} ReactiveQueryState
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* @template TValue
|
|
116
|
+
* @typedef {{
|
|
117
|
+
* readonly enabled?: boolean;
|
|
118
|
+
* readonly initialData?: TValue;
|
|
119
|
+
* }} ReactiveQueryOptions
|
|
120
|
+
*/
|
|
121
|
+
/**
|
|
122
|
+
* Framework-neutral query observer used by useQuery and SSR-safe tests.
|
|
123
|
+
*
|
|
124
|
+
* @template TValue
|
|
125
|
+
*/
|
|
126
|
+
export class ReactiveQueryObserver<TValue> {
|
|
127
|
+
/**
|
|
128
|
+
* @param {ReactiveQuerySource<TValue>} source
|
|
129
|
+
* @param {ReactiveQueryOptions<TValue>} [options]
|
|
130
|
+
*/
|
|
131
|
+
constructor(source: ReactiveQuerySource<TValue>, options?: ReactiveQueryOptions<TValue>);
|
|
132
|
+
/** @returns {ReactiveQueryState<TValue>} */
|
|
133
|
+
getSnapshot: () => ReactiveQueryState<TValue>;
|
|
134
|
+
/** @returns {ReactiveQueryState<TValue>} */
|
|
135
|
+
getServerSnapshot: () => ReactiveQueryState<TValue>;
|
|
136
|
+
/**
|
|
137
|
+
* Updates the small, hook-facing option set without replacing the external
|
|
138
|
+
* store. React may render a hook with a new options object while preserving
|
|
139
|
+
* the observer; an `enabled` transition must therefore start/stop delivery
|
|
140
|
+
* at the subscription boundary instead of leaving a stale subscription.
|
|
141
|
+
*
|
|
142
|
+
* @param {ReactiveQueryOptions<TValue>} [options]
|
|
143
|
+
* @returns {void}
|
|
144
|
+
*/
|
|
145
|
+
updateOptions(options?: ReactiveQueryOptions<TValue>): void;
|
|
146
|
+
/**
|
|
147
|
+
* @param {() => void} listener
|
|
148
|
+
* @returns {() => void}
|
|
149
|
+
*/
|
|
150
|
+
subscribe: (listener: () => void) => () => void;
|
|
151
|
+
/** @returns {void} */
|
|
152
|
+
start(): void;
|
|
153
|
+
/**
|
|
154
|
+
* Aborts HTTP polling and disposes the current transport subscription.
|
|
155
|
+
*
|
|
156
|
+
* @returns {void}
|
|
157
|
+
*/
|
|
158
|
+
dispose(): void;
|
|
159
|
+
/**
|
|
160
|
+
* Restart only while an enabled query has live observers.
|
|
161
|
+
*
|
|
162
|
+
* @returns {void}
|
|
163
|
+
*/
|
|
164
|
+
refetch(): void;
|
|
165
|
+
#private;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @typedef {{
|
|
169
|
+
* readonly signal: AbortSignal;
|
|
170
|
+
* readonly idempotencyKey: string;
|
|
171
|
+
* }} ReactiveMutationContext
|
|
172
|
+
*/
|
|
173
|
+
/** @typedef {void | (() => void) | { readonly rollback: () => void }} OptimisticRollback */
|
|
174
|
+
/**
|
|
175
|
+
* @template TValue, TVariables
|
|
176
|
+
* @typedef {{
|
|
177
|
+
* readonly mutationFn: (
|
|
178
|
+
* variables: TVariables,
|
|
179
|
+
* context: ReactiveMutationContext,
|
|
180
|
+
* ) => Promise<TValue | TerraScaleResult<TValue> | DatabaseResult<TValue>>;
|
|
181
|
+
* readonly idempotencyKey?: string | ((variables: TVariables) => string);
|
|
182
|
+
* readonly onMutate?: (variables: TVariables, context: ReactiveMutationContext) => OptimisticRollback | Promise<OptimisticRollback>;
|
|
183
|
+
* }} ReactiveMutationOptions
|
|
184
|
+
*/
|
|
185
|
+
/** @typedef {"idle" | "pending" | "success" | "error" | "cancelled"} ReactiveMutationStatus */
|
|
186
|
+
/**
|
|
187
|
+
* @template TValue
|
|
188
|
+
* @typedef {{
|
|
189
|
+
* readonly status: ReactiveMutationStatus;
|
|
190
|
+
* readonly data: TValue | undefined;
|
|
191
|
+
* readonly error: ReactiveError | undefined;
|
|
192
|
+
* readonly idempotencyKey: string | undefined;
|
|
193
|
+
* readonly isPending: boolean;
|
|
194
|
+
* }} ReactiveMutationState
|
|
195
|
+
*/
|
|
196
|
+
/**
|
|
197
|
+
* @template TValue, TVariables
|
|
198
|
+
*/
|
|
199
|
+
export class ReactiveMutationObserver<TValue, TVariables> {
|
|
200
|
+
/**
|
|
201
|
+
* @param {ReactiveMutationOptions<TValue, TVariables>} options
|
|
202
|
+
*/
|
|
203
|
+
constructor(options: ReactiveMutationOptions<TValue, TVariables>);
|
|
204
|
+
/**
|
|
205
|
+
* Keep callbacks current when a component re-renders without replacing the
|
|
206
|
+
* observer (the normal `useMutation` lifecycle). A mutation already running
|
|
207
|
+
* captures the callbacks it started with, so a render cannot redirect an
|
|
208
|
+
* in-flight durable request.
|
|
209
|
+
*
|
|
210
|
+
* @param {ReactiveMutationOptions<TValue, TVariables>} options
|
|
211
|
+
* @returns {void}
|
|
212
|
+
*/
|
|
213
|
+
updateOptions(options: ReactiveMutationOptions<TValue, TVariables>): void;
|
|
214
|
+
/** @returns {ReactiveMutationState<TValue>} */
|
|
215
|
+
getSnapshot: () => ReactiveMutationState<TValue>;
|
|
216
|
+
/**
|
|
217
|
+
* @param {() => void} listener
|
|
218
|
+
* @returns {() => void}
|
|
219
|
+
*/
|
|
220
|
+
subscribe: (listener: () => void) => () => void;
|
|
221
|
+
/**
|
|
222
|
+
* @param {TVariables} variables
|
|
223
|
+
* @returns {Promise<TValue>}
|
|
224
|
+
*/
|
|
225
|
+
mutateAsync(variables: TVariables): Promise<TValue>;
|
|
226
|
+
/**
|
|
227
|
+
* @param {TVariables} variables
|
|
228
|
+
* @returns {void}
|
|
229
|
+
*/
|
|
230
|
+
mutate(variables: TVariables): void;
|
|
231
|
+
/**
|
|
232
|
+
* Cancels the durable request and immediately rolls back its presentation.
|
|
233
|
+
*
|
|
234
|
+
* @returns {void}
|
|
235
|
+
*/
|
|
236
|
+
cancel(): void;
|
|
237
|
+
/** @returns {void} */
|
|
238
|
+
reset(): void;
|
|
239
|
+
/** @returns {void} */
|
|
240
|
+
dispose(): void;
|
|
241
|
+
#private;
|
|
242
|
+
}
|
|
243
|
+
export class ReactiveMutationError extends Error {
|
|
244
|
+
/**
|
|
245
|
+
* @param {string} code
|
|
246
|
+
* @param {string} message
|
|
247
|
+
* @param {unknown} [cause]
|
|
248
|
+
*/
|
|
249
|
+
constructor(code: string, message: string, cause?: unknown);
|
|
250
|
+
/** @override @readonly */
|
|
251
|
+
override readonly name: "ReactiveMutationError";
|
|
252
|
+
/** @readonly @type {string} */
|
|
253
|
+
readonly code: string;
|
|
254
|
+
/** @override @readonly @type {unknown} */
|
|
255
|
+
override readonly cause: unknown;
|
|
256
|
+
}
|
|
257
|
+
export type ReactiveHttpEvent<TValue> = {
|
|
258
|
+
readonly kind: "snapshot" | "data";
|
|
259
|
+
readonly value: TValue;
|
|
260
|
+
} | {
|
|
261
|
+
readonly kind: "reset";
|
|
262
|
+
readonly reset: ReactiveReset<TValue>;
|
|
263
|
+
} | {
|
|
264
|
+
readonly kind: "error";
|
|
265
|
+
readonly error: unknown;
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* Typed boundary for the owning HTTP contract. Implementations perform the
|
|
269
|
+
* documented request and polling protocol; this adapter only maps events to
|
|
270
|
+
* the hook's observer and propagates AbortSignal.
|
|
271
|
+
*/
|
|
272
|
+
export type ReactiveHttpTransport<TRequest, TValue> = {
|
|
273
|
+
readonly subscribe: (request: TRequest, observer: {
|
|
274
|
+
readonly event: (event: ReactiveHttpEvent<TValue>) => void;
|
|
275
|
+
}, context: ReactiveSubscriptionContext) => ReactiveDisposer | void | Promise<ReactiveDisposer | void>;
|
|
276
|
+
};
|
|
277
|
+
export type DatabaseQueryCallbacks = import("../database-view.js").DatabaseQueryCallbacks;
|
|
278
|
+
export type DatabaseQueryOptions = import("../database-view.js").DatabaseQueryOptions;
|
|
279
|
+
export type DatabaseQuerySource = import("../database-view.js").DatabaseQuerySource;
|
|
280
|
+
export type DatabaseViewBinding = import("../database-view.js").DatabaseViewBinding;
|
|
281
|
+
export type DatabaseViewPosition = import("../database-view.js").DatabaseViewPosition;
|
|
282
|
+
export type DatabaseViewSnapshot = import("../database-view.js").DatabaseViewSnapshot;
|
|
283
|
+
/**
|
|
284
|
+
* A subscription cleanup returned by a typed reactive transport.
|
|
285
|
+
*/
|
|
286
|
+
export type ReactiveDisposer = (() => void) | {
|
|
287
|
+
readonly dispose: () => void;
|
|
288
|
+
} | {
|
|
289
|
+
readonly unsubscribe: () => void;
|
|
290
|
+
};
|
|
291
|
+
export type ReactiveSubscriptionContext = {
|
|
292
|
+
readonly signal: AbortSignal;
|
|
293
|
+
};
|
|
294
|
+
export type ReactiveQueryCallbacks<TValue> = {
|
|
295
|
+
readonly next: (value: TValue) => void;
|
|
296
|
+
readonly error: (error: unknown) => void;
|
|
297
|
+
readonly reset: (reset: ReactiveReset<TValue>) => void;
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* The operation owned by the public reactive contract. It may use HTTP
|
|
301
|
+
* long-polling or WebSocket delivery; the hook does not know or care which.
|
|
302
|
+
*/
|
|
303
|
+
export type ReactiveQuerySource<TValue> = {
|
|
304
|
+
readonly key?: string | readonly unknown[];
|
|
305
|
+
readonly subscribe: (observer: ReactiveQueryCallbacks<TValue>, context: ReactiveSubscriptionContext) => ReactiveDisposer | void | Promise<ReactiveDisposer | void>;
|
|
306
|
+
};
|
|
307
|
+
export type ReactiveResetReason = "checkpoint-expired" | "retention-advanced" | "generation-changed" | "checkpoint-not-current" | "view-diverged" | "oversized" | "invalid-operations" | "invalid-checkpoint" | "backpressure" | (string & {});
|
|
308
|
+
/**
|
|
309
|
+
* A server-authorized reset. The checkpoint remains opaque to the hook.
|
|
310
|
+
*/
|
|
311
|
+
export type ReactiveReset<TValue> = {
|
|
312
|
+
readonly reason: ReactiveResetReason;
|
|
313
|
+
readonly data?: TValue;
|
|
314
|
+
readonly checkpoint?: string | Uint8Array;
|
|
315
|
+
readonly code?: string;
|
|
316
|
+
readonly message?: string;
|
|
317
|
+
};
|
|
318
|
+
export type ReactiveErrorKind = "subscription" | "transport" | "remote" | "cancelled" | "mutation";
|
|
319
|
+
export type ReactiveError = {
|
|
320
|
+
readonly kind: ReactiveErrorKind;
|
|
321
|
+
readonly code: string;
|
|
322
|
+
readonly message: string;
|
|
323
|
+
readonly cause?: unknown;
|
|
324
|
+
readonly terraScale?: TerraScaleError;
|
|
325
|
+
readonly database?: Extract<DatabaseResult<never>, {
|
|
326
|
+
readonly ok: false;
|
|
327
|
+
}>["error"];
|
|
328
|
+
};
|
|
329
|
+
export type ReactiveQueryStatus = "pending" | "success" | "error" | "reset";
|
|
330
|
+
export type ReactiveQueryState<TValue> = {
|
|
331
|
+
readonly status: ReactiveQueryStatus;
|
|
332
|
+
readonly data: TValue | undefined;
|
|
333
|
+
readonly error: ReactiveError | undefined;
|
|
334
|
+
readonly reset: ReactiveReset<TValue> | undefined;
|
|
335
|
+
readonly isLoading: boolean;
|
|
336
|
+
readonly isFetching: boolean;
|
|
337
|
+
readonly isStale: boolean;
|
|
338
|
+
};
|
|
339
|
+
export type ReactiveQueryOptions<TValue> = {
|
|
340
|
+
readonly enabled?: boolean;
|
|
341
|
+
readonly initialData?: TValue;
|
|
342
|
+
};
|
|
343
|
+
export type ReactiveMutationContext = {
|
|
344
|
+
readonly signal: AbortSignal;
|
|
345
|
+
readonly idempotencyKey: string;
|
|
346
|
+
};
|
|
347
|
+
export type OptimisticRollback = void | (() => void) | {
|
|
348
|
+
readonly rollback: () => void;
|
|
349
|
+
};
|
|
350
|
+
export type ReactiveMutationOptions<TValue, TVariables> = {
|
|
351
|
+
readonly mutationFn: (variables: TVariables, context: ReactiveMutationContext) => Promise<TValue | TerraScaleResult<TValue> | DatabaseResult<TValue>>;
|
|
352
|
+
readonly idempotencyKey?: string | ((variables: TVariables) => string);
|
|
353
|
+
readonly onMutate?: (variables: TVariables, context: ReactiveMutationContext) => OptimisticRollback | Promise<OptimisticRollback>;
|
|
354
|
+
};
|
|
355
|
+
export type ReactiveMutationStatus = "idle" | "pending" | "success" | "error" | "cancelled";
|
|
356
|
+
export type ReactiveMutationState<TValue> = {
|
|
357
|
+
readonly status: ReactiveMutationStatus;
|
|
358
|
+
readonly data: TValue | undefined;
|
|
359
|
+
readonly error: ReactiveError | undefined;
|
|
360
|
+
readonly idempotencyKey: string | undefined;
|
|
361
|
+
readonly isPending: boolean;
|
|
362
|
+
};
|
|
363
|
+
import type { TerraScaleError } from "../result.js";
|
|
364
|
+
import type { DatabaseResult } from "../database-types.js";
|
|
365
|
+
import type { TerraScaleResult } from "../result.js";
|
|
366
|
+
export { createDatabaseQuery, DatabaseSubscriptionError, DatabaseSubscriptionView } from "../database-view.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React's external-store primitive gives useQuery one SSR-safe snapshot and
|
|
3
|
+
* makes every server update flow through the typed reactive subscription.
|
|
4
|
+
* Keep the source object stable between renders.
|
|
5
|
+
*
|
|
6
|
+
* @template TValue
|
|
7
|
+
* @param {ReactiveQuerySource<TValue>} source
|
|
8
|
+
* @param {ReactiveQueryOptions<TValue>} [options]
|
|
9
|
+
* @returns {ReactiveQueryState<TValue> & { readonly refetch: () => void }}
|
|
10
|
+
*/
|
|
11
|
+
export function useQuery<TValue>(source: ReactiveQuerySource<TValue>, options?: ReactiveQueryOptions<TValue>): ReactiveQueryState<TValue> & {
|
|
12
|
+
readonly refetch: () => void;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @template TValue, TVariables
|
|
16
|
+
* @typedef {ReactiveMutationState<TValue> & {
|
|
17
|
+
* readonly mutate: (variables: TVariables) => void;
|
|
18
|
+
* readonly mutateAsync: (variables: TVariables) => Promise<TValue>;
|
|
19
|
+
* readonly cancel: () => void;
|
|
20
|
+
* readonly reset: () => void;
|
|
21
|
+
* }} UseMutationResult
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* A durable mutation always receives one idempotency identity and AbortSignal.
|
|
25
|
+
* The optional onMutate result is a reversible presentation rollback; it is
|
|
26
|
+
* never an offline queue and is removed on either acceptance or rejection.
|
|
27
|
+
*
|
|
28
|
+
* @template TValue
|
|
29
|
+
* @template [TVariables=void]
|
|
30
|
+
* @param {ReactiveMutationOptions<TValue, TVariables>} options
|
|
31
|
+
* @returns {UseMutationResult<TValue, TVariables>}
|
|
32
|
+
*/
|
|
33
|
+
export function useMutation<TValue, TVariables = void>(options: ReactiveMutationOptions<TValue, TVariables>): UseMutationResult<TValue, TVariables>;
|
|
34
|
+
/**
|
|
35
|
+
* Explicitly disposes a mutation observer created outside a React tree.
|
|
36
|
+
*
|
|
37
|
+
* @template TValue, TVariables
|
|
38
|
+
* @param {ReactiveMutationObserver<TValue, TVariables>} observer
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
export function disposeMutation<TValue, TVariables>(observer: ReactiveMutationObserver<TValue, TVariables>): void;
|
|
42
|
+
export * from "./core.js";
|
|
43
|
+
export type UseMutationResult<TValue, TVariables> = ReactiveMutationState<TValue> & {
|
|
44
|
+
readonly mutate: (variables: TVariables) => void;
|
|
45
|
+
readonly mutateAsync: (variables: TVariables) => Promise<TValue>;
|
|
46
|
+
readonly cancel: () => void;
|
|
47
|
+
readonly reset: () => void;
|
|
48
|
+
};
|
|
49
|
+
import type { ReactiveQuerySource } from "./core.js";
|
|
50
|
+
import type { ReactiveQueryOptions } from "./core.js";
|
|
51
|
+
import type { ReactiveQueryState } from "./core.js";
|
|
52
|
+
import type { ReactiveMutationOptions } from "./core.js";
|
|
53
|
+
import { ReactiveMutationObserver } from "./core.js";
|
|
54
|
+
import type { ReactiveMutationState } from "./core.js";
|