st-open-api
Version:
Generates API client SDKs from an OpenAPI specification written in OpenAPI version 3.x.x
197 lines • 9.14 kB
JavaScript
"use strict";
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectProperty = exports.SERVICE_REFERENCES = exports.QUERY_PARAMETER_FUNCTION_REF = exports.OPEN_API_FUNCTION_REF = exports.HTTP_ERROR_HANDLER_INTERFACE_REF = exports.HTTP_RESPONSE_INTERCEPTOR_INTERFACE_REF = exports.HTTP_REQUEST_INTERCEPTOR_INTERFACE_REF = exports.HTTP_FUNCTION_REF = void 0;
var render_mustache_1 = require("../function/render-mustache");
var unique_array_1 = require("./unique-array");
var split_by_line_break_1 = require("../function/split-by-line-break");
var formatText_1 = require("../function/formatText");
var HTTP_FUNCTION_REF = function (folder) {
return {
fileName: "http",
refKey: "HTTP_FUNCTION_REF",
className: "http",
folderPath: folder.getFunctionFolder()
};
};
exports.HTTP_FUNCTION_REF = HTTP_FUNCTION_REF;
var HTTP_REQUEST_INTERCEPTOR_INTERFACE_REF = function (folder) {
return {
fileName: "i-$-open-api",
refKey: "HTTP_REQUEST_INTERCEPTOR_INTERFACE_REF",
className: "RequestInterceptor",
folderPath: folder.getInterfaceFolder()
};
};
exports.HTTP_REQUEST_INTERCEPTOR_INTERFACE_REF = HTTP_REQUEST_INTERCEPTOR_INTERFACE_REF;
var HTTP_RESPONSE_INTERCEPTOR_INTERFACE_REF = function (folder) {
return {
fileName: "i-$-open-api",
refKey: "HTTP_RESPONSE_INTERCEPTOR_INTERFACE_REF",
className: "ResponseInterceptor",
folderPath: folder.getInterfaceFolder()
};
};
exports.HTTP_RESPONSE_INTERCEPTOR_INTERFACE_REF = HTTP_RESPONSE_INTERCEPTOR_INTERFACE_REF;
var HTTP_ERROR_HANDLER_INTERFACE_REF = function (folder) {
return {
fileName: "i-$-open-api",
refKey: "HTTP_ERROR_HANDLER_INTERFACE_REF",
className: "ErrorHandler",
folderPath: folder.getInterfaceFolder()
};
};
exports.HTTP_ERROR_HANDLER_INTERFACE_REF = HTTP_ERROR_HANDLER_INTERFACE_REF;
var OPEN_API_FUNCTION_REF = function (folder) {
return {
fileName: "open-api",
refKey: "OPEN_API_FUNCTION_REF",
className: "openApi",
folderPath: folder.getFunctionFolder()
};
};
exports.OPEN_API_FUNCTION_REF = OPEN_API_FUNCTION_REF;
var QUERY_PARAMETER_FUNCTION_REF = function (folder) {
return {
fileName: "get-query-params",
refKey: "QUERY_PARAMETER_FUNCTION_REF",
className: "getQueryParameters",
folderPath: folder.getFunctionFolder()
};
};
exports.QUERY_PARAMETER_FUNCTION_REF = QUERY_PARAMETER_FUNCTION_REF;
exports.SERVICE_REFERENCES = [
exports.HTTP_FUNCTION_REF,
exports.HTTP_REQUEST_INTERCEPTOR_INTERFACE_REF,
exports.HTTP_RESPONSE_INTERCEPTOR_INTERFACE_REF,
exports.HTTP_ERROR_HANDLER_INTERFACE_REF,
exports.OPEN_API_FUNCTION_REF,
exports.QUERY_PARAMETER_FUNCTION_REF
];
var ObjectProperty = /** @class */ (function () {
function ObjectProperty(originalName) {
this.description = [];
this.imports = new unique_array_1.UniqueArray();
this.functions = [];
this.properties = [];
this.convertName(originalName);
}
ObjectProperty.prototype.convertName = function (originalName) {
this.className = (0, formatText_1.formatText)(originalName, 'ANY', 'PascalCase');
this.fileName = (0, formatText_1.formatText)(originalName, 'ANY', 'KebabCase');
};
ObjectProperty.prototype.addImports = function (_import) {
this.imports.push(_import);
};
ObjectProperty.prototype.addFunction = function (fun) {
var _a, _b, _c, _d, _e;
var data = {
functionName: fun.functionName,
originalPath: fun.originalPath,
httpMethod: fun.httpMethod,
isDescription: !!fun.description,
description: (0, split_by_line_break_1.splitByLineBreak)(fun.description),
isPathParameters: !!fun.pathParameters,
pathParameters: (_a = fun.pathParameters) === null || _a === void 0 ? void 0 : _a.params,
pathParameterClassName: (_b = fun.pathParameters) === null || _b === void 0 ? void 0 : _b.className,
isQueryParameters: !!fun.queryParameters,
queryParameters: (_c = fun.queryParameters) === null || _c === void 0 ? void 0 : _c.params,
queryParameterClassName: (_d = fun.queryParameters) === null || _d === void 0 ? void 0 : _d.className,
isHeaderParameters: !!fun.headerParameters,
headerParameters: fun.headerParameters && Object.values(fun.headerParameters.params).map(function (param) {
return {
name: param.name,
nameOriginal: param.nameOriginal,
required: param.required,
};
}),
headerParameterClassName: (_e = fun.headerParameters) === null || _e === void 0 ? void 0 : _e.className,
isRequestBody: !!fun.requestBody.requestBodyClass,
isRequestBodyJson: !!fun.requestBody.requestBodyClass && fun.requestBody.isRequestBodyJson,
requestBodyClass: fun.requestBody.requestBodyCardinality === "oneOf"
? fun.requestBody.requestBodyClass.join(" | ")
: fun.requestBody.requestBodyClass,
forceInterceptor: fun.forceInterceptor,
isResponse: !!fun.response.responseClass,
isJsonResponse: !!fun.response.responseClass && fun.response.isJsonResponse,
isPlaintextResponse: fun.response.isPlaintextResponse,
isDownloadResponse: fun.response.isDownloadResponse,
responseClass: fun.response.responseClass
};
this.functions.push({ data: data, imports: fun.imports, name: fun.functionName });
};
ObjectProperty.prototype.addProperty = function (prop) {
var data = {
isDescription: !!prop.description,
description: (0, split_by_line_break_1.splitByLineBreak)(prop.description),
required: prop.required,
value: prop.value,
propertyName: prop.propertyName,
isArray: prop.isArray,
};
this.properties.push({ data: data, import: prop.import, name: data.propertyName });
};
ObjectProperty.prototype.render = function () {
var _this = this;
var renderedFunctions = this.functions.map(function (fun) {
return {
imports: fun.imports || [],
name: fun.name,
render: (0, render_mustache_1.renderMustache)('function-class.mustache', fun.data)
};
}).sort(function (a, b) { return a.name.localeCompare(b.name); });
var renderProperties = this.properties.map(function (prop) {
return { import: prop.import, name: prop.name, render: (0, render_mustache_1.renderMustache)('property-class.mustache', prop.data) };
}).sort(function (a, b) { return a.name.localeCompare(b.name); });
var isFunction = renderedFunctions.length > 0;
renderedFunctions.forEach(function (fun) {
var _a;
return (_a = _this.imports).push.apply(_a, __spreadArray([], __read(fun.imports), false));
});
renderProperties.forEach(function (property) { return _this.imports.push(property.import); });
var viewData = {
className: this.className,
isInterface: Object.values(this.functions).length == 0,
isImport: this.imports.get().length > 0,
imports: this.imports.get().sort(),
isDescription: (this.description || '').length > 0,
description: this.description,
isFunction: isFunction,
function: renderedFunctions.map(function (rf) { return (0, split_by_line_break_1.splitByLineBreak)(rf.render); }),
isProperties: renderProperties.length > 0,
properties: renderProperties.map(function (rf) { return (0, split_by_line_break_1.splitByLineBreak)(rf.render); })
};
return {
classEnumName: this.className,
fileName: this.fileName,
render: (0, render_mustache_1.renderMustache)('service-class.mustache', viewData)
};
};
return ObjectProperty;
}());
exports.ObjectProperty = ObjectProperty;
//# sourceMappingURL=object-property.js.map