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