@supacloud/js 0.37.0 → 0.39.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/PROCEDURES.md +100 -0
- package/QUERY.md +173 -0
- package/README.md +25 -0
- package/dist/api-fetch.d.cts +2 -0
- package/dist/api-fetch.d.mts +2 -0
- package/dist/index.cjs +191 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +191 -1
- package/dist/procedure-client.d.cts +21 -0
- package/dist/procedure-client.d.mts +21 -0
- package/dist/procedure-error.d.cts +21 -0
- package/dist/procedure-error.d.mts +21 -0
- package/dist/query.cjs +140 -0
- package/dist/query.d.cts +62 -0
- package/dist/query.d.mts +62 -0
- package/dist/query.mjs +100 -0
- package/package.json +27 -5
package/dist/index.mjs
CHANGED
|
@@ -2411,6 +2411,10 @@ function createSupaCloudArtifactFetch(options = {}) {
|
|
|
2411
2411
|
return createBoundedRpcFetch(artifactPath2, () => new Error("Artifact HTTP response could not be validated"), options.fetch);
|
|
2412
2412
|
}
|
|
2413
2413
|
// src/api-fetch.ts
|
|
2414
|
+
var invocationErrors = new WeakMap;
|
|
2415
|
+
function getSupaCloudApiFetchError(response) {
|
|
2416
|
+
return invocationErrors.get(response);
|
|
2417
|
+
}
|
|
2414
2418
|
function isRecord4(value) {
|
|
2415
2419
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2416
2420
|
}
|
|
@@ -2491,6 +2495,7 @@ function createSupaCloudApiFetch(options) {
|
|
|
2491
2495
|
delete headers[key];
|
|
2492
2496
|
}
|
|
2493
2497
|
}
|
|
2498
|
+
request.signal.throwIfAborted();
|
|
2494
2499
|
const invocation = await invoke(`${functionName}${url.pathname}${url.search}`, {
|
|
2495
2500
|
...body === undefined ? {} : { body },
|
|
2496
2501
|
...Object.keys(headers).length === 0 ? {} : { headers },
|
|
@@ -2498,8 +2503,10 @@ function createSupaCloudApiFetch(options) {
|
|
|
2498
2503
|
signal: request.signal
|
|
2499
2504
|
});
|
|
2500
2505
|
if (invocation.error !== null && invocation.error !== undefined) {
|
|
2501
|
-
if (invocation.response instanceof Response)
|
|
2506
|
+
if (invocation.response instanceof Response) {
|
|
2507
|
+
invocationErrors.set(invocation.response, invocation.error);
|
|
2502
2508
|
return invocation.response;
|
|
2509
|
+
}
|
|
2503
2510
|
if (invocation.error instanceof Error)
|
|
2504
2511
|
throw invocation.error;
|
|
2505
2512
|
throw new Error(errorMessage(invocation.error));
|
|
@@ -2507,6 +2514,187 @@ function createSupaCloudApiFetch(options) {
|
|
|
2507
2514
|
return responseFromData(invocation.data, invocation.response instanceof Response ? invocation.response : undefined);
|
|
2508
2515
|
};
|
|
2509
2516
|
}
|
|
2517
|
+
// src/procedure-error.ts
|
|
2518
|
+
function procedureErrorIdentifier(value) {
|
|
2519
|
+
return typeof value === "string" && /^[A-Za-z0-9._:-]{1,256}$/.test(value) ? value : undefined;
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
class SupaCloudProcedureError extends Error {
|
|
2523
|
+
code;
|
|
2524
|
+
status;
|
|
2525
|
+
requestId;
|
|
2526
|
+
details;
|
|
2527
|
+
response;
|
|
2528
|
+
cause;
|
|
2529
|
+
constructor(message, options) {
|
|
2530
|
+
super(message, { cause: options.cause });
|
|
2531
|
+
this.name = "SupaCloudProcedureError";
|
|
2532
|
+
this.code = options.code;
|
|
2533
|
+
this.status = options.status ?? null;
|
|
2534
|
+
this.requestId = procedureErrorIdentifier(options.requestId) ?? null;
|
|
2535
|
+
this.details = options.details;
|
|
2536
|
+
this.response = options.response;
|
|
2537
|
+
this.cause = options.cause;
|
|
2538
|
+
Object.defineProperty(this, "response", { enumerable: false });
|
|
2539
|
+
Object.defineProperty(this, "cause", { enumerable: false });
|
|
2540
|
+
Object.defineProperty(this, "details", { enumerable: false });
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
// src/procedure-client.ts
|
|
2545
|
+
function isRecord5(value) {
|
|
2546
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2547
|
+
}
|
|
2548
|
+
function asGeneratedError(value) {
|
|
2549
|
+
if (!isRecord5(value))
|
|
2550
|
+
return;
|
|
2551
|
+
if (value.name !== "ApiClientError" || typeof value.code !== "string" || typeof value.status !== "number")
|
|
2552
|
+
return;
|
|
2553
|
+
return { code: value.code, status: value.status, requestId: value.requestId, response: value.response };
|
|
2554
|
+
}
|
|
2555
|
+
async function readErrorBody(response) {
|
|
2556
|
+
if (response === undefined || response.ok || response.bodyUsed || !response.body)
|
|
2557
|
+
return;
|
|
2558
|
+
const type = response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase();
|
|
2559
|
+
if (type !== "application/json" && !type?.endsWith("+json"))
|
|
2560
|
+
return;
|
|
2561
|
+
const maxBytes = 64 * 1024;
|
|
2562
|
+
const length = response.headers.get("content-length");
|
|
2563
|
+
if (length !== null && (!/^\d+$/.test(length) || Number(length) > maxBytes))
|
|
2564
|
+
return;
|
|
2565
|
+
let reader;
|
|
2566
|
+
let timer;
|
|
2567
|
+
let finished = false;
|
|
2568
|
+
try {
|
|
2569
|
+
reader = response.clone().body?.getReader();
|
|
2570
|
+
if (!reader)
|
|
2571
|
+
return;
|
|
2572
|
+
const current = reader;
|
|
2573
|
+
const read = async () => {
|
|
2574
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
2575
|
+
let bytes = 0;
|
|
2576
|
+
let text = "";
|
|
2577
|
+
while (!finished) {
|
|
2578
|
+
const chunk = await current.read();
|
|
2579
|
+
if (finished)
|
|
2580
|
+
return;
|
|
2581
|
+
if (chunk.done) {
|
|
2582
|
+
const body = JSON.parse(text + decoder.decode());
|
|
2583
|
+
return isRecord5(body) ? body : undefined;
|
|
2584
|
+
}
|
|
2585
|
+
bytes += chunk.value.byteLength;
|
|
2586
|
+
if (bytes > maxBytes)
|
|
2587
|
+
return;
|
|
2588
|
+
text += decoder.decode(chunk.value, { stream: true });
|
|
2589
|
+
}
|
|
2590
|
+
return;
|
|
2591
|
+
};
|
|
2592
|
+
return await Promise.race([
|
|
2593
|
+
read(),
|
|
2594
|
+
new Promise((resolve) => {
|
|
2595
|
+
timer = setTimeout(() => resolve(undefined), 250);
|
|
2596
|
+
})
|
|
2597
|
+
]);
|
|
2598
|
+
} catch {
|
|
2599
|
+
return;
|
|
2600
|
+
} finally {
|
|
2601
|
+
finished = true;
|
|
2602
|
+
clearTimeout(timer);
|
|
2603
|
+
reader?.cancel().catch(() => {});
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
function errorName(error) {
|
|
2607
|
+
return isRecord5(error) && typeof error.name === "string" ? error.name : undefined;
|
|
2608
|
+
}
|
|
2609
|
+
async function normalizeProcedureError(error, transport = false) {
|
|
2610
|
+
if (error instanceof SupaCloudProcedureError)
|
|
2611
|
+
return error;
|
|
2612
|
+
if (errorName(error) === "AbortError")
|
|
2613
|
+
return error;
|
|
2614
|
+
if (errorName(error) === "FunctionsFetchError" && isRecord5(error) && errorName(error.context) === "AbortError")
|
|
2615
|
+
return error.context;
|
|
2616
|
+
const generated = asGeneratedError(error);
|
|
2617
|
+
const response = generated?.response instanceof Response ? generated.response : isRecord5(error) && error.context instanceof Response ? error.context : undefined;
|
|
2618
|
+
const original = response === undefined ? undefined : getSupaCloudApiFetchError(response);
|
|
2619
|
+
const cause = original ?? error;
|
|
2620
|
+
const responseBody = await readErrorBody(response);
|
|
2621
|
+
const name = errorName(cause);
|
|
2622
|
+
const message = error instanceof Error ? error.message : "SupaCloud procedure failed";
|
|
2623
|
+
const fallback = name === "FunctionsFetchError" ? "SUPACLOUD_TRANSPORT_ERROR" : name === "FunctionsRelayError" ? "SUPACLOUD_FUNCTIONS_ERROR" : name === "FunctionsHttpError" ? "API_HTTP_ERROR" : error instanceof SyntaxError ? "API_RESPONSE_INVALID" : transport ? "SUPACLOUD_TRANSPORT_ERROR" : "SUPACLOUD_CLIENT_ERROR";
|
|
2624
|
+
const code = name === "FunctionsRelayError" ? fallback : generated?.code ?? fallback;
|
|
2625
|
+
return new SupaCloudProcedureError(message, {
|
|
2626
|
+
code,
|
|
2627
|
+
status: generated?.status ?? response?.status ?? null,
|
|
2628
|
+
requestId: procedureErrorIdentifier(generated?.requestId) ?? procedureErrorIdentifier(response?.headers.get("x-request-id")) ?? procedureErrorIdentifier(responseBody?.requestId) ?? null,
|
|
2629
|
+
details: responseBody,
|
|
2630
|
+
response,
|
|
2631
|
+
cause
|
|
2632
|
+
});
|
|
2633
|
+
}
|
|
2634
|
+
function validateProcedureExecution(procedure, args) {
|
|
2635
|
+
const metadata = Reflect.get(procedure, "__supacloudProcedure");
|
|
2636
|
+
if (!isRecord5(metadata))
|
|
2637
|
+
return;
|
|
2638
|
+
const [first, second] = args;
|
|
2639
|
+
const execution = second ?? (isRecord5(first) && (Object.hasOwn(first, "idempotencyKey") || Object.hasOwn(first, "signal")) ? first : undefined);
|
|
2640
|
+
const key = isRecord5(execution) ? execution.idempotencyKey : undefined;
|
|
2641
|
+
if (metadata.idempotency === "required" && key === undefined || key !== undefined && (typeof key !== "string" || !/^[A-Za-z0-9._:-]{1,512}$/.test(key))) {
|
|
2642
|
+
throw new SupaCloudProcedureError("This procedure requires a valid idempotencyKey", {
|
|
2643
|
+
code: "SUPACLOUD_EXECUTION_ERROR"
|
|
2644
|
+
});
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
function wrapGeneratedClient(client) {
|
|
2648
|
+
const cache = new WeakMap;
|
|
2649
|
+
const wrap = (value, level) => {
|
|
2650
|
+
const cached = cache.get(value);
|
|
2651
|
+
if (cached !== undefined)
|
|
2652
|
+
return cached;
|
|
2653
|
+
const proxy = new Proxy(value, {
|
|
2654
|
+
async apply(current, thisArg, args) {
|
|
2655
|
+
try {
|
|
2656
|
+
validateProcedureExecution(current, args);
|
|
2657
|
+
return await Reflect.apply(current, thisArg, args);
|
|
2658
|
+
} catch (failure) {
|
|
2659
|
+
throw await normalizeProcedureError(failure);
|
|
2660
|
+
}
|
|
2661
|
+
},
|
|
2662
|
+
get(current, key, receiver) {
|
|
2663
|
+
const member = Reflect.get(current, key, receiver);
|
|
2664
|
+
if (!Object.hasOwn(current, key))
|
|
2665
|
+
return member;
|
|
2666
|
+
if (typeof member === "function" && (level === "controller" || level === "client" && key === "request" || level === "call" && (key === "query" || key === "mutate"))) {
|
|
2667
|
+
return wrap(member, "call");
|
|
2668
|
+
}
|
|
2669
|
+
if (level === "client" && isRecord5(member) && Object.getPrototypeOf(member) === Object.prototype) {
|
|
2670
|
+
return wrap(member, "controller");
|
|
2671
|
+
}
|
|
2672
|
+
return member;
|
|
2673
|
+
}
|
|
2674
|
+
});
|
|
2675
|
+
cache.set(value, proxy);
|
|
2676
|
+
return proxy;
|
|
2677
|
+
};
|
|
2678
|
+
return wrap(client, "client");
|
|
2679
|
+
}
|
|
2680
|
+
function createSupaCloudProcedureClient(options) {
|
|
2681
|
+
const transport = createSupaCloudApiFetch({
|
|
2682
|
+
supabase: options.supabase,
|
|
2683
|
+
functionName: options.functionName
|
|
2684
|
+
});
|
|
2685
|
+
const fetch2 = async (input, init) => {
|
|
2686
|
+
try {
|
|
2687
|
+
const response = await transport(input, init);
|
|
2688
|
+
const error = getSupaCloudApiFetchError(response);
|
|
2689
|
+
if (error !== undefined && (response.ok || errorName(error) === "FunctionsRelayError"))
|
|
2690
|
+
throw error;
|
|
2691
|
+
return response;
|
|
2692
|
+
} catch (error) {
|
|
2693
|
+
throw await normalizeProcedureError(error, true);
|
|
2694
|
+
}
|
|
2695
|
+
};
|
|
2696
|
+
return wrapGeneratedClient(options.generated({ fetch: fetch2 }));
|
|
2697
|
+
}
|
|
2510
2698
|
|
|
2511
2699
|
// src/index.ts
|
|
2512
2700
|
class SupaCloudTaskSubmitError extends Error {
|
|
@@ -3763,6 +3951,7 @@ export {
|
|
|
3763
3951
|
SupaCloudOAuthClientsClient,
|
|
3764
3952
|
SupaCloudOAuthServerClient,
|
|
3765
3953
|
SupaCloudOAuthServerError,
|
|
3954
|
+
SupaCloudProcedureError,
|
|
3766
3955
|
SupaCloudQueueError,
|
|
3767
3956
|
SupaCloudTaskAuthenticationError,
|
|
3768
3957
|
SupaCloudTaskDecoderError as SupaCloudTaskDecodeError,
|
|
@@ -3785,6 +3974,7 @@ export {
|
|
|
3785
3974
|
createSupaCloudClient,
|
|
3786
3975
|
createSupaCloudCommandFetch,
|
|
3787
3976
|
createSupaCloudOAuthFetch,
|
|
3977
|
+
createSupaCloudProcedureClient,
|
|
3788
3978
|
createSupaCloudTaskFetch,
|
|
3789
3979
|
createSupaCloudWorkflowFetch
|
|
3790
3980
|
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { SupabaseClient } from "./supabase-types.cjs";
|
|
2
|
+
import type { FetchTransport } from "./bounded-rpc-fetch.cjs";
|
|
3
|
+
export { SupaCloudProcedureError, type SupaCloudProcedureErrorCode, type SupaCloudProcedureErrorOptions, } from "./procedure-error.cjs";
|
|
4
|
+
export interface SupaCloudGeneratedClientConfig {
|
|
5
|
+
fetch: FetchTransport;
|
|
6
|
+
}
|
|
7
|
+
export type SupaCloudGeneratedClientFactory<TClient extends object = object> = (config: SupaCloudGeneratedClientConfig) => TClient;
|
|
8
|
+
export interface SupaCloudProcedureClientOptions<TClient extends SupabaseClient, TFactory extends SupaCloudGeneratedClientFactory> {
|
|
9
|
+
/** The existing user-scoped Supabase client; it owns auth and session refresh. */
|
|
10
|
+
supabase: TClient;
|
|
11
|
+
/** The deployed Edge Function name hosting the generated application routes. */
|
|
12
|
+
functionName: string;
|
|
13
|
+
/** The generated createApiClient factory for the application contract. */
|
|
14
|
+
generated: TFactory;
|
|
15
|
+
}
|
|
16
|
+
export type SupaCloudProcedureClient<TFactory extends SupaCloudGeneratedClientFactory> = ReturnType<TFactory>;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a typed procedure facade over a compiler-generated client while
|
|
19
|
+
* reusing Supabase JS for auth, refresh and Functions transport.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createSupaCloudProcedureClient<TFactory extends SupaCloudGeneratedClientFactory, TClient extends SupabaseClient = SupabaseClient>(options: SupaCloudProcedureClientOptions<TClient, TFactory>): SupaCloudProcedureClient<TFactory>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { SupabaseClient } from "./supabase-types.mjs";
|
|
2
|
+
import type { FetchTransport } from "./bounded-rpc-fetch.mjs";
|
|
3
|
+
export { SupaCloudProcedureError, type SupaCloudProcedureErrorCode, type SupaCloudProcedureErrorOptions, } from "./procedure-error.mjs";
|
|
4
|
+
export interface SupaCloudGeneratedClientConfig {
|
|
5
|
+
fetch: FetchTransport;
|
|
6
|
+
}
|
|
7
|
+
export type SupaCloudGeneratedClientFactory<TClient extends object = object> = (config: SupaCloudGeneratedClientConfig) => TClient;
|
|
8
|
+
export interface SupaCloudProcedureClientOptions<TClient extends SupabaseClient, TFactory extends SupaCloudGeneratedClientFactory> {
|
|
9
|
+
/** The existing user-scoped Supabase client; it owns auth and session refresh. */
|
|
10
|
+
supabase: TClient;
|
|
11
|
+
/** The deployed Edge Function name hosting the generated application routes. */
|
|
12
|
+
functionName: string;
|
|
13
|
+
/** The generated createApiClient factory for the application contract. */
|
|
14
|
+
generated: TFactory;
|
|
15
|
+
}
|
|
16
|
+
export type SupaCloudProcedureClient<TFactory extends SupaCloudGeneratedClientFactory> = ReturnType<TFactory>;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a typed procedure facade over a compiler-generated client while
|
|
19
|
+
* reusing Supabase JS for auth, refresh and Functions transport.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createSupaCloudProcedureClient<TFactory extends SupaCloudGeneratedClientFactory, TClient extends SupabaseClient = SupabaseClient>(options: SupaCloudProcedureClientOptions<TClient, TFactory>): SupaCloudProcedureClient<TFactory>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type SupaCloudProcedureErrorCode = "API_HTTP_ERROR" | "API_RESPONSE_INVALID" | "API_RESPONSE_UNDECLARED" | "SUPACLOUD_EXECUTION_ERROR" | "SUPACLOUD_FUNCTIONS_ERROR" | "SUPACLOUD_TRANSPORT_ERROR" | "SUPACLOUD_CLIENT_ERROR";
|
|
2
|
+
export interface SupaCloudProcedureErrorOptions {
|
|
3
|
+
code: string;
|
|
4
|
+
status?: number | null;
|
|
5
|
+
requestId?: string | null;
|
|
6
|
+
details?: unknown;
|
|
7
|
+
response?: Response | undefined;
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
}
|
|
10
|
+
/** Internal validation shared by error construction and response inspection. */
|
|
11
|
+
export declare function procedureErrorIdentifier(value: unknown): string | undefined;
|
|
12
|
+
/** Failure metadata is not proof that a server-side mutation rolled back. */
|
|
13
|
+
export declare class SupaCloudProcedureError extends Error {
|
|
14
|
+
readonly code: string;
|
|
15
|
+
readonly status: number | null;
|
|
16
|
+
readonly requestId: string | null;
|
|
17
|
+
readonly details: unknown;
|
|
18
|
+
readonly response: Response | undefined;
|
|
19
|
+
readonly cause: unknown;
|
|
20
|
+
constructor(message: string, options: SupaCloudProcedureErrorOptions);
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type SupaCloudProcedureErrorCode = "API_HTTP_ERROR" | "API_RESPONSE_INVALID" | "API_RESPONSE_UNDECLARED" | "SUPACLOUD_EXECUTION_ERROR" | "SUPACLOUD_FUNCTIONS_ERROR" | "SUPACLOUD_TRANSPORT_ERROR" | "SUPACLOUD_CLIENT_ERROR";
|
|
2
|
+
export interface SupaCloudProcedureErrorOptions {
|
|
3
|
+
code: string;
|
|
4
|
+
status?: number | null;
|
|
5
|
+
requestId?: string | null;
|
|
6
|
+
details?: unknown;
|
|
7
|
+
response?: Response | undefined;
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
}
|
|
10
|
+
/** Internal validation shared by error construction and response inspection. */
|
|
11
|
+
export declare function procedureErrorIdentifier(value: unknown): string | undefined;
|
|
12
|
+
/** Failure metadata is not proof that a server-side mutation rolled back. */
|
|
13
|
+
export declare class SupaCloudProcedureError extends Error {
|
|
14
|
+
readonly code: string;
|
|
15
|
+
readonly status: number | null;
|
|
16
|
+
readonly requestId: string | null;
|
|
17
|
+
readonly details: unknown;
|
|
18
|
+
readonly response: Response | undefined;
|
|
19
|
+
readonly cause: unknown;
|
|
20
|
+
constructor(message: string, options: SupaCloudProcedureErrorOptions);
|
|
21
|
+
}
|
package/dist/query.cjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/query.ts
|
|
40
|
+
var exports_query = {};
|
|
41
|
+
__export(exports_query, {
|
|
42
|
+
createSupaCloudQueryAdapter: () => createSupaCloudQueryAdapter
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(exports_query);
|
|
45
|
+
function metadataOf(procedure, kind) {
|
|
46
|
+
const metadata = procedure.__supacloudProcedure;
|
|
47
|
+
if (!metadata || metadata.kind !== kind || typeof metadata.method !== "string" || typeof metadata.path !== "string" || !["none", "required"].includes(metadata.idempotency)) {
|
|
48
|
+
throw new TypeError(`Expected a generated ${kind} procedure; regenerate the application client`);
|
|
49
|
+
}
|
|
50
|
+
return metadata;
|
|
51
|
+
}
|
|
52
|
+
function snapshot(value, ancestors = new Set) {
|
|
53
|
+
if (value === undefined || value === null || typeof value === "string" || typeof value === "boolean")
|
|
54
|
+
return value;
|
|
55
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
56
|
+
return value;
|
|
57
|
+
if (typeof value !== "object" || ancestors.has(value)) {
|
|
58
|
+
throw new TypeError("Procedure input must be acyclic JSON data");
|
|
59
|
+
}
|
|
60
|
+
const prototype = Object.getPrototypeOf(value);
|
|
61
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
|
|
62
|
+
throw new TypeError("Procedure input must be JSON data");
|
|
63
|
+
}
|
|
64
|
+
ancestors.add(value);
|
|
65
|
+
try {
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
return Object.freeze(value.map((item) => snapshot(item, ancestors) ?? null));
|
|
68
|
+
}
|
|
69
|
+
return Object.freeze(Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined).map(([key, item]) => [key, snapshot(item, ancestors)])));
|
|
70
|
+
} finally {
|
|
71
|
+
ancestors.delete(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function callProcedure(procedure, input, execution) {
|
|
75
|
+
const call = procedure;
|
|
76
|
+
const signal = execution.signal;
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const onAbort = () => reject(signal?.reason ?? new DOMException("Procedure cancelled", "AbortError"));
|
|
79
|
+
if (signal?.aborted) {
|
|
80
|
+
onAbort();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
84
|
+
Promise.resolve().then(() => {
|
|
85
|
+
signal?.throwIfAborted();
|
|
86
|
+
return call(input, execution);
|
|
87
|
+
}).then(resolve, reject).finally(() => signal?.removeEventListener("abort", onAbort));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function createSupaCloudQueryAdapter({
|
|
91
|
+
keyPrefix
|
|
92
|
+
}) {
|
|
93
|
+
if (!Array.isArray(keyPrefix) || keyPrefix.length === 0 || keyPrefix.some((part) => typeof part !== "string" || !part.trim())) {
|
|
94
|
+
throw new TypeError("keyPrefix must contain nonempty identity values");
|
|
95
|
+
}
|
|
96
|
+
const prefix = Object.freeze(["supacloud", ...keyPrefix]);
|
|
97
|
+
function procedureKey(procedure, kind) {
|
|
98
|
+
const metadata = metadataOf(procedure, kind);
|
|
99
|
+
return Object.freeze([...prefix, kind, metadata.method, metadata.path]);
|
|
100
|
+
}
|
|
101
|
+
function queryKey(procedure, ...args) {
|
|
102
|
+
if (args.length === 0)
|
|
103
|
+
return procedureKey(procedure, "query");
|
|
104
|
+
return Object.freeze([
|
|
105
|
+
...procedureKey(procedure, "query"),
|
|
106
|
+
snapshot(args[0] ?? {})
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
function queryOptions(procedure, input, options) {
|
|
110
|
+
const capturedInput = snapshot(input ?? {});
|
|
111
|
+
return {
|
|
112
|
+
...options,
|
|
113
|
+
queryKey: Object.freeze([
|
|
114
|
+
...procedureKey(procedure, "query"),
|
|
115
|
+
capturedInput
|
|
116
|
+
]),
|
|
117
|
+
queryFn: async ({ signal }) => {
|
|
118
|
+
const result = await callProcedure(procedure, capturedInput, { signal });
|
|
119
|
+
return result === undefined ? null : result;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function mutationOptions(procedure, options) {
|
|
124
|
+
return {
|
|
125
|
+
...options,
|
|
126
|
+
mutationKey: procedureKey(procedure, "mutation"),
|
|
127
|
+
retry: false,
|
|
128
|
+
mutationFn: async (variables) => {
|
|
129
|
+
const execution = variables.execution ?? {};
|
|
130
|
+
return callProcedure(procedure, variables.input, execution);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
queryKey,
|
|
136
|
+
queryOptions,
|
|
137
|
+
mutationKey: (procedure) => procedureKey(procedure, "mutation"),
|
|
138
|
+
mutationOptions
|
|
139
|
+
};
|
|
140
|
+
}
|
package/dist/query.d.cts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { DataTag, DefaultError, MutationKey, MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions } from "@tanstack/query-core";
|
|
2
|
+
type Procedure<Kind extends "query" | "mutation"> = {
|
|
3
|
+
readonly __supacloudProcedure: {
|
|
4
|
+
readonly key: string;
|
|
5
|
+
readonly method: string;
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly kind: Kind;
|
|
8
|
+
readonly idempotency: "none" | "required";
|
|
9
|
+
};
|
|
10
|
+
readonly __supacloudInput?: unknown;
|
|
11
|
+
};
|
|
12
|
+
export type SupaCloudProcedureInput<TProcedure> = TProcedure extends {
|
|
13
|
+
readonly __supacloudInput?: infer TInput;
|
|
14
|
+
} ? TInput : never;
|
|
15
|
+
export type SupaCloudProcedureResult<TProcedure> = TProcedure extends (...args: infer _Args) => Promise<infer TResult> ? TResult : unknown;
|
|
16
|
+
type ProcedureInput<TProcedure> = {} extends SupaCloudProcedureInput<TProcedure> ? SupaCloudProcedureInput<TProcedure> | undefined : SupaCloudProcedureInput<TProcedure>;
|
|
17
|
+
type QueryResult<TProcedure> = Exclude<SupaCloudProcedureResult<TProcedure>, undefined | void> | (undefined extends SupaCloudProcedureResult<TProcedure> ? null : never);
|
|
18
|
+
type TaggedQueryKey<TProcedure, TError = DefaultError> = DataTag<QueryKey, QueryResult<TProcedure>, TError>;
|
|
19
|
+
export interface SupaCloudProcedureExecutionOptions {
|
|
20
|
+
idempotencyKey?: string;
|
|
21
|
+
/** Pass an existing CommandAttempt signal to bind a mutation to component lifetime. */
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
export type SupaCloudMutationVariables<TProcedure extends Procedure<"mutation">> = ({} extends SupaCloudProcedureInput<TProcedure> ? {
|
|
25
|
+
input?: SupaCloudProcedureInput<TProcedure>;
|
|
26
|
+
} : {
|
|
27
|
+
input: SupaCloudProcedureInput<TProcedure>;
|
|
28
|
+
}) & (TProcedure["__supacloudProcedure"]["idempotency"] extends "required" ? {
|
|
29
|
+
execution: SupaCloudProcedureExecutionOptions & {
|
|
30
|
+
idempotencyKey: string;
|
|
31
|
+
};
|
|
32
|
+
} : {
|
|
33
|
+
execution?: SupaCloudProcedureExecutionOptions;
|
|
34
|
+
});
|
|
35
|
+
export type SupaCloudQueryOptions<TProcedure extends Procedure<"query">, TError = DefaultError, TData = QueryResult<TProcedure>> = Omit<QueryObserverOptions<QueryResult<TProcedure>, TError, TData, QueryResult<TProcedure>, QueryKey>, "queryKey" | "queryFn" | "queryHash" | "queryKeyHashFn">;
|
|
36
|
+
export type SupaCloudMutationOptions<TProcedure extends Procedure<"mutation">, TError = DefaultError, TContext = unknown> = Omit<MutationObserverOptions<SupaCloudProcedureResult<TProcedure>, TError, SupaCloudMutationVariables<TProcedure>, TContext>, "mutationKey" | "mutationFn" | "retry">;
|
|
37
|
+
export type SupaCloudQueryOptionsResult<TProcedure extends Procedure<"query">, TError = DefaultError, TData = QueryResult<TProcedure>> = SupaCloudQueryOptions<TProcedure, TError, TData> & {
|
|
38
|
+
queryKey: TaggedQueryKey<TProcedure, TError>;
|
|
39
|
+
queryFn(context: QueryFunctionContext<QueryKey>): Promise<QueryResult<TProcedure>>;
|
|
40
|
+
};
|
|
41
|
+
export type SupaCloudMutationOptionsResult<TProcedure extends Procedure<"mutation">, TError = DefaultError, TContext = unknown> = SupaCloudMutationOptions<TProcedure, TError, TContext> & {
|
|
42
|
+
mutationKey: MutationKey;
|
|
43
|
+
mutationFn(variables: SupaCloudMutationVariables<TProcedure>): Promise<SupaCloudProcedureResult<TProcedure>>;
|
|
44
|
+
retry: false;
|
|
45
|
+
};
|
|
46
|
+
export interface SupaCloudQueryAdapterOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Include project, tenant and actor identity. Recreate the adapter when any
|
|
49
|
+
* of those identities change so old cache entries cannot be reused.
|
|
50
|
+
*/
|
|
51
|
+
keyPrefix: readonly [string, ...string[]];
|
|
52
|
+
}
|
|
53
|
+
export declare function createSupaCloudQueryAdapter({ keyPrefix, }: SupaCloudQueryAdapterOptions): {
|
|
54
|
+
queryKey: {
|
|
55
|
+
<TProcedure extends Procedure<"query">>(procedure: TProcedure): QueryKey;
|
|
56
|
+
<TProcedure extends Procedure<"query">>(procedure: TProcedure, input: NoInfer<ProcedureInput<TProcedure>>): TaggedQueryKey<TProcedure>;
|
|
57
|
+
};
|
|
58
|
+
queryOptions: <TProcedure extends Procedure<"query">, TError = Error, TData = QueryResult<TProcedure>>(procedure: TProcedure, input: NoInfer<ProcedureInput<TProcedure>>, options?: SupaCloudQueryOptions<NoInfer<TProcedure>, TError, TData>) => SupaCloudQueryOptionsResult<TProcedure, TError, TData>;
|
|
59
|
+
mutationKey: (procedure: Procedure<"mutation">) => readonly unknown[];
|
|
60
|
+
mutationOptions: <TProcedure extends Procedure<"mutation">, TError = Error, TContext = unknown>(procedure: TProcedure, options?: SupaCloudMutationOptions<NoInfer<TProcedure>, TError, TContext>) => SupaCloudMutationOptionsResult<TProcedure, TError, TContext>;
|
|
61
|
+
};
|
|
62
|
+
export {};
|
package/dist/query.d.mts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { DataTag, DefaultError, MutationKey, MutationObserverOptions, QueryFunctionContext, QueryKey, QueryObserverOptions } from "@tanstack/query-core";
|
|
2
|
+
type Procedure<Kind extends "query" | "mutation"> = {
|
|
3
|
+
readonly __supacloudProcedure: {
|
|
4
|
+
readonly key: string;
|
|
5
|
+
readonly method: string;
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly kind: Kind;
|
|
8
|
+
readonly idempotency: "none" | "required";
|
|
9
|
+
};
|
|
10
|
+
readonly __supacloudInput?: unknown;
|
|
11
|
+
};
|
|
12
|
+
export type SupaCloudProcedureInput<TProcedure> = TProcedure extends {
|
|
13
|
+
readonly __supacloudInput?: infer TInput;
|
|
14
|
+
} ? TInput : never;
|
|
15
|
+
export type SupaCloudProcedureResult<TProcedure> = TProcedure extends (...args: infer _Args) => Promise<infer TResult> ? TResult : unknown;
|
|
16
|
+
type ProcedureInput<TProcedure> = {} extends SupaCloudProcedureInput<TProcedure> ? SupaCloudProcedureInput<TProcedure> | undefined : SupaCloudProcedureInput<TProcedure>;
|
|
17
|
+
type QueryResult<TProcedure> = Exclude<SupaCloudProcedureResult<TProcedure>, undefined | void> | (undefined extends SupaCloudProcedureResult<TProcedure> ? null : never);
|
|
18
|
+
type TaggedQueryKey<TProcedure, TError = DefaultError> = DataTag<QueryKey, QueryResult<TProcedure>, TError>;
|
|
19
|
+
export interface SupaCloudProcedureExecutionOptions {
|
|
20
|
+
idempotencyKey?: string;
|
|
21
|
+
/** Pass an existing CommandAttempt signal to bind a mutation to component lifetime. */
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
export type SupaCloudMutationVariables<TProcedure extends Procedure<"mutation">> = ({} extends SupaCloudProcedureInput<TProcedure> ? {
|
|
25
|
+
input?: SupaCloudProcedureInput<TProcedure>;
|
|
26
|
+
} : {
|
|
27
|
+
input: SupaCloudProcedureInput<TProcedure>;
|
|
28
|
+
}) & (TProcedure["__supacloudProcedure"]["idempotency"] extends "required" ? {
|
|
29
|
+
execution: SupaCloudProcedureExecutionOptions & {
|
|
30
|
+
idempotencyKey: string;
|
|
31
|
+
};
|
|
32
|
+
} : {
|
|
33
|
+
execution?: SupaCloudProcedureExecutionOptions;
|
|
34
|
+
});
|
|
35
|
+
export type SupaCloudQueryOptions<TProcedure extends Procedure<"query">, TError = DefaultError, TData = QueryResult<TProcedure>> = Omit<QueryObserverOptions<QueryResult<TProcedure>, TError, TData, QueryResult<TProcedure>, QueryKey>, "queryKey" | "queryFn" | "queryHash" | "queryKeyHashFn">;
|
|
36
|
+
export type SupaCloudMutationOptions<TProcedure extends Procedure<"mutation">, TError = DefaultError, TContext = unknown> = Omit<MutationObserverOptions<SupaCloudProcedureResult<TProcedure>, TError, SupaCloudMutationVariables<TProcedure>, TContext>, "mutationKey" | "mutationFn" | "retry">;
|
|
37
|
+
export type SupaCloudQueryOptionsResult<TProcedure extends Procedure<"query">, TError = DefaultError, TData = QueryResult<TProcedure>> = SupaCloudQueryOptions<TProcedure, TError, TData> & {
|
|
38
|
+
queryKey: TaggedQueryKey<TProcedure, TError>;
|
|
39
|
+
queryFn(context: QueryFunctionContext<QueryKey>): Promise<QueryResult<TProcedure>>;
|
|
40
|
+
};
|
|
41
|
+
export type SupaCloudMutationOptionsResult<TProcedure extends Procedure<"mutation">, TError = DefaultError, TContext = unknown> = SupaCloudMutationOptions<TProcedure, TError, TContext> & {
|
|
42
|
+
mutationKey: MutationKey;
|
|
43
|
+
mutationFn(variables: SupaCloudMutationVariables<TProcedure>): Promise<SupaCloudProcedureResult<TProcedure>>;
|
|
44
|
+
retry: false;
|
|
45
|
+
};
|
|
46
|
+
export interface SupaCloudQueryAdapterOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Include project, tenant and actor identity. Recreate the adapter when any
|
|
49
|
+
* of those identities change so old cache entries cannot be reused.
|
|
50
|
+
*/
|
|
51
|
+
keyPrefix: readonly [string, ...string[]];
|
|
52
|
+
}
|
|
53
|
+
export declare function createSupaCloudQueryAdapter({ keyPrefix, }: SupaCloudQueryAdapterOptions): {
|
|
54
|
+
queryKey: {
|
|
55
|
+
<TProcedure extends Procedure<"query">>(procedure: TProcedure): QueryKey;
|
|
56
|
+
<TProcedure extends Procedure<"query">>(procedure: TProcedure, input: NoInfer<ProcedureInput<TProcedure>>): TaggedQueryKey<TProcedure>;
|
|
57
|
+
};
|
|
58
|
+
queryOptions: <TProcedure extends Procedure<"query">, TError = Error, TData = QueryResult<TProcedure>>(procedure: TProcedure, input: NoInfer<ProcedureInput<TProcedure>>, options?: SupaCloudQueryOptions<NoInfer<TProcedure>, TError, TData>) => SupaCloudQueryOptionsResult<TProcedure, TError, TData>;
|
|
59
|
+
mutationKey: (procedure: Procedure<"mutation">) => readonly unknown[];
|
|
60
|
+
mutationOptions: <TProcedure extends Procedure<"mutation">, TError = Error, TContext = unknown>(procedure: TProcedure, options?: SupaCloudMutationOptions<NoInfer<TProcedure>, TError, TContext>) => SupaCloudMutationOptionsResult<TProcedure, TError, TContext>;
|
|
61
|
+
};
|
|
62
|
+
export {};
|