@warp-drive-mirror/utilities 5.8.0-alpha.22 → 5.8.0-alpha.23

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.
@@ -5,32 +5,49 @@ import type { FindRecordOptions, FindRecordRequestOptions, RemotelyAccessibleIde
5
5
  * Builds request options to fetch a single resource by a known id or identifier
6
6
  * configured for the url and header expectations of most JSON:API APIs.
7
7
  *
8
- * **Basic Usage**
8
+ * :::tabs
9
+ *
10
+ * == Basic Usage
9
11
  *
10
12
  * ```ts
11
13
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
14
+ * import type { Person } from '#/data/types';
12
15
  *
13
- * const data = await store.request(findRecord('person', '1'));
16
+ * const result = await store.request(
17
+ * findRecord<Person>('person', '1')
18
+ * );
14
19
  * ```
15
20
  *
16
- * **With Options**
21
+ * == With Options
17
22
  *
18
23
  * ```ts
19
24
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
25
+ * import type { Person } from '#/data/types';
20
26
  *
21
- * const options = findRecord('person', '1', { include: ['pets', 'friends'] });
22
- * const data = await store.request(options);
27
+ * const data = await store.request(
28
+ * findRecord<Person>(
29
+ * 'person', '1',
30
+ * { include: ['pets', 'friends'] }
31
+ * )
32
+ * );
23
33
  * ```
24
34
  *
25
- * **With an Identifier**
35
+ * == With an Identifier
26
36
  *
27
37
  * ```ts
28
38
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
39
+ * import type { Person } from '#/data/types';
29
40
  *
30
- * const options = findRecord({ type: 'person', id: '1' }, { include: ['pets', 'friends'] });
31
- * const data = await store.request(options);
41
+ * const data = await store.request(
42
+ * findRecord<Person>(
43
+ * { type: 'person', id: '1' },
44
+ * { include: ['pets', 'friends'] }
45
+ * )
46
+ * );
32
47
  * ```
33
48
  *
49
+ * :::
50
+ *
34
51
  * **Supplying Options to Modify the Request Behavior**
35
52
  *
36
53
  * The following options are supported:
@@ -48,8 +65,13 @@ import type { FindRecordOptions, FindRecordRequestOptions, RemotelyAccessibleIde
48
65
  * ```ts
49
66
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
50
67
  *
51
- * const options = findRecord('person', '1', { include: ['pets', 'friends'] }, { namespace: 'api/v2' });
52
- * const data = await store.request(options);
68
+ * const data = await store.request(
69
+ * findRecord(
70
+ * 'person', '1',
71
+ * { include: ['pets', 'friends'] },
72
+ * { namespace: 'api/v2' }
73
+ * )
74
+ * );
53
75
  * ```
54
76
  *
55
77
  * @public
@@ -2,6 +2,11 @@ import type { ReactiveDataDocument } from "@warp-drive-mirror/core/reactive.js";
2
2
  import type { TypedRecordInstance } from "@warp-drive-mirror/core/types/record";
3
3
  import type { ConstrainedRequestOptions, CreateRequestOptions, DeleteRequestOptions, UpdateRequestOptions } from "@warp-drive-mirror/core/types/request";
4
4
  /**
5
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Request Body**
6
+ * While this may come as a surprise, the app providing the body ensures that only
7
+ * desired and correctly formatted data is sent with the request.
8
+ * :::
9
+ *
5
10
  * Builds request options to delete record for resources,
6
11
  * configured for the url, method and header expectations of most JSON:API APIs.
7
12
  *
@@ -53,16 +58,31 @@ import type { ConstrainedRequestOptions, CreateRequestOptions, DeleteRequestOpti
53
58
  export declare function deleteRecord<T>(record: T, options?: ConstrainedRequestOptions): DeleteRequestOptions<T>;
54
59
  export declare function deleteRecord(record: unknown, options?: ConstrainedRequestOptions): DeleteRequestOptions;
55
60
  /**
61
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
62
+ * While this may come as a surprise, the app providing the body ensures that only
63
+ * desired and correctly formatted data is sent with the request.
64
+ * :::
65
+ *
56
66
  * Builds request options to create new record for resources,
57
67
  * configured for the url, method and header expectations of most JSON:API APIs.
58
68
  *
59
69
  * **Basic Usage**
60
70
  *
61
71
  * ```ts
72
+ * import { cacheKeyFor } from '@warp-drive-mirror/core';
62
73
  * import { createRecord } from '@warp-drive-mirror/utilities/json-api';
63
- *
64
- * const person = store.createRecord('person', { name: 'Ted' });
65
- * const data = await store.request(createRecord(person));
74
+ * import type { Person } from '#/data/types';
75
+ *
76
+ * const person = store.createRecord<Person>('person', { name: 'Ted' });
77
+ * const init = createRecord(person);
78
+ * init.body = JSON.stringify(
79
+ * {
80
+ * // it's likely you will want to transform this data
81
+ * // somewhat
82
+ * data: store.cache.peek(cacheKeyFor(person))
83
+ * }
84
+ * );
85
+ * const data = await store.request(init);
66
86
  * ```
67
87
  *
68
88
  * **Supplying Options to Modify the Request Behavior**
@@ -94,19 +114,34 @@ export declare function deleteRecord(record: unknown, options?: ConstrainedReque
94
114
  export declare function createRecord<T>(record: T, options?: ConstrainedRequestOptions): CreateRequestOptions<T>;
95
115
  export declare function createRecord(record: unknown, options?: ConstrainedRequestOptions): CreateRequestOptions;
96
116
  /**
117
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
118
+ * While this may come as a surprise, the app providing the body ensures that only
119
+ * desired and correctly formatted data is sent with the request.
120
+ * :::
121
+ *
97
122
  * Builds request options to update existing record for resources,
98
123
  * configured for the url, method and header expectations of most JSON:API APIs.
99
124
  *
100
- * **Basic Usage**
125
+ * **Example Usage**
101
126
  *
102
127
  * ```ts
128
+ * import { cacheKeyFor } from '@warp-drive-mirror/core';
103
129
  * import { updateRecord } from '@warp-drive-mirror/utilities/json-api';
104
- *
105
- * const person = store.peekRecord('person', '1');
106
- * person.name = 'Chris';
107
- * const data = await store.request(updateRecord(person));
130
+ * import type { EditablePerson } from '#/data/types';
131
+ *
132
+ * const mutable = await checkout<EditablePerson>(person);
133
+ * mutable.name = 'Chris';
134
+ * const init = updateRecord(mutable);
135
+ *
136
+ * init.body = JSON.stringify(
137
+ * // it's likely you will want to transform this data
138
+ * // somewhat, or serialize only specific properties instead
139
+ * serializePatch(store.cache, cacheKeyFor(mutable))
140
+ * );
141
+ * const data = await store.request(init);
108
142
  * ```
109
143
  *
144
+ *
110
145
  * **Supplying Options to Modify the Request Behavior**
111
146
  *
112
147
  * The following options are supported:
@@ -16,6 +16,13 @@ export type JsonApiResourcePatch = {
16
16
  relationships?: Record<string, ChangedRelationshipData>;
17
17
  };
18
18
  /**
19
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
20
+ *
21
+ * While this may come as a surprise, they are intended to serialize cache state for more
22
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
23
+ * app can ensure that the body is correctly formatted and contains all necessary data.
24
+ * :::
25
+ *
19
26
  * Serializes the current state of a resource or array of resources for use with POST or PUT requests.
20
27
  *
21
28
  * @public
@@ -30,7 +37,14 @@ export declare function serializeResources(cache: Cache, identifiers: ResourceKe
30
37
  data: ResourceObject[];
31
38
  };
32
39
  /**
33
- * Serializes changes to a resource for use with PATCH requests.
40
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
41
+ *
42
+ * While this may come as a surprise, they are intended to serialize cache state for more
43
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
44
+ * app can ensure that the body is correctly formatted and contains all necessary data.
45
+ * :::
46
+ *
47
+ * Serializes changes to a resource. Useful for use with building bodies for PATCH requests.
34
48
  *
35
49
  * Only attributes which are changed are serialized.
36
50
  * Only relationships which are changed are serialized.
package/dist/json-api.js CHANGED
@@ -74,32 +74,49 @@ function setBuildURLConfig(config) {
74
74
  * Builds request options to fetch a single resource by a known id or identifier
75
75
  * configured for the url and header expectations of most JSON:API APIs.
76
76
  *
77
- * **Basic Usage**
77
+ * :::tabs
78
+ *
79
+ * == Basic Usage
78
80
  *
79
81
  * ```ts
80
82
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
83
+ * import type { Person } from '#/data/types';
81
84
  *
82
- * const data = await store.request(findRecord('person', '1'));
85
+ * const result = await store.request(
86
+ * findRecord<Person>('person', '1')
87
+ * );
83
88
  * ```
84
89
  *
85
- * **With Options**
90
+ * == With Options
86
91
  *
87
92
  * ```ts
88
93
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
89
- *
90
- * const options = findRecord('person', '1', { include: ['pets', 'friends'] });
91
- * const data = await store.request(options);
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
+ * );
92
102
  * ```
93
103
  *
94
- * **With an Identifier**
104
+ * == With an Identifier
95
105
  *
96
106
  * ```ts
97
107
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
98
- *
99
- * const options = findRecord({ type: 'person', id: '1' }, { include: ['pets', 'friends'] });
100
- * const data = await store.request(options);
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
+ * );
101
116
  * ```
102
117
  *
118
+ * :::
119
+ *
103
120
  * **Supplying Options to Modify the Request Behavior**
104
121
  *
105
122
  * The following options are supported:
@@ -117,8 +134,13 @@ function setBuildURLConfig(config) {
117
134
  * ```ts
118
135
  * import { findRecord } from '@warp-drive-mirror/utilities/json-api';
119
136
  *
120
- * const options = findRecord('person', '1', { include: ['pets', 'friends'] }, { namespace: 'api/v2' });
121
- * const data = await store.request(options);
137
+ * const data = await store.request(
138
+ * findRecord(
139
+ * 'person', '1',
140
+ * { include: ['pets', 'friends'] },
141
+ * { namespace: 'api/v2' }
142
+ * )
143
+ * );
122
144
  * ```
123
145
  *
124
146
  * @public
@@ -304,6 +326,11 @@ function isExisting(identifier) {
304
326
  }
305
327
 
306
328
  /**
329
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Request Body**
330
+ * While this may come as a surprise, the app providing the body ensures that only
331
+ * desired and correctly formatted data is sent with the request.
332
+ * :::
333
+ *
307
334
  * Builds request options to delete record for resources,
308
335
  * configured for the url, method and header expectations of most JSON:API APIs.
309
336
  *
@@ -387,16 +414,31 @@ function deleteRecord(record, options = {}) {
387
414
  }
388
415
 
389
416
  /**
417
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
418
+ * While this may come as a surprise, the app providing the body ensures that only
419
+ * desired and correctly formatted data is sent with the request.
420
+ * :::
421
+ *
390
422
  * Builds request options to create new record for resources,
391
423
  * configured for the url, method and header expectations of most JSON:API APIs.
392
424
  *
393
425
  * **Basic Usage**
394
426
  *
395
427
  * ```ts
428
+ * import { cacheKeyFor } from '@warp-drive-mirror/core';
396
429
  * import { createRecord } from '@warp-drive-mirror/utilities/json-api';
397
- *
398
- * const person = store.createRecord('person', { name: 'Ted' });
399
- * const data = await store.request(createRecord(person));
430
+ * import type { Person } from '#/data/types';
431
+ *
432
+ * const person = store.createRecord<Person>('person', { name: 'Ted' });
433
+ * const init = createRecord(person);
434
+ * init.body = JSON.stringify(
435
+ * {
436
+ * // it's likely you will want to transform this data
437
+ * // somewhat
438
+ * data: store.cache.peek(cacheKeyFor(person))
439
+ * }
440
+ * );
441
+ * const data = await store.request(init);
400
442
  * ```
401
443
  *
402
444
  * **Supplying Options to Modify the Request Behavior**
@@ -455,19 +497,34 @@ function createRecord(record, options = {}) {
455
497
  }
456
498
 
457
499
  /**
500
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
501
+ * While this may come as a surprise, the app providing the body ensures that only
502
+ * desired and correctly formatted data is sent with the request.
503
+ * :::
504
+ *
458
505
  * Builds request options to update existing record for resources,
459
506
  * configured for the url, method and header expectations of most JSON:API APIs.
460
507
  *
461
- * **Basic Usage**
508
+ * **Example Usage**
462
509
  *
463
510
  * ```ts
511
+ * import { cacheKeyFor } from '@warp-drive-mirror/core';
464
512
  * import { updateRecord } from '@warp-drive-mirror/utilities/json-api';
465
- *
466
- * const person = store.peekRecord('person', '1');
467
- * person.name = 'Chris';
468
- * const data = await store.request(updateRecord(person));
513
+ * import type { EditablePerson } from '#/data/types';
514
+ *
515
+ * const mutable = await checkout<EditablePerson>(person);
516
+ * mutable.name = 'Chris';
517
+ * const init = updateRecord(mutable);
518
+ *
519
+ * init.body = JSON.stringify(
520
+ * // it's likely you will want to transform this data
521
+ * // somewhat, or serialize only specific properties instead
522
+ * serializePatch(store.cache, cacheKeyFor(mutable))
523
+ * );
524
+ * const data = await store.request(init);
469
525
  * ```
470
526
  *
527
+ *
471
528
  * **Supplying Options to Modify the Request Behavior**
472
529
  *
473
530
  * The following options are supported:
@@ -531,6 +588,13 @@ function updateRecord(record, options = {}) {
531
588
  }
532
589
 
533
590
  /**
591
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
592
+ *
593
+ * While this may come as a surprise, they are intended to serialize cache state for more
594
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
595
+ * app can ensure that the body is correctly formatted and contains all necessary data.
596
+ * :::
597
+ *
534
598
  * Serializes the current state of a resource or array of resources for use with POST or PUT requests.
535
599
  *
536
600
  * @public
@@ -603,7 +667,14 @@ function _serializeResource(cache, identifier) {
603
667
  }
604
668
 
605
669
  /**
606
- * Serializes changes to a resource for use with PATCH requests.
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.
607
678
  *
608
679
  * Only attributes which are changed are serialized.
609
680
  * Only relationships which are changed are serialized.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warp-drive-mirror/utilities",
3
- "version": "5.8.0-alpha.22",
3
+ "version": "5.8.0-alpha.23",
4
4
  "description": "Utilities package for WarpDrive | Things your app might find useful",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -36,7 +36,7 @@
36
36
  }
37
37
  },
38
38
  "peerDependencies": {
39
- "@warp-drive-mirror/core": "5.8.0-alpha.22"
39
+ "@warp-drive-mirror/core": "5.8.0-alpha.23"
40
40
  },
41
41
  "dependencies": {
42
42
  "@embroider/macros": "^1.18.1"
@@ -45,8 +45,8 @@
45
45
  "@babel/core": "^7.28.3",
46
46
  "@babel/plugin-transform-typescript": "^7.28.0",
47
47
  "@babel/preset-typescript": "^7.27.1",
48
- "@warp-drive/internal-config": "5.8.0-alpha.22",
49
- "@warp-drive-mirror/core": "5.8.0-alpha.22",
48
+ "@warp-drive/internal-config": "5.8.0-alpha.23",
49
+ "@warp-drive-mirror/core": "5.8.0-alpha.23",
50
50
  "decorator-transforms": "^2.3.0",
51
51
  "expect-type": "^1.2.2",
52
52
  "typescript": "^5.9.2",