@warp-drive/utilities 5.9.0-alpha.2 → 5.9.0-alpha.21
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/cjs-dist/string.cjs +448 -0
- package/cjs-dist/string.cjs.map +1 -0
- package/declarations/-private.d.ts +12 -1
- package/declarations/-private.d.ts.map +1 -0
- package/declarations/active-record.d.ts +260 -3
- package/declarations/active-record.d.ts.map +1 -0
- package/declarations/derivations.d.ts +26 -23
- package/declarations/derivations.d.ts.map +1 -0
- package/declarations/handlers.d.ts +282 -9
- package/declarations/handlers.d.ts.map +1 -0
- package/declarations/index-BeMAuqgZ.d.ts +462 -0
- package/declarations/index-BeMAuqgZ.d.ts.map +1 -0
- package/declarations/index.d.ts +2 -208
- package/declarations/json-api.d.ts +496 -5
- package/declarations/json-api.d.ts.map +1 -0
- package/declarations/rest.d.ts +258 -3
- package/declarations/rest.d.ts.map +1 -0
- package/declarations/string.d.ts +164 -13
- package/declarations/string.d.ts.map +1 -0
- package/dist/-private.js +2 -6
- package/dist/active-record.js +254 -379
- package/dist/active-record.js.map +1 -0
- package/dist/builder-utils-BkXwUT0x.js +16 -0
- package/dist/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/derivations.js +26 -26
- package/dist/derivations.js.map +1 -0
- package/dist/handlers.js +240 -283
- package/dist/handlers.js.map +1 -0
- package/dist/index.js +357 -335
- package/dist/index.js.map +1 -0
- package/dist/inflections-DzBsjJqj.js +80 -0
- package/dist/inflections-DzBsjJqj.js.map +1 -0
- package/dist/json-api.js +470 -706
- package/dist/json-api.js.map +1 -0
- package/dist/rest.js +254 -377
- package/dist/rest.js.map +1 -0
- package/dist/string-CWmAluxh.js +290 -0
- package/dist/string-CWmAluxh.js.map +1 -0
- package/dist/string.js +2 -427
- package/dist/unpkg/dev/-private.js +2 -6
- package/dist/unpkg/dev/active-record.js +253 -378
- package/dist/unpkg/dev/active-record.js.map +1 -0
- package/dist/unpkg/dev/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/dev/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/dev/derivations.js +26 -26
- package/dist/unpkg/dev/derivations.js.map +1 -0
- package/dist/unpkg/dev/handlers.js +238 -282
- package/dist/unpkg/dev/handlers.js.map +1 -0
- package/dist/unpkg/dev/index.js +356 -336
- package/dist/unpkg/dev/index.js.map +1 -0
- package/dist/unpkg/dev/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/dev/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/dev/json-api.js +469 -706
- package/dist/unpkg/dev/json-api.js.map +1 -0
- package/dist/unpkg/dev/rest.js +253 -376
- package/dist/unpkg/dev/rest.js.map +1 -0
- package/dist/unpkg/dev/string-j4ur-TDs.js +289 -0
- package/dist/unpkg/dev/string-j4ur-TDs.js.map +1 -0
- package/dist/unpkg/dev/string.js +3 -1
- package/dist/unpkg/dev-deprecated/-private.js +2 -6
- package/dist/unpkg/dev-deprecated/active-record.js +253 -378
- package/dist/unpkg/dev-deprecated/active-record.js.map +1 -0
- package/dist/unpkg/dev-deprecated/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/dev-deprecated/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/dev-deprecated/derivations.js +26 -26
- package/dist/unpkg/dev-deprecated/derivations.js.map +1 -0
- package/dist/unpkg/dev-deprecated/handlers.js +238 -282
- package/dist/unpkg/dev-deprecated/handlers.js.map +1 -0
- package/dist/unpkg/dev-deprecated/index.js +356 -336
- package/dist/unpkg/dev-deprecated/index.js.map +1 -0
- package/dist/unpkg/dev-deprecated/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/dev-deprecated/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/dev-deprecated/json-api.js +469 -706
- package/dist/unpkg/dev-deprecated/json-api.js.map +1 -0
- package/dist/unpkg/dev-deprecated/rest.js +253 -376
- package/dist/unpkg/dev-deprecated/rest.js.map +1 -0
- package/dist/unpkg/dev-deprecated/string-j4ur-TDs.js +289 -0
- package/dist/unpkg/dev-deprecated/string-j4ur-TDs.js.map +1 -0
- package/dist/unpkg/dev-deprecated/string.js +3 -1
- package/dist/unpkg/prod/-private.js +2 -6
- package/dist/unpkg/prod/active-record.js +236 -351
- package/dist/unpkg/prod/active-record.js.map +1 -0
- package/dist/unpkg/prod/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/prod/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/prod/derivations.js +26 -26
- package/dist/unpkg/prod/derivations.js.map +1 -0
- package/dist/unpkg/prod/handlers.js +231 -272
- package/dist/unpkg/prod/handlers.js.map +1 -0
- package/dist/unpkg/prod/index.js +265 -215
- package/dist/unpkg/prod/index.js.map +1 -0
- package/dist/unpkg/prod/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/prod/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/prod/json-api.js +446 -669
- package/dist/unpkg/prod/json-api.js.map +1 -0
- package/dist/unpkg/prod/rest.js +236 -349
- package/dist/unpkg/prod/rest.js.map +1 -0
- package/dist/unpkg/prod/string-T1ahjLdj.js +283 -0
- package/dist/unpkg/prod/string-T1ahjLdj.js.map +1 -0
- package/dist/unpkg/prod/string.js +3 -1
- package/dist/unpkg/prod-deprecated/-private.js +2 -6
- package/dist/unpkg/prod-deprecated/active-record.js +236 -351
- package/dist/unpkg/prod-deprecated/active-record.js.map +1 -0
- package/dist/unpkg/prod-deprecated/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/prod-deprecated/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/prod-deprecated/derivations.js +26 -26
- package/dist/unpkg/prod-deprecated/derivations.js.map +1 -0
- package/dist/unpkg/prod-deprecated/handlers.js +231 -272
- package/dist/unpkg/prod-deprecated/handlers.js.map +1 -0
- package/dist/unpkg/prod-deprecated/index.js +265 -215
- package/dist/unpkg/prod-deprecated/index.js.map +1 -0
- package/dist/unpkg/prod-deprecated/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/prod-deprecated/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/prod-deprecated/json-api.js +446 -669
- package/dist/unpkg/prod-deprecated/json-api.js.map +1 -0
- package/dist/unpkg/prod-deprecated/rest.js +236 -349
- package/dist/unpkg/prod-deprecated/rest.js.map +1 -0
- package/dist/unpkg/prod-deprecated/string-T1ahjLdj.js +283 -0
- package/dist/unpkg/prod-deprecated/string-T1ahjLdj.js.map +1 -0
- package/dist/unpkg/prod-deprecated/string.js +3 -1
- package/package.json +11 -10
- package/declarations/-private/active-record/find-record.d.ts +0 -64
- package/declarations/-private/active-record/query.d.ts +0 -53
- package/declarations/-private/active-record/save-record.d.ts +0 -146
- package/declarations/-private/builder-utils.d.ts +0 -4
- package/declarations/-private/handlers/auto-compress.d.ts +0 -158
- package/declarations/-private/handlers/gated.d.ts +0 -19
- package/declarations/-private/handlers/meta-doc.d.ts +0 -47
- package/declarations/-private/handlers/utils.d.ts +0 -41
- package/declarations/-private/json-api/-utils.d.ts +0 -109
- package/declarations/-private/json-api/find-record.d.ts +0 -84
- package/declarations/-private/json-api/query.d.ts +0 -100
- package/declarations/-private/json-api/save-record.d.ts +0 -228
- package/declarations/-private/json-api/serialize.d.ts +0 -70
- package/declarations/-private/rest/find-record.d.ts +0 -62
- package/declarations/-private/rest/query.d.ts +0 -53
- package/declarations/-private/rest/save-record.d.ts +0 -146
- package/declarations/-private/string/inflect.d.ts +0 -82
- package/declarations/-private/string/inflections.d.ts +0 -9
- package/declarations/-private/string/transform.d.ts +0 -77
- package/dist/builder-utils-Donkk-BZ.js +0 -22
- package/dist/inflect-BSG696t-.js +0 -344
- package/dist/string.cjs +0 -430
- package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/dev/inflect-BEv8WqY1.js +0 -343
- package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/dev-deprecated/inflect-BEv8WqY1.js +0 -343
- package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/prod/inflect-Dh9dyEYx.js +0 -333
- package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/prod-deprecated/inflect-Dh9dyEYx.js +0 -333
|
@@ -1,740 +1,503 @@
|
|
|
1
|
-
import { setBuildURLConfig as setBuildURLConfig$1
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { recordIdentifierFor } from
|
|
5
|
-
|
|
1
|
+
import { buildBaseURL, buildQueryParams, setBuildURLConfig as setBuildURLConfig$1 } from "./index.js";
|
|
2
|
+
import { s as pluralize } from "./string-j4ur-TDs.js";
|
|
3
|
+
import { n as extractCacheOptions, t as copyForwardUrlOptions } from "./builder-utils-BkXwUT0x.js";
|
|
4
|
+
import { recordIdentifierFor } from "@warp-drive/core";
|
|
5
|
+
|
|
6
|
+
//#region src/-private/json-api/-utils.ts
|
|
7
|
+
const JsonApiAccept = "application/vnd.api+json";
|
|
6
8
|
const DEFAULT_CONFIG = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
host: "",
|
|
10
|
+
namespace: ""
|
|
9
11
|
};
|
|
10
|
-
let
|
|
11
|
-
|
|
12
|
+
let CONFIG = DEFAULT_CONFIG;
|
|
13
|
+
let ACCEPT_HEADER_VALUE = "application/vnd.api+json";
|
|
12
14
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
15
|
+
* Allows setting extensions and profiles to be used in the `Accept` header.
|
|
16
|
+
*
|
|
17
|
+
* Extensions and profiles are keyed by their namespace with the value being
|
|
18
|
+
* their URI.
|
|
19
|
+
*
|
|
20
|
+
* Example:
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* setBuildURLConfig({
|
|
24
|
+
* extensions: {
|
|
25
|
+
* atomic: 'https://jsonapi.org/ext/atomic'
|
|
26
|
+
* },
|
|
27
|
+
* profiles: {
|
|
28
|
+
* pagination: 'https://jsonapi.org/profiles/ethanresnick/cursor-pagination'
|
|
29
|
+
* }
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* This also sets the global configuration for `buildBaseURL`
|
|
34
|
+
* for host and namespace values for the global coniguration
|
|
35
|
+
* done via `import { setBuildURLConfig } from '@warp-drive/utilities';`
|
|
36
|
+
*
|
|
37
|
+
* These values may still be overridden by passing
|
|
38
|
+
* them to buildBaseURL directly.
|
|
39
|
+
*
|
|
40
|
+
* This method may be called as many times as needed
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* type BuildURLConfig = {
|
|
44
|
+
* host: string;
|
|
45
|
+
* namespace: string'
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
* @param {BuildURLConfig} config
|
|
51
|
+
* @return {void}
|
|
52
|
+
*/
|
|
51
53
|
function setBuildURLConfig(config) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
ACCEPT_HEADER_VALUE = accept;
|
|
68
|
-
}
|
|
69
|
-
setBuildURLConfig$1(config);
|
|
54
|
+
CONFIG = Object.assign({}, DEFAULT_CONFIG, config);
|
|
55
|
+
if (config.profiles || config.extensions) {
|
|
56
|
+
let accept = JsonApiAccept;
|
|
57
|
+
if (config.profiles) {
|
|
58
|
+
const profiles = Object.values(config.profiles);
|
|
59
|
+
if (profiles.length) accept += ";profile=\"" + profiles.join(" ") + "\"";
|
|
60
|
+
}
|
|
61
|
+
if (config.extensions) {
|
|
62
|
+
const extensions = Object.values(config.extensions);
|
|
63
|
+
if (extensions.length) accept += ";ext=" + extensions.join(" ");
|
|
64
|
+
}
|
|
65
|
+
ACCEPT_HEADER_VALUE = accept;
|
|
66
|
+
}
|
|
67
|
+
setBuildURLConfig$1(config);
|
|
70
68
|
}
|
|
71
69
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
* configured for the url and header expectations of most JSON:API APIs.
|
|
75
|
-
*
|
|
76
|
-
* :::tabs
|
|
77
|
-
*
|
|
78
|
-
* == Basic Usage
|
|
79
|
-
*
|
|
80
|
-
* ```ts
|
|
81
|
-
* import { findRecord } from '@warp-drive/utilities/json-api';
|
|
82
|
-
* import type { Person } from '#/data/types';
|
|
83
|
-
*
|
|
84
|
-
* const result = await store.request(
|
|
85
|
-
* findRecord<Person>('person', '1')
|
|
86
|
-
* );
|
|
87
|
-
* ```
|
|
88
|
-
*
|
|
89
|
-
* == With Options
|
|
90
|
-
*
|
|
91
|
-
* ```ts
|
|
92
|
-
* import { findRecord } from '@warp-drive/utilities/json-api';
|
|
93
|
-
* import type { Person } from '#/data/types';
|
|
94
|
-
*
|
|
95
|
-
* const data = await store.request(
|
|
96
|
-
* findRecord<Person>(
|
|
97
|
-
* 'person', '1',
|
|
98
|
-
* { include: ['pets', 'friends'] }
|
|
99
|
-
* )
|
|
100
|
-
* );
|
|
101
|
-
* ```
|
|
102
|
-
*
|
|
103
|
-
* == With an Identifier
|
|
104
|
-
*
|
|
105
|
-
* ```ts
|
|
106
|
-
* import { findRecord } from '@warp-drive/utilities/json-api';
|
|
107
|
-
* import type { Person } from '#/data/types';
|
|
108
|
-
*
|
|
109
|
-
* const data = await store.request(
|
|
110
|
-
* findRecord<Person>(
|
|
111
|
-
* { type: 'person', id: '1' },
|
|
112
|
-
* { include: ['pets', 'friends'] }
|
|
113
|
-
* )
|
|
114
|
-
* );
|
|
115
|
-
* ```
|
|
116
|
-
*
|
|
117
|
-
* :::
|
|
118
|
-
*
|
|
119
|
-
* **Supplying Options to Modify the Request Behavior**
|
|
120
|
-
*
|
|
121
|
-
* The following options are supported:
|
|
122
|
-
*
|
|
123
|
-
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
124
|
-
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
125
|
-
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
126
|
-
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
127
|
-
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
128
|
-
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
129
|
-
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
130
|
-
* defaulting to `false` if none is configured.
|
|
131
|
-
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
132
|
-
*
|
|
133
|
-
* ```ts
|
|
134
|
-
* import { findRecord } from '@warp-drive/utilities/json-api';
|
|
135
|
-
*
|
|
136
|
-
* const data = await store.request(
|
|
137
|
-
* findRecord(
|
|
138
|
-
* 'person', '1',
|
|
139
|
-
* { include: ['pets', 'friends'] },
|
|
140
|
-
* { namespace: 'api/v2' }
|
|
141
|
-
* )
|
|
142
|
-
* );
|
|
143
|
-
* ```
|
|
144
|
-
*
|
|
145
|
-
* @public
|
|
146
|
-
*/
|
|
147
|
-
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/-private/json-api/find-record.ts
|
|
148
72
|
function findRecord(arg1, arg2, arg3) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
records: [identifier]
|
|
173
|
-
};
|
|
73
|
+
const identifier = typeof arg1 === "string" ? {
|
|
74
|
+
type: arg1,
|
|
75
|
+
id: arg2
|
|
76
|
+
} : arg1;
|
|
77
|
+
const options = (typeof arg1 === "string" ? arg3 : arg2) || {};
|
|
78
|
+
const cacheOptions = extractCacheOptions(options);
|
|
79
|
+
const urlOptions = {
|
|
80
|
+
identifier,
|
|
81
|
+
op: "findRecord",
|
|
82
|
+
resourcePath: pluralize(identifier.type)
|
|
83
|
+
};
|
|
84
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
85
|
+
const url = buildBaseURL(urlOptions);
|
|
86
|
+
const headers = new Headers();
|
|
87
|
+
headers.append("Accept", ACCEPT_HEADER_VALUE);
|
|
88
|
+
return {
|
|
89
|
+
url: options.include?.length ? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}` : url,
|
|
90
|
+
method: "GET",
|
|
91
|
+
headers,
|
|
92
|
+
cacheOptions,
|
|
93
|
+
op: "findRecord",
|
|
94
|
+
records: [identifier]
|
|
95
|
+
};
|
|
174
96
|
}
|
|
175
97
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
* import { query } from '@warp-drive/utilities/json-api';
|
|
198
|
-
*
|
|
199
|
-
* const options = query('person', { include: ['pets', 'friends'] });
|
|
200
|
-
* const data = await store.request(options);
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* **Supplying Options to Modify the Request Behavior**
|
|
204
|
-
*
|
|
205
|
-
* The following options are supported:
|
|
206
|
-
*
|
|
207
|
-
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
208
|
-
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
209
|
-
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
210
|
-
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
211
|
-
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
212
|
-
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
213
|
-
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
214
|
-
* defaulting to `false` if none is configured.
|
|
215
|
-
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
216
|
-
*
|
|
217
|
-
* ```ts
|
|
218
|
-
* import { query } from '@warp-drive/utilities/json-api';
|
|
219
|
-
*
|
|
220
|
-
* const options = query('person', { include: ['pets', 'friends'] }, { reload: true });
|
|
221
|
-
* const data = await store.request(options);
|
|
222
|
-
* ```
|
|
223
|
-
*
|
|
224
|
-
* @public
|
|
225
|
-
*/
|
|
226
|
-
|
|
227
|
-
function query(type,
|
|
228
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
229
|
-
query = {}, options = {}) {
|
|
230
|
-
const cacheOptions = extractCacheOptions(options);
|
|
231
|
-
const urlOptions = {
|
|
232
|
-
identifier: {
|
|
233
|
-
type
|
|
234
|
-
},
|
|
235
|
-
op: 'query',
|
|
236
|
-
resourcePath: pluralize(type)
|
|
237
|
-
};
|
|
238
|
-
copyForwardUrlOptions(urlOptions, options);
|
|
239
|
-
const url = buildBaseURL(urlOptions);
|
|
240
|
-
const headers = new Headers();
|
|
241
|
-
headers.append('Accept', ACCEPT_HEADER_VALUE);
|
|
242
|
-
const queryString = buildQueryParams(query, options.urlParamsSettings);
|
|
243
|
-
return {
|
|
244
|
-
url: queryString ? `${url}?${queryString}` : url,
|
|
245
|
-
method: 'GET',
|
|
246
|
-
headers,
|
|
247
|
-
cacheOptions,
|
|
248
|
-
op: 'query'
|
|
249
|
-
};
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/-private/json-api/query.ts
|
|
100
|
+
function query(type, query = {}, options = {}) {
|
|
101
|
+
const cacheOptions = extractCacheOptions(options);
|
|
102
|
+
const urlOptions = {
|
|
103
|
+
identifier: { type },
|
|
104
|
+
op: "query",
|
|
105
|
+
resourcePath: pluralize(type)
|
|
106
|
+
};
|
|
107
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
108
|
+
const url = buildBaseURL(urlOptions);
|
|
109
|
+
const headers = new Headers();
|
|
110
|
+
headers.append("Accept", ACCEPT_HEADER_VALUE);
|
|
111
|
+
const queryString = buildQueryParams(query, options.urlParamsSettings);
|
|
112
|
+
return {
|
|
113
|
+
url: queryString ? `${url}?${queryString}` : url,
|
|
114
|
+
method: "GET",
|
|
115
|
+
headers,
|
|
116
|
+
cacheOptions,
|
|
117
|
+
op: "query"
|
|
118
|
+
};
|
|
250
119
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
* The following options are supported:
|
|
273
|
-
*
|
|
274
|
-
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
275
|
-
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
276
|
-
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
277
|
-
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
278
|
-
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
279
|
-
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
280
|
-
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
281
|
-
* defaulting to `false` if none is configured.
|
|
282
|
-
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
283
|
-
*
|
|
284
|
-
* ```ts
|
|
285
|
-
* import { postQuery } from '@warp-drive/utilities/json-api';
|
|
286
|
-
*
|
|
287
|
-
* const options = postQuery('person', { include: ['pets', 'friends'] }, { reload: true });
|
|
288
|
-
* const data = await store.request(options);
|
|
289
|
-
* ```
|
|
290
|
-
*
|
|
291
|
-
* @public
|
|
292
|
-
* @param identifier
|
|
293
|
-
* @param query
|
|
294
|
-
* @param options
|
|
295
|
-
*/
|
|
296
|
-
|
|
297
|
-
function postQuery(type,
|
|
298
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
299
|
-
query = {}, options = {}) {
|
|
300
|
-
const cacheOptions = extractCacheOptions(options);
|
|
301
|
-
const urlOptions = {
|
|
302
|
-
identifier: {
|
|
303
|
-
type
|
|
304
|
-
},
|
|
305
|
-
op: 'query',
|
|
306
|
-
resourcePath: options.resourcePath ?? pluralize(type)
|
|
307
|
-
};
|
|
308
|
-
copyForwardUrlOptions(urlOptions, options);
|
|
309
|
-
const url = buildBaseURL(urlOptions);
|
|
310
|
-
const headers = new Headers();
|
|
311
|
-
headers.append('Accept', ACCEPT_HEADER_VALUE);
|
|
312
|
-
const queryData = structuredClone(query);
|
|
313
|
-
cacheOptions.key = cacheOptions.key ?? `${url}?${buildQueryParams(queryData, options.urlParamsSettings)}`;
|
|
314
|
-
return {
|
|
315
|
-
url,
|
|
316
|
-
method: 'POST',
|
|
317
|
-
body: JSON.stringify(query),
|
|
318
|
-
headers,
|
|
319
|
-
cacheOptions: cacheOptions,
|
|
320
|
-
op: 'query'
|
|
321
|
-
};
|
|
120
|
+
function postQuery(type, query = {}, options = {}) {
|
|
121
|
+
const cacheOptions = extractCacheOptions(options);
|
|
122
|
+
const urlOptions = {
|
|
123
|
+
identifier: { type },
|
|
124
|
+
op: "query",
|
|
125
|
+
resourcePath: options.resourcePath ?? pluralize(type)
|
|
126
|
+
};
|
|
127
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
128
|
+
const url = buildBaseURL(urlOptions);
|
|
129
|
+
const headers = new Headers();
|
|
130
|
+
headers.append("Accept", ACCEPT_HEADER_VALUE);
|
|
131
|
+
const queryData = structuredClone(query);
|
|
132
|
+
cacheOptions.key = cacheOptions.key ?? `${url}?${buildQueryParams(queryData, options.urlParamsSettings)}`;
|
|
133
|
+
return {
|
|
134
|
+
url,
|
|
135
|
+
method: "POST",
|
|
136
|
+
body: JSON.stringify(query),
|
|
137
|
+
headers,
|
|
138
|
+
cacheOptions,
|
|
139
|
+
op: "query"
|
|
140
|
+
};
|
|
322
141
|
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/-private/json-api/save-record.ts
|
|
323
145
|
function isExisting(identifier) {
|
|
324
|
-
|
|
146
|
+
return "id" in identifier && identifier.id !== null && "type" in identifier && identifier.type !== null;
|
|
325
147
|
}
|
|
326
|
-
|
|
327
148
|
/**
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
149
|
+
* :::warning ⚠️ **These Mutation Builders DO NOT Set The Request Body**
|
|
150
|
+
* While this may come as a surprise, the app providing the body ensures that only
|
|
151
|
+
* desired and correctly formatted data is sent with the request.
|
|
152
|
+
* :::
|
|
153
|
+
*
|
|
154
|
+
* Builds request options to delete record for resources,
|
|
155
|
+
* configured for the url, method and header expectations of most JSON:API APIs.
|
|
156
|
+
*
|
|
157
|
+
* **Basic Usage**
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { deleteRecord } from '@warp-drive/utilities/json-api';
|
|
161
|
+
*
|
|
162
|
+
* const person = store.peekRecord('person', '1');
|
|
163
|
+
*
|
|
164
|
+
* // mark record as deleted
|
|
165
|
+
* store.deleteRecord(person);
|
|
166
|
+
*
|
|
167
|
+
* // persist deletion
|
|
168
|
+
* const data = await store.request(deleteRecord(person));
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* **Supplying Options to Modify the Request Behavior**
|
|
172
|
+
*
|
|
173
|
+
* The following options are supported:
|
|
174
|
+
*
|
|
175
|
+
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
176
|
+
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
177
|
+
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
178
|
+
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
179
|
+
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
180
|
+
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
181
|
+
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
182
|
+
* defaulting to `false` if none is configured.
|
|
183
|
+
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
184
|
+
*
|
|
185
|
+
* ```ts
|
|
186
|
+
* import { deleteRecord } from '@warp-drive/utilities/json-api';
|
|
187
|
+
*
|
|
188
|
+
* const person = store.peekRecord('person', '1');
|
|
189
|
+
*
|
|
190
|
+
* // mark record as deleted
|
|
191
|
+
* store.deleteRecord(person);
|
|
192
|
+
*
|
|
193
|
+
* // persist deletion
|
|
194
|
+
* const options = deleteRecord(person, { namespace: 'api/v1' });
|
|
195
|
+
* const data = await store.request(options);
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @public
|
|
199
|
+
* @param record
|
|
200
|
+
* @param options
|
|
201
|
+
*/
|
|
382
202
|
function deleteRecord(record, options = {}) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
op: 'deleteRecord',
|
|
408
|
-
data: {
|
|
409
|
-
record: identifier
|
|
410
|
-
},
|
|
411
|
-
records: [identifier]
|
|
412
|
-
};
|
|
203
|
+
const identifier = recordIdentifierFor(record);
|
|
204
|
+
((test) => {
|
|
205
|
+
if (!test) throw new Error(`Expected to be given a record instance`);
|
|
206
|
+
})(identifier);
|
|
207
|
+
((test) => {
|
|
208
|
+
if (!test) throw new Error(`Cannot delete a record that does not have an associated type and id.`);
|
|
209
|
+
})(isExisting(identifier));
|
|
210
|
+
const urlOptions = {
|
|
211
|
+
identifier,
|
|
212
|
+
op: "deleteRecord",
|
|
213
|
+
resourcePath: pluralize(identifier.type)
|
|
214
|
+
};
|
|
215
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
216
|
+
const url = buildBaseURL(urlOptions);
|
|
217
|
+
const headers = new Headers();
|
|
218
|
+
headers.append("Accept", ACCEPT_HEADER_VALUE);
|
|
219
|
+
return {
|
|
220
|
+
url,
|
|
221
|
+
method: "DELETE",
|
|
222
|
+
headers,
|
|
223
|
+
op: "deleteRecord",
|
|
224
|
+
data: { record: identifier },
|
|
225
|
+
records: [identifier]
|
|
226
|
+
};
|
|
413
227
|
}
|
|
414
|
-
|
|
415
228
|
/**
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
229
|
+
* :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
|
|
230
|
+
* While this may come as a surprise, the app providing the body ensures that only
|
|
231
|
+
* desired and correctly formatted data is sent with the request.
|
|
232
|
+
* :::
|
|
233
|
+
*
|
|
234
|
+
* Builds request options to create new record for resources,
|
|
235
|
+
* configured for the url, method and header expectations of most JSON:API APIs.
|
|
236
|
+
*
|
|
237
|
+
* **Basic Usage**
|
|
238
|
+
*
|
|
239
|
+
* ```ts
|
|
240
|
+
* import { cacheKeyFor } from '@warp-drive/core';
|
|
241
|
+
* import { createRecord } from '@warp-drive/utilities/json-api';
|
|
242
|
+
* import type { Person } from '#/data/types';
|
|
243
|
+
*
|
|
244
|
+
* const person = store.createRecord<Person>('person', { name: 'Ted' });
|
|
245
|
+
* const init = createRecord(person);
|
|
246
|
+
* init.body = JSON.stringify(
|
|
247
|
+
* {
|
|
248
|
+
* // it's likely you will want to transform this data
|
|
249
|
+
* // somewhat
|
|
250
|
+
* data: store.cache.peek(cacheKeyFor(person))
|
|
251
|
+
* }
|
|
252
|
+
* );
|
|
253
|
+
* const data = await store.request(init);
|
|
254
|
+
* ```
|
|
255
|
+
*
|
|
256
|
+
* **Supplying Options to Modify the Request Behavior**
|
|
257
|
+
*
|
|
258
|
+
* The following options are supported:
|
|
259
|
+
*
|
|
260
|
+
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
261
|
+
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
262
|
+
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
263
|
+
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
264
|
+
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
265
|
+
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
266
|
+
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
267
|
+
* defaulting to `false` if none is configured.
|
|
268
|
+
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
269
|
+
*
|
|
270
|
+
* ```ts
|
|
271
|
+
* import { createRecord } from '@warp-drive/utilities/json-api';
|
|
272
|
+
*
|
|
273
|
+
* const person = store.createRecord('person', { name: 'Ted' });
|
|
274
|
+
* const options = createRecord(person, { namespace: 'api/v1' });
|
|
275
|
+
* const data = await store.request(options);
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* @public
|
|
279
|
+
* @param record
|
|
280
|
+
* @param options
|
|
281
|
+
*/
|
|
470
282
|
function createRecord(record, options = {}) {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
record: identifier
|
|
493
|
-
},
|
|
494
|
-
records: [identifier]
|
|
495
|
-
};
|
|
283
|
+
const identifier = recordIdentifierFor(record);
|
|
284
|
+
((test) => {
|
|
285
|
+
if (!test) throw new Error(`Expected to be given a record instance`);
|
|
286
|
+
})(identifier);
|
|
287
|
+
const urlOptions = {
|
|
288
|
+
identifier,
|
|
289
|
+
op: "createRecord",
|
|
290
|
+
resourcePath: pluralize(identifier.type)
|
|
291
|
+
};
|
|
292
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
293
|
+
const url = buildBaseURL(urlOptions);
|
|
294
|
+
const headers = new Headers();
|
|
295
|
+
headers.append("Accept", ACCEPT_HEADER_VALUE);
|
|
296
|
+
return {
|
|
297
|
+
url,
|
|
298
|
+
method: "POST",
|
|
299
|
+
headers,
|
|
300
|
+
op: "createRecord",
|
|
301
|
+
data: { record: identifier },
|
|
302
|
+
records: [identifier]
|
|
303
|
+
};
|
|
496
304
|
}
|
|
497
|
-
|
|
498
305
|
/**
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
306
|
+
* :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
|
|
307
|
+
* While this may come as a surprise, the app providing the body ensures that only
|
|
308
|
+
* desired and correctly formatted data is sent with the request.
|
|
309
|
+
* :::
|
|
310
|
+
*
|
|
311
|
+
* Builds request options to update existing record for resources,
|
|
312
|
+
* configured for the url, method and header expectations of most JSON:API APIs.
|
|
313
|
+
*
|
|
314
|
+
* **Example Usage**
|
|
315
|
+
*
|
|
316
|
+
* ```ts
|
|
317
|
+
* import { cacheKeyFor } from '@warp-drive/core';
|
|
318
|
+
* import { updateRecord } from '@warp-drive/utilities/json-api';
|
|
319
|
+
* import type { EditablePerson } from '#/data/types';
|
|
320
|
+
*
|
|
321
|
+
* const mutable = await checkout<EditablePerson>(person);
|
|
322
|
+
* mutable.name = 'Chris';
|
|
323
|
+
* const init = updateRecord(mutable);
|
|
324
|
+
*
|
|
325
|
+
* init.body = JSON.stringify(
|
|
326
|
+
* // it's likely you will want to transform this data
|
|
327
|
+
* // somewhat, or serialize only specific properties instead
|
|
328
|
+
* serializePatch(store.cache, cacheKeyFor(mutable))
|
|
329
|
+
* );
|
|
330
|
+
* const data = await store.request(init);
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
*
|
|
334
|
+
* **Supplying Options to Modify the Request Behavior**
|
|
335
|
+
*
|
|
336
|
+
* The following options are supported:
|
|
337
|
+
*
|
|
338
|
+
* - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
|
|
339
|
+
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
340
|
+
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
341
|
+
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
342
|
+
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
343
|
+
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
344
|
+
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
345
|
+
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
346
|
+
* defaulting to `false` if none is configured.
|
|
347
|
+
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
348
|
+
*
|
|
349
|
+
* ```ts
|
|
350
|
+
* import { updateRecord } from '@warp-drive/utilities/json-api';
|
|
351
|
+
*
|
|
352
|
+
* const person = store.peekRecord('person', '1');
|
|
353
|
+
* person.name = 'Chris';
|
|
354
|
+
* const options = updateRecord(person, { patch: true });
|
|
355
|
+
* const data = await store.request(options);
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @public
|
|
359
|
+
* @param record
|
|
360
|
+
* @param options
|
|
361
|
+
*/
|
|
556
362
|
function updateRecord(record, options = {}) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
op: 'updateRecord',
|
|
582
|
-
data: {
|
|
583
|
-
record: identifier
|
|
584
|
-
},
|
|
585
|
-
records: [identifier]
|
|
586
|
-
};
|
|
363
|
+
const identifier = recordIdentifierFor(record);
|
|
364
|
+
((test) => {
|
|
365
|
+
if (!test) throw new Error(`Expected to be given a record instance`);
|
|
366
|
+
})(identifier);
|
|
367
|
+
((test) => {
|
|
368
|
+
if (!test) throw new Error(`Cannot update a record that does not have an associated type and id.`);
|
|
369
|
+
})(isExisting(identifier));
|
|
370
|
+
const urlOptions = {
|
|
371
|
+
identifier,
|
|
372
|
+
op: "updateRecord",
|
|
373
|
+
resourcePath: pluralize(identifier.type)
|
|
374
|
+
};
|
|
375
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
376
|
+
const url = buildBaseURL(urlOptions);
|
|
377
|
+
const headers = new Headers();
|
|
378
|
+
headers.append("Accept", ACCEPT_HEADER_VALUE);
|
|
379
|
+
return {
|
|
380
|
+
url,
|
|
381
|
+
method: options.patch ? "PATCH" : "PUT",
|
|
382
|
+
headers,
|
|
383
|
+
op: "updateRecord",
|
|
384
|
+
data: { record: identifier },
|
|
385
|
+
records: [identifier]
|
|
386
|
+
};
|
|
587
387
|
}
|
|
588
388
|
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/-private/json-api/serialize.ts
|
|
589
391
|
/**
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
392
|
+
* :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
|
|
393
|
+
*
|
|
394
|
+
* While this may come as a surprise, they are intended to serialize cache state for more
|
|
395
|
+
* generalized usage. {json:api} has a large variance in acceptable shapes, and only your
|
|
396
|
+
* app can ensure that the body is correctly formatted and contains all necessary data.
|
|
397
|
+
* :::
|
|
398
|
+
*
|
|
399
|
+
* Serializes the current state of a resource or array of resources for use with POST or PUT requests.
|
|
400
|
+
*
|
|
401
|
+
* @public
|
|
402
|
+
* @param cache - the cache to serialize the resource(s) from
|
|
403
|
+
* @param identifiers - the resource(s) to serialize
|
|
404
|
+
* @return an object with a `data` property containing the serialized resource(s)
|
|
405
|
+
*/
|
|
605
406
|
function serializeResources(cache, identifiers) {
|
|
606
|
-
|
|
607
|
-
data: Array.isArray(identifiers) ? identifiers.map(identifier => _serializeResource(cache, identifier)) : _serializeResource(cache, identifiers)
|
|
608
|
-
};
|
|
407
|
+
return { data: Array.isArray(identifiers) ? identifiers.map((identifier) => _serializeResource(cache, identifier)) : _serializeResource(cache, identifiers) };
|
|
609
408
|
}
|
|
610
|
-
function fixRef({
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
621
|
-
return {
|
|
622
|
-
id,
|
|
623
|
-
lid,
|
|
624
|
-
type
|
|
625
|
-
};
|
|
409
|
+
function fixRef({ id, lid, type }) {
|
|
410
|
+
if (id !== null) return {
|
|
411
|
+
id,
|
|
412
|
+
type
|
|
413
|
+
};
|
|
414
|
+
return {
|
|
415
|
+
id,
|
|
416
|
+
lid,
|
|
417
|
+
type
|
|
418
|
+
};
|
|
626
419
|
}
|
|
627
420
|
function fixRelData(rel) {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
return fixRef(rel);
|
|
632
|
-
}
|
|
633
|
-
return null;
|
|
421
|
+
if (Array.isArray(rel)) return rel.map((ref) => fixRef(ref));
|
|
422
|
+
else if (typeof rel === "object" && rel !== null) return fixRef(rel);
|
|
423
|
+
return null;
|
|
634
424
|
}
|
|
635
425
|
function _serializeResource(cache, identifier) {
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
// remove lid from anything that has an ID and slice any relationship arrays
|
|
651
|
-
if (record.id !== null) {
|
|
652
|
-
delete record.lid;
|
|
653
|
-
}
|
|
654
|
-
if (record.relationships) {
|
|
655
|
-
for (const key of Object.keys(record.relationships)) {
|
|
656
|
-
const relationship = record.relationships[key];
|
|
657
|
-
if (Array.isArray(relationship.data)) {
|
|
658
|
-
relationship.data = relationship.data.map(ref => fixRef(ref));
|
|
659
|
-
} else if (typeof relationship.data === 'object' && relationship.data !== null) {
|
|
660
|
-
relationship.data = fixRef(relationship.data);
|
|
661
|
-
} else if (Object.keys(relationship ?? {}).length === 0) {
|
|
662
|
-
delete record.relationships[key];
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
return record;
|
|
426
|
+
const { id, lid, type } = identifier;
|
|
427
|
+
const record = structuredClone(cache.peek(identifier));
|
|
428
|
+
((test) => {
|
|
429
|
+
if (!test) throw new Error(`A record with id ${String(id)} and type ${type} for lid ${lid} was not found not in the supplied Cache.`);
|
|
430
|
+
})(record);
|
|
431
|
+
if (record.id !== null) delete record.lid;
|
|
432
|
+
if (record.relationships) for (const key of Object.keys(record.relationships)) {
|
|
433
|
+
const relationship = record.relationships[key];
|
|
434
|
+
if (Array.isArray(relationship.data)) relationship.data = relationship.data.map((ref) => fixRef(ref));
|
|
435
|
+
else if (typeof relationship.data === "object" && relationship.data !== null) relationship.data = fixRef(relationship.data);
|
|
436
|
+
else if (Object.keys(relationship ?? {}).length === 0) delete record.relationships[key];
|
|
437
|
+
}
|
|
438
|
+
return record;
|
|
667
439
|
}
|
|
668
|
-
|
|
669
440
|
/**
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
441
|
+
* :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
|
|
442
|
+
*
|
|
443
|
+
* While this may come as a surprise, they are intended to serialize cache state for more
|
|
444
|
+
* generalized usage. {json:api} has a large variance in acceptable shapes, and only your
|
|
445
|
+
* app can ensure that the body is correctly formatted and contains all necessary data.
|
|
446
|
+
* :::
|
|
447
|
+
*
|
|
448
|
+
* Serializes changes to a resource. Useful for use with building bodies for PATCH requests.
|
|
449
|
+
*
|
|
450
|
+
* Only attributes which are changed are serialized.
|
|
451
|
+
* Only relationships which are changed are serialized.
|
|
452
|
+
*
|
|
453
|
+
* Collection relationships serialize the collection as a whole.
|
|
454
|
+
*
|
|
455
|
+
* If you would like to serialize updates to a collection more granularly
|
|
456
|
+
* (for instance, as operations) request the diff from the store and
|
|
457
|
+
* serialize as desired:
|
|
458
|
+
*
|
|
459
|
+
* ```ts
|
|
460
|
+
* const relationshipDiffMap = cache.changedRelationships(identifier);
|
|
461
|
+
* ```
|
|
462
|
+
*
|
|
463
|
+
* @public
|
|
464
|
+
* @param cache - the cache to serialize the resource's changes from
|
|
465
|
+
* @param identifier - the resource whose changes should be serialized
|
|
466
|
+
* @return an object with a `data` property containing the serialized resource patch
|
|
467
|
+
*/
|
|
697
468
|
function serializePatch(cache, identifier) {
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
const relationships = {};
|
|
729
|
-
changedRelationships.forEach((diff, key) => {
|
|
730
|
-
relationships[key] = {
|
|
731
|
-
data: fixRelData(diff.localState)
|
|
732
|
-
};
|
|
733
|
-
});
|
|
734
|
-
data.relationships = relationships;
|
|
735
|
-
}
|
|
736
|
-
return {
|
|
737
|
-
data
|
|
738
|
-
};
|
|
469
|
+
const { id, lid, type } = identifier;
|
|
470
|
+
((test) => {
|
|
471
|
+
if (!test) throw new Error(`A record with id ${String(id)} and type ${type} for lid ${lid} was not found not in the supplied Cache.`);
|
|
472
|
+
})(cache.peek(identifier));
|
|
473
|
+
const data = id === null ? {
|
|
474
|
+
type,
|
|
475
|
+
lid,
|
|
476
|
+
id
|
|
477
|
+
} : {
|
|
478
|
+
type,
|
|
479
|
+
id
|
|
480
|
+
};
|
|
481
|
+
if (cache.hasChangedAttrs(identifier)) {
|
|
482
|
+
const attrsChanges = cache.changedAttrs(identifier);
|
|
483
|
+
const attributes = {};
|
|
484
|
+
Object.keys(attrsChanges).forEach((key) => {
|
|
485
|
+
const newVal = attrsChanges[key][1];
|
|
486
|
+
attributes[key] = newVal === void 0 ? null : structuredClone(newVal);
|
|
487
|
+
});
|
|
488
|
+
data.attributes = attributes;
|
|
489
|
+
}
|
|
490
|
+
const changedRelationships = cache.changedRelationships(identifier);
|
|
491
|
+
if (changedRelationships.size) {
|
|
492
|
+
const relationships = {};
|
|
493
|
+
changedRelationships.forEach((diff, key) => {
|
|
494
|
+
relationships[key] = { data: fixRelData(diff.localState) };
|
|
495
|
+
});
|
|
496
|
+
data.relationships = relationships;
|
|
497
|
+
}
|
|
498
|
+
return { data };
|
|
739
499
|
}
|
|
740
|
-
|
|
500
|
+
|
|
501
|
+
//#endregion
|
|
502
|
+
export { createRecord, deleteRecord, findRecord, postQuery, query, serializePatch, serializeResources, setBuildURLConfig, updateRecord };
|
|
503
|
+
//# sourceMappingURL=json-api.js.map
|