@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/index.js
CHANGED
|
@@ -1,361 +1,383 @@
|
|
|
1
|
-
import { getOrSetGlobal } from
|
|
2
|
-
import {
|
|
1
|
+
import { getOrSetGlobal } from "@warp-drive/core/types/-private";
|
|
2
|
+
import { getGlobalConfig, macroCondition } from "@embroider/macros";
|
|
3
3
|
|
|
4
|
+
//#region src/index.ts
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
* @module
|
|
7
|
+
* @mergeModuleWith <project>
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The global configuration used by {@link buildBaseURL} when a call does
|
|
11
|
+
* not provide its own `host`/`namespace`. Set via {@link setBuildURLConfig}.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const CONFIG = getOrSetGlobal("CONFIG", {
|
|
16
|
+
host: "",
|
|
17
|
+
namespace: ""
|
|
14
18
|
});
|
|
15
|
-
|
|
16
19
|
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
20
|
+
* Sets the global configuration for `buildBaseURL`
|
|
21
|
+
* for host and namespace values for the application.
|
|
22
|
+
*
|
|
23
|
+
* These values may still be overridden by passing
|
|
24
|
+
* them to buildBaseURL directly.
|
|
25
|
+
*
|
|
26
|
+
* This method may be called as many times as needed.
|
|
27
|
+
* host values of `''` or `'/'` are equivalent.
|
|
28
|
+
*
|
|
29
|
+
* Except for the value of `/` as host, host should not
|
|
30
|
+
* end with `/`.
|
|
31
|
+
*
|
|
32
|
+
* namespace should not start or end with a `/`.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* type BuildURLConfig = {
|
|
36
|
+
* host: string;
|
|
37
|
+
* namespace: string'
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* Example:
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { setBuildURLConfig } from '@warp-drive/utilities';
|
|
45
|
+
*
|
|
46
|
+
* setBuildURLConfig({
|
|
47
|
+
* host: 'https://api.example.com',
|
|
48
|
+
* namespace: 'api/v1'
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
51
54
|
function setBuildURLConfig(config) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
70
|
-
if (!test) {
|
|
71
|
-
throw new Error(`buildBaseURL: namespace must NOT start with '/', received '${CONFIG.namespace}'`);
|
|
72
|
-
}
|
|
73
|
-
})(!CONFIG.namespace.startsWith('/')) : {};
|
|
74
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
75
|
-
if (!test) {
|
|
76
|
-
throw new Error(`buildBaseURL: namespace must NOT end with '/', received '${CONFIG.namespace}'`);
|
|
77
|
-
}
|
|
78
|
-
})(!CONFIG.namespace.endsWith('/')) : {};
|
|
55
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
56
|
+
if (!test) throw new Error(`setBuildURLConfig: You must pass a config object`);
|
|
57
|
+
})(config);
|
|
58
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
59
|
+
if (!test) throw new Error(`setBuildURLConfig: You must pass a config object with a 'host' or 'namespace' property`);
|
|
60
|
+
})("host" in config || "namespace" in config);
|
|
61
|
+
CONFIG.host = config.host || "";
|
|
62
|
+
CONFIG.namespace = config.namespace || "";
|
|
63
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
64
|
+
if (!test) throw new Error(`buildBaseURL: host must NOT end with '/', received '${CONFIG.host}'`);
|
|
65
|
+
})(CONFIG.host === "/" || !CONFIG.host.endsWith("/"));
|
|
66
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
67
|
+
if (!test) throw new Error(`buildBaseURL: namespace must NOT start with '/', received '${CONFIG.namespace}'`);
|
|
68
|
+
})(!CONFIG.namespace.startsWith("/"));
|
|
69
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
70
|
+
if (!test) throw new Error(`buildBaseURL: namespace must NOT end with '/', received '${CONFIG.namespace}'`);
|
|
71
|
+
})(!CONFIG.namespace.endsWith("/"));
|
|
79
72
|
}
|
|
80
|
-
|
|
73
|
+
/**
|
|
74
|
+
* {@link buildBaseURL} options for a `findRecord` request.
|
|
75
|
+
*
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* {@link buildBaseURL} options for a `query` request.
|
|
80
|
+
*
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
/**
|
|
84
|
+
* {@link buildBaseURL} options for a `findMany` request.
|
|
85
|
+
*
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
/**
|
|
89
|
+
* {@link buildBaseURL} options for a `findRelatedCollection` request.
|
|
90
|
+
*
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* {@link buildBaseURL} options for a `findRelatedRecord` request.
|
|
95
|
+
*
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
/**
|
|
99
|
+
* {@link buildBaseURL} options for a `createRecord` request.
|
|
100
|
+
*
|
|
101
|
+
* @public
|
|
102
|
+
*/
|
|
103
|
+
/**
|
|
104
|
+
* {@link buildBaseURL} options for an `updateRecord` request.
|
|
105
|
+
*
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
108
|
+
/**
|
|
109
|
+
* {@link buildBaseURL} options for a `deleteRecord` request.
|
|
110
|
+
*
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* {@link buildBaseURL} options for building a URL directly from a `resourcePath`
|
|
115
|
+
* without an associated request operation.
|
|
116
|
+
*
|
|
117
|
+
* @public
|
|
118
|
+
*/
|
|
119
|
+
/**
|
|
120
|
+
* The union of all `op`-specific option shapes accepted by {@link buildBaseURL}, one of:
|
|
121
|
+
*
|
|
122
|
+
* - {@link FindRecordUrlOptions}
|
|
123
|
+
* - {@link QueryUrlOptions}
|
|
124
|
+
* - {@link FindManyUrlOptions}
|
|
125
|
+
* - {@link FindRelatedCollectionUrlOptions}
|
|
126
|
+
* - {@link FindRelatedResourceUrlOptions}
|
|
127
|
+
* - {@link CreateRecordUrlOptions}
|
|
128
|
+
* - {@link UpdateRecordUrlOptions}
|
|
129
|
+
* - {@link DeleteRecordUrlOptions}
|
|
130
|
+
* - {@link GenericUrlOptions}
|
|
131
|
+
*
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
134
|
+
const OPERATIONS_WITH_PRIMARY_RECORDS = /* @__PURE__ */ new Set([
|
|
135
|
+
"findRecord",
|
|
136
|
+
"findRelatedRecord",
|
|
137
|
+
"findRelatedCollection",
|
|
138
|
+
"updateRecord",
|
|
139
|
+
"deleteRecord"
|
|
140
|
+
]);
|
|
81
141
|
function isOperationWithPrimaryRecord(options) {
|
|
82
|
-
|
|
142
|
+
return "op" in options && OPERATIONS_WITH_PRIMARY_RECORDS.has(options.op);
|
|
83
143
|
}
|
|
84
144
|
function hasResourcePath(options) {
|
|
85
|
-
|
|
145
|
+
return "resourcePath" in options && typeof options.resourcePath === "string" && options.resourcePath.length > 0;
|
|
86
146
|
}
|
|
87
147
|
function resourcePathForType(options) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
})('op' in options && typeof options.op === 'string') : {};
|
|
93
|
-
return options.op === 'findMany' ? options.identifiers[0].type : options.identifier.type;
|
|
148
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
149
|
+
if (!test) throw new Error(`resourcePathForType: You must pass a valid op as part of options`);
|
|
150
|
+
})("op" in options && typeof options.op === "string");
|
|
151
|
+
return options.op === "findMany" ? options.identifiers[0].type : options.identifier.type;
|
|
94
152
|
}
|
|
95
|
-
|
|
96
153
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
154
|
+
* Builds a URL for a request based on the provided options.
|
|
155
|
+
* Does not include support for building query params (see `buildQueryParams`)
|
|
156
|
+
* so that it may be composed cleanly with other query-params strategies.
|
|
157
|
+
*
|
|
158
|
+
* Usage:
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* import { buildBaseURL } from '@warp-drive/utilities';
|
|
162
|
+
*
|
|
163
|
+
* const url = buildBaseURL({
|
|
164
|
+
* host: 'https://api.example.com',
|
|
165
|
+
* namespace: 'api/v1',
|
|
166
|
+
* resourcePath: 'emberDevelopers',
|
|
167
|
+
* op: 'query',
|
|
168
|
+
* identifier: { type: 'ember-developer' }
|
|
169
|
+
* });
|
|
170
|
+
*
|
|
171
|
+
* // => 'https://api.example.com/api/v1/emberDevelopers'
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* On the surface this may seem like a lot of work to do something simple, but
|
|
175
|
+
* it is designed to be composable with other utilities and interfaces that the
|
|
176
|
+
* average product engineer will never need to see or use.
|
|
177
|
+
*
|
|
178
|
+
* A few notes:
|
|
179
|
+
*
|
|
180
|
+
* - `resourcePath` is optional, but if it is not provided, `identifier.type` will be used.
|
|
181
|
+
* - `host` and `namespace` are optional, but if they are not provided, the values globally
|
|
182
|
+
* configured via `setBuildURLConfig` will be used.
|
|
183
|
+
* - `op` is required and must be one of the following:
|
|
184
|
+
* - 'findRecord' 'query' 'findMany' 'findRelatedCollection' 'findRelatedRecord'` 'createRecord' 'updateRecord' 'deleteRecord'
|
|
185
|
+
* - Depending on the value of `op`, `identifier` or `identifiers` will be required.
|
|
186
|
+
*
|
|
187
|
+
* @public
|
|
188
|
+
*/
|
|
132
189
|
function buildBaseURL(urlOptions) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
219
|
-
}
|
|
220
|
-
})(!fieldPath.endsWith('/')) : {};
|
|
221
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
222
|
-
if (!test) {
|
|
223
|
-
throw new Error(`buildBaseURL: idPath must NOT start with '/', received '${idPath}'`);
|
|
224
|
-
}
|
|
225
|
-
})(!idPath.startsWith('/')) : {};
|
|
226
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
227
|
-
if (!test) {
|
|
228
|
-
throw new Error(`buildBaseURL: idPath must NOT end with '/', received '${idPath}'`);
|
|
229
|
-
}
|
|
230
|
-
})(!idPath.endsWith('/')) : {};
|
|
231
|
-
const hasHost = host !== '' && host !== '/';
|
|
232
|
-
const url = [hasHost ? host : '', namespace, resourcePath, idPath, fieldPath].filter(Boolean).join('/');
|
|
233
|
-
return hasHost ? url : `/${url}`;
|
|
190
|
+
const options = Object.assign({
|
|
191
|
+
host: CONFIG.host,
|
|
192
|
+
namespace: CONFIG.namespace
|
|
193
|
+
}, urlOptions);
|
|
194
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
195
|
+
if (!test) throw new Error(`buildBaseURL: You must pass \`op\` as part of options`);
|
|
196
|
+
})(hasResourcePath(options) || typeof options.op === "string" && options.op.length > 0);
|
|
197
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
198
|
+
if (!test) throw new Error(`buildBaseURL: You must pass \`identifier\` as part of options`);
|
|
199
|
+
})(hasResourcePath(options) || options.op === "findMany" || options.identifier && typeof options.identifier === "object");
|
|
200
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
201
|
+
if (!test) throw new Error(`buildBaseURL: You must pass \`identifiers\` as part of options`);
|
|
202
|
+
})(hasResourcePath(options) || options.op !== "findMany" || options.identifiers && Array.isArray(options.identifiers) && options.identifiers.length > 0 && options.identifiers.every((i) => i && typeof i === "object"));
|
|
203
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
204
|
+
if (!test) throw new Error(`buildBaseURL: You must pass valid \`identifier\` as part of options, expected 'id'`);
|
|
205
|
+
})(hasResourcePath(options) || !isOperationWithPrimaryRecord(options) || typeof options.identifier.id === "string" && options.identifier.id.length > 0);
|
|
206
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
207
|
+
if (!test) throw new Error(`buildBaseURL: You must pass \`identifiers\` as part of options`);
|
|
208
|
+
})(hasResourcePath(options) || options.op !== "findMany" || options.identifiers.every((i) => typeof i.id === "string" && i.id.length > 0));
|
|
209
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
210
|
+
if (!test) throw new Error(`buildBaseURL: You must pass valid \`identifier\` as part of options, expected 'type'`);
|
|
211
|
+
})(hasResourcePath(options) || options.op === "findMany" || typeof options.identifier.type === "string" && options.identifier.type.length > 0);
|
|
212
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
213
|
+
if (!test) throw new Error(`buildBaseURL: You must pass valid \`identifiers\` as part of options, expected 'type'`);
|
|
214
|
+
})(hasResourcePath(options) || options.op !== "findMany" || typeof options.identifiers[0].type === "string" && options.identifiers[0].type.length > 0);
|
|
215
|
+
const idPath = isOperationWithPrimaryRecord(options) ? encodeURIComponent(options.identifier.id) : "";
|
|
216
|
+
const resourcePath = options.resourcePath || resourcePathForType(options);
|
|
217
|
+
const { host, namespace } = options;
|
|
218
|
+
const fieldPath = "fieldPath" in options ? options.fieldPath : "";
|
|
219
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
220
|
+
if (!test) throw new Error(`buildBaseURL: You tried to build a url for a ${String("op" in options ? options.op + " " : "")}request to ${resourcePath} but resourcePath must be set or op must be one of "${[
|
|
221
|
+
"findRecord",
|
|
222
|
+
"findRelatedRecord",
|
|
223
|
+
"findRelatedCollection",
|
|
224
|
+
"updateRecord",
|
|
225
|
+
"deleteRecord",
|
|
226
|
+
"createRecord",
|
|
227
|
+
"query",
|
|
228
|
+
"findMany"
|
|
229
|
+
].join("\",\"")}".`);
|
|
230
|
+
})(hasResourcePath(options) || [
|
|
231
|
+
"findRecord",
|
|
232
|
+
"query",
|
|
233
|
+
"findMany",
|
|
234
|
+
"findRelatedCollection",
|
|
235
|
+
"findRelatedRecord",
|
|
236
|
+
"createRecord",
|
|
237
|
+
"updateRecord",
|
|
238
|
+
"deleteRecord"
|
|
239
|
+
].includes(options.op));
|
|
240
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
241
|
+
if (!test) throw new Error(`buildBaseURL: host must NOT end with '/', received '${host}'`);
|
|
242
|
+
})(host === "/" || !host.endsWith("/"));
|
|
243
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
244
|
+
if (!test) throw new Error(`buildBaseURL: namespace must NOT start with '/', received '${namespace}'`);
|
|
245
|
+
})(!namespace.startsWith("/"));
|
|
246
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
247
|
+
if (!test) throw new Error(`buildBaseURL: namespace must NOT end with '/', received '${namespace}'`);
|
|
248
|
+
})(!namespace.endsWith("/"));
|
|
249
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
250
|
+
if (!test) throw new Error(`buildBaseURL: resourcePath must NOT start with '/', received '${resourcePath}'`);
|
|
251
|
+
})(!resourcePath.startsWith("/"));
|
|
252
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
253
|
+
if (!test) throw new Error(`buildBaseURL: resourcePath must NOT end with '/', received '${resourcePath}'`);
|
|
254
|
+
})(!resourcePath.endsWith("/"));
|
|
255
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
256
|
+
if (!test) throw new Error(`buildBaseURL: fieldPath must NOT start with '/', received '${fieldPath}'`);
|
|
257
|
+
})(!fieldPath.startsWith("/"));
|
|
258
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
259
|
+
if (!test) throw new Error(`buildBaseURL: fieldPath must NOT end with '/', received '${fieldPath}'`);
|
|
260
|
+
})(!fieldPath.endsWith("/"));
|
|
261
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
262
|
+
if (!test) throw new Error(`buildBaseURL: idPath must NOT start with '/', received '${idPath}'`);
|
|
263
|
+
})(!idPath.startsWith("/"));
|
|
264
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
265
|
+
if (!test) throw new Error(`buildBaseURL: idPath must NOT end with '/', received '${idPath}'`);
|
|
266
|
+
})(!idPath.endsWith("/"));
|
|
267
|
+
const hasHost = host !== "" && host !== "/";
|
|
268
|
+
const url = [
|
|
269
|
+
hasHost ? host : "",
|
|
270
|
+
namespace,
|
|
271
|
+
resourcePath,
|
|
272
|
+
idPath,
|
|
273
|
+
fieldPath
|
|
274
|
+
].filter(Boolean).join("/");
|
|
275
|
+
return hasHost ? url : `/${url}`;
|
|
234
276
|
}
|
|
235
|
-
const DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS = {
|
|
236
|
-
arrayFormat: 'comma'
|
|
237
|
-
};
|
|
277
|
+
const DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS = { arrayFormat: "comma" };
|
|
238
278
|
function handleInclude(include) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
})(typeof include === 'string' || Array.isArray(include)) : {};
|
|
244
|
-
return typeof include === 'string' ? include.split(',') : include;
|
|
279
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) && ((test) => {
|
|
280
|
+
if (!test) throw new Error(`Expected include to be a string or array, got ${typeof include}`);
|
|
281
|
+
})(typeof include === "string" || Array.isArray(include));
|
|
282
|
+
return typeof include === "string" ? include.split(",") : include;
|
|
245
283
|
}
|
|
246
|
-
|
|
247
284
|
/**
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
285
|
+
* filter out keys of an object that have falsy values or point to empty arrays
|
|
286
|
+
* returning a new object with only those keys that have truthy values / non-empty arrays
|
|
287
|
+
*
|
|
288
|
+
* @public
|
|
289
|
+
* @param source object to filter keys with empty values from
|
|
290
|
+
* @return A new object with the keys that contained empty values removed
|
|
291
|
+
*/
|
|
255
292
|
function filterEmpty(source) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return result;
|
|
293
|
+
const result = {};
|
|
294
|
+
for (const key in source) {
|
|
295
|
+
const value = source[key];
|
|
296
|
+
if (value !== void 0 && value !== null && value !== "") {
|
|
297
|
+
if (!Array.isArray(value) || value.length > 0) result[key] = source[key];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return result;
|
|
267
301
|
}
|
|
268
|
-
|
|
269
302
|
/**
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
303
|
+
* Sorts query params by both key and value returning a new URLSearchParams
|
|
304
|
+
* object with the keys inserted in sorted order.
|
|
305
|
+
*
|
|
306
|
+
* Treats `included` specially, splicing it into an array if it is a string and sorting the array.
|
|
307
|
+
*
|
|
308
|
+
* Options:
|
|
309
|
+
* - arrayFormat: 'bracket' | 'indices' | 'repeat' | 'comma'
|
|
310
|
+
*
|
|
311
|
+
* 'bracket': appends [] to the key for every value e.g. `&ids[]=1&ids[]=2`
|
|
312
|
+
* 'indices': appends [i] to the key for every value e.g. `&ids[0]=1&ids[1]=2`
|
|
313
|
+
* 'repeat': appends the key for every value e.g. `&ids=1&ids=2`
|
|
314
|
+
* 'comma' (default): appends the key once with a comma separated list of values e.g. `&ids=1,2`
|
|
315
|
+
*
|
|
316
|
+
* @public
|
|
317
|
+
* @return A {@link URLSearchParams} with keys inserted in sorted order
|
|
318
|
+
*/
|
|
286
319
|
function sortQueryParams(params, options) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
urlParams.append(key, String(v));
|
|
328
|
-
});
|
|
329
|
-
return;
|
|
330
|
-
case 'comma':
|
|
331
|
-
default:
|
|
332
|
-
urlParams.append(key, value.join(','));
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
} else {
|
|
336
|
-
urlParams.append(key, String(value));
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
return urlParams;
|
|
320
|
+
const opts = Object.assign({}, DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS, options);
|
|
321
|
+
const paramsIsObject = !(params instanceof URLSearchParams);
|
|
322
|
+
const urlParams = new URLSearchParams();
|
|
323
|
+
const dictionaryParams = paramsIsObject ? params : {};
|
|
324
|
+
if (!paramsIsObject) params.forEach((value, key) => {
|
|
325
|
+
if (!(key in dictionaryParams)) dictionaryParams[key] = value;
|
|
326
|
+
else {
|
|
327
|
+
const existingValue = dictionaryParams[key];
|
|
328
|
+
if (Array.isArray(existingValue)) existingValue.push(value);
|
|
329
|
+
else dictionaryParams[key] = [existingValue, value];
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
if ("include" in dictionaryParams) dictionaryParams.include = handleInclude(dictionaryParams.include);
|
|
333
|
+
Object.keys(dictionaryParams).sort().forEach((key) => {
|
|
334
|
+
const value = dictionaryParams[key];
|
|
335
|
+
if (Array.isArray(value)) {
|
|
336
|
+
value.sort();
|
|
337
|
+
switch (opts.arrayFormat) {
|
|
338
|
+
case "indices":
|
|
339
|
+
value.forEach((v, i) => {
|
|
340
|
+
urlParams.append(`${key}[${i}]`, String(v));
|
|
341
|
+
});
|
|
342
|
+
return;
|
|
343
|
+
case "bracket":
|
|
344
|
+
value.forEach((v) => {
|
|
345
|
+
urlParams.append(`${key}[]`, String(v));
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
case "repeat":
|
|
349
|
+
value.forEach((v) => {
|
|
350
|
+
urlParams.append(key, String(v));
|
|
351
|
+
});
|
|
352
|
+
return;
|
|
353
|
+
default:
|
|
354
|
+
urlParams.append(key, value.join(","));
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
} else urlParams.append(key, String(value));
|
|
358
|
+
});
|
|
359
|
+
return urlParams;
|
|
340
360
|
}
|
|
341
|
-
|
|
342
361
|
/**
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
362
|
+
* Sorts query params by both key and value, returning a query params string
|
|
363
|
+
*
|
|
364
|
+
* Treats `included` specially, splicing it into an array if it is a string and sorting the array.
|
|
365
|
+
*
|
|
366
|
+
* Options:
|
|
367
|
+
* - arrayFormat: 'bracket' | 'indices' | 'repeat' | 'comma'
|
|
368
|
+
*
|
|
369
|
+
* 'bracket': appends [] to the key for every value e.g. `ids[]=1&ids[]=2`
|
|
370
|
+
* 'indices': appends [i] to the key for every value e.g. `ids[0]=1&ids[1]=2`
|
|
371
|
+
* 'repeat': appends the key for every value e.g. `ids=1&ids=2`
|
|
372
|
+
* 'comma' (default): appends the key once with a comma separated list of values e.g. `ids=1,2`
|
|
373
|
+
*
|
|
374
|
+
* @public
|
|
375
|
+
* @return A sorted query params string without the leading `?`
|
|
376
|
+
*/
|
|
358
377
|
function buildQueryParams(params, options) {
|
|
359
|
-
|
|
378
|
+
return sortQueryParams(params, options).toString();
|
|
360
379
|
}
|
|
361
|
-
|
|
380
|
+
|
|
381
|
+
//#endregion
|
|
382
|
+
export { buildBaseURL, buildQueryParams, filterEmpty, setBuildURLConfig, sortQueryParams };
|
|
383
|
+
//# sourceMappingURL=index.js.map
|