@warp-drive/utilities 5.9.0-alpha.2 → 5.9.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs-dist/string.cjs +448 -0
- package/cjs-dist/string.cjs.map +1 -0
- package/declarations/-private.d.ts +12 -1
- package/declarations/-private.d.ts.map +1 -0
- package/declarations/active-record.d.ts +260 -3
- package/declarations/active-record.d.ts.map +1 -0
- package/declarations/derivations.d.ts +26 -23
- package/declarations/derivations.d.ts.map +1 -0
- package/declarations/handlers.d.ts +282 -9
- package/declarations/handlers.d.ts.map +1 -0
- package/declarations/index-BeMAuqgZ.d.ts +462 -0
- package/declarations/index-BeMAuqgZ.d.ts.map +1 -0
- package/declarations/index.d.ts +2 -208
- package/declarations/json-api.d.ts +496 -5
- package/declarations/json-api.d.ts.map +1 -0
- package/declarations/rest.d.ts +258 -3
- package/declarations/rest.d.ts.map +1 -0
- package/declarations/string.d.ts +164 -13
- package/declarations/string.d.ts.map +1 -0
- package/dist/-private.js +2 -6
- package/dist/active-record.js +254 -379
- package/dist/active-record.js.map +1 -0
- package/dist/builder-utils-BkXwUT0x.js +16 -0
- package/dist/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/derivations.js +26 -26
- package/dist/derivations.js.map +1 -0
- package/dist/handlers.js +240 -283
- package/dist/handlers.js.map +1 -0
- package/dist/index.js +357 -335
- package/dist/index.js.map +1 -0
- package/dist/inflections-DzBsjJqj.js +80 -0
- package/dist/inflections-DzBsjJqj.js.map +1 -0
- package/dist/json-api.js +470 -706
- package/dist/json-api.js.map +1 -0
- package/dist/rest.js +254 -377
- package/dist/rest.js.map +1 -0
- package/dist/string-CWmAluxh.js +290 -0
- package/dist/string-CWmAluxh.js.map +1 -0
- package/dist/string.js +2 -427
- package/dist/unpkg/dev/-private.js +2 -6
- package/dist/unpkg/dev/active-record.js +253 -378
- package/dist/unpkg/dev/active-record.js.map +1 -0
- package/dist/unpkg/dev/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/dev/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/dev/derivations.js +26 -26
- package/dist/unpkg/dev/derivations.js.map +1 -0
- package/dist/unpkg/dev/handlers.js +238 -282
- package/dist/unpkg/dev/handlers.js.map +1 -0
- package/dist/unpkg/dev/index.js +356 -336
- package/dist/unpkg/dev/index.js.map +1 -0
- package/dist/unpkg/dev/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/dev/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/dev/json-api.js +469 -706
- package/dist/unpkg/dev/json-api.js.map +1 -0
- package/dist/unpkg/dev/rest.js +253 -376
- package/dist/unpkg/dev/rest.js.map +1 -0
- package/dist/unpkg/dev/string-j4ur-TDs.js +289 -0
- package/dist/unpkg/dev/string-j4ur-TDs.js.map +1 -0
- package/dist/unpkg/dev/string.js +3 -1
- package/dist/unpkg/dev-deprecated/-private.js +2 -6
- package/dist/unpkg/dev-deprecated/active-record.js +253 -378
- package/dist/unpkg/dev-deprecated/active-record.js.map +1 -0
- package/dist/unpkg/dev-deprecated/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/dev-deprecated/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/dev-deprecated/derivations.js +26 -26
- package/dist/unpkg/dev-deprecated/derivations.js.map +1 -0
- package/dist/unpkg/dev-deprecated/handlers.js +238 -282
- package/dist/unpkg/dev-deprecated/handlers.js.map +1 -0
- package/dist/unpkg/dev-deprecated/index.js +356 -336
- package/dist/unpkg/dev-deprecated/index.js.map +1 -0
- package/dist/unpkg/dev-deprecated/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/dev-deprecated/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/dev-deprecated/json-api.js +469 -706
- package/dist/unpkg/dev-deprecated/json-api.js.map +1 -0
- package/dist/unpkg/dev-deprecated/rest.js +253 -376
- package/dist/unpkg/dev-deprecated/rest.js.map +1 -0
- package/dist/unpkg/dev-deprecated/string-j4ur-TDs.js +289 -0
- package/dist/unpkg/dev-deprecated/string-j4ur-TDs.js.map +1 -0
- package/dist/unpkg/dev-deprecated/string.js +3 -1
- package/dist/unpkg/prod/-private.js +2 -6
- package/dist/unpkg/prod/active-record.js +236 -351
- package/dist/unpkg/prod/active-record.js.map +1 -0
- package/dist/unpkg/prod/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/prod/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/prod/derivations.js +26 -26
- package/dist/unpkg/prod/derivations.js.map +1 -0
- package/dist/unpkg/prod/handlers.js +231 -272
- package/dist/unpkg/prod/handlers.js.map +1 -0
- package/dist/unpkg/prod/index.js +265 -215
- package/dist/unpkg/prod/index.js.map +1 -0
- package/dist/unpkg/prod/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/prod/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/prod/json-api.js +446 -669
- package/dist/unpkg/prod/json-api.js.map +1 -0
- package/dist/unpkg/prod/rest.js +236 -349
- package/dist/unpkg/prod/rest.js.map +1 -0
- package/dist/unpkg/prod/string-T1ahjLdj.js +283 -0
- package/dist/unpkg/prod/string-T1ahjLdj.js.map +1 -0
- package/dist/unpkg/prod/string.js +3 -1
- package/dist/unpkg/prod-deprecated/-private.js +2 -6
- package/dist/unpkg/prod-deprecated/active-record.js +236 -351
- package/dist/unpkg/prod-deprecated/active-record.js.map +1 -0
- package/dist/unpkg/prod-deprecated/builder-utils-BkXwUT0x.js +16 -0
- package/dist/unpkg/prod-deprecated/builder-utils-BkXwUT0x.js.map +1 -0
- package/dist/unpkg/prod-deprecated/derivations.js +26 -26
- package/dist/unpkg/prod-deprecated/derivations.js.map +1 -0
- package/dist/unpkg/prod-deprecated/handlers.js +231 -272
- package/dist/unpkg/prod-deprecated/handlers.js.map +1 -0
- package/dist/unpkg/prod-deprecated/index.js +265 -215
- package/dist/unpkg/prod-deprecated/index.js.map +1 -0
- package/dist/unpkg/prod-deprecated/inflections-DzBsjJqj.js +80 -0
- package/dist/unpkg/prod-deprecated/inflections-DzBsjJqj.js.map +1 -0
- package/dist/unpkg/prod-deprecated/json-api.js +446 -669
- package/dist/unpkg/prod-deprecated/json-api.js.map +1 -0
- package/dist/unpkg/prod-deprecated/rest.js +236 -349
- package/dist/unpkg/prod-deprecated/rest.js.map +1 -0
- package/dist/unpkg/prod-deprecated/string-T1ahjLdj.js +283 -0
- package/dist/unpkg/prod-deprecated/string-T1ahjLdj.js.map +1 -0
- package/dist/unpkg/prod-deprecated/string.js +3 -1
- package/package.json +11 -10
- package/declarations/-private/active-record/find-record.d.ts +0 -64
- package/declarations/-private/active-record/query.d.ts +0 -53
- package/declarations/-private/active-record/save-record.d.ts +0 -146
- package/declarations/-private/builder-utils.d.ts +0 -4
- package/declarations/-private/handlers/auto-compress.d.ts +0 -158
- package/declarations/-private/handlers/gated.d.ts +0 -19
- package/declarations/-private/handlers/meta-doc.d.ts +0 -47
- package/declarations/-private/handlers/utils.d.ts +0 -41
- package/declarations/-private/json-api/-utils.d.ts +0 -109
- package/declarations/-private/json-api/find-record.d.ts +0 -84
- package/declarations/-private/json-api/query.d.ts +0 -100
- package/declarations/-private/json-api/save-record.d.ts +0 -228
- package/declarations/-private/json-api/serialize.d.ts +0 -70
- package/declarations/-private/rest/find-record.d.ts +0 -62
- package/declarations/-private/rest/query.d.ts +0 -53
- package/declarations/-private/rest/save-record.d.ts +0 -146
- package/declarations/-private/string/inflect.d.ts +0 -82
- package/declarations/-private/string/inflections.d.ts +0 -9
- package/declarations/-private/string/transform.d.ts +0 -77
- package/dist/builder-utils-Donkk-BZ.js +0 -22
- package/dist/inflect-BSG696t-.js +0 -344
- package/dist/string.cjs +0 -430
- package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/dev/inflect-BEv8WqY1.js +0 -343
- package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/dev-deprecated/inflect-BEv8WqY1.js +0 -343
- package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/prod/inflect-Dh9dyEYx.js +0 -333
- package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +0 -22
- package/dist/unpkg/prod-deprecated/inflect-Dh9dyEYx.js +0 -333
package/dist/active-record.js
CHANGED
|
@@ -1,395 +1,270 @@
|
|
|
1
|
-
import { buildBaseURL, buildQueryParams } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
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/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/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/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/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
|
-
*/
|
|
1
|
+
import { buildBaseURL, buildQueryParams } from "./index.js";
|
|
2
|
+
import { g as underscore, s as pluralize } from "./string-CWmAluxh.js";
|
|
3
|
+
import { n as extractCacheOptions, t as copyForwardUrlOptions } from "./builder-utils-BkXwUT0x.js";
|
|
4
|
+
import { getGlobalConfig, macroCondition } from "@embroider/macros";
|
|
5
|
+
import { recordIdentifierFor } from "@warp-drive/core";
|
|
62
6
|
|
|
7
|
+
//#region src/-private/active-record/find-record.ts
|
|
63
8
|
function findRecord(arg1, arg2, arg3) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
records: [identifier]
|
|
88
|
-
};
|
|
9
|
+
const identifier = typeof arg1 === "string" ? {
|
|
10
|
+
type: arg1,
|
|
11
|
+
id: arg2
|
|
12
|
+
} : arg1;
|
|
13
|
+
const options = (typeof arg1 === "string" ? arg3 : arg2) || {};
|
|
14
|
+
const cacheOptions = extractCacheOptions(options);
|
|
15
|
+
const urlOptions = {
|
|
16
|
+
identifier,
|
|
17
|
+
op: "findRecord",
|
|
18
|
+
resourcePath: pluralize(underscore(identifier.type))
|
|
19
|
+
};
|
|
20
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
21
|
+
const url = buildBaseURL(urlOptions);
|
|
22
|
+
const headers = new Headers();
|
|
23
|
+
headers.append("Accept", "application/json;charset=utf-8");
|
|
24
|
+
return {
|
|
25
|
+
url: options.include?.length ? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}` : url,
|
|
26
|
+
method: "GET",
|
|
27
|
+
headers,
|
|
28
|
+
cacheOptions,
|
|
29
|
+
op: "findRecord",
|
|
30
|
+
records: [identifier]
|
|
31
|
+
};
|
|
89
32
|
}
|
|
90
33
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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/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
|
-
};
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/-private/active-record/query.ts
|
|
36
|
+
function query(type, query = {}, options = {}) {
|
|
37
|
+
const cacheOptions = extractCacheOptions(options);
|
|
38
|
+
const urlOptions = {
|
|
39
|
+
identifier: { type },
|
|
40
|
+
op: "query",
|
|
41
|
+
resourcePath: pluralize(underscore(type))
|
|
42
|
+
};
|
|
43
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
44
|
+
const url = buildBaseURL(urlOptions);
|
|
45
|
+
const headers = new Headers();
|
|
46
|
+
headers.append("Accept", "application/json;charset=utf-8");
|
|
47
|
+
const queryString = buildQueryParams(query, options.urlParamsSettings);
|
|
48
|
+
return {
|
|
49
|
+
url: queryString ? `${url}?${queryString}` : url,
|
|
50
|
+
method: "GET",
|
|
51
|
+
headers,
|
|
52
|
+
cacheOptions,
|
|
53
|
+
op: "query"
|
|
54
|
+
};
|
|
164
55
|
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/-private/active-record/save-record.ts
|
|
165
59
|
function isExisting(identifier) {
|
|
166
|
-
|
|
60
|
+
return "id" in identifier && identifier.id !== null && "type" in identifier && identifier.type !== null;
|
|
167
61
|
}
|
|
168
|
-
|
|
169
62
|
/**
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
63
|
+
* Builds request options to delete record for resources,
|
|
64
|
+
* configured for the url, method and header expectations of ActiveRecord APIs.
|
|
65
|
+
*
|
|
66
|
+
* **Basic Usage**
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { deleteRecord } from '@warp-drive/utilities/active-record';
|
|
70
|
+
*
|
|
71
|
+
* const person = store.peekRecord('person', '1');
|
|
72
|
+
*
|
|
73
|
+
* // mark record as deleted
|
|
74
|
+
* store.deleteRecord(person);
|
|
75
|
+
*
|
|
76
|
+
* // persist deletion
|
|
77
|
+
* const data = await store.request(deleteRecord(person));
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* **Supplying Options to Modify the Request Behavior**
|
|
81
|
+
*
|
|
82
|
+
* The following options are supported:
|
|
83
|
+
*
|
|
84
|
+
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
85
|
+
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
86
|
+
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
87
|
+
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
88
|
+
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
89
|
+
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
90
|
+
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
91
|
+
* defaulting to `false` if none is configured.
|
|
92
|
+
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { deleteRecord } from '@warp-drive/utilities/active-record';
|
|
96
|
+
*
|
|
97
|
+
* const person = store.peekRecord('person', '1');
|
|
98
|
+
*
|
|
99
|
+
* // mark record as deleted
|
|
100
|
+
* store.deleteRecord(person);
|
|
101
|
+
*
|
|
102
|
+
* // persist deletion
|
|
103
|
+
* const options = deleteRecord(person, { namespace: 'api/v1' });
|
|
104
|
+
* const data = await store.request(options);
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @public
|
|
108
|
+
* @param record
|
|
109
|
+
* @param options
|
|
110
|
+
*/
|
|
219
111
|
function deleteRecord(record, options = {}) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
op: 'deleteRecord',
|
|
245
|
-
data: {
|
|
246
|
-
record: identifier
|
|
247
|
-
},
|
|
248
|
-
records: [identifier]
|
|
249
|
-
};
|
|
112
|
+
const identifier = recordIdentifierFor(record);
|
|
113
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
114
|
+
if (!test) throw new Error(`Expected to be given a record instance`);
|
|
115
|
+
})(identifier);
|
|
116
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
117
|
+
if (!test) throw new Error(`Cannot delete a record that does not have an associated type and id.`);
|
|
118
|
+
})(isExisting(identifier));
|
|
119
|
+
const urlOptions = {
|
|
120
|
+
identifier,
|
|
121
|
+
op: "deleteRecord",
|
|
122
|
+
resourcePath: pluralize(underscore(identifier.type))
|
|
123
|
+
};
|
|
124
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
125
|
+
const url = buildBaseURL(urlOptions);
|
|
126
|
+
const headers = new Headers();
|
|
127
|
+
headers.append("Accept", "application/json;charset=utf-8");
|
|
128
|
+
return {
|
|
129
|
+
url,
|
|
130
|
+
method: "DELETE",
|
|
131
|
+
headers,
|
|
132
|
+
op: "deleteRecord",
|
|
133
|
+
data: { record: identifier },
|
|
134
|
+
records: [identifier]
|
|
135
|
+
};
|
|
250
136
|
}
|
|
251
|
-
|
|
252
137
|
/**
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
138
|
+
* Builds request options to create new record for resources,
|
|
139
|
+
* configured for the url, method and header expectations of most ActiveRecord APIs.
|
|
140
|
+
*
|
|
141
|
+
* **Basic Usage**
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* import { createRecord } from '@warp-drive/utilities/active-record';
|
|
145
|
+
*
|
|
146
|
+
* const person = store.createRecord('person', { name: 'Ted' });
|
|
147
|
+
* const data = await store.request(createRecord(person));
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* **Supplying Options to Modify the Request Behavior**
|
|
151
|
+
*
|
|
152
|
+
* The following options are supported:
|
|
153
|
+
*
|
|
154
|
+
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
155
|
+
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
156
|
+
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
157
|
+
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
158
|
+
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
159
|
+
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
160
|
+
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
161
|
+
* defaulting to `false` if none is configured.
|
|
162
|
+
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* import { createRecord } from '@warp-drive/utilities/active-record';
|
|
166
|
+
*
|
|
167
|
+
* const person = store.createRecord('person', { name: 'Ted' });
|
|
168
|
+
* const options = createRecord(person, { namespace: 'api/v1' });
|
|
169
|
+
* const data = await store.request(options);
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* @public
|
|
173
|
+
* @param record
|
|
174
|
+
* @param options
|
|
175
|
+
*/
|
|
292
176
|
function createRecord(record, options = {}) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
record: identifier
|
|
315
|
-
},
|
|
316
|
-
records: [identifier]
|
|
317
|
-
};
|
|
177
|
+
const identifier = recordIdentifierFor(record);
|
|
178
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
179
|
+
if (!test) throw new Error(`Expected to be given a record instance`);
|
|
180
|
+
})(identifier);
|
|
181
|
+
const urlOptions = {
|
|
182
|
+
identifier,
|
|
183
|
+
op: "createRecord",
|
|
184
|
+
resourcePath: pluralize(underscore(identifier.type))
|
|
185
|
+
};
|
|
186
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
187
|
+
const url = buildBaseURL(urlOptions);
|
|
188
|
+
const headers = new Headers();
|
|
189
|
+
headers.append("Accept", "application/json;charset=utf-8");
|
|
190
|
+
return {
|
|
191
|
+
url,
|
|
192
|
+
method: "POST",
|
|
193
|
+
headers,
|
|
194
|
+
op: "createRecord",
|
|
195
|
+
data: { record: identifier },
|
|
196
|
+
records: [identifier]
|
|
197
|
+
};
|
|
318
198
|
}
|
|
319
|
-
|
|
320
199
|
/**
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
200
|
+
* Builds request options to update existing record for resources,
|
|
201
|
+
* configured for the url, method and header expectations of most ActiveRecord APIs.
|
|
202
|
+
*
|
|
203
|
+
* **Basic Usage**
|
|
204
|
+
*
|
|
205
|
+
* ```ts
|
|
206
|
+
* import { updateRecord } from '@warp-drive/utilities/active-record';
|
|
207
|
+
*
|
|
208
|
+
* const person = store.peekRecord('person', '1');
|
|
209
|
+
* person.name = 'Chris';
|
|
210
|
+
* const data = await store.request(updateRecord(person));
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* **Supplying Options to Modify the Request Behavior**
|
|
214
|
+
*
|
|
215
|
+
* The following options are supported:
|
|
216
|
+
*
|
|
217
|
+
* - `patch` - Allows caller to specify whether to use a PATCH request instead of a PUT request, defaults to `false`.
|
|
218
|
+
* - `host` - The host to use for the request, defaults to the `host` configured with `setBuildURLConfig`.
|
|
219
|
+
* - `namespace` - The namespace to use for the request, defaults to the `namespace` configured with `setBuildURLConfig`.
|
|
220
|
+
* - `resourcePath` - The resource path to use for the request, defaults to pluralizing the supplied type
|
|
221
|
+
* - `reload` - Whether to forcibly reload the request if it is already in the store, not supplying this
|
|
222
|
+
* option will delegate to the store's CachePolicy, defaulting to `false` if none is configured.
|
|
223
|
+
* - `backgroundReload` - Whether to reload the request if it is already in the store, but to also resolve the
|
|
224
|
+
* promise with the cached value, not supplying this option will delegate to the store's CachePolicy,
|
|
225
|
+
* defaulting to `false` if none is configured.
|
|
226
|
+
* - `urlParamsSetting` - an object containing options for how to serialize the query params (see `buildQueryParams`)
|
|
227
|
+
*
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { updateRecord } from '@warp-drive/utilities/active-record';
|
|
230
|
+
*
|
|
231
|
+
* const person = store.peekRecord('person', '1');
|
|
232
|
+
* person.name = 'Chris';
|
|
233
|
+
* const options = updateRecord(person, { patch: true });
|
|
234
|
+
* const data = await store.request(options);
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* @public
|
|
238
|
+
* @param record
|
|
239
|
+
* @param options
|
|
240
|
+
*/
|
|
363
241
|
function updateRecord(record, options = {}) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
op: 'updateRecord',
|
|
389
|
-
data: {
|
|
390
|
-
record: identifier
|
|
391
|
-
},
|
|
392
|
-
records: [identifier]
|
|
393
|
-
};
|
|
242
|
+
const identifier = recordIdentifierFor(record);
|
|
243
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
244
|
+
if (!test) throw new Error(`Expected to be given a record instance`);
|
|
245
|
+
})(identifier);
|
|
246
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
247
|
+
if (!test) throw new Error(`Cannot update a record that does not have an associated type and id.`);
|
|
248
|
+
})(isExisting(identifier));
|
|
249
|
+
const urlOptions = {
|
|
250
|
+
identifier,
|
|
251
|
+
op: "updateRecord",
|
|
252
|
+
resourcePath: pluralize(underscore(identifier.type))
|
|
253
|
+
};
|
|
254
|
+
copyForwardUrlOptions(urlOptions, options);
|
|
255
|
+
const url = buildBaseURL(urlOptions);
|
|
256
|
+
const headers = new Headers();
|
|
257
|
+
headers.append("Accept", "application/json;charset=utf-8");
|
|
258
|
+
return {
|
|
259
|
+
url,
|
|
260
|
+
method: options.patch ? "PATCH" : "PUT",
|
|
261
|
+
headers,
|
|
262
|
+
op: "updateRecord",
|
|
263
|
+
data: { record: identifier },
|
|
264
|
+
records: [identifier]
|
|
265
|
+
};
|
|
394
266
|
}
|
|
395
|
-
|
|
267
|
+
|
|
268
|
+
//#endregion
|
|
269
|
+
export { createRecord, deleteRecord, findRecord, query, updateRecord };
|
|
270
|
+
//# sourceMappingURL=active-record.js.map
|