@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
|
@@ -1,239 +1,289 @@
|
|
|
1
|
-
import { getOrSetGlobal } from
|
|
1
|
+
import { getOrSetGlobal } from "@warp-drive/core/types/-private";
|
|
2
2
|
|
|
3
|
+
//#region src/index.ts
|
|
3
4
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
* @module
|
|
6
|
+
* @mergeModuleWith <project>
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The global configuration used by {@link buildBaseURL} when a call does
|
|
10
|
+
* not provide its own `host`/`namespace`. Set via {@link setBuildURLConfig}.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
const CONFIG = getOrSetGlobal("CONFIG", {
|
|
15
|
+
host: "",
|
|
16
|
+
namespace: ""
|
|
15
17
|
});
|
|
16
|
-
|
|
17
18
|
/**
|
|
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
|
-
|
|
51
|
-
|
|
19
|
+
* Sets the global configuration for `buildBaseURL`
|
|
20
|
+
* for host and namespace values for the application.
|
|
21
|
+
*
|
|
22
|
+
* These values may still be overridden by passing
|
|
23
|
+
* them to buildBaseURL directly.
|
|
24
|
+
*
|
|
25
|
+
* This method may be called as many times as needed.
|
|
26
|
+
* host values of `''` or `'/'` are equivalent.
|
|
27
|
+
*
|
|
28
|
+
* Except for the value of `/` as host, host should not
|
|
29
|
+
* end with `/`.
|
|
30
|
+
*
|
|
31
|
+
* namespace should not start or end with a `/`.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* type BuildURLConfig = {
|
|
35
|
+
* host: string;
|
|
36
|
+
* namespace: string'
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Example:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { setBuildURLConfig } from '@warp-drive/utilities';
|
|
44
|
+
*
|
|
45
|
+
* setBuildURLConfig({
|
|
46
|
+
* host: 'https://api.example.com',
|
|
47
|
+
* namespace: 'api/v1'
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
52
53
|
function setBuildURLConfig(config) {
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
CONFIG.host = config.host || "";
|
|
55
|
+
CONFIG.namespace = config.namespace || "";
|
|
55
56
|
}
|
|
56
|
-
|
|
57
|
+
/**
|
|
58
|
+
* {@link buildBaseURL} options for a `findRecord` request.
|
|
59
|
+
*
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
/**
|
|
63
|
+
* {@link buildBaseURL} options for a `query` request.
|
|
64
|
+
*
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
68
|
+
* {@link buildBaseURL} options for a `findMany` request.
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* {@link buildBaseURL} options for a `findRelatedCollection` request.
|
|
74
|
+
*
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
/**
|
|
78
|
+
* {@link buildBaseURL} options for a `findRelatedRecord` request.
|
|
79
|
+
*
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* {@link buildBaseURL} options for a `createRecord` request.
|
|
84
|
+
*
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
/**
|
|
88
|
+
* {@link buildBaseURL} options for an `updateRecord` request.
|
|
89
|
+
*
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
/**
|
|
93
|
+
* {@link buildBaseURL} options for a `deleteRecord` request.
|
|
94
|
+
*
|
|
95
|
+
* @public
|
|
96
|
+
*/
|
|
97
|
+
/**
|
|
98
|
+
* {@link buildBaseURL} options for building a URL directly from a `resourcePath`
|
|
99
|
+
* without an associated request operation.
|
|
100
|
+
*
|
|
101
|
+
* @public
|
|
102
|
+
*/
|
|
103
|
+
/**
|
|
104
|
+
* The union of all `op`-specific option shapes accepted by {@link buildBaseURL}, one of:
|
|
105
|
+
*
|
|
106
|
+
* - {@link FindRecordUrlOptions}
|
|
107
|
+
* - {@link QueryUrlOptions}
|
|
108
|
+
* - {@link FindManyUrlOptions}
|
|
109
|
+
* - {@link FindRelatedCollectionUrlOptions}
|
|
110
|
+
* - {@link FindRelatedResourceUrlOptions}
|
|
111
|
+
* - {@link CreateRecordUrlOptions}
|
|
112
|
+
* - {@link UpdateRecordUrlOptions}
|
|
113
|
+
* - {@link DeleteRecordUrlOptions}
|
|
114
|
+
* - {@link GenericUrlOptions}
|
|
115
|
+
*
|
|
116
|
+
* @public
|
|
117
|
+
*/
|
|
118
|
+
const OPERATIONS_WITH_PRIMARY_RECORDS = /* @__PURE__ */ new Set([
|
|
119
|
+
"findRecord",
|
|
120
|
+
"findRelatedRecord",
|
|
121
|
+
"findRelatedCollection",
|
|
122
|
+
"updateRecord",
|
|
123
|
+
"deleteRecord"
|
|
124
|
+
]);
|
|
57
125
|
function isOperationWithPrimaryRecord(options) {
|
|
58
|
-
|
|
126
|
+
return "op" in options && OPERATIONS_WITH_PRIMARY_RECORDS.has(options.op);
|
|
59
127
|
}
|
|
60
128
|
function resourcePathForType(options) {
|
|
61
|
-
|
|
129
|
+
return options.op === "findMany" ? options.identifiers[0].type : options.identifier.type;
|
|
62
130
|
}
|
|
63
|
-
|
|
64
131
|
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
132
|
+
* Builds a URL for a request based on the provided options.
|
|
133
|
+
* Does not include support for building query params (see `buildQueryParams`)
|
|
134
|
+
* so that it may be composed cleanly with other query-params strategies.
|
|
135
|
+
*
|
|
136
|
+
* Usage:
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* import { buildBaseURL } from '@warp-drive/utilities';
|
|
140
|
+
*
|
|
141
|
+
* const url = buildBaseURL({
|
|
142
|
+
* host: 'https://api.example.com',
|
|
143
|
+
* namespace: 'api/v1',
|
|
144
|
+
* resourcePath: 'emberDevelopers',
|
|
145
|
+
* op: 'query',
|
|
146
|
+
* identifier: { type: 'ember-developer' }
|
|
147
|
+
* });
|
|
148
|
+
*
|
|
149
|
+
* // => 'https://api.example.com/api/v1/emberDevelopers'
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* On the surface this may seem like a lot of work to do something simple, but
|
|
153
|
+
* it is designed to be composable with other utilities and interfaces that the
|
|
154
|
+
* average product engineer will never need to see or use.
|
|
155
|
+
*
|
|
156
|
+
* A few notes:
|
|
157
|
+
*
|
|
158
|
+
* - `resourcePath` is optional, but if it is not provided, `identifier.type` will be used.
|
|
159
|
+
* - `host` and `namespace` are optional, but if they are not provided, the values globally
|
|
160
|
+
* configured via `setBuildURLConfig` will be used.
|
|
161
|
+
* - `op` is required and must be one of the following:
|
|
162
|
+
* - 'findRecord' 'query' 'findMany' 'findRelatedCollection' 'findRelatedRecord'` 'createRecord' 'updateRecord' 'deleteRecord'
|
|
163
|
+
* - Depending on the value of `op`, `identifier` or `identifiers` will be required.
|
|
164
|
+
*
|
|
165
|
+
* @public
|
|
166
|
+
*/
|
|
100
167
|
function buildBaseURL(urlOptions) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
168
|
+
const options = Object.assign({
|
|
169
|
+
host: CONFIG.host,
|
|
170
|
+
namespace: CONFIG.namespace
|
|
171
|
+
}, urlOptions);
|
|
172
|
+
const idPath = isOperationWithPrimaryRecord(options) ? encodeURIComponent(options.identifier.id) : "";
|
|
173
|
+
const resourcePath = options.resourcePath || resourcePathForType(options);
|
|
174
|
+
const { host, namespace } = options;
|
|
175
|
+
const fieldPath = "fieldPath" in options ? options.fieldPath : "";
|
|
176
|
+
const hasHost = host !== "" && host !== "/";
|
|
177
|
+
const url = [
|
|
178
|
+
hasHost ? host : "",
|
|
179
|
+
namespace,
|
|
180
|
+
resourcePath,
|
|
181
|
+
idPath,
|
|
182
|
+
fieldPath
|
|
183
|
+
].filter(Boolean).join("/");
|
|
184
|
+
return hasHost ? url : `/${url}`;
|
|
117
185
|
}
|
|
118
|
-
const DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS = {
|
|
119
|
-
arrayFormat: 'comma'
|
|
120
|
-
};
|
|
186
|
+
const DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS = { arrayFormat: "comma" };
|
|
121
187
|
function handleInclude(include) {
|
|
122
|
-
|
|
188
|
+
return typeof include === "string" ? include.split(",") : include;
|
|
123
189
|
}
|
|
124
|
-
|
|
125
190
|
/**
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
191
|
+
* filter out keys of an object that have falsy values or point to empty arrays
|
|
192
|
+
* returning a new object with only those keys that have truthy values / non-empty arrays
|
|
193
|
+
*
|
|
194
|
+
* @public
|
|
195
|
+
* @param source object to filter keys with empty values from
|
|
196
|
+
* @return A new object with the keys that contained empty values removed
|
|
197
|
+
*/
|
|
133
198
|
function filterEmpty(source) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return result;
|
|
199
|
+
const result = {};
|
|
200
|
+
for (const key in source) {
|
|
201
|
+
const value = source[key];
|
|
202
|
+
if (value !== void 0 && value !== null && value !== "") {
|
|
203
|
+
if (!Array.isArray(value) || value.length > 0) result[key] = source[key];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return result;
|
|
145
207
|
}
|
|
146
|
-
|
|
147
208
|
/**
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
209
|
+
* Sorts query params by both key and value returning a new URLSearchParams
|
|
210
|
+
* object with the keys inserted in sorted order.
|
|
211
|
+
*
|
|
212
|
+
* Treats `included` specially, splicing it into an array if it is a string and sorting the array.
|
|
213
|
+
*
|
|
214
|
+
* Options:
|
|
215
|
+
* - arrayFormat: 'bracket' | 'indices' | 'repeat' | 'comma'
|
|
216
|
+
*
|
|
217
|
+
* 'bracket': appends [] to the key for every value e.g. `&ids[]=1&ids[]=2`
|
|
218
|
+
* 'indices': appends [i] to the key for every value e.g. `&ids[0]=1&ids[1]=2`
|
|
219
|
+
* 'repeat': appends the key for every value e.g. `&ids=1&ids=2`
|
|
220
|
+
* 'comma' (default): appends the key once with a comma separated list of values e.g. `&ids=1,2`
|
|
221
|
+
*
|
|
222
|
+
* @public
|
|
223
|
+
* @return A {@link URLSearchParams} with keys inserted in sorted order
|
|
224
|
+
*/
|
|
164
225
|
function sortQueryParams(params, options) {
|
|
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
|
-
urlParams.append(key, String(v));
|
|
206
|
-
});
|
|
207
|
-
return;
|
|
208
|
-
case 'comma':
|
|
209
|
-
default:
|
|
210
|
-
urlParams.append(key, value.join(','));
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
urlParams.append(key, String(value));
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
return urlParams;
|
|
226
|
+
const opts = Object.assign({}, DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS, options);
|
|
227
|
+
const paramsIsObject = !(params instanceof URLSearchParams);
|
|
228
|
+
const urlParams = new URLSearchParams();
|
|
229
|
+
const dictionaryParams = paramsIsObject ? params : {};
|
|
230
|
+
if (!paramsIsObject) params.forEach((value, key) => {
|
|
231
|
+
if (!(key in dictionaryParams)) dictionaryParams[key] = value;
|
|
232
|
+
else {
|
|
233
|
+
const existingValue = dictionaryParams[key];
|
|
234
|
+
if (Array.isArray(existingValue)) existingValue.push(value);
|
|
235
|
+
else dictionaryParams[key] = [existingValue, value];
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
if ("include" in dictionaryParams) dictionaryParams.include = handleInclude(dictionaryParams.include);
|
|
239
|
+
Object.keys(dictionaryParams).sort().forEach((key) => {
|
|
240
|
+
const value = dictionaryParams[key];
|
|
241
|
+
if (Array.isArray(value)) {
|
|
242
|
+
value.sort();
|
|
243
|
+
switch (opts.arrayFormat) {
|
|
244
|
+
case "indices":
|
|
245
|
+
value.forEach((v, i) => {
|
|
246
|
+
urlParams.append(`${key}[${i}]`, String(v));
|
|
247
|
+
});
|
|
248
|
+
return;
|
|
249
|
+
case "bracket":
|
|
250
|
+
value.forEach((v) => {
|
|
251
|
+
urlParams.append(`${key}[]`, String(v));
|
|
252
|
+
});
|
|
253
|
+
return;
|
|
254
|
+
case "repeat":
|
|
255
|
+
value.forEach((v) => {
|
|
256
|
+
urlParams.append(key, String(v));
|
|
257
|
+
});
|
|
258
|
+
return;
|
|
259
|
+
default:
|
|
260
|
+
urlParams.append(key, value.join(","));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
} else urlParams.append(key, String(value));
|
|
264
|
+
});
|
|
265
|
+
return urlParams;
|
|
218
266
|
}
|
|
219
|
-
|
|
220
267
|
/**
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
268
|
+
* Sorts query params by both key and value, returning a query params string
|
|
269
|
+
*
|
|
270
|
+
* Treats `included` specially, splicing it into an array if it is a string and sorting the array.
|
|
271
|
+
*
|
|
272
|
+
* Options:
|
|
273
|
+
* - arrayFormat: 'bracket' | 'indices' | 'repeat' | 'comma'
|
|
274
|
+
*
|
|
275
|
+
* 'bracket': appends [] to the key for every value e.g. `ids[]=1&ids[]=2`
|
|
276
|
+
* 'indices': appends [i] to the key for every value e.g. `ids[0]=1&ids[1]=2`
|
|
277
|
+
* 'repeat': appends the key for every value e.g. `ids=1&ids=2`
|
|
278
|
+
* 'comma' (default): appends the key once with a comma separated list of values e.g. `ids=1,2`
|
|
279
|
+
*
|
|
280
|
+
* @public
|
|
281
|
+
* @return A sorted query params string without the leading `?`
|
|
282
|
+
*/
|
|
236
283
|
function buildQueryParams(params, options) {
|
|
237
|
-
|
|
284
|
+
return sortQueryParams(params, options).toString();
|
|
238
285
|
}
|
|
239
|
-
|
|
286
|
+
|
|
287
|
+
//#endregion
|
|
288
|
+
export { buildBaseURL, buildQueryParams, filterEmpty, setBuildURLConfig, sortQueryParams };
|
|
289
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["getOrSetGlobal","CONFIG","host","namespace","setBuildURLConfig","config","OPERATIONS_WITH_PRIMARY_RECORDS","Set","isOperationWithPrimaryRecord","options","has","op","hasResourcePath","resourcePath","length","resourcePathForType","identifiers","type","identifier","buildBaseURL","urlOptions","Object","assign","idPath","encodeURIComponent","id","fieldPath","hasHost","url","filter","Boolean","join","DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS","arrayFormat","handleInclude","include","split","filterEmpty","source","result","key","value","undefined","Array","isArray","sortQueryParams","params","opts","paramsIsObject","URLSearchParams","urlParams","dictionaryParams","forEach","hasExisting","existingValue","push","sortedKeys","keys","sort","v","i","append","String","buildQueryParams","toString"],"sources":["../../../src/index.ts"],"sourcesContent":["/**\n * @module\n * @mergeModuleWith <project>\n */\n\nimport { assert } from '@warp-drive/core/build-config/macros';\nimport { getOrSetGlobal } from '@warp-drive/core/types/-private';\nimport type { QueryParamsSerializationOptions, QueryParamsSource, Serializable } from '@warp-drive/core/types/params';\n\n// prevents the final constructed object from needing to add\n// host and namespace which are provided by the final consuming\n// class to the prototype which can result in overwrite errors\n\n/**\n * The global configuration used by {@link buildBaseURL} when a call does\n * not provide its own `host`/`namespace`. Set via {@link setBuildURLConfig}.\n *\n * @public\n */\nexport interface BuildURLConfig {\n /**\n * The scheme, domain and port (if any) to prefix built URLs with, e.g. `'https://api.example.com'`.\n */\n host: string | null;\n /**\n * The path segment to insert between `host` and the resource path, e.g. `'api/v1'`.\n */\n namespace: string | null;\n}\n\nconst CONFIG: BuildURLConfig = getOrSetGlobal('CONFIG', {\n host: '',\n namespace: '',\n});\n\n/**\n * Sets the global configuration for `buildBaseURL`\n * for host and namespace values for the application.\n *\n * These values may still be overridden by passing\n * them to buildBaseURL directly.\n *\n * This method may be called as many times as needed.\n * host values of `''` or `'/'` are equivalent.\n *\n * Except for the value of `/` as host, host should not\n * end with `/`.\n *\n * namespace should not start or end with a `/`.\n *\n * ```ts\n * type BuildURLConfig = {\n * host: string;\n * namespace: string'\n * }\n * ```\n *\n * Example:\n *\n * ```ts\n * import { setBuildURLConfig } from '@warp-drive/utilities';\n *\n * setBuildURLConfig({\n * host: 'https://api.example.com',\n * namespace: 'api/v1'\n * });\n * ```\n *\n * @public\n */\nexport function setBuildURLConfig(config: BuildURLConfig): void {\n assert(`setBuildURLConfig: You must pass a config object`, config);\n assert(\n `setBuildURLConfig: You must pass a config object with a 'host' or 'namespace' property`,\n 'host' in config || 'namespace' in config\n );\n\n CONFIG.host = config.host || '';\n CONFIG.namespace = config.namespace || '';\n\n assert(\n `buildBaseURL: host must NOT end with '/', received '${CONFIG.host}'`,\n CONFIG.host === '/' || !CONFIG.host.endsWith('/')\n );\n assert(\n `buildBaseURL: namespace must NOT start with '/', received '${CONFIG.namespace}'`,\n !CONFIG.namespace.startsWith('/')\n );\n assert(\n `buildBaseURL: namespace must NOT end with '/', received '${CONFIG.namespace}'`,\n !CONFIG.namespace.endsWith('/')\n );\n}\n\n/**\n * {@link buildBaseURL} options for a `findRecord` request.\n *\n * @public\n */\nexport interface FindRecordUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'findRecord';\n /**\n * The type and id of the record to find.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n /**\n * The resource id.\n */\n id: string;\n };\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for a `query` request.\n *\n * @public\n */\nexport interface QueryUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'query';\n /**\n * The type of the records to query.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n };\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for a `findMany` request.\n *\n * @public\n */\nexport interface FindManyUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'findMany';\n /**\n * The type and id of each record to find.\n */\n identifiers: {\n /**\n * The resource type.\n */\n type: string;\n /**\n * The resource id.\n */\n id: string;\n }[];\n /**\n * The path segment for the resource, defaults to the first identifier's `type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n/**\n * {@link buildBaseURL} options for a `findRelatedCollection` request.\n *\n * @public\n */\nexport interface FindRelatedCollectionUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'findRelatedCollection';\n /**\n * The type and id of the record whose relationship is being fetched.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n /**\n * The resource id.\n */\n id: string;\n };\n /**\n * The relationship field name, appended to the resource path.\n */\n fieldPath: string;\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for a `findRelatedRecord` request.\n *\n * @public\n */\nexport interface FindRelatedResourceUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'findRelatedRecord';\n /**\n * The type and id of the record whose relationship is being fetched.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n /**\n * The resource id.\n */\n id: string;\n };\n /**\n * The relationship field name, appended to the resource path.\n */\n fieldPath: string;\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for a `createRecord` request.\n *\n * @public\n */\nexport interface CreateRecordUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'createRecord';\n /**\n * The type of the record being created.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n };\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for an `updateRecord` request.\n *\n * @public\n */\nexport interface UpdateRecordUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'updateRecord';\n /**\n * The type and id of the record being updated.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n /**\n * The resource id.\n */\n id: string;\n };\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for a `deleteRecord` request.\n *\n * @public\n */\nexport interface DeleteRecordUrlOptions {\n /**\n * The request operation this URL is for.\n */\n op: 'deleteRecord';\n /**\n * The type and id of the record being deleted.\n */\n identifier: {\n /**\n * The resource type.\n */\n type: string;\n /**\n * The resource id.\n */\n id: string;\n };\n /**\n * The path segment for the resource, defaults to `identifier.type` if not provided.\n */\n resourcePath?: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * {@link buildBaseURL} options for building a URL directly from a `resourcePath`\n * without an associated request operation.\n *\n * @public\n */\nexport interface GenericUrlOptions {\n /**\n * The path segment for the resource.\n */\n resourcePath: string;\n /**\n * Overrides the globally configured host for this call only.\n */\n host?: string;\n /**\n * Overrides the globally configured namespace for this call only.\n */\n namespace?: string;\n}\n\n/**\n * The union of all `op`-specific option shapes accepted by {@link buildBaseURL}, one of:\n *\n * - {@link FindRecordUrlOptions}\n * - {@link QueryUrlOptions}\n * - {@link FindManyUrlOptions}\n * - {@link FindRelatedCollectionUrlOptions}\n * - {@link FindRelatedResourceUrlOptions}\n * - {@link CreateRecordUrlOptions}\n * - {@link UpdateRecordUrlOptions}\n * - {@link DeleteRecordUrlOptions}\n * - {@link GenericUrlOptions}\n *\n * @public\n */\nexport type UrlOptions =\n | FindRecordUrlOptions\n | QueryUrlOptions\n | FindManyUrlOptions\n | FindRelatedCollectionUrlOptions\n | FindRelatedResourceUrlOptions\n | CreateRecordUrlOptions\n | UpdateRecordUrlOptions\n | DeleteRecordUrlOptions\n | GenericUrlOptions;\n\nconst OPERATIONS_WITH_PRIMARY_RECORDS = new Set([\n 'findRecord',\n 'findRelatedRecord',\n 'findRelatedCollection',\n 'updateRecord',\n 'deleteRecord',\n]);\n\nfunction isOperationWithPrimaryRecord(\n options: UrlOptions\n): options is\n | FindRecordUrlOptions\n | FindRelatedCollectionUrlOptions\n | FindRelatedResourceUrlOptions\n | UpdateRecordUrlOptions\n | DeleteRecordUrlOptions {\n return 'op' in options && OPERATIONS_WITH_PRIMARY_RECORDS.has(options.op);\n}\n\nfunction hasResourcePath(options: UrlOptions): options is GenericUrlOptions {\n return 'resourcePath' in options && typeof options.resourcePath === 'string' && options.resourcePath.length > 0;\n}\n\nfunction resourcePathForType(options: UrlOptions): string {\n assert(\n `resourcePathForType: You must pass a valid op as part of options`,\n 'op' in options && typeof options.op === 'string'\n );\n return options.op === 'findMany' ? options.identifiers[0].type : options.identifier.type;\n}\n\n/**\n * Builds a URL for a request based on the provided options.\n * Does not include support for building query params (see `buildQueryParams`)\n * so that it may be composed cleanly with other query-params strategies.\n *\n * Usage:\n *\n * ```ts\n * import { buildBaseURL } from '@warp-drive/utilities';\n *\n * const url = buildBaseURL({\n * host: 'https://api.example.com',\n * namespace: 'api/v1',\n * resourcePath: 'emberDevelopers',\n * op: 'query',\n * identifier: { type: 'ember-developer' }\n * });\n *\n * // => 'https://api.example.com/api/v1/emberDevelopers'\n * ```\n *\n * On the surface this may seem like a lot of work to do something simple, but\n * it is designed to be composable with other utilities and interfaces that the\n * average product engineer will never need to see or use.\n *\n * A few notes:\n *\n * - `resourcePath` is optional, but if it is not provided, `identifier.type` will be used.\n * - `host` and `namespace` are optional, but if they are not provided, the values globally\n * configured via `setBuildURLConfig` will be used.\n * - `op` is required and must be one of the following:\n * - 'findRecord' 'query' 'findMany' 'findRelatedCollection' 'findRelatedRecord'` 'createRecord' 'updateRecord' 'deleteRecord'\n * - Depending on the value of `op`, `identifier` or `identifiers` will be required.\n *\n * @public\n */\nexport function buildBaseURL(urlOptions: UrlOptions): string {\n const options = Object.assign(\n {\n host: CONFIG.host,\n namespace: CONFIG.namespace,\n },\n urlOptions\n );\n assert(\n `buildBaseURL: You must pass \\`op\\` as part of options`,\n hasResourcePath(options) || (typeof options.op === 'string' && options.op.length > 0)\n );\n assert(\n `buildBaseURL: You must pass \\`identifier\\` as part of options`,\n hasResourcePath(options) ||\n options.op === 'findMany' ||\n (options.identifier && typeof options.identifier === 'object')\n );\n assert(\n `buildBaseURL: You must pass \\`identifiers\\` as part of options`,\n hasResourcePath(options) ||\n options.op !== 'findMany' ||\n (options.identifiers &&\n Array.isArray(options.identifiers) &&\n options.identifiers.length > 0 &&\n options.identifiers.every((i) => i && typeof i === 'object'))\n );\n assert(\n `buildBaseURL: You must pass valid \\`identifier\\` as part of options, expected 'id'`,\n hasResourcePath(options) ||\n !isOperationWithPrimaryRecord(options) ||\n (typeof options.identifier.id === 'string' && options.identifier.id.length > 0)\n );\n assert(\n `buildBaseURL: You must pass \\`identifiers\\` as part of options`,\n hasResourcePath(options) ||\n options.op !== 'findMany' ||\n options.identifiers.every((i) => typeof i.id === 'string' && i.id.length > 0)\n );\n assert(\n `buildBaseURL: You must pass valid \\`identifier\\` as part of options, expected 'type'`,\n hasResourcePath(options) ||\n options.op === 'findMany' ||\n (typeof options.identifier.type === 'string' && options.identifier.type.length > 0)\n );\n assert(\n `buildBaseURL: You must pass valid \\`identifiers\\` as part of options, expected 'type'`,\n hasResourcePath(options) ||\n options.op !== 'findMany' ||\n (typeof options.identifiers[0].type === 'string' && options.identifiers[0].type.length > 0)\n );\n\n // prettier-ignore\n const idPath: string =\n isOperationWithPrimaryRecord(options) ? encodeURIComponent(options.identifier.id)\n : '';\n const resourcePath = options.resourcePath || resourcePathForType(options);\n const { host, namespace } = options;\n const fieldPath = 'fieldPath' in options ? options.fieldPath : '';\n\n assert(\n `buildBaseURL: You tried to build a url for a ${String(\n 'op' in options ? options.op + ' ' : ''\n )}request to ${resourcePath} but resourcePath must be set or op must be one of \"${[\n 'findRecord',\n 'findRelatedRecord',\n 'findRelatedCollection',\n 'updateRecord',\n 'deleteRecord',\n 'createRecord',\n 'query',\n 'findMany',\n ].join('\",\"')}\".`,\n hasResourcePath(options) ||\n [\n 'findRecord',\n 'query',\n 'findMany',\n 'findRelatedCollection',\n 'findRelatedRecord',\n 'createRecord',\n 'updateRecord',\n 'deleteRecord',\n ].includes(options.op)\n );\n\n assert(`buildBaseURL: host must NOT end with '/', received '${host}'`, host === '/' || !host.endsWith('/'));\n assert(`buildBaseURL: namespace must NOT start with '/', received '${namespace}'`, !namespace.startsWith('/'));\n assert(`buildBaseURL: namespace must NOT end with '/', received '${namespace}'`, !namespace.endsWith('/'));\n assert(\n `buildBaseURL: resourcePath must NOT start with '/', received '${resourcePath}'`,\n !resourcePath.startsWith('/')\n );\n assert(`buildBaseURL: resourcePath must NOT end with '/', received '${resourcePath}'`, !resourcePath.endsWith('/'));\n assert(`buildBaseURL: fieldPath must NOT start with '/', received '${fieldPath}'`, !fieldPath.startsWith('/'));\n assert(`buildBaseURL: fieldPath must NOT end with '/', received '${fieldPath}'`, !fieldPath.endsWith('/'));\n assert(`buildBaseURL: idPath must NOT start with '/', received '${idPath}'`, !idPath.startsWith('/'));\n assert(`buildBaseURL: idPath must NOT end with '/', received '${idPath}'`, !idPath.endsWith('/'));\n\n const hasHost = host !== '' && host !== '/';\n const url = [hasHost ? host : '', namespace, resourcePath, idPath, fieldPath].filter(Boolean).join('/');\n return hasHost ? url : `/${url}`;\n}\n\nconst DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS: QueryParamsSerializationOptions = {\n arrayFormat: 'comma',\n};\n\nfunction handleInclude(include: string | string[]): string[] {\n assert(\n `Expected include to be a string or array, got ${typeof include}`,\n typeof include === 'string' || Array.isArray(include)\n );\n return typeof include === 'string' ? include.split(',') : include;\n}\n\n/**\n * filter out keys of an object that have falsy values or point to empty arrays\n * returning a new object with only those keys that have truthy values / non-empty arrays\n *\n * @public\n * @param source object to filter keys with empty values from\n * @return A new object with the keys that contained empty values removed\n */\nexport function filterEmpty(source: Record<string, Serializable>): Record<string, Serializable> {\n const result: Record<string, Serializable> = {};\n for (const key in source) {\n const value = source[key];\n // Allow `0` and `false` but filter falsy values that indicate \"empty\"\n if (value !== undefined && value !== null && value !== '') {\n if (!Array.isArray(value) || value.length > 0) {\n result[key] = source[key];\n }\n }\n }\n return result;\n}\n\n/**\n * Sorts query params by both key and value returning a new URLSearchParams\n * object with the keys inserted in sorted order.\n *\n * Treats `included` specially, splicing it into an array if it is a string and sorting the array.\n *\n * Options:\n * - arrayFormat: 'bracket' | 'indices' | 'repeat' | 'comma'\n *\n * 'bracket': appends [] to the key for every value e.g. `&ids[]=1&ids[]=2`\n * 'indices': appends [i] to the key for every value e.g. `&ids[0]=1&ids[1]=2`\n * 'repeat': appends the key for every value e.g. `&ids=1&ids=2`\n * 'comma' (default): appends the key once with a comma separated list of values e.g. `&ids=1,2`\n *\n * @public\n * @return A {@link URLSearchParams} with keys inserted in sorted order\n */\nexport function sortQueryParams(params: QueryParamsSource, options?: QueryParamsSerializationOptions): URLSearchParams {\n const opts = Object.assign({}, DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS, options);\n const paramsIsObject = !(params instanceof URLSearchParams);\n const urlParams = new URLSearchParams();\n const dictionaryParams: Record<string, Serializable> = paramsIsObject ? params : {};\n\n if (!paramsIsObject) {\n params.forEach((value, key) => {\n const hasExisting = key in dictionaryParams;\n if (!hasExisting) {\n dictionaryParams[key] = value;\n } else {\n const existingValue = dictionaryParams[key];\n if (Array.isArray(existingValue)) {\n existingValue.push(value);\n } else {\n dictionaryParams[key] = [existingValue, value];\n }\n }\n });\n }\n\n if ('include' in dictionaryParams) {\n dictionaryParams.include = handleInclude(dictionaryParams.include as string | string[]);\n }\n\n const sortedKeys = Object.keys(dictionaryParams).sort();\n sortedKeys.forEach((key) => {\n const value = dictionaryParams[key];\n if (Array.isArray(value)) {\n value.sort();\n switch (opts.arrayFormat) {\n case 'indices':\n value.forEach((v, i) => {\n urlParams.append(`${key}[${i}]`, String(v));\n });\n return;\n case 'bracket':\n value.forEach((v) => {\n urlParams.append(`${key}[]`, String(v));\n });\n return;\n case 'repeat':\n value.forEach((v) => {\n urlParams.append(key, String(v));\n });\n return;\n case 'comma':\n default:\n urlParams.append(key, value.join(','));\n return;\n }\n } else {\n urlParams.append(key, String(value));\n }\n });\n\n return urlParams;\n}\n\n/**\n * Sorts query params by both key and value, returning a query params string\n *\n * Treats `included` specially, splicing it into an array if it is a string and sorting the array.\n *\n * Options:\n * - arrayFormat: 'bracket' | 'indices' | 'repeat' | 'comma'\n *\n * 'bracket': appends [] to the key for every value e.g. `ids[]=1&ids[]=2`\n * 'indices': appends [i] to the key for every value e.g. `ids[0]=1&ids[1]=2`\n * 'repeat': appends the key for every value e.g. `ids=1&ids=2`\n * 'comma' (default): appends the key once with a comma separated list of values e.g. `ids=1,2`\n *\n * @public\n * @return A sorted query params string without the leading `?`\n */\nexport function buildQueryParams(params: QueryParamsSource, options?: QueryParamsSerializationOptions): string {\n return sortQueryParams(params, options).toString();\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//#region src/-private/string/inflections.ts
|
|
2
|
+
const defaultRules = {
|
|
3
|
+
plurals: [
|
|
4
|
+
[/$/, "s"],
|
|
5
|
+
[/s$/i, "s"],
|
|
6
|
+
[/^(ax|test)is$/i, "$1es"],
|
|
7
|
+
[/(octop|vir)us$/i, "$1i"],
|
|
8
|
+
[/(octop|vir)i$/i, "$1i"],
|
|
9
|
+
[/(alias|status|bonus)$/i, "$1es"],
|
|
10
|
+
[/(tu|bu)s$/i, "$1ses"],
|
|
11
|
+
[/(buffal|tomat)o$/i, "$1oes"],
|
|
12
|
+
[/([ti])um$/i, "$1a"],
|
|
13
|
+
[/([ti])a$/i, "$1a"],
|
|
14
|
+
[/sis$/i, "ses"],
|
|
15
|
+
[/(?:([^f])fe|([lr])f)$/i, "$1$2ves"],
|
|
16
|
+
[/(hive)$/i, "$1s"],
|
|
17
|
+
[/([^aeiouy]|qu)y$/i, "$1ies"],
|
|
18
|
+
[/(x|ch|ss|sh)$/i, "$1es"],
|
|
19
|
+
[/(matr|vert|ind)(?:ix|ex)$/i, "$1ices"],
|
|
20
|
+
[/^(m|l)ouse$/i, "$1ice"],
|
|
21
|
+
[/^(m|l)ice$/i, "$1ice"],
|
|
22
|
+
[/^(ox)$/i, "$1en"],
|
|
23
|
+
[/^(oxen)$/i, "$1"],
|
|
24
|
+
[/(quiz)$/i, "$1zes"]
|
|
25
|
+
],
|
|
26
|
+
singular: [
|
|
27
|
+
[/s$/i, ""],
|
|
28
|
+
[/(ss)$/i, "$1"],
|
|
29
|
+
[/(n)ews$/i, "$1ews"],
|
|
30
|
+
[/([ti])a$/i, "$1um"],
|
|
31
|
+
[/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, "$1sis"],
|
|
32
|
+
[/(^analy)(sis|ses)$/i, "$1sis"],
|
|
33
|
+
[/([^f])ves$/i, "$1fe"],
|
|
34
|
+
[/(hive)s$/i, "$1"],
|
|
35
|
+
[/(tive)s$/i, "$1"],
|
|
36
|
+
[/([lr])ves$/i, "$1f"],
|
|
37
|
+
[/([^aeiouy]|qu)ies$/i, "$1y"],
|
|
38
|
+
[/(s)eries$/i, "$1eries"],
|
|
39
|
+
[/(m)ovies$/i, "$1ovie"],
|
|
40
|
+
[/(x|ch|ss|sh)es$/i, "$1"],
|
|
41
|
+
[/^(m|l)ice$/i, "$1ouse"],
|
|
42
|
+
[/(tus|bus)(es)?$/i, "$1"],
|
|
43
|
+
[/(o)es$/i, "$1"],
|
|
44
|
+
[/(shoe)s$/i, "$1"],
|
|
45
|
+
[/(cris|test)(is|es)$/i, "$1is"],
|
|
46
|
+
[/^(a)x[ie]s$/i, "$1xis"],
|
|
47
|
+
[/(octop|vir)(us|i)$/i, "$1us"],
|
|
48
|
+
[/(alias|status|bonus)(es)?$/i, "$1"],
|
|
49
|
+
[/^(ox)en/i, "$1"],
|
|
50
|
+
[/(vert|ind)ices$/i, "$1ex"],
|
|
51
|
+
[/(matr)ices$/i, "$1ix"],
|
|
52
|
+
[/(quiz)zes$/i, "$1"],
|
|
53
|
+
[/(database)s$/i, "$1"]
|
|
54
|
+
],
|
|
55
|
+
irregularPairs: [
|
|
56
|
+
["person", "people"],
|
|
57
|
+
["man", "men"],
|
|
58
|
+
["child", "children"],
|
|
59
|
+
["sex", "sexes"],
|
|
60
|
+
["move", "moves"],
|
|
61
|
+
["cow", "kine"],
|
|
62
|
+
["zombie", "zombies"]
|
|
63
|
+
],
|
|
64
|
+
uncountable: [
|
|
65
|
+
"equipment",
|
|
66
|
+
"information",
|
|
67
|
+
"rice",
|
|
68
|
+
"money",
|
|
69
|
+
"species",
|
|
70
|
+
"series",
|
|
71
|
+
"fish",
|
|
72
|
+
"sheep",
|
|
73
|
+
"jeans",
|
|
74
|
+
"police"
|
|
75
|
+
]
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { defaultRules as t };
|
|
80
|
+
//# sourceMappingURL=inflections-DzBsjJqj.js.map
|