@warp-drive-mirror/utilities 5.8.0-alpha.30 → 5.8.0-alpha.34

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/dist/string.js +428 -1
  2. package/dist/unpkg/dev/-private.js +7 -0
  3. package/dist/unpkg/dev/active-record.js +395 -0
  4. package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +22 -0
  5. package/dist/unpkg/dev/declarations/-private/active-record/find-record.d.ts +64 -0
  6. package/dist/unpkg/dev/declarations/-private/active-record/query.d.ts +53 -0
  7. package/dist/unpkg/dev/declarations/-private/active-record/save-record.d.ts +146 -0
  8. package/dist/unpkg/dev/declarations/-private/builder-utils.d.ts +4 -0
  9. package/dist/unpkg/dev/declarations/-private/handlers/auto-compress.d.ts +158 -0
  10. package/dist/unpkg/dev/declarations/-private/handlers/gated.d.ts +19 -0
  11. package/dist/unpkg/dev/declarations/-private/handlers/meta-doc.d.ts +47 -0
  12. package/dist/unpkg/dev/declarations/-private/handlers/utils.d.ts +41 -0
  13. package/dist/unpkg/dev/declarations/-private/json-api/-utils.d.ts +109 -0
  14. package/dist/unpkg/dev/declarations/-private/json-api/find-record.d.ts +84 -0
  15. package/dist/unpkg/dev/declarations/-private/json-api/query.d.ts +100 -0
  16. package/dist/unpkg/dev/declarations/-private/json-api/save-record.d.ts +228 -0
  17. package/dist/unpkg/dev/declarations/-private/json-api/serialize.d.ts +70 -0
  18. package/dist/unpkg/dev/declarations/-private/rest/find-record.d.ts +62 -0
  19. package/dist/unpkg/dev/declarations/-private/rest/query.d.ts +53 -0
  20. package/dist/unpkg/dev/declarations/-private/rest/save-record.d.ts +146 -0
  21. package/dist/unpkg/dev/declarations/-private/string/inflect.d.ts +82 -0
  22. package/dist/unpkg/dev/declarations/-private/string/inflections.d.ts +9 -0
  23. package/dist/unpkg/dev/declarations/-private/string/transform.d.ts +77 -0
  24. package/dist/unpkg/dev/declarations/-private.d.ts +1 -0
  25. package/dist/unpkg/dev/declarations/active-record.d.ts +3 -0
  26. package/dist/unpkg/dev/declarations/derivations.d.ts +27 -0
  27. package/dist/unpkg/dev/declarations/handlers.d.ts +10 -0
  28. package/dist/unpkg/dev/declarations/index.d.ts +208 -0
  29. package/dist/unpkg/dev/declarations/json-api.d.ts +5 -0
  30. package/dist/unpkg/dev/declarations/rest.d.ts +3 -0
  31. package/dist/unpkg/dev/declarations/string.d.ts +14 -0
  32. package/dist/unpkg/dev/derivations.js +30 -0
  33. package/dist/unpkg/dev/handlers.js +317 -0
  34. package/dist/unpkg/dev/index.js +361 -0
  35. package/dist/unpkg/dev/inflect-BSG696t-.js +344 -0
  36. package/dist/unpkg/dev/json-api.js +740 -0
  37. package/dist/unpkg/dev/rest.js +393 -0
  38. package/dist/unpkg/dev/string.js +1 -0
  39. package/dist/unpkg/dev-deprecated/-private.js +7 -0
  40. package/dist/unpkg/dev-deprecated/active-record.js +395 -0
  41. package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +22 -0
  42. package/dist/unpkg/dev-deprecated/declarations/-private/active-record/find-record.d.ts +64 -0
  43. package/dist/unpkg/dev-deprecated/declarations/-private/active-record/query.d.ts +53 -0
  44. package/dist/unpkg/dev-deprecated/declarations/-private/active-record/save-record.d.ts +146 -0
  45. package/dist/unpkg/dev-deprecated/declarations/-private/builder-utils.d.ts +4 -0
  46. package/dist/unpkg/dev-deprecated/declarations/-private/handlers/auto-compress.d.ts +158 -0
  47. package/dist/unpkg/dev-deprecated/declarations/-private/handlers/gated.d.ts +19 -0
  48. package/dist/unpkg/dev-deprecated/declarations/-private/handlers/meta-doc.d.ts +47 -0
  49. package/dist/unpkg/dev-deprecated/declarations/-private/handlers/utils.d.ts +41 -0
  50. package/dist/unpkg/dev-deprecated/declarations/-private/json-api/-utils.d.ts +109 -0
  51. package/dist/unpkg/dev-deprecated/declarations/-private/json-api/find-record.d.ts +84 -0
  52. package/dist/unpkg/dev-deprecated/declarations/-private/json-api/query.d.ts +100 -0
  53. package/dist/unpkg/dev-deprecated/declarations/-private/json-api/save-record.d.ts +228 -0
  54. package/dist/unpkg/dev-deprecated/declarations/-private/json-api/serialize.d.ts +70 -0
  55. package/dist/unpkg/dev-deprecated/declarations/-private/rest/find-record.d.ts +62 -0
  56. package/dist/unpkg/dev-deprecated/declarations/-private/rest/query.d.ts +53 -0
  57. package/dist/unpkg/dev-deprecated/declarations/-private/rest/save-record.d.ts +146 -0
  58. package/dist/unpkg/dev-deprecated/declarations/-private/string/inflect.d.ts +82 -0
  59. package/dist/unpkg/dev-deprecated/declarations/-private/string/inflections.d.ts +9 -0
  60. package/dist/unpkg/dev-deprecated/declarations/-private/string/transform.d.ts +77 -0
  61. package/dist/unpkg/dev-deprecated/declarations/-private.d.ts +1 -0
  62. package/dist/unpkg/dev-deprecated/declarations/active-record.d.ts +3 -0
  63. package/dist/unpkg/dev-deprecated/declarations/derivations.d.ts +27 -0
  64. package/dist/unpkg/dev-deprecated/declarations/handlers.d.ts +10 -0
  65. package/dist/unpkg/dev-deprecated/declarations/index.d.ts +208 -0
  66. package/dist/unpkg/dev-deprecated/declarations/json-api.d.ts +5 -0
  67. package/dist/unpkg/dev-deprecated/declarations/rest.d.ts +3 -0
  68. package/dist/unpkg/dev-deprecated/declarations/string.d.ts +14 -0
  69. package/dist/unpkg/dev-deprecated/derivations.js +30 -0
  70. package/dist/unpkg/dev-deprecated/handlers.js +317 -0
  71. package/dist/unpkg/dev-deprecated/index.js +361 -0
  72. package/dist/unpkg/dev-deprecated/inflect-BSG696t-.js +344 -0
  73. package/dist/unpkg/dev-deprecated/json-api.js +740 -0
  74. package/dist/unpkg/dev-deprecated/rest.js +393 -0
  75. package/dist/unpkg/dev-deprecated/string.js +1 -0
  76. package/dist/unpkg/prod/-private.js +7 -0
  77. package/dist/unpkg/prod/active-record.js +395 -0
  78. package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +22 -0
  79. package/dist/unpkg/prod/declarations/-private/active-record/find-record.d.ts +64 -0
  80. package/dist/unpkg/prod/declarations/-private/active-record/query.d.ts +53 -0
  81. package/dist/unpkg/prod/declarations/-private/active-record/save-record.d.ts +146 -0
  82. package/dist/unpkg/prod/declarations/-private/builder-utils.d.ts +4 -0
  83. package/dist/unpkg/prod/declarations/-private/handlers/auto-compress.d.ts +158 -0
  84. package/dist/unpkg/prod/declarations/-private/handlers/gated.d.ts +19 -0
  85. package/dist/unpkg/prod/declarations/-private/handlers/meta-doc.d.ts +47 -0
  86. package/dist/unpkg/prod/declarations/-private/handlers/utils.d.ts +41 -0
  87. package/dist/unpkg/prod/declarations/-private/json-api/-utils.d.ts +109 -0
  88. package/dist/unpkg/prod/declarations/-private/json-api/find-record.d.ts +84 -0
  89. package/dist/unpkg/prod/declarations/-private/json-api/query.d.ts +100 -0
  90. package/dist/unpkg/prod/declarations/-private/json-api/save-record.d.ts +228 -0
  91. package/dist/unpkg/prod/declarations/-private/json-api/serialize.d.ts +70 -0
  92. package/dist/unpkg/prod/declarations/-private/rest/find-record.d.ts +62 -0
  93. package/dist/unpkg/prod/declarations/-private/rest/query.d.ts +53 -0
  94. package/dist/unpkg/prod/declarations/-private/rest/save-record.d.ts +146 -0
  95. package/dist/unpkg/prod/declarations/-private/string/inflect.d.ts +82 -0
  96. package/dist/unpkg/prod/declarations/-private/string/inflections.d.ts +9 -0
  97. package/dist/unpkg/prod/declarations/-private/string/transform.d.ts +77 -0
  98. package/dist/unpkg/prod/declarations/-private.d.ts +1 -0
  99. package/dist/unpkg/prod/declarations/active-record.d.ts +3 -0
  100. package/dist/unpkg/prod/declarations/derivations.d.ts +27 -0
  101. package/dist/unpkg/prod/declarations/handlers.d.ts +10 -0
  102. package/dist/unpkg/prod/declarations/index.d.ts +208 -0
  103. package/dist/unpkg/prod/declarations/json-api.d.ts +5 -0
  104. package/dist/unpkg/prod/declarations/rest.d.ts +3 -0
  105. package/dist/unpkg/prod/declarations/string.d.ts +14 -0
  106. package/dist/unpkg/prod/derivations.js +30 -0
  107. package/dist/unpkg/prod/handlers.js +317 -0
  108. package/dist/unpkg/prod/index.js +361 -0
  109. package/dist/unpkg/prod/inflect-BSG696t-.js +344 -0
  110. package/dist/unpkg/prod/json-api.js +740 -0
  111. package/dist/unpkg/prod/rest.js +393 -0
  112. package/dist/unpkg/prod/string.js +1 -0
  113. package/dist/unpkg/prod-deprecated/-private.js +7 -0
  114. package/dist/unpkg/prod-deprecated/active-record.js +395 -0
  115. package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +22 -0
  116. package/dist/unpkg/prod-deprecated/declarations/-private/active-record/find-record.d.ts +64 -0
  117. package/dist/unpkg/prod-deprecated/declarations/-private/active-record/query.d.ts +53 -0
  118. package/dist/unpkg/prod-deprecated/declarations/-private/active-record/save-record.d.ts +146 -0
  119. package/dist/unpkg/prod-deprecated/declarations/-private/builder-utils.d.ts +4 -0
  120. package/dist/unpkg/prod-deprecated/declarations/-private/handlers/auto-compress.d.ts +158 -0
  121. package/dist/unpkg/prod-deprecated/declarations/-private/handlers/gated.d.ts +19 -0
  122. package/dist/unpkg/prod-deprecated/declarations/-private/handlers/meta-doc.d.ts +47 -0
  123. package/dist/unpkg/prod-deprecated/declarations/-private/handlers/utils.d.ts +41 -0
  124. package/dist/unpkg/prod-deprecated/declarations/-private/json-api/-utils.d.ts +109 -0
  125. package/dist/unpkg/prod-deprecated/declarations/-private/json-api/find-record.d.ts +84 -0
  126. package/dist/unpkg/prod-deprecated/declarations/-private/json-api/query.d.ts +100 -0
  127. package/dist/unpkg/prod-deprecated/declarations/-private/json-api/save-record.d.ts +228 -0
  128. package/dist/unpkg/prod-deprecated/declarations/-private/json-api/serialize.d.ts +70 -0
  129. package/dist/unpkg/prod-deprecated/declarations/-private/rest/find-record.d.ts +62 -0
  130. package/dist/unpkg/prod-deprecated/declarations/-private/rest/query.d.ts +53 -0
  131. package/dist/unpkg/prod-deprecated/declarations/-private/rest/save-record.d.ts +146 -0
  132. package/dist/unpkg/prod-deprecated/declarations/-private/string/inflect.d.ts +82 -0
  133. package/dist/unpkg/prod-deprecated/declarations/-private/string/inflections.d.ts +9 -0
  134. package/dist/unpkg/prod-deprecated/declarations/-private/string/transform.d.ts +77 -0
  135. package/dist/unpkg/prod-deprecated/declarations/-private.d.ts +1 -0
  136. package/dist/unpkg/prod-deprecated/declarations/active-record.d.ts +3 -0
  137. package/dist/unpkg/prod-deprecated/declarations/derivations.d.ts +27 -0
  138. package/dist/unpkg/prod-deprecated/declarations/handlers.d.ts +10 -0
  139. package/dist/unpkg/prod-deprecated/declarations/index.d.ts +208 -0
  140. package/dist/unpkg/prod-deprecated/declarations/json-api.d.ts +5 -0
  141. package/dist/unpkg/prod-deprecated/declarations/rest.d.ts +3 -0
  142. package/dist/unpkg/prod-deprecated/declarations/string.d.ts +14 -0
  143. package/dist/unpkg/prod-deprecated/derivations.js +30 -0
  144. package/dist/unpkg/prod-deprecated/handlers.js +317 -0
  145. package/dist/unpkg/prod-deprecated/index.js +361 -0
  146. package/dist/unpkg/prod-deprecated/inflect-BSG696t-.js +344 -0
  147. package/dist/unpkg/prod-deprecated/json-api.js +740 -0
  148. package/dist/unpkg/prod-deprecated/rest.js +393 -0
  149. package/dist/unpkg/prod-deprecated/string.js +1 -0
  150. package/package.json +28 -4
@@ -0,0 +1,740 @@
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-mirror/core';
5
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
6
+ const JsonApiAccept = 'application/vnd.api+json';
7
+ const DEFAULT_CONFIG = {
8
+ host: '',
9
+ namespace: ''
10
+ };
11
+ let ACCEPT_HEADER_VALUE = 'application/vnd.api+json';
12
+
13
+ /**
14
+ * Allows setting extensions and profiles to be used in the `Accept` header.
15
+ *
16
+ * Extensions and profiles are keyed by their namespace with the value being
17
+ * their URI.
18
+ *
19
+ * Example:
20
+ *
21
+ * ```ts
22
+ * setBuildURLConfig({
23
+ * extensions: {
24
+ * atomic: 'https://jsonapi.org/ext/atomic'
25
+ * },
26
+ * profiles: {
27
+ * pagination: 'https://jsonapi.org/profiles/ethanresnick/cursor-pagination'
28
+ * }
29
+ * });
30
+ * ```
31
+ *
32
+ * This also sets the global configuration for `buildBaseURL`
33
+ * for host and namespace values for the global coniguration
34
+ * done via `import { setBuildURLConfig } from '@warp-drive-mirror/utilities';`
35
+ *
36
+ * These values may still be overridden by passing
37
+ * them to buildBaseURL directly.
38
+ *
39
+ * This method may be called as many times as needed
40
+ *
41
+ * ```ts
42
+ * type BuildURLConfig = {
43
+ * host: string;
44
+ * namespace: string'
45
+ * }
46
+ * ```
47
+ *
48
+ * @public
49
+ * @param {BuildURLConfig} config
50
+ * @return {void}
51
+ */
52
+ function setBuildURLConfig(config) {
53
+ Object.assign({}, DEFAULT_CONFIG, config);
54
+ if (config.profiles || config.extensions) {
55
+ let accept = JsonApiAccept;
56
+ if (config.profiles) {
57
+ const profiles = Object.values(config.profiles);
58
+ if (profiles.length) {
59
+ accept += ';profile="' + profiles.join(' ') + '"';
60
+ }
61
+ }
62
+ if (config.extensions) {
63
+ const extensions = Object.values(config.extensions);
64
+ if (extensions.length) {
65
+ accept += ';ext=' + extensions.join(' ');
66
+ }
67
+ }
68
+ ACCEPT_HEADER_VALUE = accept;
69
+ }
70
+ setBuildURLConfig$1(config);
71
+ }
72
+
73
+ /**
74
+ * Builds request options to fetch a single resource by a known id or identifier
75
+ * configured for the url and header expectations of most JSON:API APIs.
76
+ *
77
+ * :::tabs
78
+ *
79
+ * == Basic Usage
80
+ *
81
+ * ```ts
82
+ * import { findRecord } from '@warp-drive-mirror/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-mirror/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-mirror/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-mirror/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
+
149
+ 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
+ };
175
+ }
176
+
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-mirror/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-mirror/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-mirror/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
+ };
251
+ }
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-mirror/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-mirror/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
+ };
323
+ }
324
+ function isExisting(identifier) {
325
+ return 'id' in identifier && identifier.id !== null && 'type' in identifier && identifier.type !== null;
326
+ }
327
+
328
+ /**
329
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Request Body**
330
+ * While this may come as a surprise, the app providing the body ensures that only
331
+ * desired and correctly formatted data is sent with the request.
332
+ * :::
333
+ *
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-mirror/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-mirror/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
+
383
+ function deleteRecord(record, options = {}) {
384
+ const identifier = recordIdentifierFor(record);
385
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
386
+ if (!test) {
387
+ throw new Error(`Expected to be given a record instance`);
388
+ }
389
+ })(identifier) : {};
390
+ macroCondition(getGlobalConfig().WarpDriveMirror.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
+ };
414
+ }
415
+
416
+ /**
417
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
418
+ * While this may come as a surprise, the app providing the body ensures that only
419
+ * desired and correctly formatted data is sent with the request.
420
+ * :::
421
+ *
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-mirror/core';
429
+ * import { createRecord } from '@warp-drive-mirror/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-mirror/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
+
471
+ function createRecord(record, options = {}) {
472
+ const identifier = recordIdentifierFor(record);
473
+ macroCondition(getGlobalConfig().WarpDriveMirror.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
+ };
497
+ }
498
+
499
+ /**
500
+ * :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
501
+ * While this may come as a surprise, the app providing the body ensures that only
502
+ * desired and correctly formatted data is sent with the request.
503
+ * :::
504
+ *
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-mirror/core';
512
+ * import { updateRecord } from '@warp-drive-mirror/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-mirror/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
+
557
+ function updateRecord(record, options = {}) {
558
+ const identifier = recordIdentifierFor(record);
559
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
560
+ if (!test) {
561
+ throw new Error(`Expected to be given a record instance`);
562
+ }
563
+ })(identifier) : {};
564
+ macroCondition(getGlobalConfig().WarpDriveMirror.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
+ };
588
+ }
589
+
590
+ /**
591
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
592
+ *
593
+ * While this may come as a surprise, they are intended to serialize cache state for more
594
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
595
+ * app can ensure that the body is correctly formatted and contains all necessary data.
596
+ * :::
597
+ *
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
+ */
605
+ function serializeResources(cache, identifiers) {
606
+ return {
607
+ data: Array.isArray(identifiers) ? identifiers.map(identifier => _serializeResource(cache, identifier)) : _serializeResource(cache, identifiers)
608
+ };
609
+ }
610
+ function fixRef({
611
+ id,
612
+ lid,
613
+ type
614
+ }) {
615
+ if (id !== null) {
616
+ return {
617
+ id,
618
+ type
619
+ };
620
+ }
621
+ return {
622
+ id,
623
+ lid,
624
+ type
625
+ };
626
+ }
627
+ function fixRelData(rel) {
628
+ if (Array.isArray(rel)) {
629
+ return rel.map(ref => fixRef(ref));
630
+ } else if (typeof rel === 'object' && rel !== null) {
631
+ return fixRef(rel);
632
+ }
633
+ return null;
634
+ }
635
+ function _serializeResource(cache, identifier) {
636
+ const {
637
+ id,
638
+ lid,
639
+ type
640
+ } = identifier;
641
+ // peek gives us everything we want, but since its referentially the same data
642
+ // as is in the cache we clone it to avoid any accidental mutations
643
+ const record = structuredClone(cache.peek(identifier));
644
+ macroCondition(getGlobalConfig().WarpDriveMirror.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;
667
+ }
668
+
669
+ /**
670
+ * :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
671
+ *
672
+ * While this may come as a surprise, they are intended to serialize cache state for more
673
+ * generalized usage. {json:api} has a large variance in acceptable shapes, and only your
674
+ * app can ensure that the body is correctly formatted and contains all necessary data.
675
+ * :::
676
+ *
677
+ * Serializes changes to a resource. Useful for use with building bodies for PATCH requests.
678
+ *
679
+ * Only attributes which are changed are serialized.
680
+ * Only relationships which are changed are serialized.
681
+ *
682
+ * Collection relationships serialize the collection as a whole.
683
+ *
684
+ * If you would like to serialize updates to a collection more granularly
685
+ * (for instance, as operations) request the diff from the store and
686
+ * serialize as desired:
687
+ *
688
+ * ```ts
689
+ * const relationshipDiffMap = cache.changedRelationships(identifier);
690
+ * ```
691
+ *
692
+ * @public
693
+ * @param {Cache} cache}
694
+ * @param {ResourceKey} identifier
695
+ * @return {Object} An object with a `data` property containing the serialized resource patch
696
+ */
697
+ function serializePatch(cache, identifier) {
698
+ const {
699
+ id,
700
+ lid,
701
+ type
702
+ } = identifier;
703
+ macroCondition(getGlobalConfig().WarpDriveMirror.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
+ };
739
+ }
740
+ export { createRecord, deleteRecord, findRecord, postQuery, query, serializePatch, serializeResources, setBuildURLConfig, updateRecord };