@mountaya/client 0.0.0-stage → 0.1.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/CHANGELOG.md +104 -0
- package/LICENCE.fr.txt +280 -0
- package/LICENSE.txt +262 -0
- package/README.md +1260 -2
- package/dist/admin/index.cjs +3521 -0
- package/dist/admin/index.cjs.map +1 -0
- package/dist/admin/index.d.cts +1070 -0
- package/dist/admin/index.d.ts +1070 -0
- package/dist/admin/index.js +301 -0
- package/dist/admin/index.js.map +1 -0
- package/dist/browser.cjs +8073 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +106 -0
- package/dist/browser.d.ts +106 -0
- package/dist/browser.js +53 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-BM526BHG.js +342 -0
- package/dist/chunk-BM526BHG.js.map +1 -0
- package/dist/chunk-D2YEVTG7.js +327 -0
- package/dist/chunk-D2YEVTG7.js.map +1 -0
- package/dist/chunk-OHKVUREN.js +3500 -0
- package/dist/chunk-OHKVUREN.js.map +1 -0
- package/dist/chunk-TDTK2L5H.js +3842 -0
- package/dist/chunk-TDTK2L5H.js.map +1 -0
- package/dist/coreOperations.gen-BMYPoBbz.d.ts +1760 -0
- package/dist/coreOperations.gen-BxX5xRxw.d.cts +1760 -0
- package/dist/errors-B-GqAtQv.d.cts +75960 -0
- package/dist/errors-B-GqAtQv.d.ts +75960 -0
- package/dist/events/index.cjs +1972 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/index.d.cts +781 -0
- package/dist/events/index.d.ts +781 -0
- package/dist/events/index.js +826 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.cjs +8072 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +76 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/operations.gen-CvTyzKNF.d.cts +3993 -0
- package/dist/operations.gen-DUD406TV.d.ts +3993 -0
- package/package.json +127 -4
- package/src/admin/index.ts +47 -0
- package/src/admin/rules/mountayaAdmin.ts +102 -0
- package/src/browser.ts +232 -0
- package/src/core/engines/sessionToken.ts +870 -0
- package/src/core/rules/auth.ts +1017 -0
- package/src/core/rules/clientHeader.ts +28 -0
- package/src/core/rules/dates.ts +200 -0
- package/src/core/rules/decode.ts +154 -0
- package/src/core/rules/envelope.ts +41 -0
- package/src/core/rules/errors.ts +1486 -0
- package/src/core/rules/graphql.ts +576 -0
- package/src/core/rules/headerValue.ts +42 -0
- package/src/core/rules/idempotencyKey.ts +64 -0
- package/src/core/rules/nullMask.ts +127 -0
- package/src/core/rules/nullMasks.gen.ts +416 -0
- package/src/core/rules/paginate.ts +316 -0
- package/src/core/rules/rateLimit.ts +507 -0
- package/src/core/rules/redaction.ts +219 -0
- package/src/core/rules/resourceRef.ts +110 -0
- package/src/core/rules/responseBody.ts +492 -0
- package/src/core/rules/retry.ts +386 -0
- package/src/core/rules/selection.ts +416 -0
- package/src/core/rules/sessionAnswer.ts +90 -0
- package/src/core/rules/sessionSchedule.ts +131 -0
- package/src/core/rules/sessionWait.ts +112 -0
- package/src/core/rules/transport.ts +2386 -0
- package/src/core/rules/version.gen.ts +16 -0
- package/src/core/rules/webAddress.ts +165 -0
- package/src/core/rules/webAddresses.gen.ts +30 -0
- package/src/data/operations.gen.ts +603 -0
- package/src/data/schema.gen.ts +8078 -0
- package/src/data/selection.gen.ts +472 -0
- package/src/events/index.ts +67 -0
- package/src/events/rules/agent.ts +439 -0
- package/src/events/rules/collab.ts +554 -0
- package/src/events/rules/collabNulls.gen.ts +17 -0
- package/src/events/rules/eventStream.ts +94 -0
- package/src/events/rules/openSseStream.ts +594 -0
- package/src/events/rules/sse.ts +349 -0
- package/src/index.ts +231 -0
- package/src/rest/adminOperations.gen.ts +70 -0
- package/src/rest/adminResources.gen.ts +136 -0
- package/src/rest/coreOperations.gen.ts +74 -0
- package/src/rest/coreResources.gen.ts +244 -0
- package/src/rest/dates.gen.ts +217 -0
- package/src/rest/operations.gen.ts +4341 -0
- package/src/rest/resources.gen.ts +369 -0
- package/src/rest/schema.gen.ts +56840 -0
- package/src/tiles/operations.gen.ts +206 -0
- package/src/tiles/schema.gen.ts +8408 -0
- package/src/webhooks/rules/constructWebhookEvent.ts +482 -0
- package/src/webhooks/rules/webhookEvent.ts +38 -0
- package/src/webhooks/rules/webhookEvents.gen.ts +732 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every request names the SDK that made it as.
|
|
3
|
+
*
|
|
4
|
+
* The contract has every SDK name itself on each request it makes, as
|
|
5
|
+
* `X-Mountaya-Client: mountaya-<language>/<version>`: the language is `typescript` here, and the
|
|
6
|
+
* version is this package's own, never the contract tag it speaks, which `SCHEMA_VERSION`
|
|
7
|
+
* states. It is a header of Mountaya's own rather than `User-Agent`, which a browser page cannot
|
|
8
|
+
* set.
|
|
9
|
+
*
|
|
10
|
+
* It is the client's, as `Accept-Language` is: no option and no method takes it, and the
|
|
11
|
+
* transport writes it beside the credential on every request, a stream's open included.
|
|
12
|
+
*
|
|
13
|
+
* The version is read from this package's manifest, the one place a release moves it. The build
|
|
14
|
+
* inlines what it reads: tsup bundles the manifest's `version` alone into the entry points, so
|
|
15
|
+
* `dist` carries it as a literal and reads no file at run time, and a toolchain that resolves the
|
|
16
|
+
* `@mountaya/source` condition reads the manifest that ships beside `src`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import manifest from "../../../package.json" with { type: "json" };
|
|
20
|
+
|
|
21
|
+
/** CLIENT_VERSION is this package's own version, as its manifest states it. */
|
|
22
|
+
export const CLIENT_VERSION: string = manifest.version;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* CLIENT_HEADER_VALUE is what every request carries as `X-Mountaya-Client`:
|
|
26
|
+
* `mountaya-typescript/` and {@link CLIENT_VERSION}.
|
|
27
|
+
*/
|
|
28
|
+
export const CLIENT_HEADER_VALUE = `mountaya-typescript/${CLIENT_VERSION}`;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Date handling.
|
|
3
|
+
*
|
|
4
|
+
* The wire carries ISO 8601 strings, and this package hands back a `Date` wherever the contract
|
|
5
|
+
* declares an instant, so no caller writes `new Date(itinerary.created_at)` or remembers which
|
|
6
|
+
* fields are timestamps. Wherever the contract declares none, a string stays a string however
|
|
7
|
+
* much it looks like a timestamp: a track named by its device's clock, a property value an author
|
|
8
|
+
* wrote as a date, a draft a peer is typing.
|
|
9
|
+
*
|
|
10
|
+
* The outbound direction needs no counterpart. `Date.prototype.toJSON` already serializes
|
|
11
|
+
* to ISO 8601, so a `Date` placed in a request body survives `JSON.stringify` correctly
|
|
12
|
+
* without help.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { MountayaError } from "./errors.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* DateMask names the instants in a JSON value: the strings a response decodes to a `Date`.
|
|
19
|
+
*
|
|
20
|
+
* `true` marks the value itself. An object marks each field it names by that field's own mask,
|
|
21
|
+
* and leaves every field it does not name as it arrived. A one-element array marks every element
|
|
22
|
+
* of an array by that element's mask. A generated operation states the mask its contract
|
|
23
|
+
* declares, so a field decodes to a `Date` exactly where its generated type says `Date`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* // An envelope whose `data` is a list of zones, each carrying two instants.
|
|
28
|
+
* const dates: DateMask = { data: [{ created_at: true, updated_at: true }] };
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type DateMask = true | { readonly [field: string]: DateMask } | readonly [DateMask];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* isoDate matches an ISO 8601 date-time carrying a zone, and captures the fields the calendar
|
|
35
|
+
* check reads: year, month, day, hour, minute, second, and the offset's hours and minutes.
|
|
36
|
+
*
|
|
37
|
+
* Anchored and specific even inside a declared field: a date-only value, a time with no zone,
|
|
38
|
+
* a lowercase `t` or `z`, an offset without its colon and a sentence that contains a timestamp
|
|
39
|
+
* name no instant a caller could rely on.
|
|
40
|
+
*/
|
|
41
|
+
const isoDate =
|
|
42
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-](\d{2}):(\d{2}))$/;
|
|
43
|
+
|
|
44
|
+
/** The shortest string {@link isoDate} can match: `2026-08-25T12:34:56Z`. */
|
|
45
|
+
const ISO_DATE_MIN_LENGTH = 20;
|
|
46
|
+
|
|
47
|
+
/** `T`, the separator {@link isoDate} requires at index 10. */
|
|
48
|
+
const ISO_DATE_SEPARATOR = 84;
|
|
49
|
+
|
|
50
|
+
/** The days in each month of a common year, January first. */
|
|
51
|
+
const MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
52
|
+
|
|
53
|
+
/** isLeapYear reports whether a year of the proleptic Gregorian calendar has a leap day. */
|
|
54
|
+
const isLeapYear = (year: number): boolean =>
|
|
55
|
+
(year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
56
|
+
|
|
57
|
+
/** daysIn answers how many days a month of a year has, January being 1. */
|
|
58
|
+
const daysIn = (year: number, month: number): number =>
|
|
59
|
+
month === 2 && isLeapYear(year) ? 29 : (MONTH_DAYS[month - 1] ?? 0);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* instantOf answers the `Date` a string names, or undefined where it names none a `Date` holds.
|
|
63
|
+
*
|
|
64
|
+
* Two comparisons run before the pattern: anything shorter than {@link ISO_DATE_MIN_LENGTH}
|
|
65
|
+
* cannot match it, and the pattern fixes a literal `T` at index 10. Both are derived from the
|
|
66
|
+
* pattern, so they reject nothing it would accept, and an encoded geometry of several thousand
|
|
67
|
+
* characters costs the same two comparisons as a short name.
|
|
68
|
+
*
|
|
69
|
+
* The calendar is checked after the shape. `new Date` reads `2026-02-30T00:00:00Z` as the second
|
|
70
|
+
* of March, a different instant from the one the value names, and reads a thirteenth month or a
|
|
71
|
+
* sixtieth second as an Invalid Date. Neither is an instant the API wrote.
|
|
72
|
+
*/
|
|
73
|
+
export const instantOf = (value: string): Date | undefined => {
|
|
74
|
+
if (value.length < ISO_DATE_MIN_LENGTH || value.charCodeAt(10) !== ISO_DATE_SEPARATOR) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const fields = isoDate.exec(value);
|
|
79
|
+
if (fields === null) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// An offset of `Z` captures nothing, which reads as zero hours and zero minutes.
|
|
84
|
+
const field = (index: number): number => Number(fields[index] ?? 0);
|
|
85
|
+
const year = field(1);
|
|
86
|
+
const month = field(2);
|
|
87
|
+
const day = field(3);
|
|
88
|
+
const named =
|
|
89
|
+
month >= 1 &&
|
|
90
|
+
month <= 12 &&
|
|
91
|
+
day >= 1 &&
|
|
92
|
+
day <= daysIn(year, month) &&
|
|
93
|
+
field(4) <= 23 &&
|
|
94
|
+
field(5) <= 59 &&
|
|
95
|
+
field(6) <= 59 &&
|
|
96
|
+
field(7) <= 23 &&
|
|
97
|
+
field(8) <= 59;
|
|
98
|
+
if (!named) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const instant = new Date(value);
|
|
103
|
+
|
|
104
|
+
return Number.isNaN(instant.getTime()) ? undefined : instant;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** isListMask reports whether a mask marks every element of a list. */
|
|
108
|
+
const isListMask = (mask: DateMask): mask is readonly [DateMask] => Array.isArray(mask);
|
|
109
|
+
|
|
110
|
+
/** isList reports whether a value is a list, as one of unknown elements. */
|
|
111
|
+
const isList = (value: unknown): value is unknown[] => Array.isArray(value);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* decodeDates decodes to a `Date` each instant `mask` names in `value`, in place, and answers
|
|
115
|
+
* the value: the object or list it was given, or the `Date` a string at the root decodes to.
|
|
116
|
+
*
|
|
117
|
+
* A string the mask marks becomes a `Date` where it is a full ISO 8601 timestamp carrying a zone
|
|
118
|
+
* and naming an instant a `Date` holds. One that names none fails the decode: the contract
|
|
119
|
+
* declares an instant there, and kept as a string it would reach a caller behind a type that says
|
|
120
|
+
* `Date`. Every string the mask does not reach stays one. A value whose shape differs from its
|
|
121
|
+
* mask (a `null` where an instant or an object is declared, an object where a list is) is left as
|
|
122
|
+
* it arrived: a body that disagrees with its declaration in shape is the server's to answer for.
|
|
123
|
+
* A field the value does not own is never read, so a mask cannot reach an inherited member.
|
|
124
|
+
*
|
|
125
|
+
* With no mask, nothing is decoded.
|
|
126
|
+
*
|
|
127
|
+
* @param field - The name a failure gives the value, the nearest field holding it, which a
|
|
128
|
+
* decode at the root leaves as `the answer`.
|
|
129
|
+
* @throws `MountayaError` for a string the mask marks that names no instant, naming its field.
|
|
130
|
+
*/
|
|
131
|
+
export const decodeDates = (
|
|
132
|
+
value: unknown,
|
|
133
|
+
mask: DateMask | undefined,
|
|
134
|
+
field = "the answer",
|
|
135
|
+
): unknown => {
|
|
136
|
+
if (mask === undefined) {
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (mask === true) {
|
|
141
|
+
if (typeof value !== "string") {
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const instant = instantOf(value);
|
|
146
|
+
if (instant === undefined) {
|
|
147
|
+
throw new MountayaError(
|
|
148
|
+
`${field}, which the contract declares an instant, holds a string that names none: an instant is a full ISO 8601 timestamp carrying its zone.`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return instant;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (isListMask(mask)) {
|
|
156
|
+
if (isList(value)) {
|
|
157
|
+
const [element] = mask;
|
|
158
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
159
|
+
value[index] = decodeDates(value[index], element, field);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return value;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (typeof value === "object" && value !== null && !isList(value)) {
|
|
167
|
+
for (const [member, memberMask] of Object.entries(mask)) {
|
|
168
|
+
if (Object.hasOwn(value, member)) {
|
|
169
|
+
const current: unknown = Reflect.get(value, member);
|
|
170
|
+
const decoded = decodeDates(current, memberMask, member);
|
|
171
|
+
|
|
172
|
+
// Only a string becomes something else; an object or a list is decoded where it lies.
|
|
173
|
+
if (decoded !== current) {
|
|
174
|
+
Reflect.set(value, member, decoded);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return value;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* parseJsonWithDates parses a response body and decodes to a `Date` each instant `mask` names,
|
|
185
|
+
* as {@link decodeDates} does. With no mask, it decodes none.
|
|
186
|
+
*
|
|
187
|
+
* The parse takes no reviver: a reviver is called for every value in the body, coordinates
|
|
188
|
+
* included, and the mask visits only the fields that can hold an instant, so a multi-megabyte
|
|
189
|
+
* itinerary costs about what `JSON.parse` alone does (`bench/dates.bench.ts` measures both).
|
|
190
|
+
*
|
|
191
|
+
* An empty body yields `undefined` rather than throwing. That case is real and routine:
|
|
192
|
+
* every delete of the Map Studio API answers `200` with a `NoData` envelope, and several
|
|
193
|
+
* endpoints answer with no body at all, so a parser that threw on empty would turn the
|
|
194
|
+
* success path of a whole verb into an error.
|
|
195
|
+
*
|
|
196
|
+
* @throws `SyntaxError` for a body that is not JSON, and `MountayaError` for a string the mask
|
|
197
|
+
* marks that names no instant.
|
|
198
|
+
*/
|
|
199
|
+
export const parseJsonWithDates = (text: string, mask?: DateMask): unknown =>
|
|
200
|
+
text.length > 0 ? decodeDates(JSON.parse(text), mask) : undefined;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decoding JSON a caller already holds.
|
|
3
|
+
*
|
|
4
|
+
* A response decodes its instants on the way in, by the mask its operation states, and reads null
|
|
5
|
+
* at each member its type marks required and nullable that the answer leaves out. JSON that
|
|
6
|
+
* reaches a program some other way (a fixture baked into a bundle, a record read back from a
|
|
7
|
+
* cache, a page of server-rendered state) meets no operation, so nothing decodes it, and a cast
|
|
8
|
+
* to the contract's type leaves every instant a string behind a type that says `Date`.
|
|
9
|
+
* {@link decodeRestSchema} is the same decoding, named by schema.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { restSchemaDates } from "../../rest/dates.gen.js";
|
|
13
|
+
import type { components } from "../../rest/schema.gen.js";
|
|
14
|
+
import { type DateMask, instantOf } from "./dates.js";
|
|
15
|
+
import { type NullableMask, withAbsentNulls } from "./nullMask.js";
|
|
16
|
+
import { restSchemaNulls } from "./nullMasks.gen.js";
|
|
17
|
+
|
|
18
|
+
/** The schemas the Map Studio contract declares, by the name the contract gives each. */
|
|
19
|
+
type RestSchemas = components["schemas"];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The mask of each schema that declares an instant, readable by any schema's name: a schema that
|
|
23
|
+
* declares none has no entry.
|
|
24
|
+
*/
|
|
25
|
+
const schemaDates: { readonly [schema: string]: DateMask } = restSchemaDates;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The mask of each schema whose values hold a member that reads null where they leave it out, by
|
|
29
|
+
* the schema's name: a schema whose values hold none has no entry.
|
|
30
|
+
*/
|
|
31
|
+
const schemaNulls: { readonly [schema: string]: NullableMask } = restSchemaNulls;
|
|
32
|
+
|
|
33
|
+
/** isList reports whether a value or a mask is a list, a read-only one included. */
|
|
34
|
+
const isList = (value: unknown): value is readonly unknown[] => Array.isArray(value);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* decodedCopy answers `value` with each instant `mask` names decoded to a `Date`, as a response
|
|
38
|
+
* decodes one, and never writes to `value`.
|
|
39
|
+
*
|
|
40
|
+
* It copies on the way back up: an object or a list is copied, one level, only where something
|
|
41
|
+
* under it decoded, and is answered as itself otherwise. So the answer shares with `value`
|
|
42
|
+
* everything that holds no decoded instant, and a value with nothing to decode is its own answer.
|
|
43
|
+
*
|
|
44
|
+
* It reaches what a response's decode reaches and no more: a field the mask names and the value
|
|
45
|
+
* owns, each member of a list where the mask declares a list. A value whose shape differs from its
|
|
46
|
+
* mask is left as it arrived, and so is a string that names no instant, which fails a response's
|
|
47
|
+
* decode: this decodes JSON the caller holds and validates none of it.
|
|
48
|
+
*/
|
|
49
|
+
const decodedCopy = (value: unknown, mask: DateMask): unknown => {
|
|
50
|
+
if (mask === true) {
|
|
51
|
+
return typeof value === "string" ? (instantOf(value) ?? value) : value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (isList(mask)) {
|
|
55
|
+
if (!isList(value)) {
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const [element] = mask;
|
|
60
|
+
let copy: unknown[] | undefined;
|
|
61
|
+
for (const [index, member] of value.entries()) {
|
|
62
|
+
const decoded = decodedCopy(member, element);
|
|
63
|
+
if (decoded !== member) {
|
|
64
|
+
copy ??= [...value];
|
|
65
|
+
copy[index] = decoded;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return copy ?? value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (typeof value !== "object" || value === null || isList(value)) {
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let copy: Record<string, unknown> | undefined;
|
|
77
|
+
for (const [field, fieldMask] of Object.entries(mask)) {
|
|
78
|
+
if (Object.hasOwn(value, field)) {
|
|
79
|
+
const current: unknown = Reflect.get(value, field);
|
|
80
|
+
const decoded = decodedCopy(current, fieldMask);
|
|
81
|
+
if (decoded !== current) {
|
|
82
|
+
copy ??= { ...value };
|
|
83
|
+
copy[field] = decoded;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return copy ?? value;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* decodeRestSchema answers `json` as the Map Studio schema `schema` names, with every instant that
|
|
93
|
+
* schema declares decoded to a `Date`, and null at every member it types required and nullable
|
|
94
|
+
* that `json` leaves out: the value a generated operation hands back, for JSON that reached the
|
|
95
|
+
* caller some other way, such as a fixture baked into a bundle or a record read back from a cache.
|
|
96
|
+
*
|
|
97
|
+
* It decodes what a response decodes and nothing else. A string becomes a `Date` only in a field
|
|
98
|
+
* the schema types as one, at any depth, and only where it is a full ISO 8601 timestamp carrying
|
|
99
|
+
* a zone and naming a real instant, so a `name` that holds a timestamp stays the string it is. A
|
|
100
|
+
* cast (`json as ItineraryWithDetails`) leaves every instant a string behind a type that says
|
|
101
|
+
* `Date`, which throws at the first `.getTime()`, and a member left out `undefined` behind a type
|
|
102
|
+
* that says null. A member the schema types optional is left out as it arrived.
|
|
103
|
+
*
|
|
104
|
+
* It decodes and never validates: the answer is typed by the schema whatever `json` holds, and a
|
|
105
|
+
* value whose shape differs from the schema is left as it arrived, as is a string in a field the
|
|
106
|
+
* schema declares an instant that names none, which fails the decode of a response. JSON from a
|
|
107
|
+
* source the caller does not trust is the caller's to validate, before this call.
|
|
108
|
+
*
|
|
109
|
+
* `json` is never written to, so a frozen value and a JSON module several importers share are
|
|
110
|
+
* safe to pass. Each object and list holding an instant that decodes, or leaving out a member that
|
|
111
|
+
* reads null, is copied, and everything else (a geometry, a list of plain strings) is shared with
|
|
112
|
+
* the answer. A value with nothing to decode comes back as itself: one decoded already, and any
|
|
113
|
+
* value of a schema that declares no instant and no member that reads null.
|
|
114
|
+
*
|
|
115
|
+
* @param schema - A name under `RestSchema.components["schemas"]`, such as
|
|
116
|
+
* `"ItineraryWithDetails"`.
|
|
117
|
+
* @param json - The parsed JSON of one value of that schema. A list of them is decoded by
|
|
118
|
+
* calling this once per member.
|
|
119
|
+
* @returns `json` as the schema's type, its declared instants decoded.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* declare const baked: unknown; // A fixture baked into the bundle, as JSON.parse answers it.
|
|
124
|
+
*
|
|
125
|
+
* const itinerary = decodeRestSchema("ItineraryWithDetails", baked);
|
|
126
|
+
*
|
|
127
|
+
* itinerary.created_at.getTime(); // A Date, as `getItinerary` answers one.
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export const decodeRestSchema = <N extends keyof RestSchemas>(
|
|
131
|
+
schema: N,
|
|
132
|
+
json: unknown,
|
|
133
|
+
): RestSchemas[N] =>
|
|
134
|
+
// The cast is this function's contract: the schema types the answer, and nothing is validated.
|
|
135
|
+
withAbsentNulls(
|
|
136
|
+
decodeSchemaInstants(schema, json),
|
|
137
|
+
Object.hasOwn(schemaNulls, schema) ? schemaNulls[schema] : undefined,
|
|
138
|
+
) as RestSchemas[N];
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* decodeSchemaInstants answers `json` with every instant the schema `schema` names declares
|
|
142
|
+
* decoded to a `Date`, as {@link decodeRestSchema} decodes them, and writes no null: for a value
|
|
143
|
+
* that holds a part of one of the schema's values, such as the members a webhook event's
|
|
144
|
+
* `previous_attributes` held before a change, where a member left out is one that did not change.
|
|
145
|
+
*
|
|
146
|
+
* @param schema - A name under `RestSchema.components["schemas"]`.
|
|
147
|
+
* @param json - The parsed JSON.
|
|
148
|
+
* @returns `json`, its declared instants decoded, never written to.
|
|
149
|
+
*/
|
|
150
|
+
export const decodeSchemaInstants = (schema: string, json: unknown): unknown => {
|
|
151
|
+
const mask = schemaDates[schema];
|
|
152
|
+
|
|
153
|
+
return mask === undefined ? json : decodedCopy(json, mask);
|
|
154
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The response envelope a Map Studio API or Tile API method resolves to.
|
|
3
|
+
*
|
|
4
|
+
* Every success those two surfaces answer with JSON is an envelope: the operation's payload at
|
|
5
|
+
* `data`, and beside it `extensions`, whose `metadata` carries what the operation documents (the
|
|
6
|
+
* cursor of a listing's next page, the id of a part a write created). A method hands the envelope
|
|
7
|
+
* back whole rather than its payload alone, so nothing an answer says beside its payload is lost
|
|
8
|
+
* on the way to the caller. The Data API answers no envelope: its methods resolve to the value of
|
|
9
|
+
* the operation's own field.
|
|
10
|
+
*
|
|
11
|
+
* Each generated method types its own answer by its operation. This is the shape every one of
|
|
12
|
+
* them shares, which a caller names to hold an answer, or to write one function over any of them.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { SuccessExtensions } from "../../rest/schema.gen.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* MountayaResponse is what a Map Studio API or Tile API method resolves to: the response envelope
|
|
19
|
+
* whole, its payload at `data` and the response-scoped metadata the operation documents at
|
|
20
|
+
* `extensions.metadata`.
|
|
21
|
+
*
|
|
22
|
+
* A resource the contract tags carries its tag as its own `etag` member inside `data`, exactly as
|
|
23
|
+
* the API sent it, and that tag is what a conditional write sends back as `ifMatch`. A write whose
|
|
24
|
+
* answer has no payload, a removal, answers `data` as `null`, and the envelope still comes back
|
|
25
|
+
* whole.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam T The payload's type.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import type { MountayaResponse } from "@mountaya/client";
|
|
32
|
+
*
|
|
33
|
+
* function payloadOf<T>(response: MountayaResponse<T>): T {
|
|
34
|
+
* return response.data;
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export interface MountayaResponse<T = unknown> extends SuccessExtensions {
|
|
39
|
+
/** The operation's payload, `null` on a write whose answer has none. */
|
|
40
|
+
readonly data: T;
|
|
41
|
+
}
|