2020-04-05 00:57:34 +09:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-06-13 19:03:12 +09:00
|
|
|
exports.apiFunctionsOf = exports.SchemaType = exports.resolveRef = exports.isObjectSchema = exports.isArraySchema = exports.ELParameterIn = void 0;
|
2020-04-05 00:57:34 +09:00
|
|
|
var StrictTypeParser_1 = require("./utils/StrictTypeParser");
|
|
|
|
var warn = function (x) { return console.warn('\x1b[1;33mWarning: ' + x + '\x1b[0m'); };
|
|
|
|
var ELMethod = ['get', 'put', 'post', 'delete', 'patch'];
|
|
|
|
exports.ELParameterIn = [
|
|
|
|
'path', 'query', 'header', 'cookie'
|
|
|
|
];
|
|
|
|
function isArraySchema(x) {
|
|
|
|
return x.type === 'array';
|
|
|
|
}
|
|
|
|
exports.isArraySchema = isArraySchema;
|
|
|
|
function isObjectSchema(x) {
|
|
|
|
return x.type === 'object';
|
|
|
|
}
|
|
|
|
exports.isObjectSchema = isObjectSchema;
|
|
|
|
function isReference(x) {
|
|
|
|
return typeof x.$ref === 'string';
|
|
|
|
}
|
|
|
|
// api
|
|
|
|
var APIFunction = /** @class */ (function () {
|
|
|
|
function APIFunction(method, url, reqTypes, resTypes) {
|
|
|
|
this.method = method;
|
|
|
|
this.url = url;
|
|
|
|
this.reqTypes = reqTypes;
|
|
|
|
this.resTypes = resTypes;
|
|
|
|
}
|
|
|
|
return APIFunction;
|
|
|
|
}());
|
|
|
|
/* ==== ==== */
|
2020-05-24 15:15:53 +09:00
|
|
|
// Reference
|
|
|
|
function resolveRef(obj, dict, prefix) {
|
|
|
|
do {
|
|
|
|
if (!isReference(obj))
|
|
|
|
return obj;
|
|
|
|
var ref = obj.$ref;
|
|
|
|
if (ref.startsWith(prefix)) {
|
|
|
|
var name_1 = ref.substring(prefix.length + 1); // $prefix/
|
|
|
|
var obj0 = dict === null || dict === void 0 ? void 0 : dict[name_1];
|
|
|
|
if (obj0 === undefined) {
|
2022-06-13 19:03:12 +09:00
|
|
|
console.error("ref not found: ".concat(ref));
|
2020-05-24 15:15:53 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
obj = obj0;
|
|
|
|
}
|
|
|
|
else {
|
2022-06-13 19:03:12 +09:00
|
|
|
console.error("Invalid ref: ".concat(ref, ", expect prefix ").concat(prefix));
|
2020-05-24 15:15:53 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
exports.resolveRef = resolveRef;
|
2020-04-05 00:57:34 +09:00
|
|
|
function mediaTypes2type(content, required) {
|
|
|
|
var media = content === null || content === void 0 ? void 0 : content['application/json']; // TODO
|
|
|
|
if (media == null) {
|
|
|
|
if (Object.keys(content !== null && content !== void 0 ? content : {}).length > 0) {
|
|
|
|
warn('only support application/json now');
|
|
|
|
}
|
2020-08-17 14:12:34 +09:00
|
|
|
return new SchemaType('any', false, false);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
// schema
|
|
|
|
var schema = media.schema;
|
2020-08-17 14:12:34 +09:00
|
|
|
return new SchemaType(schema !== null && schema !== void 0 ? schema : 'any', required !== null && required !== void 0 ? required : false, false);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
var SchemaType = /** @class */ (function () {
|
2020-08-17 14:12:34 +09:00
|
|
|
function SchemaType(schema, _required, _sameFile) {
|
2020-04-05 00:57:34 +09:00
|
|
|
this._required = _required;
|
2020-08-17 14:12:34 +09:00
|
|
|
this._sameFile = _sameFile;
|
2020-04-05 00:57:34 +09:00
|
|
|
this.schema = typeof schema === 'string' ? { type: schema } : schema;
|
|
|
|
}
|
|
|
|
Object.defineProperty(SchemaType.prototype, "typeName", {
|
|
|
|
get: function () {
|
|
|
|
var _a;
|
2020-08-17 14:12:34 +09:00
|
|
|
return (_a = this._typeName) !== null && _a !== void 0 ? _a : (this._typeName = SchemaType.typeNameOf(this.schema, this._sameFile));
|
2020-04-05 00:57:34 +09:00
|
|
|
},
|
2022-06-13 19:03:12 +09:00
|
|
|
enumerable: false,
|
2020-04-05 00:57:34 +09:00
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
Object.defineProperty(SchemaType.prototype, "required", {
|
|
|
|
get: function () {
|
|
|
|
return this._required;
|
|
|
|
},
|
2022-06-13 19:03:12 +09:00
|
|
|
enumerable: false,
|
2020-04-05 00:57:34 +09:00
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
Object.defineProperty(SchemaType.prototype, "maxSize", {
|
|
|
|
get: function () {
|
|
|
|
return this.schema.maxSize;
|
|
|
|
},
|
2022-06-13 19:03:12 +09:00
|
|
|
enumerable: false,
|
2020-04-05 00:57:34 +09:00
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
SchemaType.prototype.forProp = function (prop) {
|
2022-06-13 19:03:12 +09:00
|
|
|
return "".concat(prop).concat(this.required ? '' : '?', ": ").concat(this.typeName);
|
2020-04-05 00:57:34 +09:00
|
|
|
};
|
2020-08-17 14:12:34 +09:00
|
|
|
SchemaType.prototype.stp = function (prop, label, partial, sameFile) {
|
2020-04-08 20:17:57 +09:00
|
|
|
if (partial === void 0) { partial = false; }
|
2020-08-17 14:12:34 +09:00
|
|
|
if (sameFile === void 0) { sameFile = false; }
|
|
|
|
var stp = SchemaType.gcStp(prop, this.schema, label, partial, sameFile);
|
2022-06-13 19:03:12 +09:00
|
|
|
return (this.required ? '' : "".concat(prop, "===void 0 ? void 0 : ")) + stp;
|
2020-04-05 00:57:34 +09:00
|
|
|
};
|
2020-08-17 14:12:34 +09:00
|
|
|
SchemaType.typeNameOf = function (schema, sameFile) {
|
2020-04-05 00:57:34 +09:00
|
|
|
var _a;
|
|
|
|
if (isReference(schema)) {
|
|
|
|
var $ref = schema.$ref;
|
|
|
|
var typeName = (_a = /^#\/components\/schemas\/(\w+)$/g.exec($ref)) === null || _a === void 0 ? void 0 : _a[1];
|
|
|
|
if (typeName == null) {
|
2022-06-13 19:03:12 +09:00
|
|
|
warn("Invalid $ref, use any instead: ".concat($ref));
|
2020-04-05 00:57:34 +09:00
|
|
|
return 'any';
|
|
|
|
}
|
2022-06-13 19:03:12 +09:00
|
|
|
return sameFile ? typeName : "Schemas.".concat(typeName);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
var type = schema.type, format = schema.format, nullable = schema.nullable, readOnly = schema.readOnly;
|
|
|
|
var sType = type;
|
|
|
|
if (isArraySchema(schema)) {
|
2022-06-13 19:03:12 +09:00
|
|
|
sType = "Array<".concat(SchemaType.typeNameOf(schema.items, sameFile), ">");
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
else if (isObjectSchema(schema)) {
|
|
|
|
sType = '{';
|
|
|
|
for (var _i = 0, _b = Object.entries(schema.properties); _i < _b.length; _i++) {
|
2020-05-24 15:15:53 +09:00
|
|
|
var _c = _b[_i], name_2 = _c[0], sub = _c[1];
|
2022-06-13 19:03:12 +09:00
|
|
|
sType += "".concat(name_2, ": ").concat(SchemaType.typeNameOf(sub, sameFile), ", ");
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
sType += '}';
|
|
|
|
}
|
|
|
|
else if (type === 'string') {
|
|
|
|
if (format === 'date-time')
|
|
|
|
sType = 'Date';
|
|
|
|
else if (format === 'date')
|
|
|
|
sType = 'FullDate';
|
|
|
|
else if (format === 'byte')
|
|
|
|
sType = 'string'; // TODO Buffer
|
|
|
|
else if (format === 'binary')
|
|
|
|
sType = 'string'; // TODO Buffer
|
|
|
|
else if (format)
|
2022-06-13 19:03:12 +09:00
|
|
|
warn("Unknown format ".concat(format, ", use string instead"));
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
else if (type === 'integer')
|
|
|
|
sType = 'number'; // TODO integer
|
|
|
|
if (nullable)
|
2022-06-13 19:03:12 +09:00
|
|
|
sType = "".concat(sType, " | null");
|
2020-04-05 00:57:34 +09:00
|
|
|
if (readOnly)
|
2022-06-13 19:03:12 +09:00
|
|
|
sType = "Readonly<".concat(sType, ">");
|
2020-04-05 00:57:34 +09:00
|
|
|
return sType;
|
|
|
|
};
|
2020-08-17 14:12:34 +09:00
|
|
|
SchemaType.gcStp = function (para, schema, label, partial, sameFile) {
|
2020-04-08 20:17:57 +09:00
|
|
|
// partial: Object only, 1 layer only
|
2020-04-05 00:57:34 +09:00
|
|
|
// object
|
|
|
|
if (isReference(schema)) {
|
2020-08-17 14:12:34 +09:00
|
|
|
var typeName = new SchemaType(schema, true, sameFile).typeName;
|
2022-06-13 19:03:12 +09:00
|
|
|
return "".concat(typeName, ".").concat(partial ? 'Partial' : 'from', "(").concat(para, ")");
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
// any
|
|
|
|
var type = schema.type, nullable = schema.nullable, format = schema.format;
|
2020-04-08 20:17:57 +09:00
|
|
|
var sStp;
|
2020-04-05 00:57:34 +09:00
|
|
|
if (type === 'any')
|
|
|
|
return para;
|
|
|
|
if (isArraySchema(schema)) {
|
2022-06-13 19:03:12 +09:00
|
|
|
sStp = "(v, l)=>STP._Array(v, l, elm=>".concat(SchemaType.gcStp('elm', schema.items, "".concat(label, "[]"), false, sameFile), ")");
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
else if (isObjectSchema(schema)) {
|
2020-04-08 20:17:57 +09:00
|
|
|
sStp = '()=>({';
|
2020-04-05 00:57:34 +09:00
|
|
|
for (var _i = 0, _a = Object.entries(schema.properties); _i < _a.length; _i++) {
|
2020-05-24 15:15:53 +09:00
|
|
|
var _b = _a[_i], name_3 = _b[0], sub = _b[1];
|
2022-06-13 19:03:12 +09:00
|
|
|
sStp += "".concat(name_3, ": ").concat(SchemaType.gcStp(para + '.' + name_3, sub, label + '.' + name_3, false, sameFile), ", ");
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
2020-04-08 20:17:57 +09:00
|
|
|
sStp += '})';
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var t = void 0;
|
|
|
|
if (type === 'string') {
|
|
|
|
if (format === 'date-time')
|
|
|
|
t = 'Date';
|
|
|
|
else if (format === 'date')
|
|
|
|
t = 'FullDate';
|
|
|
|
else if (format === 'byte')
|
2020-04-08 20:17:57 +09:00
|
|
|
t = 'byte';
|
2020-04-05 00:57:34 +09:00
|
|
|
else if (format === 'binary')
|
2020-04-08 20:17:57 +09:00
|
|
|
t = 'binary';
|
2020-04-05 00:57:34 +09:00
|
|
|
else {
|
2020-04-08 20:17:57 +09:00
|
|
|
if (format) {
|
2022-06-13 19:03:12 +09:00
|
|
|
warn("Unknown string format ".concat(format, ", use string instead"));
|
2020-04-08 20:17:57 +09:00
|
|
|
}
|
2020-04-05 00:57:34 +09:00
|
|
|
t = 'string';
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 20:17:57 +09:00
|
|
|
else if (type === 'integer') {
|
|
|
|
if (format === 'int32')
|
|
|
|
t = 'int32';
|
|
|
|
else {
|
2022-06-13 19:03:12 +09:00
|
|
|
if (format && format !== 'int64') {
|
|
|
|
warn("Unsupport integer format ".concat(format, ", use number instead"));
|
2020-05-20 05:17:08 +09:00
|
|
|
}
|
2020-04-08 20:17:57 +09:00
|
|
|
t = 'number'; // TODO int64
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 00:57:34 +09:00
|
|
|
else
|
|
|
|
t = type;
|
|
|
|
if (!StrictTypeParser_1.StrictTypeParser.supportTypes.includes(t)) {
|
2022-06-13 19:03:12 +09:00
|
|
|
warn("Unsupport type ".concat(type, " ").concat(format, ", use any instead"));
|
2020-04-05 00:57:34 +09:00
|
|
|
return para;
|
|
|
|
}
|
2022-06-13 19:03:12 +09:00
|
|
|
sStp = "STP._".concat(t);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
// nullable
|
2020-04-08 20:17:57 +09:00
|
|
|
var funcName = nullable ? 'nullableParse' : 'parse';
|
|
|
|
// result
|
2022-06-13 19:03:12 +09:00
|
|
|
var sLabel = "'".concat(label.replace(/'/g, '\\\''), "'"); // escape
|
|
|
|
return "STP.".concat(funcName, "(").concat(sStp, ", ").concat(para, ", ").concat(sLabel, ")");
|
2020-04-05 00:57:34 +09:00
|
|
|
};
|
|
|
|
return SchemaType;
|
|
|
|
}());
|
|
|
|
exports.SchemaType = SchemaType;
|
|
|
|
function apiFunctionsOf(openAPI) {
|
2020-05-24 15:15:53 +09:00
|
|
|
var paths = openAPI.paths, comps = openAPI.components;
|
|
|
|
var compPrefix = '#/components/';
|
2020-04-05 00:57:34 +09:00
|
|
|
var functions = {};
|
|
|
|
for (var _i = 0, _a = Object.entries(paths); _i < _a.length; _i++) {
|
|
|
|
var _b = _a[_i], url = _b[0], pathItem = _b[1];
|
|
|
|
for (var _c = 0, ELMethod_1 = ELMethod; _c < ELMethod_1.length; _c++) {
|
|
|
|
var method = ELMethod_1[_c];
|
|
|
|
var op = pathItem[method];
|
|
|
|
if (op == null)
|
|
|
|
continue;
|
|
|
|
// operationId
|
|
|
|
var operationId = op.operationId, parameters = op.parameters, requestBody = op.requestBody, responses = op.responses;
|
|
|
|
if (operationId == null) {
|
2022-06-13 19:03:12 +09:00
|
|
|
warn("ignore operation in ".concat(method, " ").concat(url, ": ") +
|
2020-04-05 00:57:34 +09:00
|
|
|
'operationId should be given');
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-24 15:15:53 +09:00
|
|
|
var name_4 = operationId;
|
2020-04-05 00:57:34 +09:00
|
|
|
var reqTypes = {};
|
|
|
|
var resTypes = {};
|
|
|
|
// reqParas
|
|
|
|
if (parameters != null) {
|
|
|
|
for (var _d = 0, parameters_1 = parameters; _d < parameters_1.length; _d++) {
|
2020-05-24 15:15:53 +09:00
|
|
|
var rPara = parameters_1[_d];
|
|
|
|
var para = resolveRef(rPara, comps === null || comps === void 0 ? void 0 : comps.parameters, compPrefix + 'parameters');
|
|
|
|
if (para == null)
|
|
|
|
continue;
|
|
|
|
var name_5 = para.name, _in = para.in, required = para.required, schema = para.schema;
|
2020-04-05 00:57:34 +09:00
|
|
|
// add
|
|
|
|
if (reqTypes[_in] == null)
|
|
|
|
reqTypes[_in] = {};
|
2020-08-17 14:12:34 +09:00
|
|
|
reqTypes[_in][name_5] = new SchemaType(schema !== null && schema !== void 0 ? schema : 'any', required !== null && required !== void 0 ? required : false, false);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// requestBody
|
|
|
|
if (requestBody != null) {
|
2020-05-24 15:15:53 +09:00
|
|
|
var requestBodyO = resolveRef(requestBody, comps === null || comps === void 0 ? void 0 : comps.requestBodies, compPrefix + 'requestBodies');
|
|
|
|
if (requestBodyO == null)
|
|
|
|
continue;
|
|
|
|
reqTypes.body = mediaTypes2type(requestBodyO.content, requestBodyO.required);
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
// responses
|
|
|
|
for (var _e = 0, _f = Object.entries(responses); _e < _f.length; _e++) {
|
2020-05-24 15:15:53 +09:00
|
|
|
var _g = _f[_e], status_1 = _g[0], rRes = _g[1];
|
|
|
|
var res = resolveRef(rRes, comps === null || comps === void 0 ? void 0 : comps.responses, compPrefix + 'responses');
|
|
|
|
if (res == null)
|
|
|
|
continue;
|
2020-04-05 00:57:34 +09:00
|
|
|
resTypes[status_1] = mediaTypes2type(res.content, true);
|
|
|
|
}
|
|
|
|
// add to group
|
|
|
|
var saf = new APIFunction(method, url, reqTypes, resTypes);
|
2020-05-24 15:15:53 +09:00
|
|
|
functions[name_4] = saf;
|
2020-04-05 00:57:34 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return functions;
|
|
|
|
}
|
|
|
|
exports.apiFunctionsOf = apiFunctionsOf;
|