@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,395 @@
1
+ import { buildBaseURL, buildQueryParams } from './index.js';
2
+ import { p as pluralize, j as underscore } 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
+
7
+ /**
8
+ * Builds request options to fetch a single resource by a known id or identifier
9
+ * configured for the url and header expectations of most ActiveRecord APIs.
10
+ *
11
+ * **Basic Usage**
12
+ *
13
+ * ```ts
14
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
15
+ *
16
+ * const data = await store.request(findRecord('person', '1'));
17
+ * ```
18
+ *
19
+ * **With Options**
20
+ *
21
+ * ```ts
22
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
23
+ *
24
+ * const options = findRecord('person', '1', { include: ['pets', 'friends'] });
25
+ * const data = await store.request(options);
26
+ * ```
27
+ *
28
+ * **With an Identifier**
29
+ *
30
+ * ```ts
31
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
32
+ *
33
+ * const options = findRecord({ type: 'person', id: '1' }, { include: ['pets', 'friends'] });
34
+ * const data = await store.request(options);
35
+ * ```
36
+ *
37
+ * **Supplying Options to Modify the Request Behavior**
38
+ *
39
+ * The following options are supported:
40
+ *
41
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
42
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
43
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
44
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
45
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
46
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
47
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
48
+ * defaulting to `false` if none is configured.
49
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
50
+ *
51
+ * ```ts
52
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
53
+ *
54
+ * const options = findRecord('person', '1', { include: ['pets', 'friends'] }, { namespace: 'api/v2' });
55
+ * const data = await store.request(options);
56
+ * ```
57
+ *
58
+ * @public
59
+ * @param identifier
60
+ * @param options
61
+ */
62
+
63
+ function findRecord(arg1, arg2, arg3) {
64
+ const identifier = typeof arg1 === 'string' ? {
65
+ type: arg1,
66
+ id: arg2
67
+ } : arg1;
68
+ const options = (typeof arg1 === 'string' ? arg3 : arg2) || {};
69
+ const cacheOptions = extractCacheOptions(options);
70
+ const urlOptions = {
71
+ identifier,
72
+ op: 'findRecord',
73
+ resourcePath: pluralize(underscore(identifier.type))
74
+ };
75
+ copyForwardUrlOptions(urlOptions, options);
76
+ const url = buildBaseURL(urlOptions);
77
+ const headers = new Headers();
78
+ headers.append('Accept', 'application/json;charset=utf-8');
79
+ return {
80
+ url: options.include?.length ? `${url}?${buildQueryParams({
81
+ include: options.include
82
+ }, options.urlParamsSettings)}` : url,
83
+ method: 'GET',
84
+ headers,
85
+ cacheOptions,
86
+ op: 'findRecord',
87
+ records: [identifier]
88
+ };
89
+ }
90
+
91
+ /** @deprecated use {@link ReactiveDataDocument} instead */
92
+
93
+ /**
94
+ * Builds request options to query for resources, usually by a primary
95
+ * type, configured for the url and header expectations of most ActiveRecord APIs.
96
+ *
97
+ * **Basic Usage**
98
+ *
99
+ * ```ts
100
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
101
+ *
102
+ * const data = await store.request(query('person'));
103
+ * ```
104
+ *
105
+ * **With Query Params**
106
+ *
107
+ * ```ts
108
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
109
+ *
110
+ * const options = query('person', { include: ['pets', 'friends'] });
111
+ * const data = await store.request(options);
112
+ * ```
113
+ *
114
+ * **Supplying Options to Modify the Request Behavior**
115
+ *
116
+ * The following options are supported:
117
+ *
118
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
119
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
120
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
121
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
122
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
123
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
124
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
125
+ * defaulting to `false` if none is configured.
126
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
127
+ *
128
+ * ```ts
129
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
130
+ *
131
+ * const options = query('person', { include: ['pets', 'friends'] }, { reload: true });
132
+ * const data = await store.request(options);
133
+ * ```
134
+ *
135
+ * @public
136
+ * @param identifier
137
+ * @param query
138
+ * @param options
139
+ */
140
+
141
+ function query(type,
142
+ // eslint-disable-next-line @typescript-eslint/no-shadow
143
+ query = {}, options = {}) {
144
+ const cacheOptions = extractCacheOptions(options);
145
+ const urlOptions = {
146
+ identifier: {
147
+ type
148
+ },
149
+ op: 'query',
150
+ resourcePath: pluralize(underscore(type))
151
+ };
152
+ copyForwardUrlOptions(urlOptions, options);
153
+ const url = buildBaseURL(urlOptions);
154
+ const headers = new Headers();
155
+ headers.append('Accept', 'application/json;charset=utf-8');
156
+ const queryString = buildQueryParams(query, options.urlParamsSettings);
157
+ return {
158
+ url: queryString ? `${url}?${queryString}` : url,
159
+ method: 'GET',
160
+ headers,
161
+ cacheOptions,
162
+ op: 'query'
163
+ };
164
+ }
165
+ function isExisting(identifier) {
166
+ return 'id' in identifier && identifier.id !== null && 'type' in identifier && identifier.type !== null;
167
+ }
168
+
169
+ /**
170
+ * Builds request options to delete record for resources,
171
+ * configured for the url, method and header expectations of ActiveRecord APIs.
172
+ *
173
+ * **Basic Usage**
174
+ *
175
+ * ```ts
176
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/active-record';
177
+ *
178
+ * const person = store.peekRecord('person', '1');
179
+ *
180
+ * // mark record as deleted
181
+ * store.deleteRecord(person);
182
+ *
183
+ * // persist deletion
184
+ * const data = await store.request(deleteRecord(person));
185
+ * ```
186
+ *
187
+ * **Supplying Options to Modify the Request Behavior**
188
+ *
189
+ * The following options are supported:
190
+ *
191
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
192
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
193
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
194
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
195
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
196
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
197
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
198
+ * defaulting to `false` if none is configured.
199
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
200
+ *
201
+ * ```ts
202
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/active-record';
203
+ *
204
+ * const person = store.peekRecord('person', '1');
205
+ *
206
+ * // mark record as deleted
207
+ * store.deleteRecord(person);
208
+ *
209
+ * // persist deletion
210
+ * const options = deleteRecord(person, { namespace: 'api/v1' });
211
+ * const data = await store.request(options);
212
+ * ```
213
+ *
214
+ * @public
215
+ * @param record
216
+ * @param options
217
+ */
218
+
219
+ function deleteRecord(record, options = {}) {
220
+ const identifier = recordIdentifierFor(record);
221
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
222
+ if (!test) {
223
+ throw new Error(`Expected to be given a record instance`);
224
+ }
225
+ })(identifier) : {};
226
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
227
+ if (!test) {
228
+ throw new Error(`Cannot delete a record that does not have an associated type and id.`);
229
+ }
230
+ })(isExisting(identifier)) : {};
231
+ const urlOptions = {
232
+ identifier: identifier,
233
+ op: 'deleteRecord',
234
+ resourcePath: pluralize(underscore(identifier.type))
235
+ };
236
+ copyForwardUrlOptions(urlOptions, options);
237
+ const url = buildBaseURL(urlOptions);
238
+ const headers = new Headers();
239
+ headers.append('Accept', 'application/json;charset=utf-8');
240
+ return {
241
+ url,
242
+ method: 'DELETE',
243
+ headers,
244
+ op: 'deleteRecord',
245
+ data: {
246
+ record: identifier
247
+ },
248
+ records: [identifier]
249
+ };
250
+ }
251
+
252
+ /**
253
+ * Builds request options to create new record for resources,
254
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
255
+ *
256
+ * **Basic Usage**
257
+ *
258
+ * ```ts
259
+ * import { createRecord } from '@warp-drive-mirror/utilities/active-record';
260
+ *
261
+ * const person = store.createRecord('person', { name: 'Ted' });
262
+ * const data = await store.request(createRecord(person));
263
+ * ```
264
+ *
265
+ * **Supplying Options to Modify the Request Behavior**
266
+ *
267
+ * The following options are supported:
268
+ *
269
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
270
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
271
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
272
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
273
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
274
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
275
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
276
+ * defaulting to `false` if none is configured.
277
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
278
+ *
279
+ * ```ts
280
+ * import { createRecord } from '@warp-drive-mirror/utilities/active-record';
281
+ *
282
+ * const person = store.createRecord('person', { name: 'Ted' });
283
+ * const options = createRecord(person, { namespace: 'api/v1' });
284
+ * const data = await store.request(options);
285
+ * ```
286
+ *
287
+ * @public
288
+ * @param record
289
+ * @param options
290
+ */
291
+
292
+ function createRecord(record, options = {}) {
293
+ const identifier = recordIdentifierFor(record);
294
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
295
+ if (!test) {
296
+ throw new Error(`Expected to be given a record instance`);
297
+ }
298
+ })(identifier) : {};
299
+ const urlOptions = {
300
+ identifier: identifier,
301
+ op: 'createRecord',
302
+ resourcePath: pluralize(underscore(identifier.type))
303
+ };
304
+ copyForwardUrlOptions(urlOptions, options);
305
+ const url = buildBaseURL(urlOptions);
306
+ const headers = new Headers();
307
+ headers.append('Accept', 'application/json;charset=utf-8');
308
+ return {
309
+ url,
310
+ method: 'POST',
311
+ headers,
312
+ op: 'createRecord',
313
+ data: {
314
+ record: identifier
315
+ },
316
+ records: [identifier]
317
+ };
318
+ }
319
+
320
+ /**
321
+ * Builds request options to update existing record for resources,
322
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
323
+ *
324
+ * **Basic Usage**
325
+ *
326
+ * ```ts
327
+ * import { updateRecord } from '@warp-drive-mirror/utilities/active-record';
328
+ *
329
+ * const person = store.peekRecord('person', '1');
330
+ * person.name = 'Chris';
331
+ * const data = await store.request(updateRecord(person));
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-mirror/utilities/active-record';
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
+ */
362
+
363
+ function updateRecord(record, options = {}) {
364
+ const identifier = recordIdentifierFor(record);
365
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
366
+ if (!test) {
367
+ throw new Error(`Expected to be given a record instance`);
368
+ }
369
+ })(identifier) : {};
370
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
371
+ if (!test) {
372
+ throw new Error(`Cannot update a record that does not have an associated type and id.`);
373
+ }
374
+ })(isExisting(identifier)) : {};
375
+ const urlOptions = {
376
+ identifier: identifier,
377
+ op: 'updateRecord',
378
+ resourcePath: pluralize(underscore(identifier.type))
379
+ };
380
+ copyForwardUrlOptions(urlOptions, options);
381
+ const url = buildBaseURL(urlOptions);
382
+ const headers = new Headers();
383
+ headers.append('Accept', 'application/json;charset=utf-8');
384
+ return {
385
+ url,
386
+ method: options.patch ? 'PATCH' : 'PUT',
387
+ headers,
388
+ op: 'updateRecord',
389
+ data: {
390
+ record: identifier
391
+ },
392
+ records: [identifier]
393
+ };
394
+ }
395
+ 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,64 @@
1
+ import type { ReactiveDataDocument } from "@warp-drive-mirror/core/reactive";
2
+ import type { TypeFromInstance } from "@warp-drive-mirror/core/types/record";
3
+ import type { FindRecordOptions, FindRecordRequestOptions, RemotelyAccessibleIdentifier } from "@warp-drive-mirror/core/types/request";
4
+ /**
5
+ * Builds request options to fetch a single resource by a known id or identifier
6
+ * configured for the url and header expectations of most ActiveRecord APIs.
7
+ *
8
+ * **Basic Usage**
9
+ *
10
+ * ```ts
11
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
12
+ *
13
+ * const data = await store.request(findRecord('person', '1'));
14
+ * ```
15
+ *
16
+ * **With Options**
17
+ *
18
+ * ```ts
19
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
20
+ *
21
+ * const options = findRecord('person', '1', { include: ['pets', 'friends'] });
22
+ * const data = await store.request(options);
23
+ * ```
24
+ *
25
+ * **With an Identifier**
26
+ *
27
+ * ```ts
28
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
29
+ *
30
+ * const options = findRecord({ type: 'person', id: '1' }, { include: ['pets', 'friends'] });
31
+ * const data = await store.request(options);
32
+ * ```
33
+ *
34
+ * **Supplying Options to Modify the Request Behavior**
35
+ *
36
+ * The following options are supported:
37
+ *
38
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
39
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
40
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
41
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
42
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
43
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
44
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
45
+ * defaulting to `false` if none is configured.
46
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
47
+ *
48
+ * ```ts
49
+ * import { findRecord } from '@warp-drive-mirror/utilities/active-record';
50
+ *
51
+ * const options = findRecord('person', '1', { include: ['pets', 'friends'] }, { namespace: 'api/v2' });
52
+ * const data = await store.request(options);
53
+ * ```
54
+ *
55
+ * @public
56
+ * @param identifier
57
+ * @param options
58
+ */
59
+ export declare function findRecord<T>(identifier: RemotelyAccessibleIdentifier<TypeFromInstance<T>>, options?: FindRecordOptions): FindRecordRequestOptions<ReactiveDataDocument<T>, T>;
60
+ export declare function findRecord(identifier: RemotelyAccessibleIdentifier, options?: FindRecordOptions): FindRecordRequestOptions;
61
+ export declare function findRecord<T>(type: TypeFromInstance<T>, id: string, options?: FindRecordOptions): FindRecordRequestOptions<ReactiveDataDocument<T>, T>;
62
+ export declare function findRecord(type: string, id: string, options?: FindRecordOptions): FindRecordRequestOptions;
63
+ /** @deprecated use {@link ReactiveDataDocument} instead */
64
+ export type FindRecordResultDocument<T> = ReactiveDataDocument<T>;
@@ -0,0 +1,53 @@
1
+ import type { ReactiveDataDocument } from "@warp-drive-mirror/core/reactive";
2
+ import type { QueryParamsSource } from "@warp-drive-mirror/core/types/params";
3
+ import type { TypeFromInstance } from "@warp-drive-mirror/core/types/record";
4
+ import type { ConstrainedRequestOptions, QueryRequestOptions } from "@warp-drive-mirror/core/types/request";
5
+ /**
6
+ * Builds request options to query for resources, usually by a primary
7
+ * type, configured for the url and header expectations of most ActiveRecord APIs.
8
+ *
9
+ * **Basic Usage**
10
+ *
11
+ * ```ts
12
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
13
+ *
14
+ * const data = await store.request(query('person'));
15
+ * ```
16
+ *
17
+ * **With Query Params**
18
+ *
19
+ * ```ts
20
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
21
+ *
22
+ * const options = query('person', { include: ['pets', 'friends'] });
23
+ * const data = await store.request(options);
24
+ * ```
25
+ *
26
+ * **Supplying Options to Modify the Request Behavior**
27
+ *
28
+ * The following options are supported:
29
+ *
30
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
31
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
32
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing and underscoring the supplied type
33
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
34
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
35
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
36
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
37
+ * defaulting to `false` if none is configured.
38
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
39
+ *
40
+ * ```ts
41
+ * import { query } from '@warp-drive-mirror/utilities/active-record';
42
+ *
43
+ * const options = query('person', { include: ['pets', 'friends'] }, { reload: true });
44
+ * const data = await store.request(options);
45
+ * ```
46
+ *
47
+ * @public
48
+ * @param identifier
49
+ * @param query
50
+ * @param options
51
+ */
52
+ export declare function query<T>(type: TypeFromInstance<T>, query?: QueryParamsSource, options?: ConstrainedRequestOptions): QueryRequestOptions<ReactiveDataDocument<T[]>>;
53
+ export declare function query(type: string, query?: QueryParamsSource, options?: ConstrainedRequestOptions): QueryRequestOptions;
@@ -0,0 +1,146 @@
1
+ import type { ReactiveDataDocument } from "@warp-drive-mirror/core/reactive";
2
+ import type { TypedRecordInstance } from "@warp-drive-mirror/core/types/record";
3
+ import type { ConstrainedRequestOptions, CreateRequestOptions, DeleteRequestOptions, UpdateRequestOptions } from "@warp-drive-mirror/core/types/request";
4
+ /**
5
+ * Builds request options to delete record for resources,
6
+ * configured for the url, method and header expectations of ActiveRecord APIs.
7
+ *
8
+ * **Basic Usage**
9
+ *
10
+ * ```ts
11
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/active-record';
12
+ *
13
+ * const person = store.peekRecord('person', '1');
14
+ *
15
+ * // mark record as deleted
16
+ * store.deleteRecord(person);
17
+ *
18
+ * // persist deletion
19
+ * const data = await store.request(deleteRecord(person));
20
+ * ```
21
+ *
22
+ * **Supplying Options to Modify the Request Behavior**
23
+ *
24
+ * The following options are supported:
25
+ *
26
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
27
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
28
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
29
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
30
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
31
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
32
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
33
+ * defaulting to `false` if none is configured.
34
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
35
+ *
36
+ * ```ts
37
+ * import { deleteRecord } from '@warp-drive-mirror/utilities/active-record';
38
+ *
39
+ * const person = store.peekRecord('person', '1');
40
+ *
41
+ * // mark record as deleted
42
+ * store.deleteRecord(person);
43
+ *
44
+ * // persist deletion
45
+ * const options = deleteRecord(person, { namespace: 'api/v1' });
46
+ * const data = await store.request(options);
47
+ * ```
48
+ *
49
+ * @public
50
+ * @param record
51
+ * @param options
52
+ */
53
+ export declare function deleteRecord<T>(record: T, options?: ConstrainedRequestOptions): DeleteRequestOptions<T>;
54
+ export declare function deleteRecord(record: unknown, options?: ConstrainedRequestOptions): DeleteRequestOptions;
55
+ /**
56
+ * Builds request options to create new record for resources,
57
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
58
+ *
59
+ * **Basic Usage**
60
+ *
61
+ * ```ts
62
+ * import { createRecord } from '@warp-drive-mirror/utilities/active-record';
63
+ *
64
+ * const person = store.createRecord('person', { name: 'Ted' });
65
+ * const data = await store.request(createRecord(person));
66
+ * ```
67
+ *
68
+ * **Supplying Options to Modify the Request Behavior**
69
+ *
70
+ * The following options are supported:
71
+ *
72
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
73
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
74
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
75
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
76
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
77
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
78
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
79
+ * defaulting to `false` if none is configured.
80
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
81
+ *
82
+ * ```ts
83
+ * import { createRecord } from '@warp-drive-mirror/utilities/active-record';
84
+ *
85
+ * const person = store.createRecord('person', { name: 'Ted' });
86
+ * const options = createRecord(person, { namespace: 'api/v1' });
87
+ * const data = await store.request(options);
88
+ * ```
89
+ *
90
+ * @public
91
+ * @param record
92
+ * @param options
93
+ */
94
+ export declare function createRecord<T>(record: T, options?: ConstrainedRequestOptions): CreateRequestOptions<T>;
95
+ export declare function createRecord(record: unknown, options?: ConstrainedRequestOptions): CreateRequestOptions;
96
+ /**
97
+ * Builds request options to update existing record for resources,
98
+ * configured for the url, method and header expectations of most ActiveRecord APIs.
99
+ *
100
+ * **Basic Usage**
101
+ *
102
+ * ```ts
103
+ * import { updateRecord } from '@warp-drive-mirror/utilities/active-record';
104
+ *
105
+ * const person = store.peekRecord('person', '1');
106
+ * person.name = 'Chris';
107
+ * const data = await store.request(updateRecord(person));
108
+ * ```
109
+ *
110
+ * **Supplying Options to Modify the Request Behavior**
111
+ *
112
+ * The following options are supported:
113
+ *
114
+ * - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
115
+ * - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
116
+ * - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
117
+ * - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
118
+ * - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
119
+ * option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
120
+ * - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
121
+ * promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
122
+ * defaulting to `false` if none is configured.
123
+ * - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
124
+ *
125
+ * ```ts
126
+ * import { updateRecord } from '@warp-drive-mirror/utilities/active-record';
127
+ *
128
+ * const person = store.peekRecord('person', '1');
129
+ * person.name = 'Chris';
130
+ * const options = updateRecord(person, { patch: true });
131
+ * const data = await store.request(options);
132
+ * ```
133
+ *
134
+ * @public
135
+ * @param record
136
+ * @param options
137
+ */
138
+ export declare function updateRecord<
139
+ T extends TypedRecordInstance,
140
+ RT extends TypedRecordInstance = T
141
+ >(record: T, options?: ConstrainedRequestOptions & {
142
+ patch?: boolean;
143
+ }): UpdateRequestOptions<ReactiveDataDocument<RT>, T>;
144
+ export declare function updateRecord(record: unknown, options?: ConstrainedRequestOptions & {
145
+ patch?: boolean;
146
+ }): UpdateRequestOptions;
@@ -0,0 +1,4 @@
1
+ import type { CacheOptions, ConstrainedRequestOptions } from "@warp-drive-mirror/core/types/request";
2
+ import type { UrlOptions } from "../index.js";
3
+ export declare function copyForwardUrlOptions(urlOptions: UrlOptions, options: ConstrainedRequestOptions): void;
4
+ export declare function extractCacheOptions(options: ConstrainedRequestOptions): CacheOptions;