@prismatic-io/spectral 10.17.2 → 10.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clients/http/index.d.ts +84 -16
- package/dist/clients/http/index.js +85 -17
- package/dist/component.d.ts +3 -3
- package/dist/component.js +8 -8
- package/dist/conditionalLogic/index.js +3 -3
- package/dist/errors.d.ts +67 -0
- package/dist/errors.js +67 -0
- package/dist/generators/cniComponentManifest/cli.js +5 -5
- package/dist/generators/cniComponentManifest/index.d.ts +1 -1
- package/dist/generators/componentManifest/cli.js +6 -6
- package/dist/generators/componentManifest/createActions.d.ts +1 -1
- package/dist/generators/componentManifest/createActions.js +4 -4
- package/dist/generators/componentManifest/createConnections.d.ts +1 -1
- package/dist/generators/componentManifest/createConnections.js +4 -4
- package/dist/generators/componentManifest/createDataSources.d.ts +1 -1
- package/dist/generators/componentManifest/createDataSources.js +4 -4
- package/dist/generators/componentManifest/createStaticFiles.d.ts +1 -1
- package/dist/generators/componentManifest/createStaticFiles.js +1 -1
- package/dist/generators/componentManifest/createTriggers.d.ts +1 -1
- package/dist/generators/componentManifest/createTriggers.js +4 -4
- package/dist/generators/componentManifest/helpers.js +1 -1
- package/dist/generators/componentManifest/index.d.ts +2 -2
- package/dist/generators/componentManifest/index.js +1 -1
- package/dist/generators/utils/escapeSpecialCharacters.js +1 -1
- package/dist/index.d.ts +543 -53
- package/dist/index.js +545 -55
- package/dist/integration.d.ts +2 -2
- package/dist/integration.js +9 -9
- package/dist/serverTypes/asyncContext.d.ts +1 -1
- package/dist/serverTypes/context.d.ts +1 -1
- package/dist/serverTypes/context.js +2 -2
- package/dist/serverTypes/convertComponent.d.ts +2 -2
- package/dist/serverTypes/convertComponent.js +2 -2
- package/dist/serverTypes/convertIntegration.d.ts +3 -3
- package/dist/serverTypes/convertIntegration.js +13 -13
- package/dist/serverTypes/index.d.ts +2 -7
- package/dist/serverTypes/perform.d.ts +1 -1
- package/dist/serverTypes/perform.js +1 -1
- package/dist/testing.d.ts +195 -31
- package/dist/testing.js +199 -33
- package/dist/types/ActionPerformFunction.d.ts +6 -6
- package/dist/types/ConfigVars.d.ts +6 -6
- package/dist/types/ConnectionDefinition.d.ts +1 -1
- package/dist/types/FlowAttributes.d.ts +1 -1
- package/dist/types/index.d.ts +20 -20
- package/dist/types/index.js +20 -20
- package/dist/types/typeExportComponent.d.ts +15 -15
- package/dist/types/typeExportComponent.js +18 -18
- package/dist/types/typeExportIntegration.d.ts +17 -17
- package/dist/types/typeExportIntegration.js +17 -17
- package/dist/util.d.ts +29 -14
- package/dist/util.js +366 -108
- package/package.json +26 -28
package/dist/util.js
CHANGED
|
@@ -23,11 +23,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
exports.toObject = exports.lowerCaseHeaders = exports.isObjectWithTruthyKeys = exports.isObjectWithOneTruthyKey = void 0;
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
const isValid_1 =
|
|
29
|
-
const
|
|
30
|
-
const fromUnixTime_1 = __importDefault(require("date-fns/fromUnixTime"));
|
|
26
|
+
const fromUnixTime_1 = require("date-fns/fromUnixTime");
|
|
27
|
+
const isDate_1 = require("date-fns/isDate");
|
|
28
|
+
const isValid_1 = require("date-fns/isValid");
|
|
29
|
+
const parseISO_1 = require("date-fns/parseISO");
|
|
31
30
|
const omitBy_1 = __importDefault(require("lodash/omitBy"));
|
|
32
31
|
const safe_stable_stringify_1 = require("safe-stable-stringify");
|
|
33
32
|
const valid_url_1 = require("valid-url");
|
|
@@ -45,14 +44,28 @@ const isObjectWithTruthyKeys = (value, keys) => {
|
|
|
45
44
|
exports.isObjectWithTruthyKeys = isObjectWithTruthyKeys;
|
|
46
45
|
/**
|
|
47
46
|
* This function checks if value is an Element.
|
|
48
|
-
*
|
|
47
|
+
*
|
|
49
48
|
* @param value The variable to test.
|
|
50
49
|
* @returns This function returns true or false, depending on if `value` is an Element.
|
|
50
|
+
* @example
|
|
51
|
+
* import { util } from "@prismatic-io/spectral";
|
|
52
|
+
*
|
|
53
|
+
* util.types.isElement({ key: "foo" }); // true
|
|
54
|
+
* util.types.isElement({ key: "foo", label: "Foo" }); // true
|
|
55
|
+
* util.types.isElement("foo"); // false
|
|
56
|
+
* util.types.isElement({}); // false
|
|
51
57
|
*/
|
|
52
58
|
const isElement = (value) => (0, exports.isObjectWithTruthyKeys)(value, ["key"]);
|
|
53
59
|
/**
|
|
60
|
+
* Checks if a value is a valid ObjectSelection (an array of objects each with an `object` property).
|
|
61
|
+
*
|
|
54
62
|
* @param value The value to test
|
|
55
63
|
* @returns This function returns true if the type of `value` is an ObjectSelection, or false otherwise.
|
|
64
|
+
* @example
|
|
65
|
+
* import { util } from "@prismatic-io/spectral";
|
|
66
|
+
*
|
|
67
|
+
* util.types.isObjectSelection([{ object: { key: "account" } }]); // true
|
|
68
|
+
* util.types.isObjectSelection("not an object selection"); // false
|
|
56
69
|
*/
|
|
57
70
|
const isObjectSelection = (value) => {
|
|
58
71
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -62,8 +75,22 @@ const isObjectSelection = (value) => {
|
|
|
62
75
|
};
|
|
63
76
|
/**
|
|
64
77
|
* This function coerces a provided value into an ObjectSelection if possible.
|
|
78
|
+
* If the value is a JSON string it will be parsed first. Throws an error if
|
|
79
|
+
* the value cannot be coerced.
|
|
80
|
+
*
|
|
65
81
|
* @param value The value to coerce to ObjectSelection.
|
|
66
|
-
* @returns This function returns the
|
|
82
|
+
* @returns This function returns the value as an ObjectSelection if possible.
|
|
83
|
+
* @example
|
|
84
|
+
* import { util } from "@prismatic-io/spectral";
|
|
85
|
+
*
|
|
86
|
+
* const selection = util.types.toObjectSelection([
|
|
87
|
+
* { object: { key: "account", label: "Account" }, fields: [{ key: "name" }] },
|
|
88
|
+
* ]);
|
|
89
|
+
*
|
|
90
|
+
* // Also accepts a JSON string representation
|
|
91
|
+
* const fromString = util.types.toObjectSelection(
|
|
92
|
+
* '[{"object":{"key":"account"}}]'
|
|
93
|
+
* );
|
|
67
94
|
*/
|
|
68
95
|
const toObjectSelection = (value) => {
|
|
69
96
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -75,8 +102,18 @@ const toObjectSelection = (value) => {
|
|
|
75
102
|
throw new Error(`Value '${typeof value === "string" ? value : JSON.stringify(value)}' cannot be coerced to ObjectSelection.`);
|
|
76
103
|
};
|
|
77
104
|
/**
|
|
105
|
+
* Checks if a value is a valid ObjectFieldMap (an object with a `fields` array,
|
|
106
|
+
* where each field has a `field` property that is an Element).
|
|
107
|
+
*
|
|
78
108
|
* @param value The value to test
|
|
79
109
|
* @returns This function returns true if the type of `value` is an ObjectFieldMap, or false otherwise.
|
|
110
|
+
* @example
|
|
111
|
+
* import { util } from "@prismatic-io/spectral";
|
|
112
|
+
*
|
|
113
|
+
* util.types.isObjectFieldMap({
|
|
114
|
+
* fields: [{ field: { key: "name", label: "Name" } }],
|
|
115
|
+
* }); // true
|
|
116
|
+
* util.types.isObjectFieldMap("not a field map"); // false
|
|
80
117
|
*/
|
|
81
118
|
const isObjectFieldMap = (value) => {
|
|
82
119
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -92,8 +129,19 @@ const isObjectFieldMap = (value) => {
|
|
|
92
129
|
};
|
|
93
130
|
/**
|
|
94
131
|
* This function coerces a provided value into an ObjectFieldMap if possible.
|
|
132
|
+
* If the value is a JSON string it will be parsed first. Throws an error if
|
|
133
|
+
* the value cannot be coerced.
|
|
134
|
+
*
|
|
95
135
|
* @param value The value to coerce to ObjectFieldMap.
|
|
96
|
-
* @returns This function returns the
|
|
136
|
+
* @returns This function returns the value as an ObjectFieldMap if possible.
|
|
137
|
+
* @example
|
|
138
|
+
* import { util } from "@prismatic-io/spectral";
|
|
139
|
+
*
|
|
140
|
+
* const fieldMap = util.types.toObjectFieldMap({
|
|
141
|
+
* fields: [
|
|
142
|
+
* { field: { key: "email", label: "Email" }, mappedField: { key: "contact_email" } },
|
|
143
|
+
* ],
|
|
144
|
+
* });
|
|
97
145
|
*/
|
|
98
146
|
const toObjectFieldMap = (value) => {
|
|
99
147
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -105,8 +153,19 @@ const toObjectFieldMap = (value) => {
|
|
|
105
153
|
throw new Error(`Value '${typeof value === "string" ? value : JSON.stringify(value)}' cannot be coerced to ObjectFieldMap.`);
|
|
106
154
|
};
|
|
107
155
|
/**
|
|
156
|
+
* Checks if a value is a valid JSONForm (an object with `schema`, `uiSchema`, and `data` properties).
|
|
157
|
+
*
|
|
108
158
|
* @param value The value to test
|
|
109
159
|
* @returns This function returns true if the type of `value` is a JSONForm, or false otherwise.
|
|
160
|
+
* @example
|
|
161
|
+
* import { util } from "@prismatic-io/spectral";
|
|
162
|
+
*
|
|
163
|
+
* util.types.isJSONForm({
|
|
164
|
+
* schema: { type: "object", properties: { name: { type: "string" } } },
|
|
165
|
+
* uiSchema: { type: "VerticalLayout", elements: [] },
|
|
166
|
+
* data: { name: "Example" },
|
|
167
|
+
* }); // true
|
|
168
|
+
* util.types.isJSONForm({ schema: {} }); // false (missing uiSchema and data)
|
|
110
169
|
*/
|
|
111
170
|
const isJSONForm = (value) => {
|
|
112
171
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -116,8 +175,19 @@ const isJSONForm = (value) => {
|
|
|
116
175
|
};
|
|
117
176
|
/**
|
|
118
177
|
* This function coerces a provided value into a JSONForm if possible.
|
|
178
|
+
* If the value is a JSON string it will be parsed first. Throws an error if
|
|
179
|
+
* the value cannot be coerced.
|
|
180
|
+
*
|
|
119
181
|
* @param value The value to coerce to JSONForm.
|
|
120
|
-
* @returns This function returns the
|
|
182
|
+
* @returns This function returns the value as a JSONForm if possible.
|
|
183
|
+
* @example
|
|
184
|
+
* import { util } from "@prismatic-io/spectral";
|
|
185
|
+
*
|
|
186
|
+
* const form = util.types.toJSONForm({
|
|
187
|
+
* schema: { type: "object", properties: { name: { type: "string" } } },
|
|
188
|
+
* uiSchema: { type: "VerticalLayout", elements: [] },
|
|
189
|
+
* data: { name: "Default Name" },
|
|
190
|
+
* });
|
|
121
191
|
*/
|
|
122
192
|
const toJSONForm = (value) => {
|
|
123
193
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -131,11 +201,15 @@ const toJSONForm = (value) => {
|
|
|
131
201
|
/**
|
|
132
202
|
* Determine if a variable is a boolean (true or false).
|
|
133
203
|
*
|
|
134
|
-
* - `util.types.isBool(false)` will return `true`, since `false` is a boolean.
|
|
135
|
-
* - `util.types.isBool("Hello")` will return `false`, since `"Hello"` is not a boolean.
|
|
136
|
-
*
|
|
137
204
|
* @param value The variable to test.
|
|
138
205
|
* @returns True if the value is a boolean, or false otherwise.
|
|
206
|
+
* @example
|
|
207
|
+
* import { util } from "@prismatic-io/spectral";
|
|
208
|
+
*
|
|
209
|
+
* util.types.isBool(false); // true
|
|
210
|
+
* util.types.isBool(true); // true
|
|
211
|
+
* util.types.isBool("Hello"); // false
|
|
212
|
+
* util.types.isBool(0); // false
|
|
139
213
|
*/
|
|
140
214
|
const isBool = (value) => value === true || value === false;
|
|
141
215
|
/**
|
|
@@ -144,11 +218,19 @@ const isBool = (value) => value === true || value === false;
|
|
|
144
218
|
* Truthy/falsy checks are case-insensitive.
|
|
145
219
|
*
|
|
146
220
|
* In the event that `value` is undefined or an empty string, a default value can be provided.
|
|
147
|
-
* For example, `util.types.toBool('', true)` will return `true`.
|
|
148
221
|
*
|
|
149
222
|
* @param value The value to convert to a boolean.
|
|
150
223
|
* @param defaultValue The value to return if `value` is undefined or an empty string.
|
|
151
224
|
* @returns The boolean equivalent of the truthy or falsy `value`.
|
|
225
|
+
* @example
|
|
226
|
+
* import { util } from "@prismatic-io/spectral";
|
|
227
|
+
*
|
|
228
|
+
* util.types.toBool("true"); // true
|
|
229
|
+
* util.types.toBool("YES"); // true
|
|
230
|
+
* util.types.toBool("f"); // false
|
|
231
|
+
* util.types.toBool("no"); // false
|
|
232
|
+
* util.types.toBool("", true); // true (uses default)
|
|
233
|
+
* util.types.toBool(undefined, false); // false (uses default)
|
|
152
234
|
*/
|
|
153
235
|
const toBool = (value, defaultValue) => {
|
|
154
236
|
if (isBool(value)) {
|
|
@@ -167,22 +249,34 @@ const toBool = (value, defaultValue) => {
|
|
|
167
249
|
};
|
|
168
250
|
/**
|
|
169
251
|
* This function checks if value is an integer.
|
|
170
|
-
*
|
|
252
|
+
*
|
|
171
253
|
* @param value The variable to test.
|
|
172
254
|
* @returns This function returns true or false, depending on if `value` is an integer.
|
|
255
|
+
* @example
|
|
256
|
+
* import { util } from "@prismatic-io/spectral";
|
|
257
|
+
*
|
|
258
|
+
* util.types.isInt(5); // true
|
|
259
|
+
* util.types.isInt(-3); // true
|
|
260
|
+
* util.types.isInt("5"); // false (string, not a number)
|
|
261
|
+
* util.types.isInt(5.5); // false (float, not an integer)
|
|
173
262
|
*/
|
|
174
263
|
const isInt = (value) => Number.isInteger(value);
|
|
175
264
|
/**
|
|
176
265
|
* This function converts a variable to an integer if possible.
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* In the event that `value` is undefined or an empty string, a default value can be provided.
|
|
180
|
-
* For example, `util.types.toInt('', 1)` will return `1`.
|
|
266
|
+
* Floats are truncated (not rounded). Throws an error if `value`
|
|
267
|
+
* cannot be coerced and no `defaultValue` is provided.
|
|
181
268
|
*
|
|
182
|
-
* This function will throw an exception if `value` cannot be coerced to an integer.
|
|
183
269
|
* @param value The value to convert to an integer.
|
|
184
270
|
* @param defaultValue The value to return if `value` is undefined, an empty string, or not able to be coerced.
|
|
185
271
|
* @returns This function returns an integer if possible.
|
|
272
|
+
* @example
|
|
273
|
+
* import { util } from "@prismatic-io/spectral";
|
|
274
|
+
*
|
|
275
|
+
* util.types.toInt(5.5); // 5
|
|
276
|
+
* util.types.toInt("20.3"); // 20
|
|
277
|
+
* util.types.toInt("", 1); // 1 (uses default)
|
|
278
|
+
* util.types.toInt(undefined, 42); // 42 (uses default)
|
|
279
|
+
* util.types.toInt("abc"); // throws Error
|
|
186
280
|
*/
|
|
187
281
|
const toInt = (value, defaultValue) => {
|
|
188
282
|
if (isInt(value))
|
|
@@ -192,7 +286,7 @@ const toInt = (value, defaultValue) => {
|
|
|
192
286
|
return ~~value;
|
|
193
287
|
}
|
|
194
288
|
if (typeof value === "string") {
|
|
195
|
-
const intValue = Number.parseInt(value);
|
|
289
|
+
const intValue = Number.parseInt(value, 10);
|
|
196
290
|
if (!Number.isNaN(intValue)) {
|
|
197
291
|
return intValue;
|
|
198
292
|
}
|
|
@@ -208,27 +302,32 @@ const toInt = (value, defaultValue) => {
|
|
|
208
302
|
/**
|
|
209
303
|
* Determine if a variable is a number, or can easily be coerced into a number.
|
|
210
304
|
*
|
|
211
|
-
* - `util.types.isNumber(3.21)` will return `true`, since `3.21` is a number.
|
|
212
|
-
* - `util.types.isBool("5.5")` will return `true`, since the string `"5.5"` can easily be coerced into a number.
|
|
213
|
-
* - `util.types.isBool("Hello")` will return `false`, since `"Hello"` is not a number.
|
|
214
|
-
*
|
|
215
305
|
* @param value The variable to test.
|
|
216
306
|
* @returns This function returns true if `value` can easily be coerced into a number, and false otherwise.
|
|
307
|
+
* @example
|
|
308
|
+
* import { util } from "@prismatic-io/spectral";
|
|
309
|
+
*
|
|
310
|
+
* util.types.isNumber(3.21); // true
|
|
311
|
+
* util.types.isNumber("5.5"); // true (string is numeric)
|
|
312
|
+
* util.types.isNumber("Hello"); // false
|
|
217
313
|
*/
|
|
218
314
|
const isNumber = (value) => !Number.isNaN(Number(value));
|
|
219
315
|
/**
|
|
220
316
|
* This function coerces a value (number or string) into a number.
|
|
221
|
-
* In the event that `value` is undefined or an empty string, a `defaultValue` can be provided, or zero will be returned.
|
|
317
|
+
* In the event that `value` is undefined, null, or an empty string, a `defaultValue` can be provided, or zero will be returned.
|
|
222
318
|
* If `value` is not able to be coerced into a number but is defined, an error will be thrown.
|
|
223
319
|
*
|
|
224
|
-
* - `util.types.toNumber("3.22")` will return the number `3.22`.
|
|
225
|
-
* - `util.types.toNumber("", 5.5)` will return the default value `5.5`, since `value` was an empty string.
|
|
226
|
-
* - `util.types.toNumber(null, 5.5)` will return the default value `5.5`, since `value` was `null`.
|
|
227
|
-
* - `util.types.toNumber(undefined)` will return `0`, since `value` was undefined and no `defaultValue` was given.
|
|
228
|
-
* - `util.types.toNumber("Hello")` will throw an error, since the string `"Hello"` cannot be coerced into a number.
|
|
229
320
|
* @param value The value to turn into a number.
|
|
230
|
-
* @param defaultValue The value to return if `value` is undefined or an empty string.
|
|
321
|
+
* @param defaultValue The value to return if `value` is undefined, null, or an empty string.
|
|
231
322
|
* @returns This function returns the numerical version of `value` if possible, or the `defaultValue` if `value` is undefined or an empty string.
|
|
323
|
+
* @example
|
|
324
|
+
* import { util } from "@prismatic-io/spectral";
|
|
325
|
+
*
|
|
326
|
+
* util.types.toNumber("3.22"); // 3.22
|
|
327
|
+
* util.types.toNumber("", 5.5); // 5.5 (uses default)
|
|
328
|
+
* util.types.toNumber(null, 5.5); // 5.5 (uses default)
|
|
329
|
+
* util.types.toNumber(undefined); // 0 (no default given)
|
|
330
|
+
* util.types.toNumber("Hello"); // throws Error
|
|
232
331
|
*/
|
|
233
332
|
const toNumber = (value, defaultValue) => {
|
|
234
333
|
if (typeof value === "undefined" || value === "" || value === null) {
|
|
@@ -240,20 +339,33 @@ const toNumber = (value, defaultValue) => {
|
|
|
240
339
|
throw new Error(`Value '${value}' cannot be coerced to a number.`);
|
|
241
340
|
};
|
|
242
341
|
/**
|
|
342
|
+
* Checks if a value is a bigint.
|
|
343
|
+
*
|
|
243
344
|
* @param value The value to test
|
|
244
345
|
* @returns This function returns true if the type of `value` is a bigint, or false otherwise.
|
|
346
|
+
* @example
|
|
347
|
+
* import { util } from "@prismatic-io/spectral";
|
|
348
|
+
*
|
|
349
|
+
* util.types.isBigInt(3n); // true
|
|
350
|
+
* util.types.isBigInt(3); // false (number, not bigint)
|
|
351
|
+
* util.types.isBigInt("3"); // false
|
|
245
352
|
*/
|
|
246
353
|
const isBigInt = (value) => typeof value === "bigint";
|
|
247
354
|
/**
|
|
248
355
|
* This function coerces a provided value into a bigint if possible.
|
|
249
356
|
* The provided `value` must be a bigint, integer, string representing an integer, or a boolean.
|
|
357
|
+
* Throws an error if the value cannot be coerced.
|
|
250
358
|
*
|
|
251
|
-
* - `util.types.toBigInt(3)` will return `3n`.
|
|
252
|
-
* - `util.types.toBigInt("-5")` will return `-5n`.
|
|
253
|
-
* - `util.types.toBigInt(true)` will return `1n` (and `false` will return `0n`).
|
|
254
|
-
* - `util.types.toBigInt("5.5")` will throw an error, as `5.5` is not an integer.
|
|
255
359
|
* @param value The value to coerce to bigint.
|
|
256
360
|
* @returns This function returns the bigint representation of `value`.
|
|
361
|
+
* @example
|
|
362
|
+
* import { util } from "@prismatic-io/spectral";
|
|
363
|
+
*
|
|
364
|
+
* util.types.toBigInt(3); // 3n
|
|
365
|
+
* util.types.toBigInt("-5"); // -5n
|
|
366
|
+
* util.types.toBigInt(true); // 1n
|
|
367
|
+
* util.types.toBigInt(false); // 0n
|
|
368
|
+
* util.types.toBigInt("5.5"); // throws Error (not an integer)
|
|
257
369
|
*/
|
|
258
370
|
const toBigInt = (value) => {
|
|
259
371
|
if (isBigInt(value)) {
|
|
@@ -262,33 +374,54 @@ const toBigInt = (value) => {
|
|
|
262
374
|
try {
|
|
263
375
|
return BigInt(toString(value));
|
|
264
376
|
}
|
|
265
|
-
catch (
|
|
377
|
+
catch (_error) {
|
|
266
378
|
throw new Error(`Value '${value}' cannot be coerced to bigint.`);
|
|
267
379
|
}
|
|
268
380
|
};
|
|
269
|
-
/** This function returns true if `value` is a variable of type `Date`, and false otherwise. */
|
|
270
|
-
const isDate = (value) => (0, isDate_1.default)(value);
|
|
271
381
|
/**
|
|
272
|
-
* This function
|
|
273
|
-
* cannot be coerced into a Date object.
|
|
382
|
+
* This function returns true if `value` is a variable of type `Date`, and false otherwise.
|
|
274
383
|
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
384
|
+
* @param value The variable to test.
|
|
385
|
+
* @returns True if value is a Date object, false otherwise.
|
|
386
|
+
* @example
|
|
387
|
+
* import { util } from "@prismatic-io/spectral";
|
|
388
|
+
*
|
|
389
|
+
* util.types.isDate(new Date()); // true
|
|
390
|
+
* util.types.isDate("2021-03-20"); // false (string, not Date)
|
|
391
|
+
* util.types.isDate(1616198400); // false (number, not Date)
|
|
392
|
+
*/
|
|
393
|
+
const isDate = (value) => (0, isDate_1.isDate)(value);
|
|
394
|
+
/**
|
|
395
|
+
* This function parses an ISO date string or UNIX epoch timestamp if possible,
|
|
396
|
+
* or throws an error if the value provided cannot be coerced into a Date object.
|
|
397
|
+
*
|
|
398
|
+
* @param value The value to turn into a date. Accepts a Date object, ISO date string, or UNIX epoch timestamp (seconds).
|
|
280
399
|
* @returns The date equivalent of `value`.
|
|
400
|
+
* @example
|
|
401
|
+
* import { util } from "@prismatic-io/spectral";
|
|
402
|
+
*
|
|
403
|
+
* // Pass through an existing Date object
|
|
404
|
+
* util.types.toDate(new Date("1995-12-17T03:24:00"));
|
|
405
|
+
*
|
|
406
|
+
* // Parse an ISO date string
|
|
407
|
+
* util.types.toDate("2021-03-20"); // Date object for 2021-03-20
|
|
408
|
+
*
|
|
409
|
+
* // Parse a UNIX epoch timestamp (in seconds)
|
|
410
|
+
* util.types.toDate(1616198400); // Date object for 2021-03-20
|
|
411
|
+
*
|
|
412
|
+
* // Invalid input throws an error
|
|
413
|
+
* util.types.toDate("not-a-date"); // throws Error
|
|
281
414
|
*/
|
|
282
415
|
const toDate = (value) => {
|
|
283
416
|
if (isDate(value)) {
|
|
284
417
|
return value;
|
|
285
418
|
}
|
|
286
419
|
if (isNumber(value) && toNumber(value)) {
|
|
287
|
-
return (0, fromUnixTime_1.
|
|
420
|
+
return (0, fromUnixTime_1.fromUnixTime)(toNumber(value));
|
|
288
421
|
}
|
|
289
422
|
if (typeof value === "string") {
|
|
290
|
-
const dt = (0, parseISO_1.
|
|
291
|
-
if ((0, isValid_1.
|
|
423
|
+
const dt = (0, parseISO_1.parseISO)(value);
|
|
424
|
+
if ((0, isValid_1.isValid)(dt)) {
|
|
292
425
|
return dt;
|
|
293
426
|
}
|
|
294
427
|
}
|
|
@@ -299,40 +432,77 @@ const toDate = (value) => {
|
|
|
299
432
|
* Note: this function only tests that the string is a syntactically correct URL; it does not check
|
|
300
433
|
* if the URL is web accessible.
|
|
301
434
|
*
|
|
302
|
-
* - `util.types.isUrl('https://prismatic.io')` will return true.
|
|
303
|
-
* - `util.types.isUrl('https:://prismatic.io')` will return false due to the extraneous `:` symbol.
|
|
304
435
|
* @param value The URL to test.
|
|
305
436
|
* @returns This function returns true if `value` is a valid URL, and false otherwise.
|
|
437
|
+
* @example
|
|
438
|
+
* import { util } from "@prismatic-io/spectral";
|
|
439
|
+
*
|
|
440
|
+
* util.types.isUrl("https://prismatic.io"); // true
|
|
441
|
+
* util.types.isUrl("http://example.com/path?q=1"); // true
|
|
442
|
+
* util.types.isUrl("https:://prismatic.io"); // false (malformed)
|
|
443
|
+
* util.types.isUrl("not a url"); // false
|
|
306
444
|
*/
|
|
307
445
|
const isUrl = (value) => (0, valid_url_1.isWebUri)(value) !== undefined;
|
|
308
446
|
/**
|
|
309
|
-
* This function checks if value is a valid picklist.
|
|
310
|
-
*
|
|
311
|
-
* - `util.types.isPicklist(["value", new String("value")])` will return `true`.
|
|
447
|
+
* This function checks if value is a valid picklist. A picklist is an array
|
|
448
|
+
* of strings or an array of Element objects (objects with a `key` property).
|
|
312
449
|
*
|
|
313
450
|
* @param value The variable to test.
|
|
314
451
|
* @returns This function returns true if `value` is a valid picklist.
|
|
452
|
+
* @example
|
|
453
|
+
* import { util } from "@prismatic-io/spectral";
|
|
454
|
+
*
|
|
455
|
+
* util.types.isPicklist(["option1", "option2"]); // true
|
|
456
|
+
* util.types.isPicklist([{ key: "a", label: "Option A" }]); // true
|
|
457
|
+
* util.types.isPicklist("not a picklist"); // false
|
|
458
|
+
* util.types.isPicklist([1, 2, 3]); // false
|
|
315
459
|
*/
|
|
316
460
|
const isPicklist = (value) => Array.isArray(value) && (value.every(isString) || value.every(isElement));
|
|
317
461
|
/**
|
|
318
|
-
* This function checks if value is a valid schedule.
|
|
319
|
-
*
|
|
320
|
-
* - `util.types.isSchedule({value: "00 00 * * 2,3"})` will return `true`.
|
|
321
|
-
* - `util.types.isSchedule({value: "00 00 * * 2,3", scheduleType: "week", timeZone: "America/Chicago"})` will return `true`.
|
|
462
|
+
* This function checks if value is a valid schedule (an object with a truthy `value` property).
|
|
322
463
|
*
|
|
323
464
|
* @param value The variable to test.
|
|
324
465
|
* @returns This function returns true if `value` is a valid schedule.
|
|
466
|
+
* @example
|
|
467
|
+
* import { util } from "@prismatic-io/spectral";
|
|
468
|
+
*
|
|
469
|
+
* util.types.isSchedule({ value: "00 00 * * 2,3" }); // true
|
|
470
|
+
* util.types.isSchedule({
|
|
471
|
+
* value: "00 00 * * 2,3",
|
|
472
|
+
* schedule_type: "week",
|
|
473
|
+
* time_zone: "America/Chicago",
|
|
474
|
+
* }); // true
|
|
475
|
+
* util.types.isSchedule("not a schedule"); // false
|
|
325
476
|
*/
|
|
326
477
|
const isSchedule = (value) => (0, exports.isObjectWithTruthyKeys)(value, ["value"]);
|
|
327
478
|
/**
|
|
328
479
|
* This function helps to transform key-value lists to objects.
|
|
329
|
-
* This is useful for transforming inputs that
|
|
480
|
+
* This is useful for transforming inputs that use the `keyvaluelist` collection type
|
|
481
|
+
* into plain objects.
|
|
330
482
|
*
|
|
331
|
-
* For example, an input that is a collection might return `[{key: "foo", value: "bar"},{key: "baz", value: 5}]`.
|
|
332
|
-
* If that array were passed into `util.types.keyValPairListToObject()`, an object would be returned of the form
|
|
333
|
-
* `{foo: "bar", baz: 5}`.
|
|
334
483
|
* @param kvpList An array of objects with `key` and `value` properties.
|
|
335
484
|
* @param valueConverter Optional function to call for each `value`.
|
|
485
|
+
* @returns A plain object with keys and values from the input array.
|
|
486
|
+
* @example
|
|
487
|
+
* import { util } from "@prismatic-io/spectral";
|
|
488
|
+
*
|
|
489
|
+
* const headers = [
|
|
490
|
+
* { key: "Content-Type", value: "application/json" },
|
|
491
|
+
* { key: "Authorization", value: "Bearer abc123" },
|
|
492
|
+
* ];
|
|
493
|
+
* util.types.keyValPairListToObject(headers);
|
|
494
|
+
* // { "Content-Type": "application/json", "Authorization": "Bearer abc123" }
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* import { util } from "@prismatic-io/spectral";
|
|
498
|
+
*
|
|
499
|
+
* // With a value converter
|
|
500
|
+
* const params = [
|
|
501
|
+
* { key: "limit", value: "10" },
|
|
502
|
+
* { key: "offset", value: "0" },
|
|
503
|
+
* ];
|
|
504
|
+
* util.types.keyValPairListToObject(params, (v) => Number(v));
|
|
505
|
+
* // { limit: 10, offset: 0 }
|
|
336
506
|
*/
|
|
337
507
|
const keyValPairListToObject = (kvpList, valueConverter) => {
|
|
338
508
|
return (kvpList || []).reduce((result, { key, value }) => (Object.assign(Object.assign({}, result), { [key]: valueConverter ? valueConverter(value) : value })), {});
|
|
@@ -343,19 +513,40 @@ const keyValPairListToObject = (kvpList, valueConverter) => {
|
|
|
343
513
|
*
|
|
344
514
|
* @param value The value to test
|
|
345
515
|
* @returns This function returns true if `value` is a DataPayload object, and false otherwise.
|
|
516
|
+
* @example
|
|
517
|
+
* import { util } from "@prismatic-io/spectral";
|
|
518
|
+
*
|
|
519
|
+
* util.types.isBufferDataPayload({
|
|
520
|
+
* data: Buffer.from("hello"),
|
|
521
|
+
* contentType: "text/plain",
|
|
522
|
+
* }); // true
|
|
523
|
+
* util.types.isBufferDataPayload("hello"); // false
|
|
524
|
+
* util.types.isBufferDataPayload({ data: "not a buffer" }); // false
|
|
346
525
|
*/
|
|
347
526
|
const isBufferDataPayload = (value) => value instanceof Object && "data" in value && Buffer.isBuffer(value.data);
|
|
348
527
|
/**
|
|
349
|
-
* Many libraries for third-party
|
|
350
|
-
* This function helps to convert strings, Uint8Arrays, and
|
|
351
|
-
* that has a Buffer and a string representing `contentType`.
|
|
528
|
+
* Many libraries for third-party APIs that handle binary files expect `Buffer` objects.
|
|
529
|
+
* This function helps to convert strings, Uint8Arrays, objects, and arrays to a
|
|
530
|
+
* `DataPayload` structure that has a Buffer and a string representing `contentType`.
|
|
352
531
|
*
|
|
353
|
-
*
|
|
354
|
-
* `const { data, contentType } = util.types.toBufferDataPayload(someData);`
|
|
532
|
+
* Throws an error if `value` cannot be converted to a Buffer.
|
|
355
533
|
*
|
|
356
|
-
*
|
|
357
|
-
* @
|
|
358
|
-
* @
|
|
534
|
+
* @param value The string, Buffer, Uint8Array, object, or Array to convert to a Buffer.
|
|
535
|
+
* @returns An object with two keys: `data` (a `Buffer`) and `contentType` (a string).
|
|
536
|
+
* @example
|
|
537
|
+
* import { util } from "@prismatic-io/spectral";
|
|
538
|
+
*
|
|
539
|
+
* // Convert a string (becomes text/plain)
|
|
540
|
+
* const { data, contentType } = util.types.toBufferDataPayload("Hello, world!");
|
|
541
|
+
* // data: Buffer, contentType: "text/plain"
|
|
542
|
+
*
|
|
543
|
+
* // Convert an object (becomes application/json)
|
|
544
|
+
* const jsonPayload = util.types.toBufferDataPayload({ key: "value" });
|
|
545
|
+
* // jsonPayload.contentType === "application/json"
|
|
546
|
+
*
|
|
547
|
+
* // Pass through an existing DataPayload
|
|
548
|
+
* const existing = { data: Buffer.from("binary"), contentType: "application/octet-stream" };
|
|
549
|
+
* util.types.toBufferDataPayload(existing); // returns as-is
|
|
359
550
|
*/
|
|
360
551
|
const toBufferDataPayload = (value) => {
|
|
361
552
|
if (isBufferDataPayload(value)) {
|
|
@@ -411,28 +602,51 @@ const isData = (value) => isBufferDataPayload(value);
|
|
|
411
602
|
const toData = (value) => toBufferDataPayload(value);
|
|
412
603
|
/**
|
|
413
604
|
* This function checks if value is a string.
|
|
414
|
-
*
|
|
605
|
+
*
|
|
415
606
|
* @param value The variable to test.
|
|
416
607
|
* @returns This function returns true or false, depending on if `value` is a string.
|
|
608
|
+
* @example
|
|
609
|
+
* import { util } from "@prismatic-io/spectral";
|
|
610
|
+
*
|
|
611
|
+
* util.types.isString("value"); // true
|
|
612
|
+
* util.types.isString(new String("value")); // true
|
|
613
|
+
* util.types.isString(123); // false
|
|
614
|
+
* util.types.isString(null); // false
|
|
417
615
|
*/
|
|
418
616
|
const isString = (value) => typeof value === "string" || value instanceof String;
|
|
419
617
|
/**
|
|
420
618
|
* This function converts a `value` to a string.
|
|
421
|
-
* If `value` is undefined or
|
|
619
|
+
* If `value` is undefined or null, an optional `defaultValue` is returned (defaults to `""`).
|
|
422
620
|
*
|
|
423
|
-
* - `util.types.toString("Hello")` will return `"Hello"`.
|
|
424
|
-
* - `util.types.toString(5.5)` will return `"5.5"`.
|
|
425
|
-
* - `util.types.toString("", "Some default")` will return `"Some Default"`.
|
|
426
|
-
* - `util.types.toString(undefined)` will return `""`.
|
|
427
621
|
* @param value The value to convert to a string.
|
|
428
|
-
* @param defaultValue A default value to return if `value` is undefined or
|
|
429
|
-
* @returns
|
|
622
|
+
* @param defaultValue A default value to return if `value` is undefined or null. Defaults to `""`.
|
|
623
|
+
* @returns The stringified version of `value`, or `defaultValue` if `value` is undefined or null.
|
|
624
|
+
* @example
|
|
625
|
+
* import { util } from "@prismatic-io/spectral";
|
|
626
|
+
*
|
|
627
|
+
* util.types.toString("Hello"); // "Hello"
|
|
628
|
+
* util.types.toString(5.5); // "5.5"
|
|
629
|
+
* util.types.toString("", "Some default"); // ""
|
|
630
|
+
* util.types.toString(undefined); // ""
|
|
631
|
+
* util.types.toString(null, "fallback"); // "fallback"
|
|
430
632
|
*/
|
|
431
633
|
const toString = (value, defaultValue = "") => `${value !== null && value !== void 0 ? value : defaultValue}`;
|
|
432
634
|
/**
|
|
433
|
-
* This function checks if value is a valid connection
|
|
635
|
+
* This function checks if value is a valid connection object (has `key`, `label`,
|
|
636
|
+
* and `inputs` properties, with appropriate OAuth 2.0 fields if applicable).
|
|
637
|
+
*
|
|
434
638
|
* @param value The variable to test.
|
|
435
639
|
* @returns This function returns true or false, depending on if `value` is a valid connection.
|
|
640
|
+
* @see {@link https://prismatic.io/docs/custom-connectors/connections/ | Writing Custom Connections}
|
|
641
|
+
* @example
|
|
642
|
+
* import { util } from "@prismatic-io/spectral";
|
|
643
|
+
*
|
|
644
|
+
* util.types.isConnection({
|
|
645
|
+
* key: "apiKey",
|
|
646
|
+
* label: "API Key",
|
|
647
|
+
* inputs: { apiKey: { type: "password" } },
|
|
648
|
+
* }); // true
|
|
649
|
+
* util.types.isConnection("not a connection"); // false
|
|
436
650
|
*/
|
|
437
651
|
const isConnection = (value) => {
|
|
438
652
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -449,14 +663,19 @@ const isConnection = (value) => {
|
|
|
449
663
|
return false;
|
|
450
664
|
};
|
|
451
665
|
/**
|
|
452
|
-
* This function returns true if `value`
|
|
666
|
+
* This function returns true if `value` can be parsed as JSON, and false otherwise.
|
|
667
|
+
*
|
|
668
|
+
* @param value The value to test against.
|
|
669
|
+
* @returns True if `value` can be parsed by `JSON.parse()`, false otherwise.
|
|
670
|
+
* @example
|
|
671
|
+
* import { util } from "@prismatic-io/spectral";
|
|
453
672
|
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
|
|
673
|
+
* util.types.isJSON('{"name":"John","age":30}'); // true
|
|
674
|
+
* util.types.isJSON("5"); // true
|
|
675
|
+
* util.types.isJSON("true"); // true
|
|
676
|
+
* util.types.isJSON(""); // false
|
|
677
|
+
* util.types.isJSON("not json"); // false
|
|
678
|
+
*/
|
|
460
679
|
const isJSON = (value) => {
|
|
461
680
|
try {
|
|
462
681
|
JSON.parse(value);
|
|
@@ -467,41 +686,72 @@ const isJSON = (value) => {
|
|
|
467
686
|
}
|
|
468
687
|
};
|
|
469
688
|
/**
|
|
470
|
-
* This function accepts an arbitrary object/value and safely serializes it
|
|
689
|
+
* This function accepts an arbitrary object/value and safely serializes it to JSON.
|
|
690
|
+
* Unlike `JSON.stringify`, it handles cyclic references without throwing.
|
|
471
691
|
*
|
|
472
692
|
* @param value Arbitrary object/value to serialize.
|
|
473
|
-
* @param prettyPrint When true, convert to pretty printed JSON with 2 spaces and newlines. When false, JSON is compact.
|
|
474
|
-
* @param retainKeyOrder When true, the order of keys in the JSON output will be the same as the order in the input object.
|
|
693
|
+
* @param prettyPrint When true, convert to pretty printed JSON with 2 spaces and newlines. When false, JSON is compact. Defaults to `true`.
|
|
694
|
+
* @param retainKeyOrder When true, the order of keys in the JSON output will be the same as the order in the input object. Defaults to `false`.
|
|
475
695
|
* @returns JSON serialized text that can be safely logged.
|
|
696
|
+
* @example
|
|
697
|
+
* import { util } from "@prismatic-io/spectral";
|
|
698
|
+
*
|
|
699
|
+
* // Pretty-printed (default)
|
|
700
|
+
* util.types.toJSON({ name: "Acme", count: 42 });
|
|
701
|
+
* // '{\n "count": 42,\n "name": "Acme"\n}'
|
|
702
|
+
*
|
|
703
|
+
* // Compact output
|
|
704
|
+
* util.types.toJSON({ name: "Acme", count: 42 }, false);
|
|
705
|
+
* // '{"count":42,"name":"Acme"}'
|
|
706
|
+
*
|
|
707
|
+
* // Retain original key order
|
|
708
|
+
* util.types.toJSON({ name: "Acme", count: 42 }, true, true);
|
|
709
|
+
* // '{\n "name": "Acme",\n "count": 42\n}'
|
|
476
710
|
*/
|
|
477
711
|
const toJSON = (value, prettyPrint = true, retainKeyOrder = false) => {
|
|
478
712
|
const stringify = (0, safe_stable_stringify_1.configure)({
|
|
479
713
|
circularValue: undefined,
|
|
480
714
|
deterministic: !retainKeyOrder,
|
|
481
715
|
});
|
|
482
|
-
|
|
716
|
+
const result = prettyPrint ? stringify(value, null, 2) : stringify(value);
|
|
717
|
+
return result !== null && result !== void 0 ? result : "";
|
|
483
718
|
};
|
|
484
719
|
/**
|
|
485
|
-
* This function returns a
|
|
720
|
+
* This function returns a version of the headers object where all header
|
|
721
|
+
* names (keys) are lower-cased. Header values are left unchanged.
|
|
722
|
+
*
|
|
723
|
+
* @param headers The headers to convert to lower case.
|
|
724
|
+
* @returns A new header object with lower-cased keys.
|
|
725
|
+
* @example
|
|
726
|
+
* import { util } from "@prismatic-io/spectral";
|
|
486
727
|
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
728
|
+
* util.types.lowerCaseHeaders({ "Content-Type": "application/json" });
|
|
729
|
+
* // { "content-type": "application/json" }
|
|
730
|
+
*
|
|
731
|
+
* util.types.lowerCaseHeaders({
|
|
732
|
+
* "Cache-Control": "max-age=604800",
|
|
733
|
+
* "Accept-Language": "en-us",
|
|
734
|
+
* });
|
|
735
|
+
* // { "cache-control": "max-age=604800", "accept-language": "en-us" }
|
|
736
|
+
*/
|
|
493
737
|
const lowerCaseHeaders = (headers) => Object.entries(headers).reduce((result, [key, val]) => {
|
|
494
738
|
return Object.assign(Object.assign({}, result), { [key.toLowerCase()]: val });
|
|
495
739
|
}, {});
|
|
496
740
|
exports.lowerCaseHeaders = lowerCaseHeaders;
|
|
497
741
|
/**
|
|
498
|
-
* This function parses a JSON string
|
|
742
|
+
* This function parses a JSON string and returns the resulting object,
|
|
743
|
+
* or returns the value as-is if it is already an object.
|
|
499
744
|
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
745
|
+
* @param value The JSON string or object to convert.
|
|
746
|
+
* @returns An object, parsing JSON as necessary.
|
|
747
|
+
* @example
|
|
748
|
+
* import { util } from "@prismatic-io/spectral";
|
|
502
749
|
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
750
|
+
* util.types.toObject('{"foo":"bar","baz":123}');
|
|
751
|
+
* // { foo: "bar", baz: 123 }
|
|
752
|
+
*
|
|
753
|
+
* util.types.toObject({ foo: "bar", baz: 123 });
|
|
754
|
+
* // { foo: "bar", baz: 123 } (returned as-is)
|
|
505
755
|
*/
|
|
506
756
|
const toObject = (value) => {
|
|
507
757
|
if (typeof value === "string" && isJSON(value)) {
|
|
@@ -514,21 +764,29 @@ const toObject = (value) => {
|
|
|
514
764
|
exports.toObject = toObject;
|
|
515
765
|
/**
|
|
516
766
|
* This function removes any properties of an object that match a certain predicate.
|
|
517
|
-
* By default properties with values of undefined
|
|
767
|
+
* By default, properties with values of `undefined`, `null`, and `""` are removed.
|
|
768
|
+
* Useful for cleaning up request bodies before sending to an API.
|
|
769
|
+
*
|
|
770
|
+
* @param obj A key-value object to remove properties from.
|
|
771
|
+
* @param predicate A function that returns true for properties to remove. Defaults to removing properties with `undefined`, `null`, and `""` values.
|
|
772
|
+
* @returns A new object with matching properties removed.
|
|
773
|
+
* @example
|
|
774
|
+
* import { util } from "@prismatic-io/spectral";
|
|
518
775
|
*
|
|
519
|
-
*
|
|
520
|
-
*
|
|
776
|
+
* // Remove undefined, null, and empty string values (default)
|
|
777
|
+
* util.types.cleanObject({ foo: undefined, bar: "abc", baz: null, buz: "" });
|
|
778
|
+
* // { bar: "abc" }
|
|
521
779
|
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
780
|
+
* // Custom predicate: remove even numbers
|
|
781
|
+
* util.types.cleanObject({ foo: 1, bar: 2, baz: 3 }, (v) => v % 2 === 0);
|
|
782
|
+
* // { foo: 1, baz: 3 }
|
|
525
783
|
*/
|
|
526
784
|
const cleanObject = (obj, predicate) => {
|
|
527
785
|
const defaultPredicate = (v) => v === undefined || v === null || v === "";
|
|
528
786
|
return (0, omitBy_1.default)(obj, predicate || defaultPredicate);
|
|
529
787
|
};
|
|
530
|
-
__exportStar(require("./errors"), exports);
|
|
531
788
|
__exportStar(require("./conditionalLogic"), exports);
|
|
789
|
+
__exportStar(require("./errors"), exports);
|
|
532
790
|
exports.default = {
|
|
533
791
|
types: {
|
|
534
792
|
isBool,
|