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