@warp-drive-mirror/utilities 5.8.0-beta.0 → 5.8.0-beta.2

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 (74) hide show
  1. package/README.md +16 -25
  2. package/declarations/-private/active-record/find-record.d.ts +5 -6
  3. package/declarations/-private/active-record/query.d.ts +2 -2
  4. package/declarations/-private/active-record/save-record.d.ts +2 -2
  5. package/declarations/-private/handlers/meta-doc.d.ts +1 -1
  6. package/declarations/-private/json-api/find-record.d.ts +37 -18
  7. package/declarations/-private/json-api/query.d.ts +3 -6
  8. package/declarations/-private/json-api/save-record.d.ts +45 -10
  9. package/declarations/-private/json-api/serialize.d.ts +15 -1
  10. package/declarations/-private/rest/find-record.d.ts +5 -8
  11. package/declarations/-private/rest/query.d.ts +2 -2
  12. package/declarations/-private/rest/save-record.d.ts +2 -2
  13. package/dist/active-record.js +2 -0
  14. package/dist/handlers.js +3 -3
  15. package/dist/json-api.js +94 -26
  16. package/dist/rest.js +2 -2
  17. package/dist/string.js +428 -1
  18. package/dist/unpkg/dev/-private.js +7 -0
  19. package/dist/unpkg/dev/active-record.js +394 -0
  20. package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +22 -0
  21. package/dist/unpkg/dev/derivations.js +30 -0
  22. package/dist/unpkg/dev/handlers.js +316 -0
  23. package/dist/unpkg/dev/index.js +362 -0
  24. package/dist/unpkg/dev/inflect-BEv8WqY1.js +343 -0
  25. package/dist/unpkg/dev/json-api.js +740 -0
  26. package/dist/unpkg/dev/rest.js +392 -0
  27. package/dist/unpkg/dev/string.js +1 -0
  28. package/dist/unpkg/dev-deprecated/-private.js +7 -0
  29. package/dist/unpkg/dev-deprecated/active-record.js +394 -0
  30. package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +22 -0
  31. package/dist/unpkg/dev-deprecated/derivations.js +30 -0
  32. package/dist/unpkg/dev-deprecated/handlers.js +316 -0
  33. package/dist/unpkg/dev-deprecated/index.js +362 -0
  34. package/dist/unpkg/dev-deprecated/inflect-BEv8WqY1.js +343 -0
  35. package/dist/unpkg/dev-deprecated/json-api.js +740 -0
  36. package/dist/unpkg/dev-deprecated/rest.js +392 -0
  37. package/dist/unpkg/dev-deprecated/string.js +1 -0
  38. package/dist/unpkg/prod/-private.js +7 -0
  39. package/dist/unpkg/prod/active-record.js +366 -0
  40. package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +22 -0
  41. package/dist/unpkg/prod/derivations.js +30 -0
  42. package/dist/unpkg/prod/handlers.js +305 -0
  43. package/dist/unpkg/prod/index.js +239 -0
  44. package/dist/unpkg/prod/inflect-Dh9dyEYx.js +333 -0
  45. package/dist/unpkg/prod/json-api.js +702 -0
  46. package/dist/unpkg/prod/rest.js +364 -0
  47. package/dist/unpkg/prod/string.js +1 -0
  48. package/dist/unpkg/prod-deprecated/-private.js +7 -0
  49. package/dist/unpkg/prod-deprecated/active-record.js +366 -0
  50. package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +22 -0
  51. package/dist/unpkg/prod-deprecated/derivations.js +30 -0
  52. package/dist/unpkg/prod-deprecated/handlers.js +305 -0
  53. package/dist/unpkg/prod-deprecated/index.js +239 -0
  54. package/dist/unpkg/prod-deprecated/inflect-Dh9dyEYx.js +333 -0
  55. package/dist/unpkg/prod-deprecated/json-api.js +702 -0
  56. package/dist/unpkg/prod-deprecated/rest.js +364 -0
  57. package/dist/unpkg/prod-deprecated/string.js +1 -0
  58. package/logos/README.md +2 -2
  59. package/logos/logo-yellow-slab.svg +1 -0
  60. package/logos/word-mark-black.svg +1 -0
  61. package/logos/word-mark-white.svg +1 -0
  62. package/package.json +13 -5
  63. package/logos/NCC-1701-a-blue.svg +0 -4
  64. package/logos/NCC-1701-a-gold.svg +0 -4
  65. package/logos/NCC-1701-a-gold_100.svg +0 -1
  66. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  67. package/logos/NCC-1701-a.svg +0 -4
  68. package/logos/docs-badge.svg +0 -2
  69. package/logos/ember-data-logo-dark.svg +0 -12
  70. package/logos/ember-data-logo-light.svg +0 -12
  71. package/logos/social1.png +0 -0
  72. package/logos/social2.png +0 -0
  73. package/logos/warp-drive-logo-dark.svg +0 -4
  74. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,740 @@
1
+ import { setBuildURLConfig as setBuildURLConfig$1, buildBaseURL, buildQueryParams } from './index.js';
2
+ import { p as pluralize } from "./inflect-BEv8WqY1.js";
3
+ import { e as extractCacheOptions, c as copyForwardUrlOptions } from "./builder-utils-Donkk-BZ.js";
4
+ import { recordIdentifierFor } from '@warp-drive-mirror/core';
5
+ const JsonApiAccept = 'application/vnd.api+json';
6
+ const DEFAULT_CONFIG = {
7
+ host: '',
8
+ namespace: ''
9
+ };
10
+ let ACCEPT_HEADER_VALUE = 'application/vnd.api+json';
11
+
12
+ /**
13
+ * Allows setting extensions and profiles to be used in the `Accept` header.
14
+ *
15
+ * Extensions and profiles are keyed by their namespace with the value being
16
+ * their URI.
17
+ *
18
+ * Example:
19
+ *
20
+ * ```ts
21
+ * setBuildURLConfig({
22
+ * extensions: {
23
+ * atomic: 'https://jsonapi.org/ext/atomic'
24
+ * },
25
+ * profiles: {
26
+ * pagination: 'https://jsonapi.org/profiles/ethanresnick/cursor-pagination'
27
+ * }
28
+ * });
29
+ * ```
30
+ *
31
+ * This also sets the global configuration for `buildBaseURL`
32
+ * for host and namespace values for the global coniguration
33
+ * done via `import { setBuildURLConfig } from '@warp-drive-mirror/utilities';`
34
+ *
35
+ * These values may still be overridden by passing
36
+ * them to buildBaseURL directly.
37
+ *
38
+ * This method may be called as many times as needed
39
+ *
40
+ * ```ts
41
+ * type BuildURLConfig = {
42
+ * host: string;
43
+ * namespace: string'
44
+ * }
45
+ * ```
46
+ *
47
+ * @public
48
+ * @param {BuildURLConfig} config
49
+ * @return {void}
50
+ */
51
+ function setBuildURLConfig(config) {
52
+ Object.assign({}, DEFAULT_CONFIG, config);
53
+ if (config.profiles || config.extensions) {
54
+ let accept = JsonApiAccept;
55
+ if (config.profiles) {
56
+ const profiles = Object.values(config.profiles);
57
+ if (profiles.length) {
58
+ accept += ';profile="' + profiles.join(' ') + '"';
59
+ }
60
+ }
61
+ if (config.extensions) {
62
+ const extensions = Object.values(config.extensions);
63
+ if (extensions.length) {
64
+ accept += ';ext=' + extensions.join(' ');
65
+ }
66
+ }
67
+ ACCEPT_HEADER_VALUE = accept;
68
+ }
69
+ setBuildURLConfig$1(config);
70
+ }
71
+
72
+ /**
73
+ * Builds request options to fetch a single resource by a known id or identifier
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-mirror/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-mirror/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-mirror/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-mirror/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
+
148
+ function findRecord(arg1, arg2, arg3) {
149
+ const identifier = typeof arg1 === 'string' ? {
150
+ type: arg1,
151
+ id: arg2
152
+ } : arg1;
153
+ const options = (typeof arg1 === 'string' ? arg3 : arg2) || {};
154
+ const cacheOptions = extractCacheOptions(options);
155
+ const urlOptions = {
156
+ identifier,
157
+ op: 'findRecord',
158
+ resourcePath: pluralize(identifier.type)
159
+ };
160
+ copyForwardUrlOptions(urlOptions, options);
161
+ const url = buildBaseURL(urlOptions);
162
+ const headers = new Headers();
163
+ headers.append('Accept', ACCEPT_HEADER_VALUE);
164
+ return {
165
+ url: options.include?.length ? `${url}?${buildQueryParams({
166
+ include: options.include
167
+ }, options.urlParamsSettings)}` : url,
168
+ method: 'GET',
169
+ headers,
170
+ cacheOptions,
171
+ op: 'findRecord',
172
+ records: [identifier]
173
+ };
174
+ }
175
+
176
+ /** @deprecated use {@link ReactiveDataDocument} */
177
+
178
+ /**
179
+ * Builds request options to query for resources, usually by a primary
180
+ * type, configured for the url and header expectations of most JSON:API APIs.
181
+ *
182
+ * The key difference between this and `postQuery` is that this method will send the query
183
+ * as query params in the url of a "GET" request instead of as the JSON body of a "POST"
184
+ * request.
185
+ *
186
+ * **Basic Usage**
187
+ *
188
+ * ```ts
189
+ * import { query } from '@warp-drive-mirror/utilities/json-api';
190
+ *
191
+ * const data = await store.request(query('person'));
192
+ * ```
193
+ *
194
+ * **With Query Params**
195
+ *
196
+ * ```ts
197
+ * import { query } from '@warp-drive-mirror/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-mirror/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
+ };
250
+ }
251
+
252
+ /**
253
+ * Builds request options to query for resources, usually by a primary
254
+ * type, configured for the url and header expectations of most JSON:API APIs.
255
+ *
256
+ * The key difference between this and `query` is that this method will send the query
257
+ * as the JSON body of a "POST" request instead of as query params in the url of a "GET"
258
+ * request.
259
+ *
260
+ * A CacheKey is generated from the url and query params, and used to cache the response
261
+ * in the store.
262
+ *
263
+ * ```ts
264
+ * import { postQuery } from '@warp-drive-mirror/utilities/json-api';
265
+ *
266
+ * const options = postQuery('person', { include: ['pets', 'friends'] });
267
+ * const data = await store.request(options);
268
+ * ```
269
+ *
270
+ * **Supplying Options to Modify the Request Behavior**
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-mirror/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
+ };
322
+ }
323
+ function isExisting(identifier) {
324
+ return 'id' in identifier && identifier.id !== null && 'type' in identifier && identifier.type !== null;
325
+ }
326
+
327
+ /**
328
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Request Body**
329
+ * While this may come as a surprise, the app providing the body ensures that only
330
+ * desired and correctly formatted data is sent with the request.
331
+ * :::
332
+ *
333
+ * Builds request options to delete record for resources,
334
+ * configured for the url, method and header expectations of most JSON:API APIs.
335
+ *
336
+ * **Basic Usage**
337
+ *
338
+ * ```ts
339
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/json-api';
340
+ *
341
+ * const person = store.peekRecord('person', '1');
342
+ *
343
+ * // mark record as deleted
344
+ * store.deleteRecord(person);
345
+ *
346
+ * // persist deletion
347
+ * const data = await store.request(deleteRecord(person));
348
+ * ```
349
+ *
350
+ * **Supplying Options to Modify the Request Behavior**
351
+ *
352
+ * The following options are supported:
353
+ *
354
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
355
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
356
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
357
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
358
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
359
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
360
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
361
+ * defaulting to `false` if none is configured.
362
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
363
+ *
364
+ * ```ts
365
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/json-api';
366
+ *
367
+ * const person = store.peekRecord('person', '1');
368
+ *
369
+ * // mark record as deleted
370
+ * store.deleteRecord(person);
371
+ *
372
+ * // persist deletion
373
+ * const options = deleteRecord(person, { namespace: 'api/v1' });
374
+ * const data = await store.request(options);
375
+ * ```
376
+ *
377
+ * @public
378
+ * @param record
379
+ * @param options
380
+ */
381
+
382
+ function deleteRecord(record, options = {}) {
383
+ const identifier = recordIdentifierFor(record);
384
+ (test => {
385
+ if (!test) {
386
+ throw new Error(`Expected to be given a record instance`);
387
+ }
388
+ })(identifier);
389
+ (test => {
390
+ if (!test) {
391
+ throw new Error(`Cannot delete a record that does not have an associated type and id.`);
392
+ }
393
+ })(isExisting(identifier));
394
+ const urlOptions = {
395
+ identifier: identifier,
396
+ op: 'deleteRecord',
397
+ resourcePath: pluralize(identifier.type)
398
+ };
399
+ copyForwardUrlOptions(urlOptions, options);
400
+ const url = buildBaseURL(urlOptions);
401
+ const headers = new Headers();
402
+ headers.append('Accept', ACCEPT_HEADER_VALUE);
403
+ return {
404
+ url,
405
+ method: 'DELETE',
406
+ headers,
407
+ op: 'deleteRecord',
408
+ data: {
409
+ record: identifier
410
+ },
411
+ records: [identifier]
412
+ };
413
+ }
414
+
415
+ /**
416
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
417
+ * While this may come as a surprise, the app providing the body ensures that only
418
+ * desired and correctly formatted data is sent with the request.
419
+ * :::
420
+ *
421
+ * Builds request options to create new record for resources,
422
+ * configured for the url, method and header expectations of most JSON:API APIs.
423
+ *
424
+ * **Basic Usage**
425
+ *
426
+ * ```ts
427
+ * import { cacheKeyFor } from '@warp-drive-mirror/core';
428
+ * import { createRecord } from '@warp-drive-mirror/utilities/json-api';
429
+ * import type { Person } from '#/data/types';
430
+ *
431
+ * const person = store.createRecord<Person>('person', { name: 'Ted' });
432
+ * const init = createRecord(person);
433
+ * init.body = JSON.stringify(
434
+ * {
435
+ * // it's likely you will want to transform this data
436
+ * // somewhat
437
+ * data: store.cache.peek(cacheKeyFor(person))
438
+ * }
439
+ * );
440
+ * const data = await store.request(init);
441
+ * ```
442
+ *
443
+ * **Supplying Options to Modify the Request Behavior**
444
+ *
445
+ * The following options are supported:
446
+ *
447
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
448
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
449
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
450
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
451
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
452
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
453
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
454
+ * defaulting to `false` if none is configured.
455
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
456
+ *
457
+ * ```ts
458
+ * import { createRecord } from '@warp-drive-mirror/utilities/json-api';
459
+ *
460
+ * const person = store.createRecord('person', { name: 'Ted' });
461
+ * const options = createRecord(person, { namespace: 'api/v1' });
462
+ * const data = await store.request(options);
463
+ * ```
464
+ *
465
+ * @public
466
+ * @param record
467
+ * @param options
468
+ */
469
+
470
+ function createRecord(record, options = {}) {
471
+ const identifier = recordIdentifierFor(record);
472
+ (test => {
473
+ if (!test) {
474
+ throw new Error(`Expected to be given a record instance`);
475
+ }
476
+ })(identifier);
477
+ const urlOptions = {
478
+ identifier: identifier,
479
+ op: 'createRecord',
480
+ resourcePath: pluralize(identifier.type)
481
+ };
482
+ copyForwardUrlOptions(urlOptions, options);
483
+ const url = buildBaseURL(urlOptions);
484
+ const headers = new Headers();
485
+ headers.append('Accept', ACCEPT_HEADER_VALUE);
486
+ return {
487
+ url,
488
+ method: 'POST',
489
+ headers,
490
+ op: 'createRecord',
491
+ data: {
492
+ record: identifier
493
+ },
494
+ records: [identifier]
495
+ };
496
+ }
497
+
498
+ /**
499
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
500
+ * While this may come as a surprise, the app providing the body ensures that only
501
+ * desired and correctly formatted data is sent with the request.
502
+ * :::
503
+ *
504
+ * Builds request options to update existing record for resources,
505
+ * configured for the url, method and header expectations of most JSON:API APIs.
506
+ *
507
+ * **Example Usage**
508
+ *
509
+ * ```ts
510
+ * import { cacheKeyFor } from '@warp-drive-mirror/core';
511
+ * import { updateRecord } from '@warp-drive-mirror/utilities/json-api';
512
+ * import type { EditablePerson } from '#/data/types';
513
+ *
514
+ * const mutable = await checkout<EditablePerson>(person);
515
+ * mutable.name = 'Chris';
516
+ * const init = updateRecord(mutable);
517
+ *
518
+ * init.body = JSON.stringify(
519
+ * // it's likely you will want to transform this data
520
+ * // somewhat, or serialize only specific properties instead
521
+ * serializePatch(store.cache, cacheKeyFor(mutable))
522
+ * );
523
+ * const data = await store.request(init);
524
+ * ```
525
+ *
526
+ *
527
+ * **Supplying Options to Modify the Request Behavior**
528
+ *
529
+ * The following options are supported:
530
+ *
531
+ * - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
532
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
533
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
534
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
535
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
536
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
537
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
538
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
539
+ * defaulting to `false` if none is configured.
540
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
541
+ *
542
+ * ```ts
543
+ * import { updateRecord } from '@warp-drive-mirror/utilities/json-api';
544
+ *
545
+ * const person = store.peekRecord('person', '1');
546
+ * person.name = 'Chris';
547
+ * const options = updateRecord(person, { patch: true });
548
+ * const data = await store.request(options);
549
+ * ```
550
+ *
551
+ * @public
552
+ * @param record
553
+ * @param options
554
+ */
555
+
556
+ function updateRecord(record, options = {}) {
557
+ const identifier = recordIdentifierFor(record);
558
+ (test => {
559
+ if (!test) {
560
+ throw new Error(`Expected to be given a record instance`);
561
+ }
562
+ })(identifier);
563
+ (test => {
564
+ if (!test) {
565
+ throw new Error(`Cannot update a record that does not have an associated type and id.`);
566
+ }
567
+ })(isExisting(identifier));
568
+ const urlOptions = {
569
+ identifier: identifier,
570
+ op: 'updateRecord',
571
+ resourcePath: pluralize(identifier.type)
572
+ };
573
+ copyForwardUrlOptions(urlOptions, options);
574
+ const url = buildBaseURL(urlOptions);
575
+ const headers = new Headers();
576
+ headers.append('Accept', ACCEPT_HEADER_VALUE);
577
+ return {
578
+ url,
579
+ method: options.patch ? 'PATCH' : 'PUT',
580
+ headers,
581
+ op: 'updateRecord',
582
+ data: {
583
+ record: identifier
584
+ },
585
+ records: [identifier]
586
+ };
587
+ }
588
+
589
+ /**
590
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
591
+ *
592
+ * While this may come as a surprise, they are intended to serialize cache state for more
593
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
594
+ * app can ensure that the body is correctly formatted and contains all necessary data.
595
+ * :::
596
+ *
597
+ * Serializes the current state of a resource or array of resources for use with POST or PUT requests.
598
+ *
599
+ * @public
600
+ * @param {Cache} cache}
601
+ * @param {ResourceKey} identifier
602
+ * @return {Object} An object with a `data` property containing the serialized resource patch
603
+ */
604
+
605
+ function serializeResources(cache, identifiers) {
606
+ return {
607
+ data: Array.isArray(identifiers) ? identifiers.map(identifier => _serializeResource(cache, identifier)) : _serializeResource(cache, identifiers)
608
+ };
609
+ }
610
+ function fixRef({
611
+ id,
612
+ lid,
613
+ type
614
+ }) {
615
+ if (id !== null) {
616
+ return {
617
+ id,
618
+ type
619
+ };
620
+ }
621
+ return {
622
+ id,
623
+ lid,
624
+ type
625
+ };
626
+ }
627
+ function fixRelData(rel) {
628
+ if (Array.isArray(rel)) {
629
+ return rel.map(ref => fixRef(ref));
630
+ } else if (typeof rel === 'object' && rel !== null) {
631
+ return fixRef(rel);
632
+ }
633
+ return null;
634
+ }
635
+ function _serializeResource(cache, identifier) {
636
+ const {
637
+ id,
638
+ lid,
639
+ type
640
+ } = identifier;
641
+ // peek gives us everything we want, but since its referentially the same data
642
+ // as is in the cache we clone it to avoid any accidental mutations
643
+ const record = structuredClone(cache.peek(identifier));
644
+ (test => {
645
+ if (!test) {
646
+ throw new Error(`A record with id ${String(id)} and type ${type} for lid ${lid} was not found not in the supplied Cache.`);
647
+ }
648
+ })(record);
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;
667
+ }
668
+
669
+ /**
670
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
671
+ *
672
+ * While this may come as a surprise, they are intended to serialize cache state for more
673
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
674
+ * app can ensure that the body is correctly formatted and contains all necessary data.
675
+ * :::
676
+ *
677
+ * Serializes changes to a resource. Useful for use with building bodies for PATCH requests.
678
+ *
679
+ * Only attributes which are changed are serialized.
680
+ * Only relationships which are changed are serialized.
681
+ *
682
+ * Collection relationships serialize the collection as a whole.
683
+ *
684
+ * If you would like to serialize updates to a collection more granularly
685
+ * (for instance, as operations) request the diff from the store and
686
+ * serialize as desired:
687
+ *
688
+ * ```ts
689
+ * const relationshipDiffMap = cache.changedRelationships(identifier);
690
+ * ```
691
+ *
692
+ * @public
693
+ * @param {Cache} cache}
694
+ * @param {ResourceKey} identifier
695
+ * @return {Object} An object with a `data` property containing the serialized resource patch
696
+ */
697
+ function serializePatch(cache, identifier) {
698
+ const {
699
+ id,
700
+ lid,
701
+ type
702
+ } = identifier;
703
+ (test => {
704
+ if (!test) {
705
+ throw new Error(`A record with id ${String(id)} and type ${type} for lid ${lid} was not found not in the supplied Cache.`);
706
+ }
707
+ })(cache.peek(identifier));
708
+ const data = id === null ? {
709
+ type,
710
+ lid,
711
+ id
712
+ } : {
713
+ type,
714
+ id
715
+ };
716
+ if (cache.hasChangedAttrs(identifier)) {
717
+ const attrsChanges = cache.changedAttrs(identifier);
718
+ const attributes = {};
719
+ Object.keys(attrsChanges).forEach(key => {
720
+ const change = attrsChanges[key];
721
+ const newVal = change[1];
722
+ attributes[key] = newVal === undefined ? null : structuredClone(newVal);
723
+ });
724
+ data.attributes = attributes;
725
+ }
726
+ const changedRelationships = cache.changedRelationships(identifier);
727
+ if (changedRelationships.size) {
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
+ };
739
+ }
740
+ export { createRecord, deleteRecord, findRecord, postQuery, query, serializePatch, serializeResources, setBuildURLConfig, updateRecord };