@warp-drive/utilities 5.6.0-alpha.11

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