@dads-framework/client 0.0.0-stage → 0.4.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.md +21 -0
- package/README.md +83 -2
- package/dist/client.tsbuildinfo +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/access-refusal.d.ts +8 -0
- package/dist/lib/access-refusal.d.ts.map +1 -0
- package/dist/lib/access-refusal.js +11 -0
- package/dist/lib/access-refusal.js.map +1 -0
- package/dist/lib/api-error.d.ts +86 -0
- package/dist/lib/api-error.d.ts.map +1 -0
- package/dist/lib/api-error.js +124 -0
- package/dist/lib/api-error.js.map +1 -0
- package/dist/lib/api-exchange-channel.d.ts +47 -0
- package/dist/lib/api-exchange-channel.d.ts.map +1 -0
- package/dist/lib/api-exchange-channel.js +43 -0
- package/dist/lib/api-exchange-channel.js.map +1 -0
- package/dist/lib/api-verbs.d.ts +115 -0
- package/dist/lib/api-verbs.d.ts.map +1 -0
- package/dist/lib/api-verbs.js +148 -0
- package/dist/lib/api-verbs.js.map +1 -0
- package/dist/lib/build-client-context.d.ts +44 -0
- package/dist/lib/build-client-context.d.ts.map +1 -0
- package/dist/lib/build-client-context.js +74 -0
- package/dist/lib/build-client-context.js.map +1 -0
- package/dist/lib/call-api.d.ts +77 -0
- package/dist/lib/call-api.d.ts.map +1 -0
- package/dist/lib/call-api.js +149 -0
- package/dist/lib/call-api.js.map +1 -0
- package/dist/lib/client-context.d.ts +32 -0
- package/dist/lib/client-context.d.ts.map +1 -0
- package/dist/lib/client-context.js +33 -0
- package/dist/lib/client-context.js.map +1 -0
- package/dist/lib/create-request-id.d.ts +14 -0
- package/dist/lib/create-request-id.d.ts.map +1 -0
- package/dist/lib/create-request-id.js +25 -0
- package/dist/lib/create-request-id.js.map +1 -0
- package/dist/lib/failure-presentation.d.ts +21 -0
- package/dist/lib/failure-presentation.d.ts.map +1 -0
- package/dist/lib/failure-presentation.js +9 -0
- package/dist/lib/failure-presentation.js.map +1 -0
- package/dist/lib/optimistic-cache.d.ts +84 -0
- package/dist/lib/optimistic-cache.d.ts.map +1 -0
- package/dist/lib/optimistic-cache.js +117 -0
- package/dist/lib/optimistic-cache.js.map +1 -0
- package/dist/lib/read-client-environment.d.ts +67 -0
- package/dist/lib/read-client-environment.d.ts.map +1 -0
- package/dist/lib/read-client-environment.js +85 -0
- package/dist/lib/read-client-environment.js.map +1 -0
- package/dist/lib/request-locale.d.ts +6 -0
- package/dist/lib/request-locale.d.ts.map +1 -0
- package/dist/lib/request-locale.js +31 -0
- package/dist/lib/request-locale.js.map +1 -0
- package/dist/lib/request-metadata.d.ts +22 -0
- package/dist/lib/request-metadata.d.ts.map +1 -0
- package/dist/lib/request-metadata.js +26 -0
- package/dist/lib/request-metadata.js.map +1 -0
- package/dist/lib/save-with-refresh.d.ts +13 -0
- package/dist/lib/save-with-refresh.d.ts.map +1 -0
- package/dist/lib/save-with-refresh.js +16 -0
- package/dist/lib/save-with-refresh.js.map +1 -0
- package/dist/lib/search-params.d.ts +11 -0
- package/dist/lib/search-params.d.ts.map +1 -0
- package/dist/lib/search-params.js +41 -0
- package/dist/lib/search-params.js.map +1 -0
- package/dist/lib/send-api-request.d.ts +24 -0
- package/dist/lib/send-api-request.d.ts.map +1 -0
- package/dist/lib/send-api-request.js +98 -0
- package/dist/lib/send-api-request.js.map +1 -0
- package/dist/lib/signed-url-upload.d.ts +18 -0
- package/dist/lib/signed-url-upload.d.ts.map +1 -0
- package/dist/lib/signed-url-upload.js +26 -0
- package/dist/lib/signed-url-upload.js.map +1 -0
- package/dist/lib/unauthorized-response-channel.d.ts +30 -0
- package/dist/lib/unauthorized-response-channel.d.ts.map +1 -0
- package/dist/lib/unauthorized-response-channel.js +38 -0
- package/dist/lib/unauthorized-response-channel.js.map +1 -0
- package/dist/telemetry/core/index.d.ts +3 -0
- package/dist/telemetry/core/index.d.ts.map +1 -0
- package/dist/telemetry/core/index.js +3 -0
- package/dist/telemetry/core/index.js.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.d.ts +13 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.d.ts.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.js +38 -0
- package/dist/telemetry/core/lib/client-telemetry-conformance.js.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry.d.ts +48 -0
- package/dist/telemetry/core/lib/client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/core/lib/client-telemetry.js +26 -0
- package/dist/telemetry/core/lib/client-telemetry.js.map +1 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.d.ts +20 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.js +23 -0
- package/dist/telemetry/core/lib/in-memory-client-telemetry.js.map +1 -0
- package/dist/telemetry/core/lib/none-client-telemetry.d.ts +16 -0
- package/dist/telemetry/core/lib/none-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/core/lib/none-client-telemetry.js +22 -0
- package/dist/telemetry/core/lib/none-client-telemetry.js.map +1 -0
- package/dist/telemetry/index.d.ts +19 -0
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +19 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/telemetry/lib/create-client-telemetry.d.ts +38 -0
- package/dist/telemetry/lib/create-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/create-client-telemetry.js +65 -0
- package/dist/telemetry/lib/create-client-telemetry.js.map +1 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.d.ts +24 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.js +56 -0
- package/dist/telemetry/lib/fan-out-client-telemetry.js.map +1 -0
- package/dist/telemetry/lib/install-global-error-reporting.d.ts +27 -0
- package/dist/telemetry/lib/install-global-error-reporting.d.ts.map +1 -0
- package/dist/telemetry/lib/install-global-error-reporting.js +43 -0
- package/dist/telemetry/lib/install-global-error-reporting.js.map +1 -0
- package/dist/telemetry/lib/local-client-telemetry.d.ts +44 -0
- package/dist/telemetry/lib/local-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/local-client-telemetry.js +120 -0
- package/dist/telemetry/lib/local-client-telemetry.js.map +1 -0
- package/dist/telemetry/lib/logic/describe-client-error.d.ts +24 -0
- package/dist/telemetry/lib/logic/describe-client-error.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/describe-client-error.js +124 -0
- package/dist/telemetry/lib/logic/describe-client-error.js.map +1 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.d.ts +16 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.js +34 -0
- package/dist/telemetry/lib/logic/is-chunk-load-failure.js.map +1 -0
- package/dist/telemetry/lib/logic/refused-read.d.ts +24 -0
- package/dist/telemetry/lib/logic/refused-read.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/refused-read.js +33 -0
- package/dist/telemetry/lib/logic/refused-read.js.map +1 -0
- package/dist/telemetry/lib/logic/sentry-configuration.d.ts +67 -0
- package/dist/telemetry/lib/logic/sentry-configuration.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/sentry-configuration.js +74 -0
- package/dist/telemetry/lib/logic/sentry-configuration.js.map +1 -0
- package/dist/telemetry/lib/logic/should-report-failure.d.ts +19 -0
- package/dist/telemetry/lib/logic/should-report-failure.d.ts.map +1 -0
- package/dist/telemetry/lib/logic/should-report-failure.js +41 -0
- package/dist/telemetry/lib/logic/should-report-failure.js.map +1 -0
- package/dist/telemetry/lib/optional-peer.d.ts +12 -0
- package/dist/telemetry/lib/optional-peer.d.ts.map +1 -0
- package/dist/telemetry/lib/optional-peer.js +37 -0
- package/dist/telemetry/lib/optional-peer.js.map +1 -0
- package/dist/telemetry/lib/sentry-client-telemetry.d.ts +74 -0
- package/dist/telemetry/lib/sentry-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/sentry-client-telemetry.js +189 -0
- package/dist/telemetry/lib/sentry-client-telemetry.js.map +1 -0
- package/dist/telemetry/testing.d.ts +3 -0
- package/dist/telemetry/testing.d.ts.map +1 -0
- package/dist/telemetry/testing.js +3 -0
- package/dist/telemetry/testing.js.map +1 -0
- package/package.json +58 -4
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a required field says when the browser would not say anything. The
|
|
3
|
+
* contract needs `build`, `timeZone`, and `locale` present, and "unknown" is
|
|
4
|
+
* more honest in a log than an empty string that reads like a bug.
|
|
5
|
+
*/
|
|
6
|
+
const UNKNOWN = "unknown";
|
|
7
|
+
const MAX_BUILD = 128;
|
|
8
|
+
const MAX_TIME_ZONE = 64;
|
|
9
|
+
const MAX_LOCALE = 32;
|
|
10
|
+
const MAX_ROUTE_ID = 256;
|
|
11
|
+
const MAX_CONNECTION = 32;
|
|
12
|
+
/**
|
|
13
|
+
* Shape one browser snapshot into the `x-client-context` payload.
|
|
14
|
+
*
|
|
15
|
+
* Pure, and deliberately forgiving: it clamps every string to the contract's
|
|
16
|
+
* length and rounds every count to a whole number so the server's strict
|
|
17
|
+
* schema accepts the result. The server drops the whole context on the first
|
|
18
|
+
* violation (`decodeClientContext` returns `null`), so one absurd `routeId`
|
|
19
|
+
* must not take the build stamp down with it. Absent fields stay `undefined`
|
|
20
|
+
* and disappear when the context is serialized for the header.
|
|
21
|
+
*/
|
|
22
|
+
export function buildClientContext(environment) {
|
|
23
|
+
return {
|
|
24
|
+
version: 1,
|
|
25
|
+
client: "web",
|
|
26
|
+
build: text(environment.build, MAX_BUILD) ?? UNKNOWN,
|
|
27
|
+
timeZone: text(environment.timeZone, MAX_TIME_ZONE) ?? UNKNOWN,
|
|
28
|
+
locale: text(environment.locale, MAX_LOCALE) ?? UNKNOWN,
|
|
29
|
+
routeId: text(environment.routeId, MAX_ROUTE_ID),
|
|
30
|
+
viewport: viewportOf(environment.viewport),
|
|
31
|
+
screen: screenOf(environment.screen),
|
|
32
|
+
touch: environment.touch,
|
|
33
|
+
online: environment.online,
|
|
34
|
+
connection: text(environment.connection, MAX_CONNECTION),
|
|
35
|
+
deviceMemory: finite(environment.deviceMemory),
|
|
36
|
+
hardwareConcurrency: count(environment.hardwareConcurrency),
|
|
37
|
+
tabAgeMs: count(environment.tabAgeMs),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** A trimmed, length-clamped string, or nothing when there was nothing to say. */
|
|
41
|
+
function text(value, max) {
|
|
42
|
+
if (value === undefined)
|
|
43
|
+
return undefined;
|
|
44
|
+
const trimmed = value.trim();
|
|
45
|
+
return trimmed.length === 0 ? undefined : trimmed.slice(0, max);
|
|
46
|
+
}
|
|
47
|
+
function finite(value) {
|
|
48
|
+
return value === undefined || !Number.isFinite(value) ? undefined : value;
|
|
49
|
+
}
|
|
50
|
+
/** A whole, non-negative number — the shape every count in the contract has. */
|
|
51
|
+
function count(value) {
|
|
52
|
+
const known = finite(value);
|
|
53
|
+
return known === undefined ? undefined : Math.max(0, Math.round(known));
|
|
54
|
+
}
|
|
55
|
+
function viewportOf(viewport) {
|
|
56
|
+
if (viewport === undefined)
|
|
57
|
+
return undefined;
|
|
58
|
+
const w = count(viewport.w);
|
|
59
|
+
const h = count(viewport.h);
|
|
60
|
+
const dpr = finite(viewport.dpr);
|
|
61
|
+
if (w === undefined || h === undefined || dpr === undefined)
|
|
62
|
+
return undefined;
|
|
63
|
+
return { w, h, dpr };
|
|
64
|
+
}
|
|
65
|
+
function screenOf(screen) {
|
|
66
|
+
if (screen === undefined)
|
|
67
|
+
return undefined;
|
|
68
|
+
const w = count(screen.w);
|
|
69
|
+
const h = count(screen.h);
|
|
70
|
+
if (w === undefined || h === undefined)
|
|
71
|
+
return undefined;
|
|
72
|
+
return { w, h };
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=build-client-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-client-context.js","sourceRoot":"","sources":["../../src/lib/build-client-context.ts"],"names":[],"mappings":"AA2BA;;;;GAIG;AACH,MAAM,OAAO,GAAG,SAAS,CAAC;AAE1B,MAAM,SAAS,GAAG,GAAG,CAAC;AACtB,MAAM,aAAa,GAAG,EAAE,CAAC;AACzB,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,YAAY,GAAG,GAAG,CAAC;AACzB,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAAC,WAAsC;IACvE,OAAO;QACL,OAAO,EAAE,CAAC;QACV,MAAM,EAAE,KAAK;QACb,KAAK,EAAE,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,OAAO;QACpD,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,aAAa,CAAC,IAAI,OAAO;QAC9D,MAAM,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,UAAU,CAAC,IAAI,OAAO;QACvD,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,YAAY,CAAC;QAChD,QAAQ,EAAE,UAAU,CAAC,WAAW,CAAC,QAAQ,CAAC;QAC1C,MAAM,EAAE,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC;QACpC,KAAK,EAAE,WAAW,CAAC,KAAK;QACxB,MAAM,EAAE,WAAW,CAAC,MAAM;QAC1B,UAAU,EAAE,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,cAAc,CAAC;QACxD,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC;QAC9C,mBAAmB,EAAE,KAAK,CAAC,WAAW,CAAC,mBAAmB,CAAC;QAC3D,QAAQ,EAAE,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC;KACtC,CAAC;AACJ,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AAC;AAED,SAAS,QAAQ,CAAC,MAA2C;IAC3D,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC1B,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACzD,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type AnyOperationContract, type PaginationMeta } from "@dads-framework/core/contract";
|
|
3
|
+
import { type ApiPage, type ApiPageWithMeta } from "./api-verbs.js";
|
|
4
|
+
import { type ApiRequestHeaders } from "./send-api-request.js";
|
|
5
|
+
/** Transport-level per-call options accepted by every operation call. */
|
|
6
|
+
export type CallApiOptions = {
|
|
7
|
+
readonly signal?: AbortSignal;
|
|
8
|
+
readonly headers?: ApiRequestHeaders;
|
|
9
|
+
};
|
|
10
|
+
/** Parsed `{ data, meta }` reply for GET/POST operations that declare `meta`. */
|
|
11
|
+
export type ApiDataWithMeta<TData, TMeta> = {
|
|
12
|
+
readonly data: TData;
|
|
13
|
+
readonly meta: TMeta;
|
|
14
|
+
};
|
|
15
|
+
type ParamsOf<Op> = Op extends {
|
|
16
|
+
readonly params: infer P;
|
|
17
|
+
} ? P : undefined;
|
|
18
|
+
type QueryOf<Op> = Op extends {
|
|
19
|
+
readonly query: infer Q;
|
|
20
|
+
} ? Q : undefined;
|
|
21
|
+
type BodyOf<Op> = Op extends {
|
|
22
|
+
readonly body: infer B;
|
|
23
|
+
} ? B : undefined;
|
|
24
|
+
type ParamsField<ParamsSchema> = [ParamsSchema] extends [z.ZodType] ? {
|
|
25
|
+
readonly params: z.input<ParamsSchema>;
|
|
26
|
+
} : Record<never, never>;
|
|
27
|
+
type QueryField<QuerySchema> = [QuerySchema] extends [z.ZodType] ? Record<never, never> extends z.input<QuerySchema> ? {
|
|
28
|
+
readonly query?: z.input<QuerySchema>;
|
|
29
|
+
} : {
|
|
30
|
+
readonly query: z.input<QuerySchema>;
|
|
31
|
+
} : Record<never, never>;
|
|
32
|
+
type BodyField<BodySchema> = [BodySchema] extends [z.ZodType] ? undefined extends z.input<BodySchema> ? {
|
|
33
|
+
readonly body?: z.input<BodySchema>;
|
|
34
|
+
} : {
|
|
35
|
+
readonly body: z.input<BodySchema>;
|
|
36
|
+
} : Record<never, never>;
|
|
37
|
+
/** Strictly inferred input object for calling an operation contract. */
|
|
38
|
+
export type OperationCallInput<Op extends AnyOperationContract> = ParamsField<ParamsOf<Op>> & QueryField<QueryOf<Op>> & BodyField<BodyOf<Op>> & CallApiOptions;
|
|
39
|
+
/** Optional input tuple when all fields are optional; required otherwise. */
|
|
40
|
+
export type OperationCallArgs<Op extends AnyOperationContract> = Record<never, never> extends OperationCallInput<Op> ? [input?: OperationCallInput<Op>] : [input: OperationCallInput<Op>];
|
|
41
|
+
/** Strictly inferred value produced by `callApi(operation, ...)`. */
|
|
42
|
+
export type OperationCallResult<Op extends AnyOperationContract> = Op extends {
|
|
43
|
+
readonly kind: "delete" | "post-no-content";
|
|
44
|
+
} ? undefined : Op extends {
|
|
45
|
+
readonly kind: "list";
|
|
46
|
+
readonly item: infer Item extends z.ZodType;
|
|
47
|
+
readonly meta: infer Meta;
|
|
48
|
+
} ? [Meta] extends [z.ZodType<PaginationMeta>] ? ApiPageWithMeta<z.output<Item>, z.output<Meta>> : ApiPage<z.output<Item>> : Op extends {
|
|
49
|
+
readonly response: infer Response extends z.ZodType;
|
|
50
|
+
readonly meta: infer Meta;
|
|
51
|
+
} ? [Meta] extends [z.ZodType] ? ApiDataWithMeta<z.output<Response>, z.output<Meta>> : z.output<Response> : Op extends {
|
|
52
|
+
readonly response: infer Response extends z.ZodType;
|
|
53
|
+
} ? z.output<Response> : never;
|
|
54
|
+
/** Promise returned by `callApi` and `createApiClient` methods. */
|
|
55
|
+
export type OperationCallPromise<Op extends AnyOperationContract> = Op extends {
|
|
56
|
+
readonly kind: "delete" | "post-no-content";
|
|
57
|
+
} ? Promise<void> : Promise<OperationCallResult<Op>>;
|
|
58
|
+
/** Named map of standalone operations passed to `createApiClient`. */
|
|
59
|
+
export type OperationRecord = Readonly<Record<string, AnyOperationContract>>;
|
|
60
|
+
/** One typed client method synthesized from an operation contract. */
|
|
61
|
+
export type OperationClientMethod<Op extends AnyOperationContract> = (...args: OperationCallArgs<Op>) => OperationCallPromise<Op>;
|
|
62
|
+
/** Frozen, per-operation client returned by `createApiClient`. */
|
|
63
|
+
export type ApiClient<Ops extends OperationRecord> = Readonly<{
|
|
64
|
+
[K in keyof Ops]: OperationClientMethod<Ops[K]>;
|
|
65
|
+
}>;
|
|
66
|
+
/**
|
|
67
|
+
* Call one standalone operation contract with full input validation and
|
|
68
|
+
* envelope parsing.
|
|
69
|
+
*/
|
|
70
|
+
export declare function callApi<Op extends AnyOperationContract>(operation: Op, ...args: OperationCallArgs<Op>): OperationCallPromise<Op>;
|
|
71
|
+
/**
|
|
72
|
+
* Compose a frozen, strictly typed API client from any subset of standalone
|
|
73
|
+
* operation contracts.
|
|
74
|
+
*/
|
|
75
|
+
export declare function createApiClient<const Ops extends OperationRecord>(operations: Ops): ApiClient<Ops>;
|
|
76
|
+
export {};
|
|
77
|
+
//# sourceMappingURL=call-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call-api.d.ts","sourceRoot":"","sources":["../../src/lib/call-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAEL,KAAK,oBAAoB,EAKzB,KAAK,cAAc,EAKpB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EAOL,KAAK,OAAO,EACZ,KAAK,eAAe,EAErB,MAAM,gBAAgB,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AG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC,CAAC;AAE7E,sEAAsE;AACtE,MAAM,MAAM,qBAAqB,CAAC,EAAE,SAAS,oBAAoB,IAAI,CACnE,GAAG,IAAI,EAAE,iBAAiB,CAAC,EAAE,CAAC,KAC3B,oBAAoB,CAAC,EAAE,CAAC,CAAC;AAE9B,kEAAkE;AAClE,MAAM,MAAM,SAAS,CAAC,GAAG,SAAS,eAAe,IAAI,QAAQ,CAAC;KAC3D,CAAC,IAAI,MAAM,GAAG,GAAG,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAChD,CAAC,CAAC;AA0KH;;;GAGG;AACH,wBAAgB,OAAO,CAAC,EAAE,SAAS,oBAAoB,EACrD,SAAS,EAAE,EAAE,EACb,GAAG,IAAI,EAAE,iBAAiB,CAAC,EAAE,CAAC,GAC7B,oBAAoB,CAAC,EAAE,CAAC,CAAC;AAK5B;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,CAAC,GAAG,SAAS,eAAe,EAAE,UAAU,EAAE,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { apiPath, } from "@dads-framework/core/contract";
|
|
3
|
+
import { getFromApi, getPageFromApi, getPageWithMetaFromApi, sendToApi, sendToApiWithoutResponse, } from "./api-verbs.js";
|
|
4
|
+
import { toSearchParams } from "./search-params.js";
|
|
5
|
+
import { sendApiRequest } from "./send-api-request.js";
|
|
6
|
+
const dataWithMetaEnvelopeSchema = z.object({
|
|
7
|
+
data: z.unknown(),
|
|
8
|
+
meta: z.unknown(),
|
|
9
|
+
});
|
|
10
|
+
function requestOptionsOf(input) {
|
|
11
|
+
return {
|
|
12
|
+
...(input?.signal === undefined ? {} : { signal: input.signal }),
|
|
13
|
+
...(input?.headers === undefined ? {} : { headers: input.headers }),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function queryArgOf(schema, value) {
|
|
17
|
+
if (schema === undefined)
|
|
18
|
+
return {};
|
|
19
|
+
return { query: { schema, value: value ?? {} } };
|
|
20
|
+
}
|
|
21
|
+
function bodyArgOf(schema, value) {
|
|
22
|
+
if (schema === undefined)
|
|
23
|
+
return {};
|
|
24
|
+
return { body: { schema, value } };
|
|
25
|
+
}
|
|
26
|
+
async function sendWithMeta(request) {
|
|
27
|
+
const search = request.query === undefined ? undefined : toSearchParams(request.query.schema.parse(request.query.value));
|
|
28
|
+
const response = await sendApiRequest({
|
|
29
|
+
path: apiPath(request.route.path),
|
|
30
|
+
method: request.route.method,
|
|
31
|
+
body: request.body?.schema.parse(request.body.value),
|
|
32
|
+
search,
|
|
33
|
+
signal: request.signal,
|
|
34
|
+
headers: request.headers,
|
|
35
|
+
});
|
|
36
|
+
const envelope = dataWithMetaEnvelopeSchema.parse(await response.json());
|
|
37
|
+
return {
|
|
38
|
+
data: request.returns.parse(envelope.data),
|
|
39
|
+
meta: request.meta.parse(envelope.meta),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function callGet(op, input) {
|
|
43
|
+
const route = op.params === undefined ? op.bind() : op.bind(input?.params);
|
|
44
|
+
const query = queryArgOf(op.query, input?.query);
|
|
45
|
+
const options = requestOptionsOf(input);
|
|
46
|
+
if (op.meta !== undefined) {
|
|
47
|
+
return sendWithMeta({
|
|
48
|
+
route,
|
|
49
|
+
returns: op.response,
|
|
50
|
+
meta: op.meta,
|
|
51
|
+
...query,
|
|
52
|
+
...options,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return getFromApi({
|
|
56
|
+
route,
|
|
57
|
+
returns: op.response,
|
|
58
|
+
...query,
|
|
59
|
+
...options,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function callList(op, input) {
|
|
63
|
+
const route = op.params === undefined ? op.bind() : op.bind(input?.params);
|
|
64
|
+
const query = queryArgOf(op.query, input?.query);
|
|
65
|
+
const options = requestOptionsOf(input);
|
|
66
|
+
if (op.meta !== undefined) {
|
|
67
|
+
return getPageWithMetaFromApi({
|
|
68
|
+
route,
|
|
69
|
+
returns: op.item,
|
|
70
|
+
meta: op.meta,
|
|
71
|
+
...query,
|
|
72
|
+
...options,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return getPageFromApi({
|
|
76
|
+
route,
|
|
77
|
+
returns: op.item,
|
|
78
|
+
...query,
|
|
79
|
+
...options,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function callPost(op, input) {
|
|
83
|
+
const route = op.params === undefined ? op.bind() : op.bind(input?.params);
|
|
84
|
+
const query = queryArgOf(op.query, input?.query);
|
|
85
|
+
const body = bodyArgOf(op.body, input?.body);
|
|
86
|
+
const options = requestOptionsOf(input);
|
|
87
|
+
if (op.meta !== undefined) {
|
|
88
|
+
return sendWithMeta({
|
|
89
|
+
route,
|
|
90
|
+
returns: op.response,
|
|
91
|
+
meta: op.meta,
|
|
92
|
+
...query,
|
|
93
|
+
...body,
|
|
94
|
+
...options,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return sendToApi({
|
|
98
|
+
route,
|
|
99
|
+
returns: op.response,
|
|
100
|
+
...query,
|
|
101
|
+
...body,
|
|
102
|
+
...options,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function callMutate(op, input) {
|
|
106
|
+
const route = op.params === undefined ? op.bind() : op.bind(input?.params);
|
|
107
|
+
return sendToApi({
|
|
108
|
+
route,
|
|
109
|
+
returns: op.response,
|
|
110
|
+
...bodyArgOf(op.body, input?.body),
|
|
111
|
+
...requestOptionsOf(input),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function callNoContent(op, input) {
|
|
115
|
+
const route = op.params === undefined ? op.bind() : op.bind(input?.params);
|
|
116
|
+
return sendToApiWithoutResponse({
|
|
117
|
+
route,
|
|
118
|
+
...bodyArgOf(op.body, input?.body),
|
|
119
|
+
...requestOptionsOf(input),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async function executeOperation(operation, input) {
|
|
123
|
+
switch (operation.kind) {
|
|
124
|
+
case "get":
|
|
125
|
+
return await callGet(operation, input);
|
|
126
|
+
case "list":
|
|
127
|
+
return await callList(operation, input);
|
|
128
|
+
case "post":
|
|
129
|
+
return await callPost(operation, input);
|
|
130
|
+
case "patch":
|
|
131
|
+
case "put":
|
|
132
|
+
return await callMutate(operation, input);
|
|
133
|
+
case "delete":
|
|
134
|
+
case "post-no-content":
|
|
135
|
+
await callNoContent(operation, input);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export function callApi(operation, input) {
|
|
140
|
+
return executeOperation(operation, input);
|
|
141
|
+
}
|
|
142
|
+
export function createApiClient(operations) {
|
|
143
|
+
const client = {};
|
|
144
|
+
for (const [name, operation] of Object.entries(operations)) {
|
|
145
|
+
client[name] = (input) => executeOperation(operation, input);
|
|
146
|
+
}
|
|
147
|
+
return Object.freeze(client);
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=call-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call-api.js","sourceRoot":"","sources":["../../src/lib/call-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACL,OAAO,GAWR,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,UAAU,EACV,cAAc,EACd,sBAAsB,EACtB,SAAS,EACT,wBAAwB,GAKzB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAA0B,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAkG/E,MAAM,0BAA0B,GAAG,CAAC,CAAC,MAAM,CAAC;IAC1C,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE;IACjB,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE;C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K,MAAM;YACT,OAAO,MAAM,QAAQ,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;QAC1C,KAAK,MAAM;YACT,OAAO,MAAM,QAAQ,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;QAC1C,KAAK,OAAO,CAAC;QACb,KAAK,KAAK;YACR,OAAO,MAAM,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;QAC5C,KAAK,QAAQ,CAAC;QACd,KAAK,iBAAiB;YACpB,MAAM,aAAa,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;YACtC,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC;AAUD,MAAM,UAAU,OAAO,CAAC,SAA+B,EAAE,KAAoB;IAC3E,OAAO,gBAAgB,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5C,CAAC;AAOD,MAAM,UAAU,eAAe,CAC7B,UAA2B;IAE3B,MAAM,MAAM,GAA+D,EAAE,CAAC;IAC9E,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QAC3D,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,KAAoB,EAAE,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IAC9E,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ClientContext } from "@dads-framework/core/contract";
|
|
2
|
+
/**
|
|
3
|
+
* The debugging environment every outbound request describes itself with
|
|
4
|
+
* (`x-client-context`).
|
|
5
|
+
*
|
|
6
|
+
* A module-level channel rather than an argument on each call, for the same
|
|
7
|
+
* reason as `request-locale.ts`: every request carries it and no call site
|
|
8
|
+
* should have to remember. The app's composition root is the one place that
|
|
9
|
+
* holds the router, the baked build stamp, and the browser globals, so it is
|
|
10
|
+
* what calls `setClientContextProvider`.
|
|
11
|
+
*
|
|
12
|
+
* The provider is asked per request rather than once at import, because the
|
|
13
|
+
* answer changes as the reader navigates, resizes, or goes offline.
|
|
14
|
+
*/
|
|
15
|
+
type ClientContextProvider = () => ClientContext | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Describe this client on every request from here on, or pass `null` to stop.
|
|
18
|
+
*/
|
|
19
|
+
export declare function setClientContextProvider(provider: ClientContextProvider | null): void;
|
|
20
|
+
/**
|
|
21
|
+
* The header value the next request will carry, or `undefined` when there is
|
|
22
|
+
* nothing to say.
|
|
23
|
+
*
|
|
24
|
+
* This is diagnostic exhaust, so it fails silent in every direction: no
|
|
25
|
+
* provider, a provider that returns nothing, a provider that throws, or a
|
|
26
|
+
* context too large for the header budget all drop the header rather than
|
|
27
|
+
* disturb the request. A dropped header costs a debugging detail; a thrown one
|
|
28
|
+
* would cost the user their action.
|
|
29
|
+
*/
|
|
30
|
+
export declare function currentClientContextHeader(): string | undefined;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=client-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-context.d.ts","sourceRoot":"","sources":["../../src/lib/client-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiD,KAAK,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAElH;;;;;;;;;;;;GAYG;AAEH,KAAK,qBAAqB,GAAG,MAAM,aAAa,GAAG,SAAS,CAAC;AAI7D;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,qBAAqB,GAAG,IAAI,GAAG,IAAI,CAErF;AAED;;;;;;;;;GASG;AACH,wBAAgB,0BAA0B,IAAI,MAAM,GAAG,SAAS,CAU/D"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CLIENT_CONTEXT_MAX_BYTES, encodeClientContext } from "@dads-framework/core/contract";
|
|
2
|
+
let describeClient = null;
|
|
3
|
+
/**
|
|
4
|
+
* Describe this client on every request from here on, or pass `null` to stop.
|
|
5
|
+
*/
|
|
6
|
+
export function setClientContextProvider(provider) {
|
|
7
|
+
describeClient = provider;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The header value the next request will carry, or `undefined` when there is
|
|
11
|
+
* nothing to say.
|
|
12
|
+
*
|
|
13
|
+
* This is diagnostic exhaust, so it fails silent in every direction: no
|
|
14
|
+
* provider, a provider that returns nothing, a provider that throws, or a
|
|
15
|
+
* context too large for the header budget all drop the header rather than
|
|
16
|
+
* disturb the request. A dropped header costs a debugging detail; a thrown one
|
|
17
|
+
* would cost the user their action.
|
|
18
|
+
*/
|
|
19
|
+
export function currentClientContextHeader() {
|
|
20
|
+
if (describeClient === null)
|
|
21
|
+
return undefined;
|
|
22
|
+
try {
|
|
23
|
+
const context = describeClient();
|
|
24
|
+
if (context === undefined)
|
|
25
|
+
return undefined;
|
|
26
|
+
const encoded = encodeClientContext(context);
|
|
27
|
+
return encoded.length > CLIENT_CONTEXT_MAX_BYTES ? undefined : encoded;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=client-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-context.js","sourceRoot":"","sources":["../../src/lib/client-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,mBAAmB,EAAsB,MAAM,+BAA+B,CAAC;AAkBlH,IAAI,cAAc,GAAiC,IAAI,CAAC;AAExD;;GAEG;AACH,MAAM,UAAU,wBAAwB,CAAC,QAAsC;IAC7E,cAAc,GAAG,QAAQ,CAAC;AAC5B,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,0BAA0B;IACxC,IAAI,cAAc,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAC9C,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;QACjC,IAAI,OAAO,KAAK,SAAS;YAAE,OAAO,SAAS,CAAC;QAC5C,MAAM,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;QAC7C,OAAO,OAAO,CAAC,MAAM,GAAG,wBAAwB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;IACzE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A random v4 UUID for the request-id header.
|
|
3
|
+
*
|
|
4
|
+
* `crypto.randomUUID` exists only in secure contexts (https or localhost), so
|
|
5
|
+
* a page served over plain http on a local network — a shared terminal, a test
|
|
6
|
+
* computer reaching a dev machine by its LAN address — has no such function,
|
|
7
|
+
* and every API call would throw before it left the browser.
|
|
8
|
+
* `crypto.getRandomValues` is available in every context, so the fallback
|
|
9
|
+
* builds the same v4 shape from it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createRequestId(): string;
|
|
12
|
+
/** Stamp the version (4) and variant (10xx) bits, then format 8-4-4-4-12. */
|
|
13
|
+
export declare function uuidV4FromRandomBytes(bytes: Uint8Array): string;
|
|
14
|
+
//# sourceMappingURL=create-request-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-request-id.d.ts","sourceRoot":"","sources":["../../src/lib/create-request-id.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAKxC;AAED,6EAA6E;AAC7E,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAM/D"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A random v4 UUID for the request-id header.
|
|
3
|
+
*
|
|
4
|
+
* `crypto.randomUUID` exists only in secure contexts (https or localhost), so
|
|
5
|
+
* a page served over plain http on a local network — a shared terminal, a test
|
|
6
|
+
* computer reaching a dev machine by its LAN address — has no such function,
|
|
7
|
+
* and every API call would throw before it left the browser.
|
|
8
|
+
* `crypto.getRandomValues` is available in every context, so the fallback
|
|
9
|
+
* builds the same v4 shape from it.
|
|
10
|
+
*/
|
|
11
|
+
export function createRequestId() {
|
|
12
|
+
if (typeof crypto.randomUUID === "function") {
|
|
13
|
+
return crypto.randomUUID();
|
|
14
|
+
}
|
|
15
|
+
return uuidV4FromRandomBytes(crypto.getRandomValues(new Uint8Array(16)));
|
|
16
|
+
}
|
|
17
|
+
/** Stamp the version (4) and variant (10xx) bits, then format 8-4-4-4-12. */
|
|
18
|
+
export function uuidV4FromRandomBytes(bytes) {
|
|
19
|
+
const stamped = Uint8Array.from(bytes);
|
|
20
|
+
stamped[6] = ((stamped[6] ?? 0) & 0x0f) | 0x40;
|
|
21
|
+
stamped[8] = ((stamped[8] ?? 0) & 0x3f) | 0x80;
|
|
22
|
+
const hex = Array.from(stamped, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
23
|
+
return [hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20, 32)].join("-");
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=create-request-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-request-id.js","sourceRoot":"","sources":["../../src/lib/create-request-id.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,UAAU,eAAe;IAC7B,IAAI,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;QAC5C,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC;IAC7B,CAAC;IACD,OAAO,qBAAqB,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC3E,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,qBAAqB,CAAC,KAAiB;IACrD,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACvC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC/C,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC/C,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACvF,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChH,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who tells the reader that a write failed.
|
|
3
|
+
*
|
|
4
|
+
* The web app shows one global notice for every failed write, so no caller has
|
|
5
|
+
* to remember to. A screen that already shows the refusal where it happened —
|
|
6
|
+
* beside the field, in the dialog, on the row — declares so with
|
|
7
|
+
* `FAILURE_PRESENTED_INLINE` as its mutation `meta`, and the global notice
|
|
8
|
+
* stands down. Without the declaration the reader would get the same refusal
|
|
9
|
+
* twice, once in context and once as a less specific toast.
|
|
10
|
+
*
|
|
11
|
+
* The declaration belongs to the hook whose every consumer renders the
|
|
12
|
+
* failure. A hook with a consumer that does not render it stays undeclared.
|
|
13
|
+
*/
|
|
14
|
+
export type FailurePresentationMeta = {
|
|
15
|
+
readonly failurePresentation: "inline";
|
|
16
|
+
};
|
|
17
|
+
/** Mutation `meta` for a write whose screen presents its own failure. */
|
|
18
|
+
export declare const FAILURE_PRESENTED_INLINE: FailurePresentationMeta;
|
|
19
|
+
/** True when a mutation's `meta` says its screen presents the failure. */
|
|
20
|
+
export declare function isFailurePresentedInline(meta: unknown): boolean;
|
|
21
|
+
//# sourceMappingURL=failure-presentation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failure-presentation.d.ts","sourceRoot":"","sources":["../../src/lib/failure-presentation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,mBAAmB,EAAE,QAAQ,CAAC;CACxC,CAAC;AAEF,yEAAyE;AACzE,eAAO,MAAM,wBAAwB,EAAE,uBAEtC,CAAC;AAEF,0EAA0E;AAC1E,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAI/D"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Mutation `meta` for a write whose screen presents its own failure. */
|
|
2
|
+
export const FAILURE_PRESENTED_INLINE = {
|
|
3
|
+
failurePresentation: "inline",
|
|
4
|
+
};
|
|
5
|
+
/** True when a mutation's `meta` says its screen presents the failure. */
|
|
6
|
+
export function isFailurePresentedInline(meta) {
|
|
7
|
+
return (typeof meta === "object" && meta !== null && "failurePresentation" in meta && meta.failurePresentation === "inline");
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=failure-presentation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failure-presentation.js","sourceRoot":"","sources":["../../src/lib/failure-presentation.ts"],"names":[],"mappings":"AAiBA,yEAAyE;AACzE,MAAM,CAAC,MAAM,wBAAwB,GAA4B;IAC/D,mBAAmB,EAAE,QAAQ;CAC9B,CAAC;AAEF,0EAA0E;AAC1E,MAAM,UAAU,wBAAwB,CAAC,IAAa;IACpD,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,qBAAqB,IAAI,IAAI,IAAI,IAAI,CAAC,mBAAmB,KAAK,QAAQ,CACpH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
type QueryKey = readonly unknown[];
|
|
2
|
+
/** The query-cache operations needed to patch and restore optimistic reads. */
|
|
3
|
+
export type OptimisticQueryCache = {
|
|
4
|
+
cancelQueries(filters: {
|
|
5
|
+
readonly queryKey: QueryKey;
|
|
6
|
+
}): Promise<void>;
|
|
7
|
+
getQueriesData(filters: {
|
|
8
|
+
readonly queryKey: QueryKey;
|
|
9
|
+
}): readonly (readonly [QueryKey, unknown])[];
|
|
10
|
+
setQueriesData(filters: {
|
|
11
|
+
readonly queryKey: QueryKey;
|
|
12
|
+
}, updater: (cached: unknown) => unknown): void;
|
|
13
|
+
setQueryData(key: QueryKey, data: unknown): void;
|
|
14
|
+
invalidateQueries(filters: {
|
|
15
|
+
readonly queryKey: QueryKey;
|
|
16
|
+
}): Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* What one write is expected to do to the records already on screen.
|
|
20
|
+
*
|
|
21
|
+
* `change` merges fields into the record with this id wherever it is cached —
|
|
22
|
+
* a detail read, a row in a page, an item in a plain list, or a record nested
|
|
23
|
+
* one level inside a detail (a ticket's parties, a company's relationship). `remove` takes the record out of
|
|
24
|
+
* every page and list it appears in, and lowers each page's total to match.
|
|
25
|
+
* `key` names the identifying field when a record is not keyed by `id`
|
|
26
|
+
* (`staffId`, `relationshipId`).
|
|
27
|
+
*/
|
|
28
|
+
export type OptimisticRecordEffect = {
|
|
29
|
+
readonly id: string;
|
|
30
|
+
readonly key?: string;
|
|
31
|
+
} & ({
|
|
32
|
+
readonly change: Readonly<Record<string, unknown>>;
|
|
33
|
+
} | {
|
|
34
|
+
readonly remove: true;
|
|
35
|
+
});
|
|
36
|
+
/** What `onMutate` hands back so a refused write can be put back. */
|
|
37
|
+
export type OptimisticSnapshot = {
|
|
38
|
+
readonly entries: readonly (readonly [QueryKey, unknown])[];
|
|
39
|
+
};
|
|
40
|
+
/** The mutation options an optimistic write spreads into its factory. */
|
|
41
|
+
export type OptimisticRecordWriteOptions<TVariables> = {
|
|
42
|
+
readonly onMutate: (variables: TVariables) => Promise<OptimisticSnapshot>;
|
|
43
|
+
readonly onError: (error: Error, variables: TVariables, snapshot: OptimisticSnapshot | undefined) => void;
|
|
44
|
+
readonly onSettled: () => Promise<void>;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* {@link optimisticRecordWrite} for a write that takes no input — opening one
|
|
48
|
+
* order, closing one release — whose effect is known when the factory is
|
|
49
|
+
* built.
|
|
50
|
+
*
|
|
51
|
+
* Its options name no input, so the mutation keeps taking none. The snapshot
|
|
52
|
+
* to restore is the one its latest send took: such a write is sent from one
|
|
53
|
+
* control at a time, and the refetch on settle corrects any overlap.
|
|
54
|
+
*/
|
|
55
|
+
export declare function optimisticRecordWriteWithoutInput(queryClient: OptimisticQueryCache, scope: QueryKey, effect: OptimisticRecordEffect | null): {
|
|
56
|
+
onMutate: () => Promise<OptimisticSnapshot>;
|
|
57
|
+
onError: (error: Error) => void;
|
|
58
|
+
onSettled: () => Promise<void>;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Mutation options that show a write before the server answers it.
|
|
62
|
+
*
|
|
63
|
+
* The screen changes the moment the reader acts: every cached read under
|
|
64
|
+
* `scope` is patched with the expected effect. If the server refuses, each
|
|
65
|
+
* read is restored exactly as it was, and the refusal is explained by the
|
|
66
|
+
* app's single failure notice — or, when the hook declares
|
|
67
|
+
* `FAILURE_PRESENTED_INLINE` and its screen stays mounted, by that screen's
|
|
68
|
+
* own inline message. Either way the scope is refetched once the write
|
|
69
|
+
* settles, so the server's answer — not the guess — is what stays on screen.
|
|
70
|
+
*
|
|
71
|
+
* Spread it into a mutation factory beside its own `onSuccess`. The patch is
|
|
72
|
+
* structural: it recognises `{ items, meta }` pages, arrays of records, and
|
|
73
|
+
* records by their identifying field, so a resource's key factory never has
|
|
74
|
+
* to enumerate the views that show it. An effect that names a field a view
|
|
75
|
+
* does not carry changes nothing there, and the refetch corrects it.
|
|
76
|
+
*/
|
|
77
|
+
export declare function optimisticRecordWrite<TVariables>(queryClient: OptimisticQueryCache, scope: QueryKey, effectOf: (variables: TVariables) => OptimisticRecordEffect | null): OptimisticRecordWriteOptions<TVariables>;
|
|
78
|
+
/**
|
|
79
|
+
* One cached read with the effect applied; the same reference when the read
|
|
80
|
+
* does not hold the record, so untouched views do not re-render.
|
|
81
|
+
*/
|
|
82
|
+
export declare function applyRecordEffect(cached: unknown, effect: OptimisticRecordEffect): unknown;
|
|
83
|
+
export {};
|
|
84
|
+
//# sourceMappingURL=optimistic-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optimistic-cache.d.ts","sourceRoot":"","sources":["../../src/lib/optimistic-cache.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,GAAG,SAAS,OAAO,EAAE,CAAC;AAEnC,+EAA+E;AAC/E,MAAM,MAAM,oBAAoB,GAAG;IACjC,aAAa,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvE,cAAc,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,GAAG,SAAS,CAAC,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;IACpG,cAAc,CAAC,OAAO,EAAE;QAAE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAA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