@prismatic-io/spectral 5.0.0 → 5.3.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.
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * from "./utils";
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./types"), exports);
14
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1,71 @@
1
+ import { Connection } from "../../index";
2
+ import { IMTOMAttachments } from "soap";
3
+ /**
4
+ * SOAPConnection takes standard connection fields, and adds an optional `wsdlDefinitionUrl` field.
5
+ */
6
+ export interface SOAPConnection extends Connection {
7
+ fields: {
8
+ [key: string]: unknown;
9
+ wsdlDefinitionUrl?: string;
10
+ };
11
+ }
12
+ /**
13
+ * This function determines if the object presented is a SOAP connection with a `wsdlDefinitionUrl` field.
14
+ * @param connection The connection to test
15
+ * @returns This function returns true if the connection is a SOAPConnection, and false otherwise.
16
+ */
17
+ export declare const isSOAPConnection: (connection: unknown) => connection is SOAPConnection;
18
+ /**
19
+ * SOAP requests return a 4-tuple or 5-tuple with the response first, the XML response second, headers third, and the XML request fourth, and optional message attachments fifth.
20
+ */
21
+ export declare type SOAPResponse = [any, any, any, any, IMTOMAttachments?];
22
+ /**
23
+ * Overload the `soapRequest` function to take a variety of types of arguments.
24
+ */
25
+ export interface SOAPRequest {
26
+ (params: RequestParams): Promise<SOAPResponse>;
27
+ (params: RequestParams, methodParams: Record<string, unknown>): Promise<SOAPResponse>;
28
+ (params: RequestParams, methodParams: undefined): Promise<SOAPResponse>;
29
+ }
30
+ export interface RequestParams {
31
+ wsdlParam: SOAPConnection | string;
32
+ method: string;
33
+ overrides?: ClientOverrides;
34
+ debug?: boolean;
35
+ }
36
+ export interface ClientOverrides {
37
+ endpointURL?: string;
38
+ soapHeaders?: unknown[];
39
+ }
40
+ export interface HeaderPayload {
41
+ [x: string]: any;
42
+ }
43
+ export interface GenerateHeader {
44
+ (wsdlParam: SOAPConnection | string, headerPayload: HeaderPayload, headerOptions: undefined): Promise<string>;
45
+ (wsdlParam: SOAPConnection | string, headerPayload: HeaderPayload, headerOptions: {
46
+ namespace: string;
47
+ xmlns: string;
48
+ }): Promise<string>;
49
+ }
50
+ export interface BasicAuthConnection extends Connection {
51
+ fields: {
52
+ [key: string]: unknown;
53
+ username: unknown;
54
+ password: unknown;
55
+ loginMethod: unknown;
56
+ };
57
+ }
58
+ /**
59
+ * This function determines if the object presented is a Basic Auth connection with `username`, `password`, and `loginMethod` fields.
60
+ * @param connection The connection to test
61
+ * @returns This function returns true if the connection is a SOAPConnection, and false otherwise.
62
+ */
63
+ export declare const isBasicAuthConnection: (connection: Connection) => connection is BasicAuthConnection;
64
+ export interface SOAPAuth {
65
+ (connection: BasicAuthConnection, wsdlDefinition: string): Promise<any>;
66
+ (connection: BasicAuthConnection & SOAPConnection): Promise<any>;
67
+ }
68
+ export interface DescribeWSDL {
69
+ (connection: SOAPConnection): Promise<any>;
70
+ (wsdlDefinition: string): Promise<any>;
71
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBasicAuthConnection = exports.isSOAPConnection = void 0;
4
+ /**
5
+ * This function determines if the object presented is a SOAP connection with a `wsdlDefinitionUrl` field.
6
+ * @param connection The connection to test
7
+ * @returns This function returns true if the connection is a SOAPConnection, and false otherwise.
8
+ */
9
+ const isSOAPConnection = (connection) => {
10
+ var _a;
11
+ if (typeof connection === "object" && connection !== null)
12
+ return "wsdlDefinitionURL" in ((_a = connection) === null || _a === void 0 ? void 0 : _a.fields);
13
+ return false;
14
+ };
15
+ exports.isSOAPConnection = isSOAPConnection;
16
+ /**
17
+ * This function determines if the object presented is a Basic Auth connection with `username`, `password`, and `loginMethod` fields.
18
+ * @param connection The connection to test
19
+ * @returns This function returns true if the connection is a SOAPConnection, and false otherwise.
20
+ */
21
+ const isBasicAuthConnection = (connection) => {
22
+ const { fields } = connection;
23
+ return ("username" in fields && "password" in fields && "loginMethod" in fields);
24
+ };
25
+ exports.isBasicAuthConnection = isBasicAuthConnection;
@@ -0,0 +1,12 @@
1
+ import { ClientOverrides, GenerateHeader, SOAPAuth, SOAPConnection, SOAPRequest, DescribeWSDL } from "./types";
2
+ import { Client } from "soap";
3
+ declare const _default: {
4
+ describeWSDL: DescribeWSDL;
5
+ generateHeader: GenerateHeader;
6
+ getSOAPAuth: SOAPAuth;
7
+ getSOAPClient: <T extends string | SOAPConnection = string | SOAPConnection>(wsdlParam: T) => Promise<Client>;
8
+ getWSDL: (wsdlDefinitionURL: string) => Promise<string>;
9
+ overrideClientDefaults: (client: Client, overrides: ClientOverrides) => void;
10
+ soapRequest: SOAPRequest;
11
+ };
12
+ export default _default;
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
16
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
17
+ const types_1 = require("./types");
18
+ const index_1 = require("../../index");
19
+ const soap_1 = require("soap");
20
+ const axios_1 = __importDefault(require("axios"));
21
+ const promises_1 = require("fs/promises");
22
+ const uuid_1 = require("uuid");
23
+ const os_1 = __importDefault(require("os"));
24
+ const path_1 = __importDefault(require("path"));
25
+ /**
26
+ * Optionally log out SOAP requests and responses for debugging purposes
27
+ *
28
+ * @param client A SOAP client that generates requests and responses
29
+ */
30
+ const debugRequest = (client) => {
31
+ console.debug(client.lastRequest);
32
+ console.debug(client.lastResponse);
33
+ };
34
+ /**
35
+ * This function takes either the URL of a WSDL or the XML defining a WSDL, and returns an object describing the methods and attributes defined in the WSDL.
36
+ *
37
+ * @param wsdlParam Either the URL where a WSDL is stored, or the XML defining a WSDL.
38
+ * @returns An object containing the methods and attributes defined in a WSDL
39
+ */
40
+ const describeWSDL = (wsdlParam) => __awaiter(void 0, void 0, void 0, function* () {
41
+ const client = yield getSOAPClient(types_1.isSOAPConnection(wsdlParam) ? wsdlParam : index_1.util.types.toString(wsdlParam));
42
+ try {
43
+ const result = client.describe();
44
+ return result;
45
+ }
46
+ catch (error) {
47
+ throw new Error("Unable to parse WSDL Services due to circular references");
48
+ }
49
+ });
50
+ /**
51
+ * Fetch a WSDL from a URL
52
+ * @param wsdlDefinitionURL The URL where the WSDL is stored
53
+ * @returns The WSDL's raw XML
54
+ */
55
+ const getWSDL = (wsdlDefinitionURL) => __awaiter(void 0, void 0, void 0, function* () {
56
+ const httpClient = axios_1.default.create({
57
+ baseURL: wsdlDefinitionURL,
58
+ headers: { "Content-Type": "text/xml" },
59
+ });
60
+ const { data } = yield httpClient.get("");
61
+ return index_1.util.types.toString(data);
62
+ });
63
+ /**
64
+ * Create a SOAP client given a WSDL or SOAPConnection
65
+ * @param wsdlParam a SOAPConnection or XML defining a WSDL
66
+ * @returns An HTTP client configured to query a SOAP-based API
67
+ */
68
+ const getSOAPClient = (wsdlParam) => __awaiter(void 0, void 0, void 0, function* () {
69
+ if (typeof wsdlParam === "string") {
70
+ const wsdl = index_1.util.types.toString(wsdlParam);
71
+ const filePath = path_1.default.join(os_1.default.tmpdir(), `${uuid_1.v4()}.wsdl`);
72
+ yield promises_1.writeFile(filePath, wsdl);
73
+ const client = yield soap_1.createClientAsync(filePath);
74
+ yield promises_1.rm(filePath);
75
+ return client;
76
+ }
77
+ else if (types_1.isSOAPConnection(wsdlParam)) {
78
+ const { fields: { wsdlDefinitionURL }, } = wsdlParam;
79
+ if (!wsdlDefinitionURL ||
80
+ !index_1.util.types.isUrl(index_1.util.types.toString(wsdlDefinitionURL))) {
81
+ throw new Error("WSDL Definition or the Connection field 'wsdlDefinitionURL' must be supplied.");
82
+ }
83
+ const client = yield soap_1.createClientAsync(index_1.util.types.toString(wsdlDefinitionURL));
84
+ return client;
85
+ }
86
+ else {
87
+ throw new Error("WSDL Definition or the Connection field 'wsdlDefinitionURL' must be supplied.");
88
+ }
89
+ });
90
+ /**
91
+ * Override some HTTP client defaults
92
+ * @param client The client to override
93
+ * @param overrides An endpoint URL or SOAP headers to override
94
+ */
95
+ const overrideClientDefaults = (client, overrides) => {
96
+ const { endpointURL, soapHeaders } = overrides;
97
+ if (endpointURL) {
98
+ client.setEndpoint(endpointURL);
99
+ }
100
+ if (soapHeaders) {
101
+ soapHeaders.map((header) => {
102
+ client.addSoapHeader(header);
103
+ });
104
+ }
105
+ };
106
+ /**
107
+ * Make a request to a SOAP-based API
108
+ * @param param0
109
+ * @param methodParams Parameters to pass to the specified SOAP method
110
+ * @returns The results from the SOAP request, including the full XML of the request and response
111
+ */
112
+ const soapRequest = ({ wsdlParam, method, overrides, debug }, methodParams) => __awaiter(void 0, void 0, void 0, function* () {
113
+ const client = yield getSOAPClient(types_1.isSOAPConnection(wsdlParam) ? wsdlParam : index_1.util.types.toString(wsdlParam));
114
+ if (overrides) {
115
+ overrideClientDefaults(client, overrides);
116
+ }
117
+ const requestFunction = client[`${method}Async`];
118
+ let results = undefined;
119
+ try {
120
+ if (typeof methodParams === "object" && methodParams !== null) {
121
+ results = yield requestFunction(methodParams);
122
+ }
123
+ else {
124
+ results = yield requestFunction({});
125
+ }
126
+ if (index_1.util.types.isBool(debug) && debug) {
127
+ debugRequest(client);
128
+ }
129
+ return results;
130
+ }
131
+ catch (error) {
132
+ if (index_1.util.types.isBool(debug) && debug) {
133
+ debugRequest(client);
134
+ }
135
+ console.warn("Please verify that the method you specified exists in the WSDL specification.");
136
+ throw error;
137
+ }
138
+ });
139
+ /**
140
+ * Create a SOAP header
141
+ * @param wsdlParam A SOAPConnection or XML definition of a WSDL
142
+ * @param headerPayload The contents of a header XML node
143
+ * @param headerOptions Attributes for a header XML node, like namespace or xmlns
144
+ * @returns
145
+ */
146
+ const generateHeader = (wsdlParam, headerPayload, headerOptions) => __awaiter(void 0, void 0, void 0, function* () {
147
+ const client = yield getSOAPClient(types_1.isSOAPConnection(wsdlParam) ? wsdlParam : index_1.util.types.toString(wsdlParam));
148
+ let options = [];
149
+ if (headerOptions) {
150
+ options = Object.values(headerOptions);
151
+ }
152
+ const index = client.addSoapHeader(headerPayload, "", ...options);
153
+ return client.getSoapHeaders()[index];
154
+ });
155
+ /**
156
+ * Fetch authentication information for a SOAPConnection
157
+ * @param connection The SOAPConnection
158
+ * @param wsdlDefinition The XML WSDL definition
159
+ * @returns
160
+ */
161
+ const getSOAPAuth = (connection, wsdlDefinition) => __awaiter(void 0, void 0, void 0, function* () {
162
+ if (types_1.isBasicAuthConnection(connection) && wsdlDefinition) {
163
+ const { fields: { username, password, loginMethod }, } = connection;
164
+ const result = yield soapRequest({
165
+ wsdlParam: index_1.util.types.toString(wsdlDefinition),
166
+ method: index_1.util.types.toString(loginMethod),
167
+ }, { username, password });
168
+ return result;
169
+ }
170
+ else if (types_1.isSOAPConnection(connection) &&
171
+ types_1.isBasicAuthConnection(connection)) {
172
+ const { fields: { username, password, loginMethod }, } = connection;
173
+ const result = yield soapRequest({
174
+ wsdlParam: connection,
175
+ method: index_1.util.types.toString(loginMethod),
176
+ }, { username, password });
177
+ return result;
178
+ }
179
+ else {
180
+ throw new Error("Must supply a SOAP Connection or a WSDL Definition");
181
+ }
182
+ });
183
+ exports.default = {
184
+ describeWSDL,
185
+ generateHeader,
186
+ getSOAPAuth,
187
+ getSOAPClient,
188
+ getWSDL,
189
+ overrideClientDefaults,
190
+ soapRequest,
191
+ };
package/dist/index.js CHANGED
@@ -14,12 +14,31 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
14
14
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
16
  };
17
+ var __rest = (this && this.__rest) || function (s, e) {
18
+ var t = {};
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
20
+ t[p] = s[p];
21
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
22
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
23
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
24
+ t[p[i]] = s[p[i]];
25
+ }
26
+ return t;
27
+ };
17
28
  var __importDefault = (this && this.__importDefault) || function (mod) {
18
29
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
30
  };
20
31
  Object.defineProperty(exports, "__esModule", { value: true });
21
32
  exports.testing = exports.util = exports.oauth2Connection = exports.connection = exports.input = exports.trigger = exports.action = exports.component = void 0;
22
- const convertInput = (key, input) => (Object.assign(Object.assign({}, input), { key }));
33
+ /**
34
+ * Both component author-facing types and server types that
35
+ * the Prismatic API expects are imported here.
36
+ */
37
+ const types_1 = require("./types");
38
+ const convertInput = (key, _a) => {
39
+ var { default: defaultValue, type } = _a, rest = __rest(_a, ["default", "type"]);
40
+ return (Object.assign(Object.assign({}, rest), { type, default: defaultValue !== null && defaultValue !== void 0 ? defaultValue : types_1.InputFieldDefaultMap[type], key }));
41
+ };
23
42
  /**
24
43
  * This is a helper function for component() to convert an
25
44
  * action defined in TypeScript into an action object that
@@ -12,3 +12,4 @@ export declare type InputFieldTypeMap = {
12
12
  conditional: ConditionalExpression;
13
13
  connection: Connection;
14
14
  };
15
+ export declare const InputFieldDefaultMap: Record<InputFieldType, string | undefined>;
@@ -1,2 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InputFieldDefaultMap = void 0;
4
+ exports.InputFieldDefaultMap = {
5
+ string: "",
6
+ data: "",
7
+ text: "",
8
+ password: "",
9
+ boolean: "false",
10
+ code: "",
11
+ conditional: undefined,
12
+ connection: undefined,
13
+ };
@@ -1,10 +1,12 @@
1
- import { InputFieldType } from ".";
1
+ import { InputFieldDefaultMap, InputFieldType } from ".";
2
2
  export declare type Inputs = Record<string, InputFieldDefinition>;
3
3
  export declare type ConnectionInput = DefaultInputFieldDefinition & {
4
4
  shown?: boolean;
5
5
  };
6
- export declare type InputFieldDefinition = DefaultInputFieldDefinition | CodeInputFieldDefinition;
6
+ export declare type InputFieldDefinition = DefaultInputFieldDefinition | CodeInputFieldDefinition | ConditionalInputField | ConnectionInputField;
7
7
  interface BaseInputFieldDefinition {
8
+ /** Data type the InputField will collect. */
9
+ type: InputFieldType;
8
10
  /** Interface label of the InputField. */
9
11
  label: string;
10
12
  /** Collection type of the InputField */
@@ -12,7 +14,7 @@ interface BaseInputFieldDefinition {
12
14
  /** Text to show as the InputField placeholder. */
13
15
  placeholder?: string;
14
16
  /** Default value for this field. */
15
- default?: string;
17
+ default?: typeof InputFieldDefaultMap[this["type"]];
16
18
  /** Additional text to give guidance to the user configuring the InputField. */
17
19
  comments?: string;
18
20
  /** Example valid input for this InputField. */
@@ -24,13 +26,21 @@ interface BaseInputFieldDefinition {
24
26
  }
25
27
  /** Defines attributes of a InputField. */
26
28
  export interface DefaultInputFieldDefinition extends BaseInputFieldDefinition {
27
- type: Exclude<InputFieldType, "code">;
29
+ type: Exclude<InputFieldType, "code" | "conditional" | "connection">;
28
30
  }
29
31
  /** Defines attributes of a CodeInputField. */
30
32
  export interface CodeInputFieldDefinition extends BaseInputFieldDefinition {
31
33
  type: Extract<InputFieldType, "code">;
32
34
  language?: string;
33
35
  }
36
+ /** Defines attributes of a ConditionalInputField. */
37
+ export interface ConditionalInputField extends BaseInputFieldDefinition {
38
+ type: Extract<InputFieldType, "conditional">;
39
+ }
40
+ /** Defines attributes of a ConnectionInputField. */
41
+ export interface ConnectionInputField extends BaseInputFieldDefinition {
42
+ type: Extract<InputFieldType, "connection">;
43
+ }
34
44
  export interface Connection {
35
45
  /** Key of the Connection type. */
36
46
  key: string;
@@ -6,7 +6,7 @@
6
6
  */
7
7
  /// <reference types="node" />
8
8
  /** Import shared types from types/ */
9
- import { OAuth2Type } from ".";
9
+ import { OAuth2Type, InputFieldType, InputFieldDefaultMap } from ".";
10
10
  import { ActionContext } from "./ActionPerformFunction";
11
11
  import { ActionDisplayDefinition, ComponentDisplayDefinition } from "./DisplayDefinition";
12
12
  import { InputFieldChoice, InputFieldCollection } from "./Inputs";
@@ -146,7 +146,7 @@ interface DefaultInputField {
146
146
  /** Text to show as the InputField placeholder. */
147
147
  placeholder?: string;
148
148
  /** Default value for this field. */
149
- default?: string;
149
+ default?: typeof InputFieldDefaultMap[this["type"]];
150
150
  /** Additional text to give guidance to the user configuring the InputField. */
151
151
  comments?: string;
152
152
  /** Example valid input for this InputField. */
@@ -160,8 +160,6 @@ interface CodeInputField extends DefaultInputField {
160
160
  type: "code";
161
161
  language?: string;
162
162
  }
163
- /** InputField type enumeration. */
164
- export declare type InputFieldType = "string" | "text" | "password" | "boolean" | "code" | "data" | "conditional" | "connection";
165
163
  /** Binary data payload */
166
164
  export interface DataPayload {
167
165
  /** Raw binary data as a Buffer */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismatic-io/spectral",
3
- "version": "5.0.0",
3
+ "version": "5.3.0",
4
4
  "description": "Utility library for building Prismatic components",
5
5
  "keywords": [
6
6
  "prismatic"
@@ -32,13 +32,19 @@
32
32
  "dist/"
33
33
  ],
34
34
  "dependencies": {
35
+ "axios": "0.24.0",
35
36
  "date-fns": "2.17.0",
36
37
  "jest-mock": "27.0.3",
38
+ "soap": "0.43.0",
39
+ "uuid": "8.3.1",
37
40
  "valid-url": "1.0.9"
38
41
  },
39
42
  "devDependencies": {
43
+ "@types/axios": "0.14.0",
40
44
  "@types/jest": "26.0.23",
41
45
  "@types/node": "14.14.35",
46
+ "@types/sax": "1.2.4",
47
+ "@types/uuid": "8.3.1",
42
48
  "@types/valid-url": "1.0.3",
43
49
  "@typescript-eslint/eslint-plugin": "4.27.0",
44
50
  "@typescript-eslint/parser": "4.27.0",