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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +16 -25
  2. package/declarations/-private/active-record/find-record.d.ts +5 -6
  3. package/declarations/-private/active-record/query.d.ts +2 -2
  4. package/declarations/-private/active-record/save-record.d.ts +2 -2
  5. package/declarations/-private/handlers/meta-doc.d.ts +1 -1
  6. package/declarations/-private/json-api/find-record.d.ts +37 -18
  7. package/declarations/-private/json-api/query.d.ts +3 -6
  8. package/declarations/-private/json-api/save-record.d.ts +45 -10
  9. package/declarations/-private/json-api/serialize.d.ts +15 -1
  10. package/declarations/-private/rest/find-record.d.ts +5 -8
  11. package/declarations/-private/rest/query.d.ts +2 -2
  12. package/declarations/-private/rest/save-record.d.ts +2 -2
  13. package/dist/active-record.js +2 -0
  14. package/dist/handlers.js +3 -3
  15. package/dist/json-api.js +94 -26
  16. package/dist/rest.js +2 -2
  17. package/dist/string.js +428 -1
  18. package/dist/unpkg/dev/-private.js +7 -0
  19. package/dist/unpkg/dev/active-record.js +394 -0
  20. package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +22 -0
  21. package/dist/unpkg/dev/derivations.js +30 -0
  22. package/dist/unpkg/dev/handlers.js +316 -0
  23. package/dist/unpkg/dev/index.js +362 -0
  24. package/dist/unpkg/dev/inflect-BEv8WqY1.js +343 -0
  25. package/dist/unpkg/dev/json-api.js +740 -0
  26. package/dist/unpkg/dev/rest.js +392 -0
  27. package/dist/unpkg/dev/string.js +1 -0
  28. package/dist/unpkg/dev-deprecated/-private.js +7 -0
  29. package/dist/unpkg/dev-deprecated/active-record.js +394 -0
  30. package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +22 -0
  31. package/dist/unpkg/dev-deprecated/derivations.js +30 -0
  32. package/dist/unpkg/dev-deprecated/handlers.js +316 -0
  33. package/dist/unpkg/dev-deprecated/index.js +362 -0
  34. package/dist/unpkg/dev-deprecated/inflect-BEv8WqY1.js +343 -0
  35. package/dist/unpkg/dev-deprecated/json-api.js +740 -0
  36. package/dist/unpkg/dev-deprecated/rest.js +392 -0
  37. package/dist/unpkg/dev-deprecated/string.js +1 -0
  38. package/dist/unpkg/prod/-private.js +7 -0
  39. package/dist/unpkg/prod/active-record.js +366 -0
  40. package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +22 -0
  41. package/dist/unpkg/prod/derivations.js +30 -0
  42. package/dist/unpkg/prod/handlers.js +305 -0
  43. package/dist/unpkg/prod/index.js +239 -0
  44. package/dist/unpkg/prod/inflect-Dh9dyEYx.js +333 -0
  45. package/dist/unpkg/prod/json-api.js +702 -0
  46. package/dist/unpkg/prod/rest.js +364 -0
  47. package/dist/unpkg/prod/string.js +1 -0
  48. package/dist/unpkg/prod-deprecated/-private.js +7 -0
  49. package/dist/unpkg/prod-deprecated/active-record.js +366 -0
  50. package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +22 -0
  51. package/dist/unpkg/prod-deprecated/derivations.js +30 -0
  52. package/dist/unpkg/prod-deprecated/handlers.js +305 -0
  53. package/dist/unpkg/prod-deprecated/index.js +239 -0
  54. package/dist/unpkg/prod-deprecated/inflect-Dh9dyEYx.js +333 -0
  55. package/dist/unpkg/prod-deprecated/json-api.js +702 -0
  56. package/dist/unpkg/prod-deprecated/rest.js +364 -0
  57. package/dist/unpkg/prod-deprecated/string.js +1 -0
  58. package/logos/README.md +2 -2
  59. package/logos/logo-yellow-slab.svg +1 -0
  60. package/logos/word-mark-black.svg +1 -0
  61. package/logos/word-mark-white.svg +1 -0
  62. package/package.json +13 -5
  63. package/logos/NCC-1701-a-blue.svg +0 -4
  64. package/logos/NCC-1701-a-gold.svg +0 -4
  65. package/logos/NCC-1701-a-gold_100.svg +0 -1
  66. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  67. package/logos/NCC-1701-a.svg +0 -4
  68. package/logos/docs-badge.svg +0 -2
  69. package/logos/ember-data-logo-dark.svg +0 -12
  70. package/logos/ember-data-logo-light.svg +0 -12
  71. package/logos/social1.png +0 -0
  72. package/logos/social2.png +0 -0
  73. package/logos/warp-drive-logo-dark.svg +0 -4
  74. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,366 @@
1
+ import { buildBaseURL, buildQueryParams } from './index.js';
2
+ import { p as pluralize, j as underscore } from "./inflect-Dh9dyEYx.js";
3
+ import { e as extractCacheOptions, c as copyForwardUrlOptions } from "./builder-utils-Donkk-BZ.js";
4
+ import { recordIdentifierFor } from '@warp-drive-mirror/core';
5
+
6
+ /**
7
+ * Builds request options to fetch a single resource by a known id or identifier
8
+ * configured for the url and header expectations of most ActiveRecord APIs.
9
+ *
10
+ * **Basic Usage**
11
+ *
12
+ * ```ts
13
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
14
+ *
15
+ * const data = await store.request(findRecord('person', '1'));
16
+ * ```
17
+ *
18
+ * **With Options**
19
+ *
20
+ * ```ts
21
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
22
+ *
23
+ * const options = findRecord('person', '1', { include: ['pets', 'friends'] });
24
+ * const data = await store.request(options);
25
+ * ```
26
+ *
27
+ * **With an Identifier**
28
+ *
29
+ * ```ts
30
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
31
+ *
32
+ * const options = findRecord({ type: 'person', id: '1' }, { include: ['pets', 'friends'] });
33
+ * const data = await store.request(options);
34
+ * ```
35
+ *
36
+ * **Supplying Options to Modify the Request Behavior**
37
+ *
38
+ * The following options are supported:
39
+ *
40
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
41
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
42
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
43
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
44
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
45
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
46
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
47
+ * defaulting to `false` if none is configured.
48
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
49
+ *
50
+ * ```ts
51
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
52
+ *
53
+ * const options = findRecord('person', '1', { include: ['pets', 'friends'] }, { namespace: 'api/v2' });
54
+ * const data = await store.request(options);
55
+ * ```
56
+ *
57
+ * @public
58
+ * @param identifier
59
+ * @param options
60
+ */
61
+
62
+ function findRecord(arg1, arg2, arg3) {
63
+ const identifier = typeof arg1 === 'string' ? {
64
+ type: arg1,
65
+ id: arg2
66
+ } : arg1;
67
+ const options = (typeof arg1 === 'string' ? arg3 : arg2) || {};
68
+ const cacheOptions = extractCacheOptions(options);
69
+ const urlOptions = {
70
+ identifier,
71
+ op: 'findRecord',
72
+ resourcePath: pluralize(underscore(identifier.type))
73
+ };
74
+ copyForwardUrlOptions(urlOptions, options);
75
+ const url = buildBaseURL(urlOptions);
76
+ const headers = new Headers();
77
+ headers.append('Accept', 'application/json;charset=utf-8');
78
+ return {
79
+ url: options.include?.length ? `${url}?${buildQueryParams({
80
+ include: options.include
81
+ }, options.urlParamsSettings)}` : url,
82
+ method: 'GET',
83
+ headers,
84
+ cacheOptions,
85
+ op: 'findRecord',
86
+ records: [identifier]
87
+ };
88
+ }
89
+
90
+ /** @deprecated use {@link ReactiveDataDocument} instead */
91
+
92
+ /**
93
+ * Builds request options to query for resources, usually by a primary
94
+ * type, configured for the url and header expectations of most ActiveRecord APIs.
95
+ *
96
+ * **Basic Usage**
97
+ *
98
+ * ```ts
99
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
100
+ *
101
+ * const data = await store.request(query('person'));
102
+ * ```
103
+ *
104
+ * **With Query Params**
105
+ *
106
+ * ```ts
107
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
108
+ *
109
+ * const options = query('person', { include: ['pets', 'friends'] });
110
+ * const data = await store.request(options);
111
+ * ```
112
+ *
113
+ * **Supplying Options to Modify the Request Behavior**
114
+ *
115
+ * The following options are supported:
116
+ *
117
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
118
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
119
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
120
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
121
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
122
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
123
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
124
+ * defaulting to `false` if none is configured.
125
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
126
+ *
127
+ * ```ts
128
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
129
+ *
130
+ * const options = query('person', { include: ['pets', 'friends'] }, { reload: true });
131
+ * const data = await store.request(options);
132
+ * ```
133
+ *
134
+ * @public
135
+ * @param identifier
136
+ * @param query
137
+ * @param options
138
+ */
139
+
140
+ function query(type,
141
+ // eslint-disable-next-line @typescript-eslint/no-shadow
142
+ query = {}, options = {}) {
143
+ const cacheOptions = extractCacheOptions(options);
144
+ const urlOptions = {
145
+ identifier: {
146
+ type
147
+ },
148
+ op: 'query',
149
+ resourcePath: pluralize(underscore(type))
150
+ };
151
+ copyForwardUrlOptions(urlOptions, options);
152
+ const url = buildBaseURL(urlOptions);
153
+ const headers = new Headers();
154
+ headers.append('Accept', 'application/json;charset=utf-8');
155
+ const queryString = buildQueryParams(query, options.urlParamsSettings);
156
+ return {
157
+ url: queryString ? `${url}?${queryString}` : url,
158
+ method: 'GET',
159
+ headers,
160
+ cacheOptions,
161
+ op: 'query'
162
+ };
163
+ }
164
+
165
+ /**
166
+ * Builds request options to delete record for resources,
167
+ * configured for the url, method and header expectations of ActiveRecord APIs.
168
+ *
169
+ * **Basic Usage**
170
+ *
171
+ * ```ts
172
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/active-record';
173
+ *
174
+ * const person = store.peekRecord('person', '1');
175
+ *
176
+ * // mark record as deleted
177
+ * store.deleteRecord(person);
178
+ *
179
+ * // persist deletion
180
+ * const data = await store.request(deleteRecord(person));
181
+ * ```
182
+ *
183
+ * **Supplying Options to Modify the Request Behavior**
184
+ *
185
+ * The following options are supported:
186
+ *
187
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
188
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
189
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
190
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
191
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
192
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
193
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
194
+ * defaulting to `false` if none is configured.
195
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
196
+ *
197
+ * ```ts
198
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/active-record';
199
+ *
200
+ * const person = store.peekRecord('person', '1');
201
+ *
202
+ * // mark record as deleted
203
+ * store.deleteRecord(person);
204
+ *
205
+ * // persist deletion
206
+ * const options = deleteRecord(person, { namespace: 'api/v1' });
207
+ * const data = await store.request(options);
208
+ * ```
209
+ *
210
+ * @public
211
+ * @param record
212
+ * @param options
213
+ */
214
+
215
+ function deleteRecord(record, options = {}) {
216
+ const identifier = recordIdentifierFor(record);
217
+ const urlOptions = {
218
+ identifier: identifier,
219
+ op: 'deleteRecord',
220
+ resourcePath: pluralize(underscore(identifier.type))
221
+ };
222
+ copyForwardUrlOptions(urlOptions, options);
223
+ const url = buildBaseURL(urlOptions);
224
+ const headers = new Headers();
225
+ headers.append('Accept', 'application/json;charset=utf-8');
226
+ return {
227
+ url,
228
+ method: 'DELETE',
229
+ headers,
230
+ op: 'deleteRecord',
231
+ data: {
232
+ record: identifier
233
+ },
234
+ records: [identifier]
235
+ };
236
+ }
237
+
238
+ /**
239
+ * Builds request options to create new record for resources,
240
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
241
+ *
242
+ * **Basic Usage**
243
+ *
244
+ * ```ts
245
+ * import { createRecord } from '@warp-drive-mirror/utilities/active-record';
246
+ *
247
+ * const person = store.createRecord('person', { name: 'Ted' });
248
+ * const data = await store.request(createRecord(person));
249
+ * ```
250
+ *
251
+ * **Supplying Options to Modify the Request Behavior**
252
+ *
253
+ * The following options are supported:
254
+ *
255
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
256
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
257
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
258
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
259
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
260
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
261
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
262
+ * defaulting to `false` if none is configured.
263
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
264
+ *
265
+ * ```ts
266
+ * import { createRecord } from '@warp-drive-mirror/utilities/active-record';
267
+ *
268
+ * const person = store.createRecord('person', { name: 'Ted' });
269
+ * const options = createRecord(person, { namespace: 'api/v1' });
270
+ * const data = await store.request(options);
271
+ * ```
272
+ *
273
+ * @public
274
+ * @param record
275
+ * @param options
276
+ */
277
+
278
+ function createRecord(record, options = {}) {
279
+ const identifier = recordIdentifierFor(record);
280
+ const urlOptions = {
281
+ identifier: identifier,
282
+ op: 'createRecord',
283
+ resourcePath: pluralize(underscore(identifier.type))
284
+ };
285
+ copyForwardUrlOptions(urlOptions, options);
286
+ const url = buildBaseURL(urlOptions);
287
+ const headers = new Headers();
288
+ headers.append('Accept', 'application/json;charset=utf-8');
289
+ return {
290
+ url,
291
+ method: 'POST',
292
+ headers,
293
+ op: 'createRecord',
294
+ data: {
295
+ record: identifier
296
+ },
297
+ records: [identifier]
298
+ };
299
+ }
300
+
301
+ /**
302
+ * Builds request options to update existing record for resources,
303
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
304
+ *
305
+ * **Basic Usage**
306
+ *
307
+ * ```ts
308
+ * import { updateRecord } from '@warp-drive-mirror/utilities/active-record';
309
+ *
310
+ * const person = store.peekRecord('person', '1');
311
+ * person.name = 'Chris';
312
+ * const data = await store.request(updateRecord(person));
313
+ * ```
314
+ *
315
+ * **Supplying Options to Modify the Request Behavior**
316
+ *
317
+ * The following options are supported:
318
+ *
319
+ * - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
320
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
321
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
322
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
323
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
324
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
325
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
326
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
327
+ * defaulting to `false` if none is configured.
328
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
329
+ *
330
+ * ```ts
331
+ * import { updateRecord } from '@warp-drive-mirror/utilities/active-record';
332
+ *
333
+ * const person = store.peekRecord('person', '1');
334
+ * person.name = 'Chris';
335
+ * const options = updateRecord(person, { patch: true });
336
+ * const data = await store.request(options);
337
+ * ```
338
+ *
339
+ * @public
340
+ * @param record
341
+ * @param options
342
+ */
343
+
344
+ function updateRecord(record, options = {}) {
345
+ const identifier = recordIdentifierFor(record);
346
+ const urlOptions = {
347
+ identifier: identifier,
348
+ op: 'updateRecord',
349
+ resourcePath: pluralize(underscore(identifier.type))
350
+ };
351
+ copyForwardUrlOptions(urlOptions, options);
352
+ const url = buildBaseURL(urlOptions);
353
+ const headers = new Headers();
354
+ headers.append('Accept', 'application/json;charset=utf-8');
355
+ return {
356
+ url,
357
+ method: options.patch ? 'PATCH' : 'PUT',
358
+ headers,
359
+ op: 'updateRecord',
360
+ data: {
361
+ record: identifier
362
+ },
363
+ records: [identifier]
364
+ };
365
+ }
366
+ export { createRecord, deleteRecord, findRecord, query, updateRecord };
@@ -0,0 +1,22 @@
1
+ function copyForwardUrlOptions(urlOptions, options) {
2
+ if ('host' in options) {
3
+ urlOptions.host = options.host;
4
+ }
5
+ if ('namespace' in options) {
6
+ urlOptions.namespace = options.namespace;
7
+ }
8
+ if ('resourcePath' in options) {
9
+ urlOptions.resourcePath = options.resourcePath;
10
+ }
11
+ }
12
+ function extractCacheOptions(options) {
13
+ const cacheOptions = {};
14
+ if ('reload' in options) {
15
+ cacheOptions.reload = options.reload;
16
+ }
17
+ if ('backgroundReload' in options) {
18
+ cacheOptions.backgroundReload = options.backgroundReload;
19
+ }
20
+ return cacheOptions;
21
+ }
22
+ export { copyForwardUrlOptions as c, extractCacheOptions as e };
@@ -0,0 +1,30 @@
1
+ import { Type } from '@warp-drive-mirror/core/types/symbols';
2
+
3
+ /**
4
+ * A derivation for use by {@link ReactiveResource} that joins the given fields
5
+ * with the optional separator (or '' if no separator is provided).
6
+ *
7
+ * Generally you should not need to import and use this function directly.
8
+ *
9
+ * @example
10
+ * {
11
+ * name: 'fullName',
12
+ * kind: 'derived',
13
+ * type: 'concat',
14
+ * options: {
15
+ * fields: ['firstName', 'lastName'],
16
+ * separator: ' ',
17
+ * },
18
+ * }
19
+ */
20
+ const concat = (record, options, _prop) => {
21
+ if (!options) {
22
+ throw new Error(`options is required`);
23
+ }
24
+ // SAFETY: we cast internally to a more specific type, for our own use
25
+ // SAFETY: but provide the more general signature to the schema service
26
+ const opts = options;
27
+ return opts.fields.map(field => record[field]).join(opts.separator ?? '');
28
+ };
29
+ concat[Type] = 'concat';
30
+ export { concat };