@frostup/swr-request-generator
Version:
A tool for generating TypeScript code and interface from swagger by using SWR and axios as client.
131 lines (130 loc) • 5.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaResolver = void 0;
const lodash_1 = require("lodash");
const specifications_1 = require("../utils/specifications");
const formatters_1 = require("../utils/formatters");
const constants_1 = require("../constants");
class SchemaResolver {
constructor(inputs) {
this.inputs = inputs;
this.schemaType = {};
this.getSchemaType = () => this.schemaType;
this.resolve = (type) => {
const { schema = {}, results, parentKey, key } = this.inputs;
const advancedType = this.resolveRef(schema.$ref, type || schema.type);
if (schema.$ref) {
this.schemaType = advancedType;
return this;
}
if (schema.allOf || schema.oneOf || schema.anyOf) {
const schemas = schema.oneOf || schema.allOf || schema.anyOf;
this.schemaType = this.resolveRef((schemas !== null && schemas !== void 0 ? schemas : [])[0].$ref, type || schema.type);
return this;
}
if (schema.items) {
this.schemaType = this.resolveItems(schema.items, schema.type, key, parentKey);
return this;
}
if (schema.enum) {
const enumKey = this.getEnumName(key, parentKey);
// Implicit operation!: Assign enum array definition to results
results[enumKey] = schema.enum;
this.schemaType = enumKey;
return this;
}
if (schema.type === "object") {
if (schema.properties) {
this.schemaType = this.resolveProperties(schema.properties, schema.required, parentKey);
return this;
}
if (schema.title) {
this.schemaType = schema.type;
return this;
}
this.schemaType = "{[key:string]:any}";
return this;
}
if (schema.type === "string" && schema.format === "binary") {
this.schemaType = "FormData";
return this;
}
this.schemaType = this.getBasicType(schema.type, advancedType);
return this;
};
this.resolveNullable = () => {
var _a;
if ((_a = this.inputs.schema) === null || _a === void 0 ? void 0 : _a.nullable) {
this.schemaType = specifications_1.isObject(this.schemaType)
? `${JSON.stringify(this.schemaType)} | null`
: `${this.schemaType} | null`;
}
return this;
};
this.getEnumName = (propertyName, parentKey = "") => `${formatters_1.toCapitalCase(parentKey)}${formatters_1.toCapitalCase(propertyName)}${constants_1.ENUM_SUFFIX}`;
this.resolveRef = ($ref, type) => {
if (!$ref) {
return "";
}
const refType = formatters_1.addPrefixForInterface(formatters_1.toCapitalCase(this.pickTypeByRef($ref)));
return type === "array" ? `${refType}[]` : refType;
};
this.getBasicType = (basicType = "", advancedType) => {
switch (basicType) {
case "integer":
return "number";
case "array":
return this.getTypeForArray(advancedType);
case "":
return advancedType || "";
default:
return basicType;
}
};
this.getTypeForArray = (advancedType) => (advancedType ? `${advancedType}[]` : "Array<any>");
this.pickTypeByRef = (str) => {
if (!str) {
return;
}
const list = str.split("/");
return list[list.length - 1];
};
this.resolveItems = (items, type, key, parentKey) => {
if (!items) {
return {};
}
const child = lodash_1.get(items, "items");
if (type === "array") {
if (child) {
return `${this.resolveItems(child, items.type, key, parentKey)}[]`;
}
if (!lodash_1.get(items, "$ref")) {
return `${lodash_1.get(items, "type")}[]`;
}
}
if (specifications_1.isArray(items)) {
return lodash_1.map(items, (item) => SchemaResolver.of({ results: this.inputs.results, schema: item, key, parentKey })
.resolve()
.resolveNullable()
.getSchemaType());
}
return SchemaResolver.of({ results: this.inputs.results, schema: items, key, parentKey })
.resolve(type)
.resolveNullable()
.getSchemaType();
};
this.resolveProperties = (properties = {}, required = [], parentKey) => lodash_1.reduce(properties, (o, v, k) => (Object.assign(Object.assign({}, o), { [`${k}${lodash_1.indexOf(required, k) > -1 ? "" : "?"}`]: SchemaResolver.of({
results: this.inputs.results,
schema: v,
key: k,
parentKey,
})
.resolve()
.resolveNullable()
.getSchemaType() })), {});
}
static of(inputs) {
return new SchemaResolver(inputs);
}
}
exports.SchemaResolver = SchemaResolver;