UNPKG

@pipedream/gong

Version:

Pipedream Gong Components

84 lines (71 loc) 1.64 kB
import { ConfigurationError } from "@pipedream/platform"; function emptyStrToUndefined(value) { const trimmed = typeof (value) === "string" && value.trim(); return trimmed === "" ? undefined : value; } function parse(value) { const valueToParse = emptyStrToUndefined(value); if (typeof (valueToParse) === "object" || valueToParse === undefined) { return valueToParse; } try { return JSON.parse(valueToParse); } catch (e) { throw new ConfigurationError("Make sure the custom expression contains a valid object"); } } function parseArray(value) { try { if (!value) { return []; } if (Array.isArray(value)) { return value; } const parsedValue = JSON.parse(value); if (!Array.isArray(parsedValue)) { throw new Error("Not an array"); } return parsedValue; } catch (e) { throw new ConfigurationError("Make sure the custom expression contains a valid array object"); } } function parseObject(obj) { if (!obj) return undefined; if (Array.isArray(obj)) { return obj.map((item) => { if (typeof item === "string") { try { return JSON.parse(item); } catch (e) { return item; } } return item; }); } if (typeof obj === "string") { try { return JSON.parse(obj); } catch (e) { return obj; } } return obj; }; async function streamIterator(stream) { const resources = []; for await (const resource of stream) { resources.push(resource); } return resources; } export default { parseArray, parseObject, parse, streamIterator, };