@prismatic-io/spectral 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/dist/index.d.ts +43 -0
  4. package/dist/index.js +73 -0
  5. package/dist/testing.d.ts +72 -0
  6. package/dist/testing.js +130 -0
  7. package/dist/types/ActionDefinition.d.ts +25 -0
  8. package/dist/types/ActionDefinition.js +2 -0
  9. package/dist/types/ActionInputParameters.d.ts +24 -0
  10. package/dist/types/ActionInputParameters.js +2 -0
  11. package/dist/types/ActionLogger.d.ts +20 -0
  12. package/dist/types/ActionLogger.js +8 -0
  13. package/dist/types/ActionPerformFunction.d.ts +14 -0
  14. package/dist/types/ActionPerformFunction.js +2 -0
  15. package/dist/types/AuthorizationDefinition.d.ts +14 -0
  16. package/dist/types/AuthorizationDefinition.js +19 -0
  17. package/dist/types/Credential.d.ts +58 -0
  18. package/dist/types/Credential.js +8 -0
  19. package/dist/types/DisplayDefinition.d.ts +26 -0
  20. package/dist/types/DisplayDefinition.js +5 -0
  21. package/dist/types/InputFieldType.d.ts +12 -0
  22. package/dist/types/InputFieldType.js +2 -0
  23. package/dist/types/Inputs.d.ts +37 -0
  24. package/dist/types/Inputs.js +2 -0
  25. package/dist/types/PerformReturn.d.ts +19 -0
  26. package/dist/types/PerformReturn.js +2 -0
  27. package/dist/types/conditional-logic.d.ts +91 -0
  28. package/dist/types/conditional-logic.js +67 -0
  29. package/dist/types/index.d.ts +15 -0
  30. package/dist/types/index.js +27 -0
  31. package/dist/types/server-types.d.ts +135 -0
  32. package/dist/types/server-types.js +8 -0
  33. package/dist/util.d.ts +33 -0
  34. package/dist/util.js +337 -0
  35. package/dist/util.test.d.ts +1 -0
  36. package/dist/util.test.js +286 -0
  37. package/package.json +84 -0
@@ -0,0 +1,15 @@
1
+ /**
2
+ * This file exports types from all other files in types/, so users can simply
3
+ * `import { DesiredType } from "@prismatic-io/spectral"`
4
+ */
5
+ export * from "./Credential";
6
+ export * from "./AuthorizationDefinition";
7
+ export * from "./ActionDefinition";
8
+ export * from "./Inputs";
9
+ export * from "./PerformReturn";
10
+ export * from "./DisplayDefinition";
11
+ export * from "./ActionInputParameters";
12
+ export * from "./ActionLogger";
13
+ export * from "./ActionPerformFunction";
14
+ export * from "./InputFieldType";
15
+ export * from "./conditional-logic";
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * This file exports types from all other files in types/, so users can simply
4
+ * `import { DesiredType } from "@prismatic-io/spectral"`
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Credential"), exports);
18
+ __exportStar(require("./AuthorizationDefinition"), exports);
19
+ __exportStar(require("./ActionDefinition"), exports);
20
+ __exportStar(require("./Inputs"), exports);
21
+ __exportStar(require("./PerformReturn"), exports);
22
+ __exportStar(require("./DisplayDefinition"), exports);
23
+ __exportStar(require("./ActionInputParameters"), exports);
24
+ __exportStar(require("./ActionLogger"), exports);
25
+ __exportStar(require("./ActionPerformFunction"), exports);
26
+ __exportStar(require("./InputFieldType"), exports);
27
+ __exportStar(require("./conditional-logic"), exports);
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Types defined in this module describe the shape of objects that are
3
+ * sent to Prismatic's API when a component is published. Types defined
4
+ * should not generally be imported directly, but they're the types of
5
+ * objects that are created by `component()` and `action()` helper functions.
6
+ */
7
+ /// <reference types="node" />
8
+ /** Import shared types from types/ */
9
+ import { ActionContext } from "./ActionPerformFunction";
10
+ import { AuthorizationDefinition } from "./AuthorizationDefinition";
11
+ import { ActionDisplayDefinition, ComponentDisplayDefinition } from "./DisplayDefinition";
12
+ import { InputFieldChoice, InputFieldCollection } from "./Inputs";
13
+ /** Defines attributes of a Component. */
14
+ interface ComponentBase<T extends boolean> {
15
+ /** Specifies unique key for this Component. */
16
+ key: string;
17
+ /** Specifies if this Component is available for all Organizations or only your own @default false */
18
+ public?: T;
19
+ /** Defines how the Component is displayed in the Prismatic interface. */
20
+ display: ComponentDisplayDefinition<T>;
21
+ /** @deprecated Version of the Component. */
22
+ version?: string;
23
+ /** Specifies the supported Actions of this Component. */
24
+ actions: Record<string, Action>;
25
+ }
26
+ export declare type Component<T extends boolean> = ComponentBase<T> & (T extends true ? {
27
+ /** Specified the URL for the Component Documentation. */
28
+ documentationUrl: string;
29
+ } : {
30
+ /** Specified the URL for the Component Documentation. */
31
+ documentationUrl?: string;
32
+ });
33
+ /** Configuration of an Action. */
34
+ export interface Action {
35
+ /** Key used for the Actions map and to uniquely identify this Component in your tenant. */
36
+ key: string;
37
+ /** Defines how the Action is displayed in the Prismatic interface. */
38
+ display: ActionDisplayDefinition;
39
+ /** Function to perform when this Action is used and invoked. */
40
+ perform: ActionPerformFunction;
41
+ /** InputFields to present in the Prismatic interface for configuration of this Action. */
42
+ inputs: InputField[];
43
+ /** Specifies Authorization settings, if applicable */
44
+ authorization?: AuthorizationDefinition;
45
+ /** Optional attribute that specifies whether an Action will terminate execution.*/
46
+ terminateExecution?: boolean;
47
+ /** Determines whether an Action will allow Conditional Branching.*/
48
+ allowsBranching?: boolean;
49
+ /**Static Branch names associated with an Action. */
50
+ staticBranchNames?: string[];
51
+ /**The Input associated with Dynamic Branching.*/
52
+ dynamicBranchInput?: string;
53
+ /**An example of the payload outputted by an Action*/
54
+ examplePayload?: ServerPerformDataStructureReturn | ServerPerformBranchingDataStructureReturn;
55
+ }
56
+ /** Collection of input parameters provided by the user or previous steps' outputs */
57
+ interface ActionInputParameters {
58
+ [key: string]: unknown;
59
+ }
60
+ /** Used to represent returning conventional data and does not require content type to be specified */
61
+ export interface ServerPerformDataStructureReturn {
62
+ /** Data structure to return from the action */
63
+ data: boolean | number | string | Record<string, unknown> | unknown[] | unknown;
64
+ /** The Content Type of the payload data that can be optionally specified */
65
+ contentType?: string;
66
+ /** The HTTP Status code that will be used if this terminates a synchronous invocation */
67
+ statusCode?: number;
68
+ /** An optional object, the keys and values of which will be persisted in the instanceState and available for subsequent actions and executions */
69
+ state?: Record<string, unknown>;
70
+ }
71
+ /** Used to represent a binary or serialized data return as content type must be specified */
72
+ interface ServerPerformDataReturn {
73
+ /** Data payload containing data of the specified contentType */
74
+ data: Buffer | string | unknown;
75
+ /** The Content Type of the payload data */
76
+ contentType: string;
77
+ /** The HTTP Status code that will be used if this terminates a synchronous invocation */
78
+ statusCode?: number;
79
+ /** An optional object, the keys and values of which will be persisted in the instanceState and available for subsequent actions and executions */
80
+ state?: Record<string, unknown>;
81
+ }
82
+ /** Used to represent a branching return of conventional data and does not require content type to be specified */
83
+ export interface ServerPerformBranchingDataStructureReturn extends ServerPerformDataStructureReturn {
84
+ /** Name of the Branch to take. */
85
+ branch: string;
86
+ }
87
+ /** Used to represent a binary or serialized data branching return as content type must be specified */
88
+ interface ServerPerformBranchingDataReturn extends ServerPerformDataReturn {
89
+ /** Name of the Branch to take. */
90
+ branch: string;
91
+ }
92
+ /** Required return type of all action perform functions */
93
+ declare type PerformReturn = ServerPerformDataStructureReturn | ServerPerformBranchingDataStructureReturn | ServerPerformDataReturn | ServerPerformBranchingDataReturn | void;
94
+ /** Definition of the function to perform when an Action is invoked. */
95
+ declare type ActionPerformFunction = (context: ActionContext, params: ActionInputParameters) => Promise<PerformReturn>;
96
+ declare type InputField = DefaultInputField | CodeInputField;
97
+ /** Defines attributes of a InputField. */
98
+ interface DefaultInputField {
99
+ /** Unique identifier of the InputField. Must be unique within an Action. */
100
+ key: string;
101
+ /** Interface label of the InputField. */
102
+ label: string;
103
+ /** Data type the InputField will collect. */
104
+ type: InputFieldType;
105
+ /** Collection type of the InputField */
106
+ collection?: InputFieldCollection;
107
+ /** Text to show as the InputField placeholder. */
108
+ placeholder?: string;
109
+ /** Default value for this field. */
110
+ default?: string;
111
+ /** Additional text to give guidance to the user configuring the InputField. */
112
+ comments?: string;
113
+ /** Example valid input for this InputField. */
114
+ example?: string;
115
+ /** Indicate if this InputField is required. */
116
+ required?: boolean;
117
+ /** Dictates possible choices for the input. */
118
+ model?: InputFieldChoice[];
119
+ }
120
+ interface CodeInputField extends DefaultInputField {
121
+ type: "code";
122
+ language?: string;
123
+ }
124
+ /** InputField type enumeration. */
125
+ declare type InputFieldType = "string" | "text" | "password" | "boolean" | "code" | "data" | "conditional";
126
+ /** Binary data payload */
127
+ export interface DataPayload {
128
+ /** Raw binary data as a Buffer */
129
+ data: Buffer;
130
+ /** Content type of data contained within this payload */
131
+ contentType: string;
132
+ /** Suggested extension to use when writing the data */
133
+ suggestedExtension?: string;
134
+ }
135
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ /**
3
+ * Types defined in this module describe the shape of objects that are
4
+ * sent to Prismatic's API when a component is published. Types defined
5
+ * should not generally be imported directly, but they're the types of
6
+ * objects that are created by `component()` and `action()` helper functions.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/util.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The `util` module provides a set of functions commonly needed to author custom components.
3
+ * Many functions in the `util` module are used to coerce data into a particular type, and can be accessed through `util.types`.
4
+ * For example, `util.types.toInt("5.5")` will return an integer, `5`.
5
+ */
6
+ import { DataPayload } from "./types/server-types";
7
+ import { KeyValuePair } from "./types";
8
+ declare const _default: {
9
+ types: {
10
+ isBool: (value: unknown) => value is boolean;
11
+ toBool: (value: unknown, defaultValue?: boolean | undefined) => boolean;
12
+ isInt: (value: unknown) => value is number;
13
+ toInt: (value: unknown, defaultValue?: number | undefined) => number;
14
+ isNumber: (value: unknown) => boolean;
15
+ toNumber: (value: unknown, defaultValue?: number | undefined) => number;
16
+ isBigInt: (value: unknown) => value is bigint;
17
+ toBigInt: (value: unknown) => BigInt;
18
+ isDate: (value: unknown) => value is Date;
19
+ toDate: (value: unknown) => Date;
20
+ isUrl: (value: string) => boolean;
21
+ isBufferDataPayload: (value: unknown) => boolean;
22
+ toBufferDataPayload: (value: unknown) => DataPayload;
23
+ isData: (value: unknown) => boolean;
24
+ toData: (value: unknown) => DataPayload;
25
+ toString: (value: unknown, defaultValue?: string) => string;
26
+ keyValPairListToObject: (kvpList?: KeyValuePair<unknown>[]) => Record<string, unknown>;
27
+ isJSON: (value: string) => boolean;
28
+ };
29
+ docs: {
30
+ formatJsonExample: (input: unknown) => string;
31
+ };
32
+ };
33
+ export default _default;
package/dist/util.js ADDED
@@ -0,0 +1,337 @@
1
+ "use strict";
2
+ /**
3
+ * The `util` module provides a set of functions commonly needed to author custom components.
4
+ * Many functions in the `util` module are used to coerce data into a particular type, and can be accessed through `util.types`.
5
+ * For example, `util.types.toInt("5.5")` will return an integer, `5`.
6
+ */
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ /** */
12
+ const parseISO_1 = __importDefault(require("date-fns/parseISO"));
13
+ const isValid_1 = __importDefault(require("date-fns/isValid"));
14
+ const isDate_1 = __importDefault(require("date-fns/isDate"));
15
+ const valid_url_1 = require("valid-url");
16
+ /**
17
+ * Determine if a variable is a boolean (true or false).
18
+ *
19
+ * - `util.types.isBool(false)` will return `true`, since `false` is a boolean.
20
+ * - `util.types.isBool("Hello")` will return `false`, since `"Hello"` is not a boolean.
21
+ *
22
+ * @param value The variable to test.
23
+ * @returns True if the value is a boolean, or false otherwise.
24
+ */
25
+ const isBool = (value) => value === true || value === false;
26
+ /**
27
+ * Convert truthey (true, "t", "true", "y", "yes") values to boolean `true`,
28
+ * and falsey (false, "f", "false", "n", "no") values to boolean `false`.
29
+ * Truthy/falsey checks are case-insensitive.
30
+ *
31
+ * In the event that `value` is undefined or an empty string, a default value can be provided.
32
+ * For example, `util.types.toBool('', true)` will return `true`.
33
+ *
34
+ * @param value The value to convert to a boolean.
35
+ * @param defaultValue The value to return if `value` is undefined or an empty string.
36
+ * @returns The boolean equivalent of the truthey or falsey `value`.
37
+ */
38
+ const toBool = (value, defaultValue) => {
39
+ if (isBool(value)) {
40
+ return value;
41
+ }
42
+ if (typeof value === "string") {
43
+ const lowerValue = value.toLowerCase();
44
+ if (["t", "true", "y", "yes"].includes(lowerValue)) {
45
+ return true;
46
+ }
47
+ else if (["f", "false", "n", "no"].includes(lowerValue)) {
48
+ return false;
49
+ }
50
+ }
51
+ return Boolean(value || defaultValue);
52
+ };
53
+ /**
54
+ * This function checks if value is an integer.
55
+ * `util.types.isInt(5)` returns true, while `util.types.isInt("5")` or `util.types.isInt(5.5)` returns false.
56
+ * @param value The variable to test.
57
+ * @returns This function returns true or false, depending on if `value` is an integer.
58
+ */
59
+ const isInt = (value) => Number.isInteger(value);
60
+ /**
61
+ * This function converts a variable to an integer if possible.
62
+ * `util.types.toInt(5.5)` will return `5`. `util.types.toInt("20.3")` will return `20`.
63
+ *
64
+ * In the event that `value` is undefined or an empty string, a default value can be provided.
65
+ * For example, `util.types.toInt('', 1)` will return `1`.
66
+ *
67
+ * This function will throw an exception if `value` cannot be coerced to an integer.
68
+ * @param value The value to convert to an integer.
69
+ * @param defaultValue The value to return if `value` is undefined, an empty string, or not able to be coerced.
70
+ * @returns This function returns an integer if possible.
71
+ */
72
+ const toInt = (value, defaultValue) => {
73
+ if (isInt(value))
74
+ return value;
75
+ // Turn a float into an int
76
+ if (typeof value === "number") {
77
+ return ~~value;
78
+ }
79
+ if (typeof value === "string") {
80
+ const intValue = Number.parseInt(value);
81
+ if (!Number.isNaN(intValue)) {
82
+ return intValue;
83
+ }
84
+ }
85
+ if (typeof value === "undefined" || value === "") {
86
+ return defaultValue || 0;
87
+ }
88
+ if (defaultValue) {
89
+ return defaultValue;
90
+ }
91
+ throw new Error(`Value '${value}' cannot be coerced to int.`);
92
+ };
93
+ /**
94
+ * Determine if a variable is a number, or can easily be coerced into a number.
95
+ *
96
+ * - `util.types.isNumber(3.21)` will return `true`, since `3.21` is a number.
97
+ * - `util.types.isBool("5.5")` will return `true`, since the string `"5.5"` can easily be coerced into a number.
98
+ * - `util.types.isBool("Hello")` will return `false`, since `"Hello"` is not a number.
99
+ *
100
+ * @param value The varible to test.
101
+ * @returns This function returns true if `value` can easily be coerced into a number, and false otherwise.
102
+ */
103
+ const isNumber = (value) => !Number.isNaN(Number(value));
104
+ /**
105
+ * This function coerces a value (number or string) into a number.
106
+ * In the event that `value` is undefined or an empty string, a `defaultValue` can be provided, or zero will be returned.
107
+ * If `value` is not able to be coerced into a number but is defined, an error will be thrown.
108
+ *
109
+ * - `util.types.toNumber("3.22")` will return the number `3.22`.
110
+ * - `util.types.toNumber("", 5.5)` will return the default value `5.5`, since `value` was an empty string.
111
+ * - `util.types.toNumber(undefined)` will return `0`, since `value` was undefined and no `defaultValue` was given.
112
+ * - `util.types.toNumber("Hello")` will throw an error, since the string `"Hello"` cannot be coerced into a number.
113
+ * @param value The value to turn into a number.
114
+ * @param defaultValue The value to return if `value` is undefined or an empty string.
115
+ * @returns This function returns the numerical version of `value` if possible, or the `defaultValue` if `value` is undefined or an empty string.
116
+ */
117
+ const toNumber = (value, defaultValue) => {
118
+ if (isNumber(value)) {
119
+ return Number(value);
120
+ }
121
+ if (typeof value === "undefined" || value === "") {
122
+ return defaultValue || 0;
123
+ }
124
+ throw new Error(`Value '${value}' cannot be coerced to a number.`);
125
+ };
126
+ /**
127
+ * @param value The value to test
128
+ * @returns This function returns true if the type of `value` is a bigint, or false otherwise.
129
+ */
130
+ const isBigInt = (value) => typeof value === "bigint";
131
+ /**
132
+ * This function coerces a provided value into a bigint if possible.
133
+ * The provided `value` must be a bigint, integer, string representing an integer, or a boolean.
134
+ *
135
+ * - `util.types.toBigInt(3)` will return `3n`.
136
+ * - `util.types.toBigInt("-5")` will return `-5n`.
137
+ * - `util.types.toBigInt(true)` will return `1n` (and `false` will return `0n`).
138
+ * - `util.types.toBigInt("5.5")` will throw an error, as `5.5` is not an integer.
139
+ * @param value The value to coerce to bigint.
140
+ * @returns This function returns the bigint representation of `value`.
141
+ */
142
+ const toBigInt = (value) => {
143
+ if (isBigInt(value)) {
144
+ return value;
145
+ }
146
+ try {
147
+ return BigInt(toString(value));
148
+ }
149
+ catch (error) {
150
+ throw new Error(`Value '${value}' cannot be coerced to bigint.`);
151
+ }
152
+ };
153
+ /** This function returns true if `value` is a variable of type `Date`, and false otherwise. */
154
+ const isDate = (value) => isDate_1.default(value);
155
+ /**
156
+ * This function parses an ISO date if possible, or throws an error if the value provided
157
+ * cannot be coerced into a Date object.
158
+ *
159
+ * - `util.types.toDate(new Date('1995-12-17T03:24:00'))` will return `Date('1995-12-17T09:24:00.000Z')` since a `Date` object was passed in.
160
+ * - `util.types.toDate('2021-03-20')` will return `Date('2021-03-20T05:00:00.000Z')` since a valid ISO date was passed in.
161
+ * - `parseISODate('2021-03-20-05')` will throw an error since `value` was not a valid ISO date.
162
+ * @param value The value to turn into a date.
163
+ * @returns The date equivalent of `value`.
164
+ */
165
+ const toDate = (value) => {
166
+ if (isDate(value)) {
167
+ return value;
168
+ }
169
+ if (typeof value === "string") {
170
+ const dt = parseISO_1.default(value);
171
+ if (isValid_1.default(dt)) {
172
+ return dt;
173
+ }
174
+ }
175
+ throw new Error(`Value '${value}' cannot be coerced to date.`);
176
+ };
177
+ /**
178
+ * This function tests if the string provided is a valid URL, and returns `true` if the URL is valid.
179
+ * Note: this function only tests that the string is a syntactically correct URL; it does not check
180
+ * if the URL is web accessible.
181
+ *
182
+ * - `util.types.isUrl('https://prismatic.io')` will return true.
183
+ * - `util.types.isUrl('https:://prismatic.io')` will return false due to the extraneous `:` symbol.
184
+ * @param value The URL to test.
185
+ * @returns This function returns true if `value` is a valid URL, and false otherwise.
186
+ */
187
+ const isUrl = (value) => valid_url_1.isWebUri(value) !== undefined;
188
+ /**
189
+ * This function helps to transform key-value lists to objects.
190
+ * This is useful for transforming inputs that are key-value collections into objects.
191
+ *
192
+ * For example, an input that is a collection might return `[{key: "foo", value: "bar"},{key: "baz", value: 5}]`.
193
+ * If that array were passed into `util.types.keyValPairListToObject()`, an object would be returned of the form
194
+ * `{foo: "bar", baz: 5}`.
195
+ * @param kvpList An array of objects with `key` and `value` properties.
196
+ */
197
+ const keyValPairListToObject = (kvpList = []) => {
198
+ return kvpList.reduce((result, { key, value }) => (Object.assign(Object.assign({}, result), { [key]: value })), {});
199
+ };
200
+ /**
201
+ * This function tests if the object provided is a Prismatic `DataPayload` object.
202
+ * A `DataPayload` object is an object with a `data` attribute, and optional `contentType` attribute.
203
+ *
204
+ * @param value The value to test
205
+ * @returns This function returns true if `value` is a DataPayload object, and false otherwise.
206
+ */
207
+ const isBufferDataPayload = (value) => {
208
+ return value instanceof Object && "data" in value;
209
+ };
210
+ /**
211
+ * Many libraries for third-party API that handle binary files expect `Buffer` objects.
212
+ * This function helps to convert strings, Uint8Arrays, and Arrays to a data structure
213
+ * that has a Buffer and a string representing `contentType`.
214
+ *
215
+ * You can access the buffer like this:
216
+ * `const { data, contentType } = util.types.toBufferDataPayload(someData);`
217
+ *
218
+ * If `value` cannot be converted to a Buffer, an error will be thrown.
219
+ * @param value The string, Buffer, Uint8Array, or Array to convert to a Buffer.
220
+ * @returns This function returns an object with two keys: `data`, which is a `Buffer`, and `contentType`, which is a string.
221
+ */
222
+ const toBufferDataPayload = (value) => {
223
+ if (isBufferDataPayload(value)) {
224
+ return value;
225
+ }
226
+ if (typeof value === "string") {
227
+ return {
228
+ data: Buffer.from(value, "utf-8"),
229
+ contentType: "text/plain",
230
+ };
231
+ }
232
+ if (value instanceof Buffer) {
233
+ return {
234
+ data: value,
235
+ contentType: "application/octet-stream",
236
+ };
237
+ }
238
+ if (value instanceof Uint8Array) {
239
+ return {
240
+ data: Buffer.from(value),
241
+ contentType: "application/octet-stream",
242
+ };
243
+ }
244
+ if (value instanceof Object || Array.isArray(value)) {
245
+ const json = JSON.stringify(value);
246
+ return {
247
+ data: Buffer.from(json, "utf-8"),
248
+ contentType: "application/json",
249
+ };
250
+ }
251
+ throw new Error(`Value '${value}' cannot be converted to a Buffer.`);
252
+ };
253
+ /**
254
+ * @deprecated This function tests if the object provided is a Prismatic `DataPayload` object.
255
+ * A `DataPayload` object is an object with a `data` attribute, and optional `contentType` attribute.
256
+ *
257
+ * @param value The value to test
258
+ * @returns This function returns true if `value` is a DataPayload object, and false otherwise.
259
+ */
260
+ const isData = (value) => isBufferDataPayload(value);
261
+ /**
262
+ * @deprecated Many libraries for third-party API that handle binary files expect `Buffer` objects.
263
+ * This function helps to convert strings, Uint8Arrays, and Arrays to a data structure
264
+ * that has a Buffer and a string representing `contentType`.
265
+ *
266
+ * You can access the buffer like this:
267
+ * `const { data, contentType } = util.types.toData(someData);`
268
+ *
269
+ * If `value` cannot be converted to a Buffer, an error will be thrown.
270
+ * @param value The string, Buffer, Uint8Array, or Array to convert to a Buffer.
271
+ * @returns This function returns an object with two keys: `data`, which is a `Buffer`, and `contentType`, which is a string.
272
+ */
273
+ const toData = (value) => toBufferDataPayload(value);
274
+ /**
275
+ * This function helps to format JSON examples for documentation.
276
+ * @param input An object to be JSONified.
277
+ * @returns This function returns a code block that can be used for documentation.
278
+ */
279
+ const formatJsonExample = (input) => ["```json", JSON.stringify(input, undefined, 2), "```"].join("\n");
280
+ /**
281
+ * This function converts a `value` to a string.
282
+ * If `value` is undefined or an empty string, an optional `defaultValue` can be returned.
283
+ *
284
+ * - `util.types.toString("Hello")` will return `"Hello"`.
285
+ * - `util.types.toString(5.5)` will return `"5.5"`.
286
+ * - `util.types.toString("", "Some default")` will return `"Some Default"`.
287
+ * - `util.types.toString(undefined)` will return `""`.
288
+ * @param value The value to convert to a string.
289
+ * @param defaultValue A default value to return if `value` is undefined or an empty string.
290
+ * @returns This function returns the stringified version fo `value`, or `defaultValue` in the case that `value` is undefined or an empty string.
291
+ */
292
+ const toString = (value, defaultValue = "") => `${value !== null && value !== void 0 ? value : defaultValue}`;
293
+ /**
294
+ * This function returns true if `value` resembles the shape of JSON, and false otherwise.
295
+ *
296
+ * - `isJSON(undefined) will return `false`
297
+ * - `isJSON(null) will return `true`
298
+ * - `isJSON("") will return `false`
299
+ * - `isJSON(5) will return `true`
300
+ * - `isJSON('{"name":"John", "age":30, "car":null}') will return `true`
301
+ * @param value The value to test against
302
+ * @returns This function returns a boolean, dependant on whether `value` can be parsed to JSON.
303
+ * */
304
+ const isJSON = (value) => {
305
+ try {
306
+ JSON.parse(value);
307
+ return true;
308
+ }
309
+ catch (_a) {
310
+ return false;
311
+ }
312
+ };
313
+ exports.default = {
314
+ types: {
315
+ isBool,
316
+ toBool,
317
+ isInt,
318
+ toInt,
319
+ isNumber,
320
+ toNumber,
321
+ isBigInt,
322
+ toBigInt,
323
+ isDate,
324
+ toDate,
325
+ isUrl,
326
+ isBufferDataPayload,
327
+ toBufferDataPayload,
328
+ isData,
329
+ toData,
330
+ toString,
331
+ keyValPairListToObject,
332
+ isJSON,
333
+ },
334
+ docs: {
335
+ formatJsonExample,
336
+ },
337
+ };