@dads-framework/client 0.0.0-stage → 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.md +21 -0
- package/README.md +69 -2
- package/dist/client.tsbuildinfo +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -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 +150 -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/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 +31 -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 +12 -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 +119 -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 +18 -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 +100 -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 +122 -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/sentry-client-telemetry.d.ts +64 -0
- package/dist/telemetry/lib/sentry-client-telemetry.d.ts.map +1 -0
- package/dist/telemetry/lib/sentry-client-telemetry.js +187 -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,150 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { apiPath, paginationMetaSchema, } from "@dads-framework/core/contract";
|
|
3
|
+
import { toSearchParams } from "./search-params.js";
|
|
4
|
+
import { sendApiRequest } from "./send-api-request.js";
|
|
5
|
+
/**
|
|
6
|
+
* Read one resource: `GET path` unwrapped from its `{ data }` envelope.
|
|
7
|
+
*/
|
|
8
|
+
export async function getFromApi(request) {
|
|
9
|
+
const location = getRequestLocation(request);
|
|
10
|
+
const response = await sendApiRequest({
|
|
11
|
+
path: location.path,
|
|
12
|
+
method: location.method,
|
|
13
|
+
body: undefined,
|
|
14
|
+
search: toValidatedSearch(request.query),
|
|
15
|
+
signal: request.signal,
|
|
16
|
+
headers: request.headers,
|
|
17
|
+
});
|
|
18
|
+
return unwrapData(await response.json(), request.returns);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Read one page of a collection: `GET path` unwrapped to `{ items, meta }`.
|
|
22
|
+
*
|
|
23
|
+
* Separate from {@link getFromApi} because a paginated endpoint's `meta` is
|
|
24
|
+
* part of the answer, not decoration — a caller that dropped it could not
|
|
25
|
+
* render the next page.
|
|
26
|
+
*/
|
|
27
|
+
export async function getPageFromApi(request) {
|
|
28
|
+
const location = getRequestLocation(request);
|
|
29
|
+
const response = await sendApiRequest({
|
|
30
|
+
path: location.path,
|
|
31
|
+
method: location.method,
|
|
32
|
+
body: undefined,
|
|
33
|
+
search: toValidatedSearch(request.query),
|
|
34
|
+
signal: request.signal,
|
|
35
|
+
headers: request.headers,
|
|
36
|
+
});
|
|
37
|
+
const envelope = paginatedEnvelopeSchema.parse(await response.json());
|
|
38
|
+
return {
|
|
39
|
+
items: envelope.data.map((item) => request.returns.parse(item)),
|
|
40
|
+
meta: envelope.meta,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Read one page of a collection whose endpoint declares its own meta (a
|
|
45
|
+
* schema extending `paginationMetaSchema`), such as counts the page shows
|
|
46
|
+
* beside the rows: `GET path` unwrapped to `{ items, meta }`, the meta
|
|
47
|
+
* parsed by the endpoint's own meta schema.
|
|
48
|
+
*/
|
|
49
|
+
export async function getPageWithMetaFromApi(request) {
|
|
50
|
+
const location = getRequestLocation(request);
|
|
51
|
+
const response = await sendApiRequest({
|
|
52
|
+
path: location.path,
|
|
53
|
+
method: location.method,
|
|
54
|
+
body: undefined,
|
|
55
|
+
search: toValidatedSearch(request.query),
|
|
56
|
+
signal: request.signal,
|
|
57
|
+
headers: request.headers,
|
|
58
|
+
});
|
|
59
|
+
const envelope = z
|
|
60
|
+
.object({ data: z.array(z.unknown()), meta: z.unknown() })
|
|
61
|
+
.parse(await response.json());
|
|
62
|
+
return {
|
|
63
|
+
items: envelope.data.map((item) => request.returns.parse(item)),
|
|
64
|
+
meta: request.meta.parse(envelope.meta),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Change something: any non-GET verb, with the body validated before it is
|
|
69
|
+
* sent and the response unwrapped from its `{ data }` envelope.
|
|
70
|
+
*
|
|
71
|
+
* Validating the request body here rather than at the call site means a
|
|
72
|
+
* malformed payload fails in the caller's own stack trace, against the
|
|
73
|
+
* server's own contract, instead of coming back as a 400 the UI has to
|
|
74
|
+
* interpret.
|
|
75
|
+
*/
|
|
76
|
+
export async function sendToApi(request) {
|
|
77
|
+
const location = mutationRequestLocation(request);
|
|
78
|
+
const response = await sendApiRequest({
|
|
79
|
+
path: location.path,
|
|
80
|
+
method: location.method,
|
|
81
|
+
body: request.body?.schema.parse(request.body.value),
|
|
82
|
+
search: toValidatedSearch(request.query),
|
|
83
|
+
signal: request.signal,
|
|
84
|
+
headers: request.headers,
|
|
85
|
+
});
|
|
86
|
+
return unwrapData(await response.json(), request.returns);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Change something whose answer is "done": a verb the API replies to with
|
|
90
|
+
* `204 No Content`.
|
|
91
|
+
*
|
|
92
|
+
* Separate from {@link sendToApi} because a 204 has no body at all, and
|
|
93
|
+
* reading one is not a degenerate case of reading a `{ data }` envelope — it
|
|
94
|
+
* throws. Every `deleteRoute` in this repo answers this way, and each still
|
|
95
|
+
* declares a request body (an audit reason), which is why the body argument
|
|
96
|
+
* survives while the response schema does not.
|
|
97
|
+
*/
|
|
98
|
+
export async function sendToApiWithoutResponse(request) {
|
|
99
|
+
const location = mutationRequestLocation(request);
|
|
100
|
+
await sendApiRequest({
|
|
101
|
+
path: location.path,
|
|
102
|
+
method: location.method,
|
|
103
|
+
body: request.body?.schema.parse(request.body.value),
|
|
104
|
+
search: undefined,
|
|
105
|
+
signal: request.signal,
|
|
106
|
+
headers: request.headers,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** Binary endpoints keep the transport's credentials, metadata, and error mapping. */
|
|
110
|
+
export async function requestBinary(request) {
|
|
111
|
+
const response = await sendApiRequest({
|
|
112
|
+
path: apiPath(request.route.path),
|
|
113
|
+
method: request.route.method,
|
|
114
|
+
body: request.body?.schema.parse(request.body.value),
|
|
115
|
+
search: toValidatedSearch(request.query),
|
|
116
|
+
signal: request.signal,
|
|
117
|
+
headers: request.headers,
|
|
118
|
+
accept: request.accept ?? "application/octet-stream",
|
|
119
|
+
});
|
|
120
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
121
|
+
}
|
|
122
|
+
function getRequestLocation(request) {
|
|
123
|
+
return { path: apiPath(request.route.path), method: request.route.method };
|
|
124
|
+
}
|
|
125
|
+
function mutationRequestLocation(request) {
|
|
126
|
+
return { path: apiPath(request.route.path), method: request.route.method };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The envelope shape, checked before the payload it carries.
|
|
130
|
+
*
|
|
131
|
+
* `data` is left `unknown` here on purpose: the endpoint's own contract parses
|
|
132
|
+
* it in a second step, which keeps the envelope check independent of the
|
|
133
|
+
* caller's schema and gives a schema mismatch an error message that names the
|
|
134
|
+
* resource rather than the envelope.
|
|
135
|
+
*/
|
|
136
|
+
const successEnvelopeSchema = z.object({ data: z.unknown() });
|
|
137
|
+
const paginatedEnvelopeSchema = z.object({
|
|
138
|
+
data: z.array(z.unknown()),
|
|
139
|
+
meta: paginationMetaSchema,
|
|
140
|
+
});
|
|
141
|
+
function unwrapData(payload, returns) {
|
|
142
|
+
return returns.parse(successEnvelopeSchema.parse(payload).data);
|
|
143
|
+
}
|
|
144
|
+
function toValidatedSearch(query) {
|
|
145
|
+
if (query === undefined) {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
return toSearchParams(query.schema.parse(query.value));
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=api-verbs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-verbs.js","sourceRoot":"","sources":["../../src/lib/api-verbs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACL,OAAO,EAIP,oBAAoB,GACrB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAA0B,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAgG/E;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAG9B,OAAkC;IAClC,MAAM,QAAQ,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC7C,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC;QACpC,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,IAAI,EAAE,SAAS;QACf,MAAM,EAAE,iBAAiB,CAAC,OAAO,CAAC,KAAK,CAAC;QACxC,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,OAAO,EAAE,OAAO,CAAC,OAAO;KACzB,CAAC,CAAC;IACH,OAAO,UAAU,CAAC,MAAM,QAAQ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AE,CAAC;AAC7E,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAE9D,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;IACvC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IAC1B,IAAI,EAAE,oBAAoB;CAC3B,CAAC,CAAC;AAEH,SAAS,UAAU,CACjB,OAAgB,EAChB,OAAc;IAEd,OAAO,OAAO,CAAC,KAAK,CAAC,qBAAqB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAClE,CAAC;AAED,SAAS,iBAAiB,CACxB,KAA2C;IAE3C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ClientContext } from "@dads-framework/core/contract";
|
|
2
|
+
/**
|
|
3
|
+
* What one browser tab can say about itself, before any of it is shaped for
|
|
4
|
+
* the wire. Every field is optional because every read behind it can be
|
|
5
|
+
* missing: an older browser, a headless runtime, a `navigator` without the
|
|
6
|
+
* non-standard extras.
|
|
7
|
+
*
|
|
8
|
+
* Reading the values is `read-client-environment.ts`'s job; turning them into
|
|
9
|
+
* a valid {@link ClientContext} is this module's, which is why this type is a
|
|
10
|
+
* plain snapshot with no globals in it.
|
|
11
|
+
*/
|
|
12
|
+
export type ClientEnvironmentSnapshot = {
|
|
13
|
+
readonly build: string | undefined;
|
|
14
|
+
readonly timeZone: string | undefined;
|
|
15
|
+
readonly locale: string | undefined;
|
|
16
|
+
readonly routeId: string | undefined;
|
|
17
|
+
readonly viewport: {
|
|
18
|
+
readonly w: number;
|
|
19
|
+
readonly h: number;
|
|
20
|
+
readonly dpr: number;
|
|
21
|
+
} | undefined;
|
|
22
|
+
readonly screen: {
|
|
23
|
+
readonly w: number;
|
|
24
|
+
readonly h: number;
|
|
25
|
+
} | undefined;
|
|
26
|
+
readonly touch: boolean | undefined;
|
|
27
|
+
readonly online: boolean | undefined;
|
|
28
|
+
readonly connection: string | undefined;
|
|
29
|
+
readonly deviceMemory: number | undefined;
|
|
30
|
+
readonly hardwareConcurrency: number | undefined;
|
|
31
|
+
readonly tabAgeMs: number | undefined;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Shape one browser snapshot into the `x-client-context` payload.
|
|
35
|
+
*
|
|
36
|
+
* Pure, and deliberately forgiving: it clamps every string to the contract's
|
|
37
|
+
* length and rounds every count to a whole number so the server's strict
|
|
38
|
+
* schema accepts the result. The server drops the whole context on the first
|
|
39
|
+
* violation (`decodeClientContext` returns `null`), so one absurd `routeId`
|
|
40
|
+
* must not take the build stamp down with it. Absent fields stay `undefined`
|
|
41
|
+
* and disappear when the context is serialized for the header.
|
|
42
|
+
*/
|
|
43
|
+
export declare function buildClientContext(environment: ClientEnvironmentSnapshot): ClientContext;
|
|
44
|
+
//# sourceMappingURL=build-client-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-client-context.d.ts","sourceRoot":"","sources":["../../src/lib/build-client-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAEnE;;;;;;;;;GASG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,QAAQ,EACb;QAAE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;KAAE,GAChE,SAAS,CAAC;IACd,QAAQ,CAAC,MAAM,EAAE;QAAE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;IACxE,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C,QAAQ,CAAC,mBAAmB,EAAE,MAAM,GAAG,SAAS,CAAC;IACjD,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CACvC,CAAC;AAeF;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,yBAAyB,GACrC,aAAa,CAiBf"}
|
|
@@ -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":"AA6BA;;;;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,CAChC,WAAsC;IAEtC,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,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AK,CAAC,CAAC,CAAC;AAC1E,CAAC;AAED,SAAS,UAAU,CACjB,QAA+C;IAE/C,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC7C,MAAM,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC9E,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC;AACvB,CAAC;AAED,SAAS,QAAQ,CACf,MAA2C;IAE3C,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,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,EAGL,KAAK,aAAa,EACnB,MAAM,+BAA+B,CAAC;AAEvC;;;;;;;;;;;;GAYG;AAEH,KAAK,qBAAqB,GAAG,MAAM,aAAa,GAAG,SAAS,CAAC;AAI7D;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,qBAAqB,GAAG,IAAI,GACrC,IAAI,CAEN;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,EACL,wBAAwB,EACxB,mBAAmB,GAEpB,MAAM,+BAA+B,CAAC;AAkBvC,IAAI,cAAc,GAAiC,IAAI,CAAC;AAExD;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAAsC;IAEtC,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,CAc/D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 [
|
|
24
|
+
hex.slice(0, 8),
|
|
25
|
+
hex.slice(8, 12),
|
|
26
|
+
hex.slice(12, 16),
|
|
27
|
+
hex.slice(16, 20),
|
|
28
|
+
hex.slice(20, 32),
|
|
29
|
+
].join("-");
|
|
30
|
+
}
|
|
31
|
+
//# 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,CACvC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CACnC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACX,OAAO;QACL,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;QACf,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC;QAChB,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;QACjB,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;QACjB,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC;KAClB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,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,CAO/D"}
|
|
@@ -0,0 +1,12 @@
|
|
|
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" &&
|
|
8
|
+
meta !== null &&
|
|
9
|
+
"failurePresentation" in meta &&
|
|
10
|
+
meta.failurePresentation === "inline");
|
|
11
|
+
}
|
|
12
|
+
//# 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;QACxB,IAAI,KAAK,IAAI;QACb,qBAAqB,IAAI,IAAI;QAC7B,IAAI,CAAC,mBAAmB,KAAK,QAAQ,CACtC,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;QACtB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;KAC7B,GAAG,SAAS,CAAC,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;IAC9C,cAAc,CACZ,OAAO,EAAE;QAAE,QAAQ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