UNPKG

@prismatic-io/spectral

Version:

Utility library for building Prismatic connectors and code-native integrations

284 lines (283 loc) 11.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchConnectionStableKeys = exports.fetchComponentDataForManifest = void 0; const axios_1 = __importStar(require("axios")); const prism_1 = require("../utils/prism"); // Helper to transform input nodes from GraphQL response to the expected format function transformInputNodes(inputs) { return inputs.map((node) => (Object.assign(Object.assign({}, node), { collection: node.collection ? node.collection.toLowerCase() : undefined, type: node.type.toLowerCase() }))); } // This function does not return a complete Component as described in ServerTypes; // it instead selectively returns only what's needed to generate a manifest. const fetchComponentDataForManifest = (_a) => __awaiter(void 0, [_a], void 0, function* ({ componentKey, isPrivate, }) { var _b, _c, _d; const accessToken = yield (0, prism_1.getPrismAccessToken)(); const prismaticUrl = (_b = process.env.PRISMATIC_URL) !== null && _b !== void 0 ? _b : "https://app.prismatic.io"; const query = ` query componentQuery($componentKey: String!, $public: Boolean!) { components(key: $componentKey, public: $public) { nodes { id label description signature key connections(sortBy: {direction: ASC, field: KEY}) { nodes { key label comments inputs { nodes { key label type required default collection shown onPremiseControlled } } } } } } } `; try { const response = yield axios_1.default.post(new URL("/api", prismaticUrl).toString(), { query, variables: { componentKey, public: !isPrivate, }, }, { headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", "Prismatic-Client": "spectral", }, }); const component = response.data.data.components.nodes[0]; if (!component) { throw new Error(`Could not find a ${isPrivate ? "private" : "public"} component with the given key: ${componentKey}. ${!isPrivate ? 'You may need to include the "--private" flag.' : ""}`); } const componentActions = yield getComponentActions(component.id, prismaticUrl, accessToken); const actions = {}; const triggers = {}; const dataSources = {}; componentActions.forEach((node) => { var _a, _b; if (node.isTrigger) { triggers[node.key] = { key: node.key, display: { label: node.label, description: node.description, }, inputs: transformInputNodes(node.inputs.nodes), }; } else if (node.isDataSource) { dataSources[node.key] = { key: node.key, display: { label: node.label, description: node.description, }, inputs: transformInputNodes(node.inputs.nodes), dataSourceType: ((_a = node.dataSourceType) !== null && _a !== void 0 ? _a : "string").toLowerCase(), examplePayload: (_b = node.examplePayload) !== null && _b !== void 0 ? _b : {}, }; } else { actions[node.key] = { key: node.key, display: { label: node.label, description: node.description, }, inputs: transformInputNodes(node.inputs.nodes), examplePayload: node.examplePayload, }; } }); const connections = component.connections.nodes.map((node) => { return { key: node.key, label: node.label, comments: node.comments, inputs: transformInputNodes(node.inputs.nodes), }; }); return { key: component.key, signature: component.signature, public: !isPrivate, display: { label: component.label, description: component.description, }, actions, triggers, dataSources, connections, }; } catch (error) { if (error instanceof axios_1.AxiosError && ((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.errors)) { throw new Error(JSON.stringify(error.response.data.errors, null, 2)); } throw error; } }); exports.fetchComponentDataForManifest = fetchComponentDataForManifest; const fetchConnectionStableKeys = (_a) => __awaiter(void 0, [_a], void 0, function* ({ componentKey, isPrivate, }) { var _b, _c, _d, _e, _f, _g, _h; const accessToken = yield (0, prism_1.getPrismAccessToken)(); const prismaticUrl = (_b = process.env.PRISMATIC_URL) !== null && _b !== void 0 ? _b : "https://app.prismatic.io"; const query = ` query connectionStableKeysQuery($componentSelector: [ComponentSelector]!) { scopedConfigVariables(connection_Component_In: $componentSelector) { nodes { stableKey } } } `; try { const response = yield axios_1.default.post(new URL("/api", prismaticUrl).toString(), { query, variables: { componentSelector: [{ key: componentKey, isPublic: !isPrivate }], }, }, { headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", "Prismatic-Client": "spectral", }, }); const nodes = (_f = (_e = (_d = (_c = response.data) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.scopedConfigVariables) === null || _e === void 0 ? void 0 : _e.nodes) !== null && _f !== void 0 ? _f : []; return nodes.map((node) => node.stableKey); } catch (error) { if (error instanceof axios_1.AxiosError && ((_h = (_g = error.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.errors)) { throw new Error(JSON.stringify(error.response.data.errors, null, 2)); } throw error; } }); exports.fetchConnectionStableKeys = fetchConnectionStableKeys; function getComponentActions(componentId, prismaticUrl, accessToken) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let hasNextPage = true; let cursor = null; let actions = []; while (hasNextPage) { try { const query = ` query componentActionQuery($component: ID!, $after: String) { actions(component: $component, after: $after) { nodes { isDataSource isDetailDataSource dataSourceType isTrigger isCommonTrigger key label description inputs { nodes { key label type required default collection shown onPremiseControlled } } examplePayload } pageInfo { hasNextPage endCursor } } } `; const response = yield axios_1.default.post(new URL("/api", prismaticUrl).toString(), { query: query, variables: { component: componentId, after: cursor, }, }, { headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json", "Prismatic-Client": "spectral", }, }); const responseActions = response.data.data.actions.nodes; actions = actions.concat(responseActions); hasNextPage = (_a = response.data.data.actions.pageInfo) === null || _a === void 0 ? void 0 : _a.hasNextPage; cursor = (_b = response.data.data.actions.pageInfo) === null || _b === void 0 ? void 0 : _b.endCursor; } catch (error) { if (error instanceof axios_1.AxiosError && ((_d = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.errors)) { throw new Error(JSON.stringify(error.response.data.errors, null, 2)); } throw error; } } return actions.sort((a, b) => a.key.localeCompare(b.key)); }); }