@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.
Files changed (150) hide show
  1. package/cjs-dist/string.cjs +448 -0
  2. package/cjs-dist/string.cjs.map +1 -0
  3. package/declarations/-private.d.ts +12 -1
  4. package/declarations/-private.d.ts.map +1 -0
  5. package/declarations/active-record.d.ts +260 -3
  6. package/declarations/active-record.d.ts.map +1 -0
  7. package/declarations/derivations.d.ts +26 -23
  8. package/declarations/derivations.d.ts.map +1 -0
  9. package/declarations/handlers.d.ts +282 -9
  10. package/declarations/handlers.d.ts.map +1 -0
  11. package/declarations/index-BeMAuqgZ.d.ts +462 -0
  12. package/declarations/index-BeMAuqgZ.d.ts.map +1 -0
  13. package/declarations/index.d.ts +2 -208
  14. package/declarations/json-api.d.ts +496 -5
  15. package/declarations/json-api.d.ts.map +1 -0
  16. package/declarations/rest.d.ts +258 -3
  17. package/declarations/rest.d.ts.map +1 -0
  18. package/declarations/string.d.ts +164 -13
  19. package/declarations/string.d.ts.map +1 -0
  20. package/dist/-private.js +2 -6
  21. package/dist/active-record.js +254 -379
  22. package/dist/active-record.js.map +1 -0
  23. package/dist/builder-utils-BkXwUT0x.js +16 -0
  24. package/dist/builder-utils-BkXwUT0x.js.map +1 -0
  25. package/dist/derivations.js +26 -26
  26. package/dist/derivations.js.map +1 -0
  27. package/dist/handlers.js +240 -283
  28. package/dist/handlers.js.map +1 -0
  29. package/dist/index.js +357 -335
  30. package/dist/index.js.map +1 -0
  31. package/dist/inflections-DzBsjJqj.js +80 -0
  32. package/dist/inflections-DzBsjJqj.js.map +1 -0
  33. package/dist/json-api.js +470 -706
  34. package/dist/json-api.js.map +1 -0
  35. package/dist/rest.js +254 -377
  36. package/dist/rest.js.map +1 -0
  37. package/dist/string-CWmAluxh.js +290 -0
  38. package/dist/string-CWmAluxh.js.map +1 -0
  39. package/dist/string.js +2 -427
  40. package/dist/unpkg/dev/-private.js +2 -6
  41. package/dist/unpkg/dev/active-record.js +253 -378
  42. package/dist/unpkg/dev/active-record.js.map +1 -0
  43. package/dist/unpkg/dev/builder-utils-BkXwUT0x.js +16 -0
  44. package/dist/unpkg/dev/builder-utils-BkXwUT0x.js.map +1 -0
  45. package/dist/unpkg/dev/derivations.js +26 -26
  46. package/dist/unpkg/dev/derivations.js.map +1 -0
  47. package/dist/unpkg/dev/handlers.js +238 -282
  48. package/dist/unpkg/dev/handlers.js.map +1 -0
  49. package/dist/unpkg/dev/index.js +356 -336
  50. package/dist/unpkg/dev/index.js.map +1 -0
  51. package/dist/unpkg/dev/inflections-DzBsjJqj.js +80 -0
  52. package/dist/unpkg/dev/inflections-DzBsjJqj.js.map +1 -0
  53. package/dist/unpkg/dev/json-api.js +469 -706
  54. package/dist/unpkg/dev/json-api.js.map +1 -0
  55. package/dist/unpkg/dev/rest.js +253 -376
  56. package/dist/unpkg/dev/rest.js.map +1 -0
  57. package/dist/unpkg/dev/string-j4ur-TDs.js +289 -0
  58. package/dist/unpkg/dev/string-j4ur-TDs.js.map +1 -0
  59. package/dist/unpkg/dev/string.js +3 -1
  60. package/dist/unpkg/dev-deprecated/-private.js +2 -6
  61. package/dist/unpkg/dev-deprecated/active-record.js +253 -378
  62. package/dist/unpkg/dev-deprecated/active-record.js.map +1 -0
  63. package/dist/unpkg/dev-deprecated/builder-utils-BkXwUT0x.js +16 -0
  64. package/dist/unpkg/dev-deprecated/builder-utils-BkXwUT0x.js.map +1 -0
  65. package/dist/unpkg/dev-deprecated/derivations.js +26 -26
  66. package/dist/unpkg/dev-deprecated/derivations.js.map +1 -0
  67. package/dist/unpkg/dev-deprecated/handlers.js +238 -282
  68. package/dist/unpkg/dev-deprecated/handlers.js.map +1 -0
  69. package/dist/unpkg/dev-deprecated/index.js +356 -336
  70. package/dist/unpkg/dev-deprecated/index.js.map +1 -0
  71. package/dist/unpkg/dev-deprecated/inflections-DzBsjJqj.js +80 -0
  72. package/dist/unpkg/dev-deprecated/inflections-DzBsjJqj.js.map +1 -0
  73. package/dist/unpkg/dev-deprecated/json-api.js +469 -706
  74. package/dist/unpkg/dev-deprecated/json-api.js.map +1 -0
  75. package/dist/unpkg/dev-deprecated/rest.js +253 -376
  76. package/dist/unpkg/dev-deprecated/rest.js.map +1 -0
  77. package/dist/unpkg/dev-deprecated/string-j4ur-TDs.js +289 -0
  78. package/dist/unpkg/dev-deprecated/string-j4ur-TDs.js.map +1 -0
  79. package/dist/unpkg/dev-deprecated/string.js +3 -1
  80. package/dist/unpkg/prod/-private.js +2 -6
  81. package/dist/unpkg/prod/active-record.js +236 -351
  82. package/dist/unpkg/prod/active-record.js.map +1 -0
  83. package/dist/unpkg/prod/builder-utils-BkXwUT0x.js +16 -0
  84. package/dist/unpkg/prod/builder-utils-BkXwUT0x.js.map +1 -0
  85. package/dist/unpkg/prod/derivations.js +26 -26
  86. package/dist/unpkg/prod/derivations.js.map +1 -0
  87. package/dist/unpkg/prod/handlers.js +231 -272
  88. package/dist/unpkg/prod/handlers.js.map +1 -0
  89. package/dist/unpkg/prod/index.js +265 -215
  90. package/dist/unpkg/prod/index.js.map +1 -0
  91. package/dist/unpkg/prod/inflections-DzBsjJqj.js +80 -0
  92. package/dist/unpkg/prod/inflections-DzBsjJqj.js.map +1 -0
  93. package/dist/unpkg/prod/json-api.js +446 -669
  94. package/dist/unpkg/prod/json-api.js.map +1 -0
  95. package/dist/unpkg/prod/rest.js +236 -349
  96. package/dist/unpkg/prod/rest.js.map +1 -0
  97. package/dist/unpkg/prod/string-T1ahjLdj.js +283 -0
  98. package/dist/unpkg/prod/string-T1ahjLdj.js.map +1 -0
  99. package/dist/unpkg/prod/string.js +3 -1
  100. package/dist/unpkg/prod-deprecated/-private.js +2 -6
  101. package/dist/unpkg/prod-deprecated/active-record.js +236 -351
  102. package/dist/unpkg/prod-deprecated/active-record.js.map +1 -0
  103. package/dist/unpkg/prod-deprecated/builder-utils-BkXwUT0x.js +16 -0
  104. package/dist/unpkg/prod-deprecated/builder-utils-BkXwUT0x.js.map +1 -0
  105. package/dist/unpkg/prod-deprecated/derivations.js +26 -26
  106. package/dist/unpkg/prod-deprecated/derivations.js.map +1 -0
  107. package/dist/unpkg/prod-deprecated/handlers.js +231 -272
  108. package/dist/unpkg/prod-deprecated/handlers.js.map +1 -0
  109. package/dist/unpkg/prod-deprecated/index.js +265 -215
  110. package/dist/unpkg/prod-deprecated/index.js.map +1 -0
  111. package/dist/unpkg/prod-deprecated/inflections-DzBsjJqj.js +80 -0
  112. package/dist/unpkg/prod-deprecated/inflections-DzBsjJqj.js.map +1 -0
  113. package/dist/unpkg/prod-deprecated/json-api.js +446 -669
  114. package/dist/unpkg/prod-deprecated/json-api.js.map +1 -0
  115. package/dist/unpkg/prod-deprecated/rest.js +236 -349
  116. package/dist/unpkg/prod-deprecated/rest.js.map +1 -0
  117. package/dist/unpkg/prod-deprecated/string-T1ahjLdj.js +283 -0
  118. package/dist/unpkg/prod-deprecated/string-T1ahjLdj.js.map +1 -0
  119. package/dist/unpkg/prod-deprecated/string.js +3 -1
  120. package/package.json +11 -10
  121. package/declarations/-private/active-record/find-record.d.ts +0 -64
  122. package/declarations/-private/active-record/query.d.ts +0 -53
  123. package/declarations/-private/active-record/save-record.d.ts +0 -146
  124. package/declarations/-private/builder-utils.d.ts +0 -4
  125. package/declarations/-private/handlers/auto-compress.d.ts +0 -158
  126. package/declarations/-private/handlers/gated.d.ts +0 -19
  127. package/declarations/-private/handlers/meta-doc.d.ts +0 -47
  128. package/declarations/-private/handlers/utils.d.ts +0 -41
  129. package/declarations/-private/json-api/-utils.d.ts +0 -109
  130. package/declarations/-private/json-api/find-record.d.ts +0 -84
  131. package/declarations/-private/json-api/query.d.ts +0 -100
  132. package/declarations/-private/json-api/save-record.d.ts +0 -228
  133. package/declarations/-private/json-api/serialize.d.ts +0 -70
  134. package/declarations/-private/rest/find-record.d.ts +0 -62
  135. package/declarations/-private/rest/query.d.ts +0 -53
  136. package/declarations/-private/rest/save-record.d.ts +0 -146
  137. package/declarations/-private/string/inflect.d.ts +0 -82
  138. package/declarations/-private/string/inflections.d.ts +0 -9
  139. package/declarations/-private/string/transform.d.ts +0 -77
  140. package/dist/builder-utils-Donkk-BZ.js +0 -22
  141. package/dist/inflect-BSG696t-.js +0 -344
  142. package/dist/string.cjs +0 -430
  143. package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +0 -22
  144. package/dist/unpkg/dev/inflect-BEv8WqY1.js +0 -343
  145. package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +0 -22
  146. package/dist/unpkg/dev-deprecated/inflect-BEv8WqY1.js +0 -343
  147. package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +0 -22
  148. package/dist/unpkg/prod/inflect-Dh9dyEYx.js +0 -333
  149. package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +0 -22
  150. 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, buildBaseURL, buildQueryParams } from './index.js';
2
- import { p as pluralize } from "./inflect-BSG696t-.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';
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
- host: '',
9
- namespace: ''
10
+ host: "",
11
+ namespace: ""
10
12
  };
11
- let ACCEPT_HEADER_VALUE = 'application/vnd.api+json';
12
-
13
+ let CONFIG = DEFAULT_CONFIG;
14
+ let ACCEPT_HEADER_VALUE = "application/vnd.api+json";
13
15
  /**
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
- */
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
- 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);
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
- * 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
- * :::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
- const identifier = typeof arg1 === 'string' ? {
151
- type: arg1,
152
- id: arg2
153
- } : arg1;
154
- const options = (typeof arg1 === 'string' ? arg3 : arg2) || {};
155
- const cacheOptions = extractCacheOptions(options);
156
- const urlOptions = {
157
- identifier,
158
- op: 'findRecord',
159
- resourcePath: pluralize(identifier.type)
160
- };
161
- copyForwardUrlOptions(urlOptions, options);
162
- const url = buildBaseURL(urlOptions);
163
- const headers = new Headers();
164
- headers.append('Accept', ACCEPT_HEADER_VALUE);
165
- return {
166
- url: options.include?.length ? `${url}?${buildQueryParams({
167
- include: options.include
168
- }, options.urlParamsSettings)}` : url,
169
- method: 'GET',
170
- headers,
171
- cacheOptions,
172
- op: 'findRecord',
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
- /** @deprecated use {@link ReactiveDataDocument} */
178
-
179
- /**
180
- * Builds request options to query for resources, usually by a primary
181
- * type, configured for the url and header expectations of most JSON:API APIs.
182
- *
183
- * The key difference between this and `postQuery` is that this method will send the query
184
- * as query params in the url of a "GET" request instead of as the JSON body of a "POST"
185
- * request.
186
- *
187
- * **Basic Usage**
188
- *
189
- * ```ts
190
- * import { query } from '@warp-drive/utilities/json-api';
191
- *
192
- * const data = await store.request(query('person'));
193
- * ```
194
- *
195
- * **With Query Params**
196
- *
197
- * ```ts
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
- * Builds request options to query for resources, usually by a primary
255
- * type, configured for the url and header expectations of most JSON:API APIs.
256
- *
257
- * The key difference between this and `query` is that this method will send the query
258
- * as the JSON body of a "POST" request instead of as query params in the url of a "GET"
259
- * request.
260
- *
261
- * A CacheKey is generated from the url and query params, and used to cache the response
262
- * in the store.
263
- *
264
- * ```ts
265
- * import { postQuery } from '@warp-drive/utilities/json-api';
266
- *
267
- * const options = postQuery('person', { include: ['pets', 'friends'] });
268
- * const data = await store.request(options);
269
- * ```
270
- *
271
- * **Supplying Options to Modify the Request Behavior**
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
- return 'id' in identifier && identifier.id !== null && 'type' in identifier && identifier.type !== null;
147
+ return "id" in identifier && identifier.id !== null && "type" in identifier && identifier.type !== null;
326
148
  }
327
-
328
149
  /**
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
- *
334
- * Builds request options to delete record for resources,
335
- * configured for the url, method and header expectations of most JSON:API APIs.
336
- *
337
- * **Basic Usage**
338
- *
339
- * ```ts
340
- * import { deleteRecord } from '@warp-drive/utilities/json-api';
341
- *
342
- * const person = store.peekRecord('person', '1');
343
- *
344
- * // mark record as deleted
345
- * store.deleteRecord(person);
346
- *
347
- * // persist deletion
348
- * const data = await store.request(deleteRecord(person));
349
- * ```
350
- *
351
- * **Supplying Options to Modify the Request Behavior**
352
- *
353
- * The following options are supported:
354
- *
355
- * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
356
- * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
357
- * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
358
- * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
359
- * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
360
- * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
361
- * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
362
- * defaulting to `false` if none is configured.
363
- * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
364
- *
365
- * ```ts
366
- * import { deleteRecord } from '@warp-drive/utilities/json-api';
367
- *
368
- * const person = store.peekRecord('person', '1');
369
- *
370
- * // mark record as deleted
371
- * store.deleteRecord(person);
372
- *
373
- * // persist deletion
374
- * const options = deleteRecord(person, { namespace: 'api/v1' });
375
- * const data = await store.request(options);
376
- * ```
377
- *
378
- * @public
379
- * @param record
380
- * @param options
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
- const identifier = recordIdentifierFor(record);
385
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
386
- if (!test) {
387
- throw new Error(`Expected to be given a record instance`);
388
- }
389
- })(identifier) : {};
390
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
391
- if (!test) {
392
- throw new Error(`Cannot delete a record that does not have an associated type and id.`);
393
- }
394
- })(isExisting(identifier)) : {};
395
- const urlOptions = {
396
- identifier: identifier,
397
- op: 'deleteRecord',
398
- resourcePath: pluralize(identifier.type)
399
- };
400
- copyForwardUrlOptions(urlOptions, options);
401
- const url = buildBaseURL(urlOptions);
402
- const headers = new Headers();
403
- headers.append('Accept', ACCEPT_HEADER_VALUE);
404
- return {
405
- url,
406
- method: 'DELETE',
407
- headers,
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
- * :::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
- *
422
- * Builds request options to create new record for resources,
423
- * configured for the url, method and header expectations of most JSON:API APIs.
424
- *
425
- * **Basic Usage**
426
- *
427
- * ```ts
428
- * import { cacheKeyFor } from '@warp-drive/core';
429
- * import { createRecord } from '@warp-drive/utilities/json-api';
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);
442
- * ```
443
- *
444
- * **Supplying Options to Modify the Request Behavior**
445
- *
446
- * The following options are supported:
447
- *
448
- * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
449
- * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
450
- * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
451
- * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
452
- * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
453
- * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
454
- * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
455
- * defaulting to `false` if none is configured.
456
- * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
457
- *
458
- * ```ts
459
- * import { createRecord } from '@warp-drive/utilities/json-api';
460
- *
461
- * const person = store.createRecord('person', { name: 'Ted' });
462
- * const options = createRecord(person, { namespace: 'api/v1' });
463
- * const data = await store.request(options);
464
- * ```
465
- *
466
- * @public
467
- * @param record
468
- * @param options
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
- const identifier = recordIdentifierFor(record);
473
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
474
- if (!test) {
475
- throw new Error(`Expected to be given a record instance`);
476
- }
477
- })(identifier) : {};
478
- const urlOptions = {
479
- identifier: identifier,
480
- op: 'createRecord',
481
- resourcePath: pluralize(identifier.type)
482
- };
483
- copyForwardUrlOptions(urlOptions, options);
484
- const url = buildBaseURL(urlOptions);
485
- const headers = new Headers();
486
- headers.append('Accept', ACCEPT_HEADER_VALUE);
487
- return {
488
- url,
489
- method: 'POST',
490
- headers,
491
- op: 'createRecord',
492
- data: {
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
- * :::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
- *
505
- * Builds request options to update existing record for resources,
506
- * configured for the url, method and header expectations of most JSON:API APIs.
507
- *
508
- * **Example Usage**
509
- *
510
- * ```ts
511
- * import { cacheKeyFor } from '@warp-drive/core';
512
- * import { updateRecord } from '@warp-drive/utilities/json-api';
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);
525
- * ```
526
- *
527
- *
528
- * **Supplying Options to Modify the Request Behavior**
529
- *
530
- * The following options are supported:
531
- *
532
- * - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
533
- * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
534
- * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
535
- * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
536
- * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
537
- * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
538
- * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
539
- * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
540
- * defaulting to `false` if none is configured.
541
- * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
542
- *
543
- * ```ts
544
- * import { updateRecord } from '@warp-drive/utilities/json-api';
545
- *
546
- * const person = store.peekRecord('person', '1');
547
- * person.name = 'Chris';
548
- * const options = updateRecord(person, { patch: true });
549
- * const data = await store.request(options);
550
- * ```
551
- *
552
- * @public
553
- * @param record
554
- * @param options
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
- const identifier = recordIdentifierFor(record);
559
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
560
- if (!test) {
561
- throw new Error(`Expected to be given a record instance`);
562
- }
563
- })(identifier) : {};
564
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
565
- if (!test) {
566
- throw new Error(`Cannot update a record that does not have an associated type and id.`);
567
- }
568
- })(isExisting(identifier)) : {};
569
- const urlOptions = {
570
- identifier: identifier,
571
- op: 'updateRecord',
572
- resourcePath: pluralize(identifier.type)
573
- };
574
- copyForwardUrlOptions(urlOptions, options);
575
- const url = buildBaseURL(urlOptions);
576
- const headers = new Headers();
577
- headers.append('Accept', ACCEPT_HEADER_VALUE);
578
- return {
579
- url,
580
- method: options.patch ? 'PATCH' : 'PUT',
581
- headers,
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
- * :::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
- *
598
- * Serializes the current state of a resource or array of resources for use with POST or PUT requests.
599
- *
600
- * @public
601
- * @param {Cache} cache}
602
- * @param {ResourceKey} identifier
603
- * @return {Object} An object with a `data` property containing the serialized resource patch
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
- return {
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
- 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
- };
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
- 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;
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
- 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
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (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;
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
- * :::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
- */
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
- const {
699
- id,
700
- lid,
701
- type
702
- } = identifier;
703
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (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
- };
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
- export { createRecord, deleteRecord, findRecord, postQuery, query, serializePatch, serializeResources, setBuildURLConfig, updateRecord };
501
+
502
+ //#endregion
503
+ export { createRecord, deleteRecord, findRecord, postQuery, query, serializePatch, serializeResources, setBuildURLConfig, updateRecord };
504
+ //# sourceMappingURL=json-api.js.map