UNPKG

fastman

Version:

快速api测试及文档生成

1,496 lines 48.9 kB
"use strict"; /** * @license * Copyright 2017 Red Hat * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /** * Visitor used to convert from a Model into a JavaScript object. */ var OasModelToJSVisitor = /** @class */ (function () { /** * Constructor. */ function OasModelToJSVisitor() { this.reset(); } /** * Resets the visitor for a new run. */ OasModelToJSVisitor.prototype.reset = function () { this._result = null; this._modelIdToJS = {}; }; /** * Returns the result that was built up during the visit of the model. * @return {any} */ OasModelToJSVisitor.prototype.getResult = function () { return this.removeNullProperties(this._result); }; /** * Removes any property with a null value from the js object. This is done recursively. * @param object */ OasModelToJSVisitor.prototype.removeNullProperties = function (object) { if (object instanceof Array) { for (var _i = 0, _a = object; _i < _a.length; _i++) { var item = _a[_i]; this.removeNullProperties(item); } } else if (object instanceof Object) { for (var key in object) { if (object[key] == null) { delete object[key]; } else { this.removeNullProperties(object[key]); } } } return object; }; /** * Indexes the javascript object by the ModelId of the model it was created from. This allows * quick lookup (mapping) from the model to the JS object. * @param node * @param jsObject */ OasModelToJSVisitor.prototype.updateIndex = function (node, jsObject) { this._modelIdToJS[node.modelId()] = jsObject; // Note: the first JS object created by the visitor is the result (we always traverse top-down). if (this._result == null) { this._result = jsObject; } }; /** * Lookup a JS object from the ID of the model it came from. * @param modelId * @return {any} */ OasModelToJSVisitor.prototype.lookup = function (modelId) { var rval = this._modelIdToJS[modelId]; // If not found, return a throwaway object (this would happen when doing a partial // read of a subsection of a OAS document). if (!this.isDefined(rval)) { return {}; } return rval; }; /** * Lookup a JS object using the model ID of the node's parent. * @param node * @return {any} */ OasModelToJSVisitor.prototype.lookupParentJS = function (node) { return this.lookup(node.parent().modelId()); }; /** * Returns true if the given thing is defined. * @param thing * @return {boolean} */ OasModelToJSVisitor.prototype.isDefined = function (thing) { if (thing === undefined || thing === null) { return false; } else { return true; } }; /** * Merges multiple objects into a single object. This is done by iterating through * all properties of all objects and assigning them as properties of a new object. The * result is a new object with all the properties of all objects passed to the method. * @param objects */ OasModelToJSVisitor.prototype.merge = function () { var objects = []; for (var _i = 0; _i < arguments.length; _i++) { objects[_i] = arguments[_i]; } var rval = new Object(); for (var _a = 0, objects_1 = objects; _a < objects_1.length; _a++) { var object = objects_1[_a]; for (var key in object) { var val = object[key]; rval[key] = val; } } return rval; }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitInfo = function (node) { var parentJS = this.lookupParentJS(node); var info = { title: node.title, description: node.description, termsOfService: node.termsOfService, contact: null, license: null, version: node.version }; parentJS.info = info; this.updateIndex(node, info); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitContact = function (node) { var parentJS = this.lookupParentJS(node); var contact = { name: node.name, url: node.url, email: node.email }; parentJS.contact = contact; this.updateIndex(node, contact); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitLicense = function (node) { var parentJS = this.lookupParentJS(node); var license = { name: node.name, url: node.url, }; parentJS.license = license; this.updateIndex(node, license); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitPaths = function (node) { var paths = {}; var parentJS = this.lookupParentJS(node); parentJS.paths = paths; this.updateIndex(node, paths); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitResponses = function (node) { var parentJS = this.lookupParentJS(node); var responses = { default: null }; parentJS.responses = responses; this.updateIndex(node, responses); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitXML = function (node) { var parent = this.lookupParentJS(node); var xml = { name: node.name, namespace: node.namespace, prefix: node.prefix, attribute: node.attribute, wrapped: node.wrapped }; parent.xml = xml; this.updateIndex(node, xml); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitSecurityRequirement = function (node) { var parentJS = this.lookupParentJS(node); var securityRequirements = parentJS["security"]; if (!this.isDefined(securityRequirements)) { securityRequirements = []; parentJS.security = securityRequirements; } var securityReq = {}; for (var _i = 0, _a = node.securityRequirementNames(); _i < _a.length; _i++) { var name = _a[_i]; securityReq[name] = node.scopes(name); } securityRequirements.push(securityReq); this.updateIndex(node, securityReq); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitTag = function (node) { var parentJS = this.lookupParentJS(node); if (!this.isDefined(parentJS.tags)) { parentJS.tags = []; } var tag = { name: node.name, description: node.description, externalDocs: null }; parentJS.tags.push(tag); this.updateIndex(node, tag); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitExternalDocumentation = function (node) { var parentJS = this.lookupParentJS(node); parentJS.externalDocs = { description: node.description, url: node.url }; this.updateIndex(node, parentJS.externalDocs); }; /** * Visits a node. * @param node */ OasModelToJSVisitor.prototype.visitExtension = function (node) { var jsObject = this.lookupParentJS(node); jsObject[node.name] = node.value; }; OasModelToJSVisitor.prototype.visitValidationProblem = function (node) { // Validation problems are transient - they are not written to the JS. }; return OasModelToJSVisitor; }()); exports.OasModelToJSVisitor = OasModelToJSVisitor; /** * Visitor used to convert from a Model into a JavaScript object that conforms * to the OAS 2.0 specification. The resulting JS object can be stringified and * should be a valid OAS 2.0 document. */ var Oas20ModelToJSVisitor = /** @class */ (function (_super) { __extends(Oas20ModelToJSVisitor, _super); /** * Constructor. */ function Oas20ModelToJSVisitor() { return _super.call(this) || this; } /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitDocument = function (node) { var root = { swagger: node.swagger, info: null, host: node.host, basePath: node.basePath, schemes: node.schemes, consumes: node.consumes, produces: node.produces, paths: null, definitions: null, parameters: null, responses: null, securityDefinitions: null, security: null, tags: null, externalDocs: null }; this.updateIndex(node, root); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitPathItem = function (node) { var parentJS = this.lookupParentJS(node); var pathItem = { $ref: node.$ref, get: null, put: null, post: null, delete: null, options: null, head: null, patch: null, parameters: null }; parentJS[node.path()] = pathItem; this.updateIndex(node, pathItem); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitOperation = function (node) { var parentJS = this.lookupParentJS(node); var operation = { tags: node.tags, summary: node.summary, description: node.description, externalDocs: null, operationId: node.operationId, consumes: node.consumes, produces: node.produces, parameters: null, responses: null, schemes: node.schemes, deprecated: node.deprecated, security: null }; parentJS[node.method()] = operation; this.updateIndex(node, operation); }; /** * Creates a JS object for a Parameter base object. * @param node */ Oas20ModelToJSVisitor.prototype.createParameterObject = function (node) { var items = this.createItemsObject(node); var parameter = { name: node.name, in: node.in, description: node.description, required: node.required, schema: null, allowEmptyValue: node.allowEmptyValue }; parameter = this.merge(parameter, items); return parameter; }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitParameter = function (node) { var parentJS = this.lookupParentJS(node); if (parentJS.parameters == null) { parentJS.parameters = []; } var parameter = this.createParameterObject(node); var paramRef = { $ref: node.$ref }; parameter = this.merge(paramRef, parameter); parentJS.parameters.push(parameter); this.updateIndex(node, parameter); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitParameterDefinition = function (node) { var parentJS = this.lookupParentJS(node); var parameter = this.createParameterObject(node); parentJS[node.parameterName()] = parameter; this.updateIndex(node, parameter); }; /** * Creates a JS object for a response base instance. * @param node */ Oas20ModelToJSVisitor.prototype.createResponseObject = function (node) { return { description: node.description, schema: null, headers: null, examples: null }; }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitHeaders = function (node) { if (node.headerNames().length > 0) { var parentJS = this.lookupParentJS(node); var headers = {}; parentJS.headers = headers; this.updateIndex(node, headers); } }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitResponse = function (node) { var parentJS = this.lookupParentJS(node); var response = this.createResponseObject(node); var responseRef = { $ref: node.$ref }; response = this.merge(responseRef, response); if (node.statusCode() === null || node.statusCode() === "default") { parentJS.default = response; } else { parentJS[node.statusCode()] = response; } this.updateIndex(node, response); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitResponseDefinition = function (node) { var parentJS = this.lookupParentJS(node); var response = this.createResponseObject(node); parentJS[node.name()] = response; this.updateIndex(node, response); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); parentJS.schema = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitPropertySchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.properties)) { parentJS.properties = {}; } parentJS.properties[node.propertyName()] = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitSchemaDefinition = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); parentJS[node.definitionName()] = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitAdditionalPropertiesSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); parentJS.additionalProperties = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitAllOfSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.allOf)) { parentJS.allOf = []; } parentJS.allOf.push(schema); this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitItemsSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.items)) { parentJS.items = schema; } else if (Array.isArray(parentJS.items)) { parentJS.items.push(schema); } else { parentJS.items = [ parentJS.items, schema ]; } this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitHeader = function (node) { var parentJS = this.lookupParentJS(node); var headerOnly = { description: node.description }; var items = this.createItemsObject(node); var header = this.merge(headerOnly, items); parentJS[node.headerName()] = header; this.updateIndex(node, header); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitExample = function (node) { var parentJS = this.lookupParentJS(node); var examples = {}; for (var _i = 0, _a = node.exampleContentTypes(); _i < _a.length; _i++) { var ct = _a[_i]; var example = node.example(ct); examples[ct] = example; } parentJS.examples = examples; this.updateIndex(node, examples); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitItems = function (node) { var parentJS = this.lookupParentJS(node); var items = this.createItemsObject(node); parentJS.items = items; this.updateIndex(node, items); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitSecurityDefinitions = function (node) { var parent = this.lookupParentJS(node); var secDefs = {}; for (var name in node.securitySchemeNames()) { secDefs[name] = null; } parent.securityDefinitions = secDefs; this.updateIndex(node, secDefs); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitSecurityScheme = function (node) { var parent = this.lookupParentJS(node); var scheme = { type: node.type, description: node.description, name: node.name, in: node.in, flow: node.flow, authorizationUrl: node.authorizationUrl, tokenUrl: node.tokenUrl, scopes: null }; parent[node.schemeName()] = scheme; this.updateIndex(node, scheme); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitScopes = function (node) { var parent = this.lookupParentJS(node); var scopes = {}; for (var _i = 0, _a = node.scopes(); _i < _a.length; _i++) { var scope = _a[_i]; var desc = node.getScopeDescription(scope); scopes[scope] = desc; } parent.scopes = scopes; this.updateIndex(node, scopes); }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitDefinitions = function (node) { var defNames = node.definitionNames(); if (defNames && defNames.length > 0) { var parent = this.lookupParentJS(node); var definitions = {}; parent.definitions = definitions; this.updateIndex(node, definitions); } }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitParametersDefinitions = function (node) { var paramNames = node.parameterNames(); if (paramNames && paramNames.length > 0) { var parent = this.lookupParentJS(node); var parameters = {}; parent.parameters = parameters; this.updateIndex(node, parameters); } }; /** * Visits a node. * @param node */ Oas20ModelToJSVisitor.prototype.visitResponsesDefinitions = function (node) { var responseNames = node.responseNames(); if (responseNames && responseNames.length > 0) { var parent = this.lookupParentJS(node); var responses = {}; parent.responses = responses; this.updateIndex(node, responses); } }; /** * Creates an OAS 2.0 Items javascript object. * @param node */ Oas20ModelToJSVisitor.prototype.createItemsObject = function (node) { return { type: node.type, format: node.format, items: null, collectionFormat: node.collectionFormat, default: node.default, maximum: node.maximum, exclusiveMaximum: node.exclusiveMaximum, minimum: node.minimum, exclusiveMinimum: node.exclusiveMinimum, maxLength: node.maxLength, minLength: node.minLength, pattern: node.pattern, maxItems: node.maxItems, minItems: node.minItems, uniqueItems: node.uniqueItems, enum: node.enum, multipleOf: node.multipleOf }; }; /** * Shared method used to create a schema JS object. * @param node * @return {any} */ Oas20ModelToJSVisitor.prototype.createSchemaObject = function (node) { var schema = { $ref: node.$ref, format: node.format, title: node.title, description: node.description, default: node.default, multipleOf: node.multipleOf, maximum: node.maximum, exclusiveMaximum: node.exclusiveMaximum, minimum: node.minimum, exclusiveMinimum: node.exclusiveMinimum, maxLength: node.maxLength, minLength: node.minLength, pattern: node.pattern, maxItems: node.maxItems, minItems: node.minItems, uniqueItems: node.uniqueItems, maxProperties: node.maxProperties, minProperties: node.minProperties, required: node.required, enum: node.enum, type: node.type, items: null, allOf: null, properties: null, additionalProperties: null, discriminator: node.discriminator, readOnly: node.readOnly, xml: null, externalDocs: null, example: node.example }; if (typeof node.additionalProperties === "boolean") { schema.additionalProperties = node.additionalProperties; } return schema; }; return Oas20ModelToJSVisitor; }(OasModelToJSVisitor)); exports.Oas20ModelToJSVisitor = Oas20ModelToJSVisitor; /** * Visitor used to convert from a Model into a JavaScript object that conforms * to the OAS 3.0 specification. The resulting JS object can be stringified and * should be a valid OAS 3.0 document. */ var Oas30ModelToJSVisitor = /** @class */ (function (_super) { __extends(Oas30ModelToJSVisitor, _super); function Oas30ModelToJSVisitor() { return _super !== null && _super.apply(this, arguments) || this; } /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitDocument = function (node) { var root = { openapi: node.openapi, info: null, servers: null, paths: null, components: null, security: null, tags: null, externalDocs: null }; this.updateIndex(node, root); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitPathItem = function (node) { var parentJS = this.lookupParentJS(node); var pathItem = { $ref: node.$ref, summary: node.summary, description: node.description, get: null, put: null, post: null, delete: null, options: null, head: null, patch: null, trace: null, servers: null, parameters: null }; parentJS[node.path()] = pathItem; this.updateIndex(node, pathItem); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitOperation = function (node) { var parentJS = this.lookupParentJS(node); var operation = { tags: node.tags, summary: node.summary, description: node.description, externalDocs: null, operationId: node.operationId, parameters: null, requestBody: null, responses: null, callbacks: null, deprecated: node.deprecated, security: null, servers: null }; parentJS[node.method()] = operation; this.updateIndex(node, operation); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitHeader = function (node) { var parentJS = this.lookupParentJS(node); var header = this.createHeaderObject(node); if (!this.isDefined(parentJS["headers"])) { parentJS["headers"] = {}; } parentJS["headers"][node.headerName()] = header; this.updateIndex(node, header); }; /** * Creates a header object. * @param node */ Oas30ModelToJSVisitor.prototype.createHeaderObject = function (node) { var header = { $ref: node.$ref, description: node.description, required: node.required, deprecated: node.deprecated, allowEmptyValue: node.allowEmptyValue, style: node.style, explode: node.explode, allowReserved: node.allowReserved, schema: null, example: node.example, examples: null, content: null }; return header; }; /** * Creates a JS object for a Parameter base object. * @param node */ Oas30ModelToJSVisitor.prototype.createParameterObject = function (node) { var parameter = { $ref: node.$ref, name: node.name, in: node.in, description: node.description, required: node.required, deprecated: node.deprecated, allowEmptyValue: node.allowEmptyValue, style: node.style, explode: node.explode, allowReserved: node.allowReserved, schema: null, example: node.example, examples: null, content: null }; return parameter; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitParameter = function (node) { var parentJS = this.lookupParentJS(node); if (parentJS.parameters == null) { parentJS.parameters = []; } var parameter = this.createParameterObject(node); parentJS.parameters.push(parameter); this.updateIndex(node, parameter); }; /** * Creates a JS object for a response base instance. * @param node */ Oas30ModelToJSVisitor.prototype.createResponseObject = function (node) { return { $ref: node.$ref, description: node.description, headers: null, content: null, links: null }; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitResponse = function (node) { var parentJS = this.lookupParentJS(node); var response = this.createResponseObject(node); if (node.statusCode() === null || node.statusCode() === "default") { parentJS.default = response; } else { parentJS[node.statusCode()] = response; } this.updateIndex(node, response); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitLink = function (node) { var parentJS = this.lookupParentJS(node); var link = this.createLinkObject(node); if (!this.isDefined(parentJS["links"])) { parentJS["links"] = {}; } parentJS["links"][node.name()] = link; this.updateIndex(node, link); }; /** * Creates a link object. * @param node * @return {any} */ Oas30ModelToJSVisitor.prototype.createLinkObject = function (node) { var link = { $ref: node.$ref, operationRef: node.operationRef, operationId: node.operationId, parameters: null, requestBody: null, description: node.description, server: null }; return link; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitLinkServer = function (node) { var parentJS = this.lookupParentJS(node); var server = { url: node.url, description: node.description, variables: null }; parentJS.server = server; this.updateIndex(node, server); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitLinkParameterExpression = function (node) { var parentJS = this.lookupParentJS(node); var expression = node.value(); if (!this.isDefined(parentJS["parameters"])) { parentJS["parameters"] = {}; } parentJS["parameters"][node.name()] = expression; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitLinkRequestBodyExpression = function (node) { var parentJS = this.lookupParentJS(node); var expression = node.value(); parentJS.requestBody = expression; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); parentJS.schema = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitDiscriminator = function (node) { var parentJS = this.lookupParentJS(node); var discriminator = { propertyName: node.propertyName, mapping: node.mapping }; parentJS.discriminator = discriminator; this.updateIndex(node, discriminator); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitPropertySchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.properties)) { parentJS.properties = {}; } parentJS.properties[node.propertyName()] = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitAdditionalPropertiesSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); parentJS.additionalProperties = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitAllOfSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.allOf)) { parentJS.allOf = []; } parentJS.allOf.push(schema); this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitAnyOfSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.anyOf)) { parentJS.anyOf = []; } parentJS.anyOf.push(schema); this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitOneOfSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.oneOf)) { parentJS.oneOf = []; } parentJS.oneOf.push(schema); this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitNotSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); parentJS.not = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitItemsSchema = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS.items)) { parentJS.items = schema; } else if (Array.isArray(parentJS.items)) { parentJS.items.push(schema); } else { parentJS.items = [ parentJS.items, schema ]; } this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitRequestBody = function (node) { var parentJS = this.lookupParentJS(node); var requestBody = this.createRequestBodyObject(node); parentJS.requestBody = requestBody; this.updateIndex(node, requestBody); }; /** * Creates a request body object. * @param node * @return {any} */ Oas30ModelToJSVisitor.prototype.createRequestBodyObject = function (node) { var requestBody = { $ref: node.$ref, description: node.description, content: null, required: node.required }; return requestBody; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitMediaType = function (node) { var parentJS = this.lookupParentJS(node); var mediaType = { schema: null, example: node.example, examples: null, encoding: null }; if (!this.isDefined(parentJS["content"])) { parentJS["content"] = {}; } parentJS["content"][node.name()] = mediaType; this.updateIndex(node, mediaType); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitEncoding = function (node) { var parentJS = this.lookupParentJS(node); var encoding = { contentType: node.contentType, headers: null, style: node.style, explode: node.explode, allowReserved: node.allowReserved }; if (!this.isDefined(parentJS["encoding"])) { parentJS["encoding"] = {}; } parentJS["encoding"][node.name()] = encoding; this.updateIndex(node, encoding); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitExample = function (node) { var parentJS = this.lookupParentJS(node); var example = this.createExampleObject(node); if (!parentJS.examples) { parentJS.examples = {}; } parentJS.examples[node.name()] = example; this.updateIndex(node, example); }; /** * Creates an example. * @param node */ Oas30ModelToJSVisitor.prototype.createExampleObject = function (node) { var example = { $ref: node.$ref, summary: node.summary, description: node.description, value: node.value, externalValue: node.externalValue }; return example; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitCallback = function (node) { var callback = {}; var parentJS = this.lookupParentJS(node); if (this.isDefined(node.$ref)) { callback.$ref = node.$ref; } if (!this.isDefined(parentJS["callbacks"])) { parentJS["callbacks"] = {}; } parentJS["callbacks"][node.name()] = callback; this.updateIndex(node, callback); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitCallbackPathItem = function (node) { this.visitPathItem(node); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitComponents = function (node) { var components = {}; var parentJS = this.lookupParentJS(node); parentJS.components = components; this.updateIndex(node, components); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitSchemaDefinition = function (node) { var parentJS = this.lookupParentJS(node); var schema = this.createSchemaObject(node); if (!this.isDefined(parentJS["schemas"])) { parentJS["schemas"] = {}; } parentJS["schemas"][node.name()] = schema; this.updateIndex(node, schema); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitResponseDefinition = function (node) { var parentJS = this.lookupParentJS(node); var response = this.createResponseObject(node); if (!this.isDefined(parentJS["responses"])) { parentJS["responses"] = {}; } parentJS["responses"][node.name()] = response; this.updateIndex(node, response); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitParameterDefinition = function (node) { var parentJS = this.lookupParentJS(node); var parameter = this.createParameterObject(node); if (!this.isDefined(parentJS["parameters"])) { parentJS["parameters"] = {}; } parentJS["parameters"][node.parameterName()] = parameter; this.updateIndex(node, parameter); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitExampleDefinition = function (node) { var parentJS = this.lookupParentJS(node); var example = this.createExampleObject(node); if (!this.isDefined(parentJS["examples"])) { parentJS["examples"] = {}; } parentJS["examples"][node.name()] = example; this.updateIndex(node, example); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitRequestBodyDefinition = function (node) { var parentJS = this.lookupParentJS(node); var requestBody = this.createRequestBodyObject(node); if (!this.isDefined(parentJS["requestBodies"])) { parentJS["requestBodies"] = {}; } parentJS["requestBodies"][node.name()] = requestBody; this.updateIndex(node, requestBody); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitHeaderDefinition = function (node) { var parentJS = this.lookupParentJS(node); var header = this.createHeaderObject(node); if (!this.isDefined(parentJS["headers"])) { parentJS["headers"] = {}; } parentJS["headers"][node.name()] = header; this.updateIndex(node, header); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitOAuthFlows = function (node) { var parentJS = this.lookupParentJS(node); var oauthFlows = { implicit: null, password: null, clientCredentials: null, authorizationCode: null }; parentJS.flows = oauthFlows; this.updateIndex(node, oauthFlows); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitImplicitOAuthFlow = function (node) { var parentJS = this.lookupParentJS(node); var flow = this.createOAuthFlowObject(node); parentJS.implicit = flow; this.updateIndex(node, flow); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitPasswordOAuthFlow = function (node) { var parentJS = this.lookupParentJS(node); var flow = this.createOAuthFlowObject(node); parentJS.password = flow; this.updateIndex(node, flow); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitClientCredentialsOAuthFlow = function (node) { var parentJS = this.lookupParentJS(node); var flow = this.createOAuthFlowObject(node); parentJS.clientCredentials = flow; this.updateIndex(node, flow); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitAuthorizationCodeOAuthFlow = function (node) { var parentJS = this.lookupParentJS(node); var flow = this.createOAuthFlowObject(node); parentJS.authorizationCode = flow; this.updateIndex(node, flow); }; /** * Creates an OAuth Flow js object. * @param node * @return {any} */ Oas30ModelToJSVisitor.prototype.createOAuthFlowObject = function (node) { var flow = { authorizationUrl: node.authorizationUrl, tokenUrl: node.tokenUrl, refreshUrl: node.refreshUrl, scopes: node.scopes }; return flow; }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitSecurityScheme = function (node) { var parentJS = this.lookupParentJS(node); var securityScheme = { $ref: node.$ref, type: node.type, description: node.description, name: node.name, in: node.in, scheme: node.scheme, bearerFormat: node.bearerFormat, flows: null, openIdConnectUrl: node.openIdConnectUrl }; if (!this.isDefined(parentJS["securitySchemes"])) { parentJS["securitySchemes"] = {}; } parentJS["securitySchemes"][node.schemeName()] = securityScheme; this.updateIndex(node, securityScheme); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitLinkDefinition = function (node) { var parentJS = this.lookupParentJS(node); var link = this.createLinkObject(node); if (!this.isDefined(parentJS["links"])) { parentJS["links"] = {}; } parentJS["links"][node.name()] = link; this.updateIndex(node, link); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitCallbackDefinition = function (node) { var parentJS = this.lookupParentJS(node); var callback = {}; if (this.isDefined(node.$ref)) { callback.$ref = node.$ref; } if (!this.isDefined(parentJS["callbacks"])) { parentJS["callbacks"] = {}; } parentJS["callbacks"][node.name()] = callback; this.updateIndex(node, callback); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitServer = function (node) { var parentJS = this.lookupParentJS(node); if (!this.isDefined(parentJS.servers)) { parentJS.servers = []; } var server = { url: node.url, description: node.description, variables: null }; parentJS.servers.push(server); this.updateIndex(node, server); }; /** * Visits a node. * @param node */ Oas30ModelToJSVisitor.prototype.visitServerVariable = function (node) { var parentJS = this.lookupParentJS(node); var serverVariable = { enum: node.enum, default: node.default, description: node.description }; if (!this.isDefined(parentJS["variables"])) { parentJS["variables"] = {}; } parentJS["variables"][node.name()] = serverVariable; this.updateIndex(node, serverVariable); }; /** * Shared method used to create a schema JS object. * @param node * @return {any} */ Oas30ModelToJSVisitor.prototype.createSchemaObject = function (node) { var schema = { $ref: node.$ref, format: node.format, title: node.title, description: node.description, default: node.default, multipleOf: node.multipleOf, maximum: node.maximum, exclusiveMaximum: node.exclusiveMaximum, minimum: node.minimum, exclusiveMinimum: node.exclusiveMinimum, maxLength: node.maxLength, minLength: node.minLength, pattern: node.pattern, maxItems: node.maxItems, minItems: node.minItems, uniqueItems: node.uniqueItems, maxProperties: node.maxProperties, minProperties: node.minProperties, required: node.required, enum: node.enum, type: node.type, items: null, allOf: null, oneOf: null, anyOf: null, not: null, properties: null, additionalProperties: null, nullable: node.nullable, discriminator: null, readOnly: node.readOnly, writeOnly: node.writeOnly, xml: null, externalDocs: null, example: node.example, deprecated: node.deprecated }; if (typeof node.additionalProperties === "boolean") { schema.additionalProperties = node.additionalProperties; } return schema; }; return Oas30ModelToJSVisitor; }(OasModelToJSVisitor)); exports.Oas30ModelToJSVisitor = Oas30ModelToJSVisitor; //# sourceMappingURL=model2js.visitor.js.map