[v2.1.0] simplify generated code of apiRouter; remove utils symlink
This commit is contained in:
parent
07c6bc4671
commit
c73c5c7088
26 changed files with 790 additions and 645 deletions
|
@ -1,3 +1,2 @@
|
|||
dist/
|
||||
./utils/
|
||||
node_modules/
|
||||
|
|
33
.eslintrc.js
33
.eslintrc.js
|
@ -1,33 +1,20 @@
|
|||
module.exports = {
|
||||
'env': {
|
||||
env: {
|
||||
'es6': true,
|
||||
'node': true
|
||||
'node': true,
|
||||
},
|
||||
'extends': [
|
||||
'google'
|
||||
extends: [
|
||||
'@sup39/eslint-config-typescript',
|
||||
],
|
||||
'globals': {
|
||||
globals: {
|
||||
'Atomics': 'readonly',
|
||||
'SharedArrayBuffer': 'readonly'
|
||||
'SharedArrayBuffer': 'readonly',
|
||||
},
|
||||
'parser': '@typescript-eslint/parser',
|
||||
'parserOptions': {
|
||||
parserOptions: {
|
||||
'ecmaVersion': 2018,
|
||||
'sourceType': 'module'
|
||||
'sourceType': 'module',
|
||||
},
|
||||
'plugins': [
|
||||
'@typescript-eslint'
|
||||
],
|
||||
'rules': {
|
||||
'require-jsdoc': 'off',
|
||||
'arrow-parens': ['error', 'as-needed'],
|
||||
'indent': ['error', 2, {'MemberExpression': 1}],
|
||||
// no-unused-vars except ts interface
|
||||
'no-unused-vars': 'off',
|
||||
'@typescript-eslint/no-unused-vars': ['error', {
|
||||
'vars': 'all',
|
||||
'args': 'after-used',
|
||||
'ignoreRestSiblings': false
|
||||
}],
|
||||
rules: {
|
||||
'no-prototype-builtins': 'off',
|
||||
},
|
||||
};
|
||||
|
|
|
@ -608,6 +608,9 @@ interface Task {
|
|||
This tool only supports `application/json` type for request and response body. Any other type like `multipart/form` or `image/*` are **not supported** and will be ignored.
|
||||
|
||||
## Versions
|
||||
#### 2.1.0
|
||||
- Move `@sup39/api-ts-gen/utils` to `@sup39/api-ts-gen/dist/utils` since symlink is no longer allowed
|
||||
- Simplify `apiRouter.ts` by using `router.use` to check for `BadValueError`
|
||||
#### 2.0.6
|
||||
- Change Full#advance and implement Full#advanced
|
||||
- Nest [object-type properties with id property] conversion in request function
|
||||
|
|
|
@ -65,9 +65,9 @@ function parseArgv(argv) {
|
|||
|
||||
for (const arg of argv) {
|
||||
if (arg.startsWith('-')) {
|
||||
if (arg.length == 1) {
|
||||
if (arg.length === 1) {
|
||||
return badArgv(`Unexpected token: -`);
|
||||
} else if (arg[1] == '-') {
|
||||
} else if (arg[1] === '-') {
|
||||
// flag name
|
||||
setFlag(arg.substring(2));
|
||||
} else {
|
||||
|
|
3
dist/CodePrinter.js
vendored
3
dist/CodePrinter.js
vendored
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CodePrinter = exports.StringStream = void 0;
|
||||
var StringStream = /** @class */ (function () {
|
||||
function StringStream() {
|
||||
this.content = '';
|
||||
|
@ -29,7 +30,7 @@ var CodePrinter = /** @class */ (function () {
|
|||
if (pretab === void 0) { pretab = dIndent < 0; }
|
||||
if (pretab)
|
||||
this.cIndent = Math.max(0, this.cIndent + dIndent);
|
||||
this.write(this.indentString.repeat(this.cIndent) + s + "\n");
|
||||
this.write("".concat(this.indentString.repeat(this.cIndent) + s, "\n"));
|
||||
if (!pretab)
|
||||
this.cIndent = Math.max(0, this.cIndent + dIndent);
|
||||
};
|
||||
|
|
3
dist/Config.js
vendored
3
dist/Config.js
vendored
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.configDefault = void 0;
|
||||
exports.configDefault = {
|
||||
// format
|
||||
interfacePrefix: 'I',
|
||||
|
@ -11,7 +12,7 @@ exports.configDefault = {
|
|||
routerName: 'apiRouter',
|
||||
// TS path
|
||||
ServerAPITSPath: '#ServerAPI',
|
||||
utilsTSPath: '@sup39/api-ts-gen/utils',
|
||||
utilsTSPath: '@sup39/api-ts-gen/dist/utils',
|
||||
// other
|
||||
outputDir: 'api/generated',
|
||||
validateStatus: function (status) { return /^2..$/.test(status); },
|
||||
|
|
51
dist/OpenAPI.js
vendored
51
dist/OpenAPI.js
vendored
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.apiFunctionsOf = exports.SchemaType = exports.resolveRef = exports.isObjectSchema = exports.isArraySchema = exports.ELParameterIn = void 0;
|
||||
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'];
|
||||
|
@ -38,13 +39,13 @@ function resolveRef(obj, dict, 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) {
|
||||
console.error("ref not found: " + ref);
|
||||
console.error("ref not found: ".concat(ref));
|
||||
return;
|
||||
}
|
||||
obj = obj0;
|
||||
}
|
||||
else {
|
||||
console.error("Invalid ref: " + ref + ", expect prefix " + prefix);
|
||||
console.error("Invalid ref: ".concat(ref, ", expect prefix ").concat(prefix));
|
||||
return;
|
||||
}
|
||||
} while (true);
|
||||
|
@ -73,31 +74,31 @@ var SchemaType = /** @class */ (function () {
|
|||
var _a;
|
||||
return (_a = this._typeName) !== null && _a !== void 0 ? _a : (this._typeName = SchemaType.typeNameOf(this.schema, this._sameFile));
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(SchemaType.prototype, "required", {
|
||||
get: function () {
|
||||
return this._required;
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(SchemaType.prototype, "maxSize", {
|
||||
get: function () {
|
||||
return this.schema.maxSize;
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
SchemaType.prototype.forProp = function (prop) {
|
||||
return "" + prop + (this.required ? '' : '?') + ": " + this.typeName;
|
||||
return "".concat(prop).concat(this.required ? '' : '?', ": ").concat(this.typeName);
|
||||
};
|
||||
SchemaType.prototype.stp = function (prop, label, partial, sameFile) {
|
||||
if (partial === void 0) { partial = false; }
|
||||
if (sameFile === void 0) { sameFile = false; }
|
||||
var stp = SchemaType.gcStp(prop, this.schema, label, partial, sameFile);
|
||||
return (this.required ? '' : prop + "===void 0 ? void 0 : ") + stp;
|
||||
return (this.required ? '' : "".concat(prop, "===void 0 ? void 0 : ")) + stp;
|
||||
};
|
||||
SchemaType.typeNameOf = function (schema, sameFile) {
|
||||
var _a;
|
||||
|
@ -105,21 +106,21 @@ var SchemaType = /** @class */ (function () {
|
|||
var $ref = schema.$ref;
|
||||
var typeName = (_a = /^#\/components\/schemas\/(\w+)$/g.exec($ref)) === null || _a === void 0 ? void 0 : _a[1];
|
||||
if (typeName == null) {
|
||||
warn("Invalid $ref, use any instead: " + $ref);
|
||||
warn("Invalid $ref, use any instead: ".concat($ref));
|
||||
return 'any';
|
||||
}
|
||||
return sameFile ? typeName : "Schemas." + typeName;
|
||||
return sameFile ? typeName : "Schemas.".concat(typeName);
|
||||
}
|
||||
var type = schema.type, format = schema.format, nullable = schema.nullable, readOnly = schema.readOnly;
|
||||
var sType = type;
|
||||
if (isArraySchema(schema)) {
|
||||
sType = "Array<" + SchemaType.typeNameOf(schema.items, sameFile) + ">";
|
||||
sType = "Array<".concat(SchemaType.typeNameOf(schema.items, sameFile), ">");
|
||||
}
|
||||
else if (isObjectSchema(schema)) {
|
||||
sType = '{';
|
||||
for (var _i = 0, _b = Object.entries(schema.properties); _i < _b.length; _i++) {
|
||||
var _c = _b[_i], name_2 = _c[0], sub = _c[1];
|
||||
sType += name_2 + ": " + SchemaType.typeNameOf(sub, sameFile) + ", ";
|
||||
sType += "".concat(name_2, ": ").concat(SchemaType.typeNameOf(sub, sameFile), ", ");
|
||||
}
|
||||
sType += '}';
|
||||
}
|
||||
|
@ -133,14 +134,14 @@ var SchemaType = /** @class */ (function () {
|
|||
else if (format === 'binary')
|
||||
sType = 'string'; // TODO Buffer
|
||||
else if (format)
|
||||
warn("Unknown format " + format + ", use string instead");
|
||||
warn("Unknown format ".concat(format, ", use string instead"));
|
||||
}
|
||||
else if (type === 'integer')
|
||||
sType = 'number'; // TODO integer
|
||||
if (nullable)
|
||||
sType = sType + " | null";
|
||||
sType = "".concat(sType, " | null");
|
||||
if (readOnly)
|
||||
sType = "Readonly<" + sType + ">";
|
||||
sType = "Readonly<".concat(sType, ">");
|
||||
return sType;
|
||||
};
|
||||
SchemaType.gcStp = function (para, schema, label, partial, sameFile) {
|
||||
|
@ -148,7 +149,7 @@ var SchemaType = /** @class */ (function () {
|
|||
// object
|
||||
if (isReference(schema)) {
|
||||
var typeName = new SchemaType(schema, true, sameFile).typeName;
|
||||
return typeName + "." + (partial ? 'Partial' : 'from') + "(" + para + ")";
|
||||
return "".concat(typeName, ".").concat(partial ? 'Partial' : 'from', "(").concat(para, ")");
|
||||
}
|
||||
// any
|
||||
var type = schema.type, nullable = schema.nullable, format = schema.format;
|
||||
|
@ -156,13 +157,13 @@ var SchemaType = /** @class */ (function () {
|
|||
if (type === 'any')
|
||||
return para;
|
||||
if (isArraySchema(schema)) {
|
||||
sStp = "(v, l)=>STP._Array(v, l, elm=>" + SchemaType.gcStp('elm', schema.items, label + "[]", false, sameFile) + ")";
|
||||
sStp = "(v, l)=>STP._Array(v, l, elm=>".concat(SchemaType.gcStp('elm', schema.items, "".concat(label, "[]"), false, sameFile), ")");
|
||||
}
|
||||
else if (isObjectSchema(schema)) {
|
||||
sStp = '()=>({';
|
||||
for (var _i = 0, _a = Object.entries(schema.properties); _i < _a.length; _i++) {
|
||||
var _b = _a[_i], name_3 = _b[0], sub = _b[1];
|
||||
sStp += name_3 + ": " + SchemaType.gcStp(para + '.' + name_3, sub, label + '.' + name_3, false, sameFile) + ", ";
|
||||
sStp += "".concat(name_3, ": ").concat(SchemaType.gcStp(para + '.' + name_3, sub, label + '.' + name_3, false, sameFile), ", ");
|
||||
}
|
||||
sStp += '})';
|
||||
}
|
||||
|
@ -179,7 +180,7 @@ var SchemaType = /** @class */ (function () {
|
|||
t = 'binary';
|
||||
else {
|
||||
if (format) {
|
||||
warn("Unknown string format " + format + ", use string instead");
|
||||
warn("Unknown string format ".concat(format, ", use string instead"));
|
||||
}
|
||||
t = 'string';
|
||||
}
|
||||
|
@ -188,8 +189,8 @@ var SchemaType = /** @class */ (function () {
|
|||
if (format === 'int32')
|
||||
t = 'int32';
|
||||
else {
|
||||
if (format && format != 'int64') {
|
||||
warn("Unsupport integer format " + format + ", use number instead");
|
||||
if (format && format !== 'int64') {
|
||||
warn("Unsupport integer format ".concat(format, ", use number instead"));
|
||||
}
|
||||
t = 'number'; // TODO int64
|
||||
}
|
||||
|
@ -197,16 +198,16 @@ var SchemaType = /** @class */ (function () {
|
|||
else
|
||||
t = type;
|
||||
if (!StrictTypeParser_1.StrictTypeParser.supportTypes.includes(t)) {
|
||||
warn("Unsupport type " + type + " " + format + ", use any instead");
|
||||
warn("Unsupport type ".concat(type, " ").concat(format, ", use any instead"));
|
||||
return para;
|
||||
}
|
||||
sStp = "STP._" + t;
|
||||
sStp = "STP._".concat(t);
|
||||
}
|
||||
// nullable
|
||||
var funcName = nullable ? 'nullableParse' : 'parse';
|
||||
// result
|
||||
var sLabel = "'" + label.replace(/'/g, '\\\'') + "'"; // escape
|
||||
return "STP." + funcName + "(" + sStp + ", " + para + ", " + sLabel + ")";
|
||||
var sLabel = "'".concat(label.replace(/'/g, '\\\''), "'"); // escape
|
||||
return "STP.".concat(funcName, "(").concat(sStp, ", ").concat(para, ", ").concat(sLabel, ")");
|
||||
};
|
||||
return SchemaType;
|
||||
}());
|
||||
|
@ -225,7 +226,7 @@ function apiFunctionsOf(openAPI) {
|
|||
// operationId
|
||||
var operationId = op.operationId, parameters = op.parameters, requestBody = op.requestBody, responses = op.responses;
|
||||
if (operationId == null) {
|
||||
warn("ignore operation in " + method + " " + url + ": " +
|
||||
warn("ignore operation in ".concat(method, " ").concat(url, ": ") +
|
||||
'operationId should be given');
|
||||
continue;
|
||||
}
|
||||
|
|
134
dist/codegen.js
vendored
134
dist/codegen.js
vendored
|
@ -8,9 +8,9 @@ var CodePrinter_1 = require("./CodePrinter");
|
|||
function codegenIHandler(funcs, config, cp) {
|
||||
var schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, clientOnly = config.clientOnly;
|
||||
// import
|
||||
cp.writeln("import * as Schemas from './" + schemasName + "'");
|
||||
cp.writeln("import * as Schemas from './".concat(schemasName, "'"));
|
||||
cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' +
|
||||
("from '" + utilsTSPath + "'"));
|
||||
"from '".concat(utilsTSPath, "'"));
|
||||
if (!clientOnly) {
|
||||
cp.writeln('import {RouterContext as CTX} from \'@koa/router\'');
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ function codegenIHandler(funcs, config, cp) {
|
|||
for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) {
|
||||
var _b = _a[_i], funcName = _b[0], func = _b[1];
|
||||
var reqTypes = func.reqTypes, resTypes = func.resTypes, method = func.method;
|
||||
cp.writeln(funcName + ": {", 1);
|
||||
cp.writeln("".concat(funcName, ": {"), 1);
|
||||
// req
|
||||
// req.path, ...
|
||||
cp.writeln("req: {", 1);
|
||||
|
@ -29,7 +29,7 @@ function codegenIHandler(funcs, config, cp) {
|
|||
var paras = reqTypes[_in];
|
||||
if (paras == null)
|
||||
continue;
|
||||
cp.writeln(_in + ": {", 1);
|
||||
cp.writeln("".concat(_in, ": {"), 1);
|
||||
for (var _d = 0, _e = Object.entries(paras); _d < _e.length; _d++) {
|
||||
var _f = _e[_d], propName = _f[0], schemaType = _f[1];
|
||||
cp.writeln(schemaType.forProp(propName) + ';');
|
||||
|
@ -41,9 +41,9 @@ function codegenIHandler(funcs, config, cp) {
|
|||
if (body != null) {
|
||||
// PATCH's req body: Partial
|
||||
var typeName = body.typeName;
|
||||
if (method == 'patch')
|
||||
typeName = "Partial<" + typeName + ">";
|
||||
cp.writeln("body" + (body.required ? '' : '?') + ": " + typeName + ";");
|
||||
if (method === 'patch')
|
||||
typeName = "Partial<".concat(typeName, ">");
|
||||
cp.writeln("body".concat(body.required ? '' : '?', ": ").concat(typeName, ";"));
|
||||
}
|
||||
cp.writeln('}', -1); // req END
|
||||
// res
|
||||
|
@ -51,7 +51,7 @@ function codegenIHandler(funcs, config, cp) {
|
|||
for (var _g = 0, _h = Object.entries(resTypes); _g < _h.length; _g++) {
|
||||
var _j = _h[_g], status_1 = _j[0], schema = _j[1];
|
||||
cp.writeln(schema.required ?
|
||||
schema.forProp(status_1) + ";" : status_1 + ": void;");
|
||||
"".concat(schema.forProp(status_1), ";") : "".concat(status_1, ": void;"));
|
||||
}
|
||||
cp.writeln('}', -1); // res END
|
||||
// operation END
|
||||
|
@ -75,23 +75,25 @@ function codegenIHandler(funcs, config, cp) {
|
|||
function codegenRouter(funcs, config, cp) {
|
||||
var schemasName = config.schemasName, IHandlerName = config.IHandlerName, ServerAPITSPath = config.ServerAPITSPath, utilsTSPath = config.utilsTSPath;
|
||||
// import
|
||||
cp.writeln("import * as Schemas from './" + schemasName + "'");
|
||||
cp.writeln("import {IServerAPI} from './" + IHandlerName + "'");
|
||||
cp.writeln("import * as Schemas from './".concat(schemasName, "'"));
|
||||
cp.writeln("import {IServerAPI} from './".concat(IHandlerName, "'"));
|
||||
cp.writeln("import * as Router from '@koa/router'");
|
||||
cp.writeln("import {FullDate, StrictTypeParser as STP} from '" + utilsTSPath + "'");
|
||||
cp.writeln("import {FullDate, StrictTypeParser as STP} from '".concat(utilsTSPath, "'"));
|
||||
cp.writeln("import * as bodyParser from 'koa-body'");
|
||||
// api
|
||||
cp.writeln("\nimport api from '" + ServerAPITSPath + "'");
|
||||
cp.writeln("\nimport api from '".concat(ServerAPITSPath, "'"));
|
||||
cp.writeln("type IState = typeof api extends IServerAPI<infer T> ? T : any;");
|
||||
// router
|
||||
cp.writeln("type CTX = Router.RouterContext<IState>;");
|
||||
cp.writeln("\nconst router = new Router<IState>();");
|
||||
// STP
|
||||
cp.writeln("router.use((ctx, next) => next().catch(err => {\n if (err instanceof STP.BadValueError) return ctx.throw(400, err.toString());\n throw err;\n}))");
|
||||
// function
|
||||
var gcGetParams = {
|
||||
path: function (attr) { return "ctx.params['" + attr + "']"; },
|
||||
query: function (attr) { return "ctx.query['" + attr + "']"; },
|
||||
header: function (attr) { return "ctx.headers['" + attr + "']"; },
|
||||
cookie: function (attr) { return "ctx.cookies.get('" + attr + "')"; },
|
||||
path: function (attr) { return "ctx.params['".concat(attr, "']"); },
|
||||
query: function (attr) { return "ctx.query['".concat(attr, "']"); },
|
||||
header: function (attr) { return "ctx.headers['".concat(attr, "']"); },
|
||||
cookie: function (attr) { return "ctx.cookies.get('".concat(attr, "')"); },
|
||||
};
|
||||
// route
|
||||
for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) {
|
||||
|
@ -103,49 +105,41 @@ function codegenRouter(funcs, config, cp) {
|
|||
var mid = '';
|
||||
if (reqTypes.body) {
|
||||
var maxSize = reqTypes.body.maxSize; // TODO doc
|
||||
var config_1 = maxSize == null ? '' : "{jsonLimit: '" + maxSize + "'}";
|
||||
mid = "bodyParser(" + config_1 + "), ";
|
||||
var config_1 = maxSize == null ? '' : "{jsonLimit: '".concat(maxSize, "'}");
|
||||
mid = "bodyParser(".concat(config_1, "), ");
|
||||
}
|
||||
cp.writeln("router." + method + "('" + sURL + "', " + mid + "async ctx => {", 1);
|
||||
cp.writeln("router.".concat(method, "('").concat(sURL, "', ").concat(mid, "async ctx => {"), 1);
|
||||
// req
|
||||
if (Object.keys(reqTypes).length === 0) {
|
||||
cp.writeln('const req = {};');
|
||||
}
|
||||
else {
|
||||
cp.writeln('let req;');
|
||||
cp.writeln('try {', 1);
|
||||
cp.writeln('req = {', 1);
|
||||
cp.writeln('const req = {', 1);
|
||||
// paras
|
||||
for (var _c = 0, ELParameterIn_2 = OpenAPI_1.ELParameterIn; _c < ELParameterIn_2.length; _c++) {
|
||||
var _in = ELParameterIn_2[_c];
|
||||
var paras = reqTypes[_in];
|
||||
if (paras == null)
|
||||
continue;
|
||||
cp.writeln(_in + ": {", 1);
|
||||
cp.writeln("".concat(_in, ": {"), 1);
|
||||
for (var _d = 0, _e = Object.entries(paras); _d < _e.length; _d++) {
|
||||
var _f = _e[_d], name_1 = _f[0], schema = _f[1];
|
||||
var pn = gcGetParams[_in](name_1);
|
||||
var label = "req." + _in;
|
||||
cp.writeln(name_1 + ": " + schema.stp(pn, label) + ",");
|
||||
var label = "req.".concat(_in);
|
||||
cp.writeln("".concat(name_1, ": ").concat(schema.stp(pn, label), ","));
|
||||
}
|
||||
cp.writeln('},', -1);
|
||||
}
|
||||
// body
|
||||
var body = reqTypes.body;
|
||||
if (body != null) {
|
||||
cp.writeln("body: " + body.stp('ctx.request.body', 'req.body', isPartial));
|
||||
cp.writeln("body: ".concat(body.stp('ctx.request.body', 'req.body', isPartial)));
|
||||
}
|
||||
cp.writeln('}', -1);
|
||||
cp.writeln('} catch(err) {', -1);
|
||||
cp.tab(1);
|
||||
cp.writeln('if (err instanceof STP.BadValueError)', 1);
|
||||
cp.writeln('return ctx.throw(400, err.toString());');
|
||||
cp.tab(-1);
|
||||
cp.writeln('throw err;');
|
||||
cp.writeln('}', -1);
|
||||
// req END
|
||||
cp.writeln('};', -1);
|
||||
}
|
||||
// call
|
||||
cp.writeln("const r = await api." + funcName + "(req, ctx.state, ctx);");
|
||||
cp.writeln("const r = await api.".concat(funcName, "(req, ctx.state, ctx);"));
|
||||
cp.writeln("ctx.status = r[0];");
|
||||
cp.writeln("ctx.body = r[1] ?? '';");
|
||||
// ctx END
|
||||
|
@ -157,10 +151,10 @@ function codegenRouter(funcs, config, cp) {
|
|||
function codegenClientAPI(funcs, config, cp) {
|
||||
var IHandlerName = config.IHandlerName, schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, validateStatus = config.validateStatus;
|
||||
// import
|
||||
cp.writeln("import {TAPI} from './" + IHandlerName + "'");
|
||||
cp.writeln("import * as Schemas from './" + schemasName + "'");
|
||||
cp.writeln("import {TAPI} from './".concat(IHandlerName, "'"));
|
||||
cp.writeln("import * as Schemas from './".concat(schemasName, "'"));
|
||||
cp.writeln("import {APIPromise, StrictTypeParser as STP, " +
|
||||
("qStringify} from '" + utilsTSPath + "'"));
|
||||
"qStringify} from '".concat(utilsTSPath, "'"));
|
||||
cp.writeln("import axios from 'axios'");
|
||||
cp.writeln('');
|
||||
// type
|
||||
|
@ -192,38 +186,38 @@ function codegenClientAPI(funcs, config, cp) {
|
|||
cp.writeln('}, err => Promise.reject(err));', -1);
|
||||
cp.writeln('},', -1);
|
||||
var _loop_1 = function (funcName, func) {
|
||||
var gcReq = function (_in) { return "TAPI['" + funcName + "']['req']['" + _in + "']"; };
|
||||
var gcReq = function (_in) { return "TAPI['".concat(funcName, "']['req']['").concat(_in, "']"); };
|
||||
var method = func.method, url = func.url, reqTypes = func.reqTypes, resTypes = func.resTypes;
|
||||
var query = reqTypes.query, header = reqTypes.header, path_1 = reqTypes.path, body = reqTypes.body; // TODO cookie
|
||||
// name
|
||||
cp.writeln(funcName + ": (", 1);
|
||||
cp.writeln("".concat(funcName, ": ("), 1);
|
||||
// paras
|
||||
for (var _i = 0, ELParameterIn_3 = OpenAPI_1.ELParameterIn; _i < ELParameterIn_3.length; _i++) {
|
||||
var _in = ELParameterIn_3[_i];
|
||||
for (var _c = 0, ELParameterIn_3 = OpenAPI_1.ELParameterIn; _c < ELParameterIn_3.length; _c++) {
|
||||
var _in = ELParameterIn_3[_c];
|
||||
var paras = reqTypes[_in];
|
||||
if (paras == null)
|
||||
continue;
|
||||
var _required = false;
|
||||
for (var _a = 0, _b = Object.values(paras); _a < _b.length; _a++) {
|
||||
var required = _b[_a].required;
|
||||
for (var _d = 0, _e = Object.values(paras); _d < _e.length; _d++) {
|
||||
var required = _e[_d].required;
|
||||
if (required) {
|
||||
_required = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cp.writeln(_in + ": " + gcReq(_in) + (_required ? '' : '={}') + ",");
|
||||
cp.writeln("".concat(_in, ": ").concat(gcReq(_in)).concat(_required ? '' : '={}', ","));
|
||||
}
|
||||
// body
|
||||
if (body != null) {
|
||||
cp.writeln("body" + (body.required ? '' : '?') + ": ExID<" + gcReq('body') + ">,");
|
||||
cp.writeln("body".concat(body.required ? '' : '?', ": ExID<").concat(gcReq('body'), ">,"));
|
||||
}
|
||||
// return value
|
||||
cp.tab(-1);
|
||||
cp.writeln(") => APIPromise.init($http({", 1);
|
||||
// req
|
||||
cp.writeln("method: '" + method + "',");
|
||||
var sURL = "'" + url + "'";
|
||||
cp.writeln("url: " + (path_1 ? "urlReplacer(" + sURL + ", path)" : sURL) + ",");
|
||||
cp.writeln("method: '".concat(method, "',"));
|
||||
var sURL = "'".concat(url, "'");
|
||||
cp.writeln("url: ".concat(path_1 ? "urlReplacer(".concat(sURL, ", path)") : sURL, ","));
|
||||
if (query)
|
||||
cp.writeln('params: query,');
|
||||
if (header)
|
||||
|
@ -233,15 +227,15 @@ function codegenClientAPI(funcs, config, cp) {
|
|||
cp.writeln('}), {', -1);
|
||||
cp.tab(1);
|
||||
// stp
|
||||
for (var _c = 0, _d = Object.entries(resTypes); _c < _d.length; _c++) {
|
||||
var _e = _d[_c], status_2 = _e[0], schema = _e[1];
|
||||
var label = "ClientAPI[" + funcName + "][" + status_2 + "]";
|
||||
cp.writeln(status_2 + ": x => " + schema.stp('x', label) + ",");
|
||||
for (var _f = 0, _g = Object.entries(resTypes); _f < _g.length; _f++) {
|
||||
var _h = _g[_f], status_2 = _h[0], schema = _h[1];
|
||||
var label = "ClientAPI[".concat(funcName, "][").concat(status_2, "]");
|
||||
cp.writeln("".concat(status_2, ": x => ").concat(schema.stp('x', label), ","));
|
||||
}
|
||||
cp.writeln("} as TSTP<TAPI['" + funcName + "']['res']>,", -1);
|
||||
cp.writeln("} as TSTP<TAPI['".concat(funcName, "']['res']>,"), -1);
|
||||
cp.tab(1);
|
||||
// kRsv
|
||||
cp.writeln("[" + Object.keys(resTypes).filter(validateStatus).join(', ') + "]),", -1);
|
||||
cp.writeln("[".concat(Object.keys(resTypes).filter(validateStatus).join(', '), "]),"), -1);
|
||||
};
|
||||
// functions
|
||||
for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) {
|
||||
|
@ -255,17 +249,17 @@ function codegenSchemas(schemas, config, cp) {
|
|||
var _a;
|
||||
var utilsTSPath = config.utilsTSPath;
|
||||
// import
|
||||
cp.writeln("import {FullDate, StrictTypeParser as STP} from '" + utilsTSPath + "'");
|
||||
cp.writeln("import {FullDate, StrictTypeParser as STP} from '".concat(utilsTSPath, "'"));
|
||||
// schema
|
||||
for (var _i = 0, _b = Object.entries(schemas); _i < _b.length; _i++) {
|
||||
var _c = _b[_i], typeName = _c[0], rSchema = _c[1];
|
||||
var schema = OpenAPI_1.resolveRef(rSchema, schemas, '#/components/schemas');
|
||||
var schema = (0, OpenAPI_1.resolveRef)(rSchema, schemas, '#/components/schemas');
|
||||
if (schema == null)
|
||||
continue;
|
||||
cp.writeln();
|
||||
if (OpenAPI_1.isObjectSchema(schema)) {
|
||||
if ((0, OpenAPI_1.isObjectSchema)(schema)) {
|
||||
// interface
|
||||
cp.writeln("export interface " + typeName + " {", 1);
|
||||
cp.writeln("export interface ".concat(typeName, " {"), 1);
|
||||
var propTypes = [];
|
||||
var requireds = new Set((_a = schema.required) !== null && _a !== void 0 ? _a : []);
|
||||
for (var _d = 0, _e = Object.entries(schema.properties); _d < _e.length; _d++) {
|
||||
|
@ -276,33 +270,33 @@ function codegenSchemas(schemas, config, cp) {
|
|||
}
|
||||
cp.writeln('}', -1); // interface END
|
||||
// const
|
||||
cp.writeln("export const " + typeName + " = {", 1);
|
||||
cp.writeln("export const ".concat(typeName, " = {"), 1);
|
||||
// .from
|
||||
cp.writeln("from: (o: {[_: string]: any}): " + typeName + " => ({", 1);
|
||||
cp.writeln("from: (o: {[_: string]: any}): ".concat(typeName, " => ({"), 1);
|
||||
for (var _g = 0, propTypes_1 = propTypes; _g < propTypes_1.length; _g++) {
|
||||
var _h = propTypes_1[_g], n = _h[0], t = _h[1];
|
||||
cp.writeln(n + ": " + t.stp("o." + n, typeName + '.' + n, false, true) + ",");
|
||||
cp.writeln("".concat(n, ": ").concat(t.stp("o.".concat(n), typeName + '.' + n, false, true), ","));
|
||||
}
|
||||
cp.writeln('}),', -1);
|
||||
// Partial
|
||||
cp.writeln("Partial: (o: {[_: string]: any}): Partial<" + typeName + "> => {", 1);
|
||||
cp.writeln("const r: Partial<" + typeName + "> = {};");
|
||||
var locPartial = "Partial<" + typeName + ">";
|
||||
cp.writeln("Partial: (o: {[_: string]: any}): Partial<".concat(typeName, "> => {"), 1);
|
||||
cp.writeln("const r: Partial<".concat(typeName, "> = {};"));
|
||||
var locPartial = "Partial<".concat(typeName, ">");
|
||||
for (var _j = 0, propTypes_2 = propTypes; _j < propTypes_2.length; _j++) {
|
||||
var _k = propTypes_2[_j], n = _k[0], t = _k[1];
|
||||
cp.writeln("if (o." + n + " !== void 0) r." + n + " = " + t.stp("o." + n, locPartial + '.' + n, false, true) + ";");
|
||||
cp.writeln("if (o.".concat(n, " !== void 0) r.").concat(n, " = ").concat(t.stp("o.".concat(n), locPartial + '.' + n, false, true), ";"));
|
||||
}
|
||||
cp.writeln('return r;');
|
||||
cp.writeln('},', -1);
|
||||
// fields
|
||||
cp.writeln("fields: [", 1);
|
||||
cp.writeln(propTypes.map(function (e) { return "'" + e[0] + "',"; }).join(' '));
|
||||
cp.writeln("] as Array<keyof " + typeName + ">", -1);
|
||||
cp.writeln(propTypes.map(function (e) { return "'".concat(e[0], "',"); }).join(' '));
|
||||
cp.writeln("] as Array<keyof ".concat(typeName, ">"), -1);
|
||||
// end of const
|
||||
cp.writeln('}', -1);
|
||||
}
|
||||
else {
|
||||
cp.writeln("export type " + typeName + " = " +
|
||||
cp.writeln("export type ".concat(typeName, " = ") +
|
||||
OpenAPI_1.SchemaType.typeNameOf(schema, true));
|
||||
}
|
||||
}
|
||||
|
@ -314,7 +308,7 @@ function codegen(openAPI, configUser) {
|
|||
var config = Object.assign({}, Config_1.configDefault, configUser);
|
||||
// prepare
|
||||
fs.mkdirSync(config.outputDir, { recursive: true });
|
||||
var apiFuncs = OpenAPI_1.apiFunctionsOf(openAPI);
|
||||
var apiFuncs = (0, OpenAPI_1.apiFunctionsOf)(openAPI);
|
||||
var gCP = function (fn) { return new CodePrinter_1.CodePrinter(fs.createWriteStream(path.join(config.outputDir, fn + '.ts')), config.indentString); };
|
||||
var ps = [];
|
||||
// write files
|
||||
|
|
22
dist/index.js
vendored
22
dist/index.js
vendored
|
@ -1,8 +1,20 @@
|
|||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.codegen = void 0;
|
||||
var codegen_1 = require("./codegen");
|
||||
exports.codegen = codegen_1.default;
|
||||
__export(require("./Config"));
|
||||
Object.defineProperty(exports, "codegen", { enumerable: true, get: function () { return codegen_1.default; } });
|
||||
__exportStar(require("./Config"), exports);
|
||||
|
|
7
dist/utils/APIPromise.js
vendored
7
dist/utils/APIPromise.js
vendored
|
@ -3,16 +3,19 @@ var __extends = (this && this.__extends) || (function () {
|
|||
var extendStatics = function (d, b) {
|
||||
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]; };
|
||||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
if (typeof b !== "function" && b !== null)
|
||||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.APIPromise = exports.BadResponseError = void 0;
|
||||
function typeGuard(checker) {
|
||||
return function (x) {
|
||||
return checker(x);
|
||||
|
@ -21,7 +24,7 @@ function typeGuard(checker) {
|
|||
var BadResponseError = /** @class */ (function (_super) {
|
||||
__extends(BadResponseError, _super);
|
||||
function BadResponseError(res, label) {
|
||||
var _this = _super.call(this, label + " status code: " + res.status + "\ndata: " + (typeof res.data === 'object' ? JSON.stringify(res.data) : res.data)) || this;
|
||||
var _this = _super.call(this, "".concat(label, " status code: ").concat(res.status, "\ndata: ").concat(typeof res.data === 'object' ? JSON.stringify(res.data) : res.data)) || this;
|
||||
_this.res = res;
|
||||
Object.setPrototypeOf(_this, BadResponseError.prototype);
|
||||
return _this;
|
||||
|
|
17
dist/utils/FullDate.js
vendored
17
dist/utils/FullDate.js
vendored
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FullDate = void 0;
|
||||
function removeTime(date) {
|
||||
return new Date(date.setHours(0, 0, 0, 0));
|
||||
}
|
||||
|
@ -11,9 +12,9 @@ var FullDate = /** @class */ (function () {
|
|||
}
|
||||
this._date = removeTime((function () {
|
||||
var _a, _b, _c;
|
||||
if (argv.length == 0)
|
||||
if (argv.length === 0)
|
||||
return new Date();
|
||||
if (argv.length == 1) {
|
||||
if (argv.length === 1) {
|
||||
var arg = argv[0];
|
||||
if (arg instanceof FullDate)
|
||||
return new Date(+arg);
|
||||
|
@ -32,7 +33,7 @@ var FullDate = /** @class */ (function () {
|
|||
FullDate.prototype.toString = function () {
|
||||
var d = this._date;
|
||||
var f = function (s) { return ('0' + s).slice(-2); };
|
||||
return d.getFullYear() + "-" + f(d.getMonth() + 1) + "-" + f(d.getDate());
|
||||
return "".concat(d.getFullYear(), "-").concat(f(d.getMonth() + 1), "-").concat(f(d.getDate()));
|
||||
};
|
||||
FullDate.prototype.toJSON = function () {
|
||||
return this.toString();
|
||||
|
@ -45,7 +46,7 @@ var FullDate = /** @class */ (function () {
|
|||
get: function () {
|
||||
return new Date(this._date);
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(FullDate.prototype, "year", {
|
||||
|
@ -56,7 +57,7 @@ var FullDate = /** @class */ (function () {
|
|||
set: function (val) {
|
||||
this._date.setFullYear(val);
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(FullDate.prototype, "month", {
|
||||
|
@ -66,7 +67,7 @@ var FullDate = /** @class */ (function () {
|
|||
set: function (val) {
|
||||
this._date.setMonth(val - 1);
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(FullDate.prototype, "day", {
|
||||
|
@ -76,14 +77,14 @@ var FullDate = /** @class */ (function () {
|
|||
set: function (val) {
|
||||
this._date.setDate(val);
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(FullDate.prototype, "dayOfWeek", {
|
||||
get: function () {
|
||||
return this._date.getDay();
|
||||
},
|
||||
enumerable: true,
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
// func
|
||||
|
|
1
dist/utils/StrictTypeParser.d.ts
vendored
1
dist/utils/StrictTypeParser.d.ts
vendored
|
@ -19,6 +19,7 @@ export declare module StrictTypeParser {
|
|||
function _byte(x: any, label: string): string;
|
||||
function _binary(x: any, label: string): string;
|
||||
function _Array<T>(x: any, label: string, mapper: (x: any) => T): Array<T>;
|
||||
function undefinedCheck(val: any, label: string): void;
|
||||
function parse<T>(stp: (val: any, label: string) => T, val: any, label: string): T;
|
||||
function nullableParse<T>(stp: (val: any, label: string) => T, val: any, label: string): T | null;
|
||||
const supportTypes: string[];
|
||||
|
|
16
dist/utils/StrictTypeParser.js
vendored
16
dist/utils/StrictTypeParser.js
vendored
|
@ -3,16 +3,19 @@ var __extends = (this && this.__extends) || (function () {
|
|||
var extendStatics = function (d, b) {
|
||||
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]; };
|
||||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
if (typeof b !== "function" && b !== null)
|
||||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StrictTypeParser = void 0;
|
||||
var FullDate_1 = require("./FullDate");
|
||||
var StrictTypeParser;
|
||||
(function (StrictTypeParser) {
|
||||
|
@ -31,8 +34,8 @@ var StrictTypeParser;
|
|||
var BadTypeError = /** @class */ (function (_super) {
|
||||
__extends(BadTypeError, _super);
|
||||
function BadTypeError(label, type, value) {
|
||||
var _this = _super.call(this, label, label + ": Can not convert `" + (['object', 'array'].includes(typeof value) ?
|
||||
JSON.stringify(value) : "" + value) + "` to type " + type) || this;
|
||||
var _this = _super.call(this, label, "".concat(label, ": Can not convert `").concat(['object', 'array'].includes(typeof value) ?
|
||||
JSON.stringify(value) : "".concat(value), "` to type ").concat(type)) || this;
|
||||
_this.label = label;
|
||||
_this.type = type;
|
||||
_this.value = value;
|
||||
|
@ -75,7 +78,7 @@ var StrictTypeParser;
|
|||
if (typeof x === 'boolean')
|
||||
return x;
|
||||
if (typeof x === 'number')
|
||||
return x != 0;
|
||||
return x !== 0;
|
||||
if (x === 'true')
|
||||
return true;
|
||||
if (x === 'false')
|
||||
|
@ -123,14 +126,15 @@ var StrictTypeParser;
|
|||
StrictTypeParser._Array = _Array;
|
||||
function undefinedCheck(val, label) {
|
||||
if (val === undefined) {
|
||||
throw new BadValueError(label, label + " is required, but got undefined");
|
||||
throw new BadValueError(label, "".concat(label, " is required, but got undefined"));
|
||||
}
|
||||
}
|
||||
StrictTypeParser.undefinedCheck = undefinedCheck;
|
||||
function parse(stp, val, label) {
|
||||
// body
|
||||
undefinedCheck(val, label);
|
||||
if (val === null) {
|
||||
throw new BadValueError(label, label + " is not nullable, but got null");
|
||||
throw new BadValueError(label, "".concat(label, " is not nullable, but got null"));
|
||||
}
|
||||
return stp(val, label);
|
||||
}
|
||||
|
|
25
dist/utils/index.js
vendored
25
dist/utils/index.js
vendored
|
@ -1,9 +1,20 @@
|
|||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__export(require("./APIPromise"));
|
||||
__export(require("./FullDate"));
|
||||
__export(require("./StrictTypeParser"));
|
||||
__export(require("./qStringify"));
|
||||
__exportStar(require("./APIPromise"), exports);
|
||||
__exportStar(require("./FullDate"), exports);
|
||||
__exportStar(require("./StrictTypeParser"), exports);
|
||||
__exportStar(require("./qStringify"), exports);
|
||||
|
|
1
dist/utils/qStringify.js
vendored
1
dist/utils/qStringify.js
vendored
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.qStringify = void 0;
|
||||
var Qs0 = require("qs");
|
||||
var options0 = {
|
||||
filter: function (prefix, value) {
|
||||
|
|
|
@ -5,7 +5,7 @@ interface WriteStream {
|
|||
}
|
||||
|
||||
export class StringStream implements WriteStream {
|
||||
private content = ''
|
||||
private content = '';
|
||||
write(s: string) {
|
||||
this.content += s;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ export const configDefault: ConfigOptional = {
|
|||
routerName: 'apiRouter',
|
||||
// TS path
|
||||
ServerAPITSPath: '#ServerAPI',
|
||||
utilsTSPath: '@sup39/api-ts-gen/utils',
|
||||
utilsTSPath: '@sup39/api-ts-gen/dist/utils',
|
||||
// other
|
||||
outputDir: 'api/generated',
|
||||
validateStatus: (status: string) => /^2..$/.test(status),
|
||||
|
|
|
@ -257,7 +257,7 @@ export class SchemaType {
|
|||
} else if (type === 'integer') {
|
||||
if (format === 'int32') t = 'int32';
|
||||
else {
|
||||
if (format && format != 'int64') {
|
||||
if (format && format !== 'int64') {
|
||||
warn(`Unsupport integer format ${format}, use number instead`);
|
||||
}
|
||||
t = 'number'; // TODO int64
|
||||
|
|
|
@ -42,7 +42,7 @@ function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
|||
if (body != null) {
|
||||
// PATCH's req body: Partial
|
||||
let {typeName} = body;
|
||||
if (method == 'patch') typeName = `Partial<${typeName}>`;
|
||||
if (method === 'patch') typeName = `Partial<${typeName}>`;
|
||||
cp.writeln(`body${body.required ? '' : '?'}: ${typeName};`);
|
||||
}
|
||||
cp.writeln('}', -1); // req END
|
||||
|
@ -89,6 +89,11 @@ function codegenRouter(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
|||
// router
|
||||
cp.writeln(`type CTX = Router.RouterContext<IState>;`);
|
||||
cp.writeln(`\nconst router = new Router<IState>();`);
|
||||
// STP
|
||||
cp.writeln(`router.use((ctx, next) => next().catch(err => {
|
||||
if (err instanceof STP.BadValueError) return ctx.throw(400, err.toString());
|
||||
throw err;
|
||||
}))`);
|
||||
// function
|
||||
const gcGetParams = {
|
||||
path: (attr: string) => `ctx.params['${attr}']`,
|
||||
|
@ -115,9 +120,7 @@ function codegenRouter(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
|||
if (Object.keys(reqTypes).length === 0) {
|
||||
cp.writeln('const req = {};');
|
||||
} else {
|
||||
cp.writeln('let req;');
|
||||
cp.writeln('try {', 1);
|
||||
cp.writeln('req = {', 1);
|
||||
cp.writeln('const req = {', 1);
|
||||
// paras
|
||||
for (const _in of ELParameterIn) {
|
||||
const paras = reqTypes[_in];
|
||||
|
@ -136,12 +139,8 @@ function codegenRouter(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
|||
cp.writeln(
|
||||
`body: ${body.stp('ctx.request.body', 'req.body', isPartial)}`);
|
||||
}
|
||||
cp.writeln('}', -1);
|
||||
cp.writeln('} catch(err) {', -1); cp.tab(1);
|
||||
cp.writeln('if (err instanceof STP.BadValueError)', 1);
|
||||
cp.writeln('return ctx.throw(400, err.toString());'); cp.tab(-1);
|
||||
cp.writeln('throw err;');
|
||||
cp.writeln('}', -1);
|
||||
// req END
|
||||
cp.writeln('};', -1);
|
||||
}
|
||||
// call
|
||||
cp.writeln(`const r = await api.${funcName}(req, ctx.state, ctx);`);
|
||||
|
|
|
@ -24,7 +24,7 @@ export class APIPromise<
|
|||
THdl extends {[K in KRsv]: (data: TRes[K]) => any},
|
||||
KOn extends keyof TRes = keyof TRes,
|
||||
> implements PromiseLike<RHandler<THdl>> {
|
||||
private promise: Promise<RHandler<THdl>>
|
||||
private promise: Promise<RHandler<THdl>>;
|
||||
|
||||
constructor(
|
||||
resPromise: Promise<AxiosResponse>,
|
||||
|
|
|
@ -12,8 +12,8 @@ export class FullDate {
|
|||
constructor(y: number, m: number, d?: number);
|
||||
constructor(...argv: [any?, number?, number?]) {
|
||||
this._date = removeTime((() => {
|
||||
if (argv.length == 0) return new Date();
|
||||
if (argv.length == 1) {
|
||||
if (argv.length === 0) return new Date();
|
||||
if (argv.length === 1) {
|
||||
const arg = argv[0];
|
||||
if (arg instanceof FullDate) return new Date(+arg);
|
||||
if (arg instanceof Date) return arg;
|
||||
|
|
|
@ -40,7 +40,7 @@ export module StrictTypeParser {
|
|||
}
|
||||
export function _boolean(x: any, label: string): boolean {
|
||||
if (typeof x === 'boolean') return x;
|
||||
if (typeof x === 'number') return x!=0;
|
||||
if (typeof x === 'number') return x!==0;
|
||||
if (x==='true') return true;
|
||||
if (x==='false') return false;
|
||||
throw new BadTypeError(label, 'boolean', x);
|
||||
|
@ -72,7 +72,7 @@ export module StrictTypeParser {
|
|||
throw new BadTypeError(label, 'Array', x);
|
||||
}
|
||||
|
||||
function undefinedCheck(val: any, label: string) {
|
||||
export function undefinedCheck(val: any, label: string) {
|
||||
if (val === undefined) {
|
||||
throw new BadValueError(label,
|
||||
`${label} is required, but got undefined`);
|
||||
|
|
25
package.json
25
package.json
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"name": "@sup39/api-ts-gen",
|
||||
"version": "2.0.6-a",
|
||||
"version": "2.1.0",
|
||||
"description": "OpenAPI code generator for TypeScript",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"main": "dist/lib/index.js",
|
||||
"types": "dist/lib/index.d.ts",
|
||||
"scripts": {
|
||||
"clean": "rm -rf dist",
|
||||
"build": "tsc",
|
||||
|
@ -23,25 +23,22 @@
|
|||
],
|
||||
"files": [
|
||||
"dist/*",
|
||||
"bin/*",
|
||||
"utils"
|
||||
"bin/*"
|
||||
],
|
||||
"bin": {
|
||||
"api-codegen": "bin/api-codegen.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/js-yaml": "^3.12.3",
|
||||
"@types/node": "^14.0.27",
|
||||
"@sup39/eslint-config-typescript": "^0.1.0",
|
||||
"@types/js-yaml": "^4.0.5",
|
||||
"@types/node": "^17.0.41",
|
||||
"@types/qs": "^6.9.3",
|
||||
"@typescript-eslint/eslint-plugin": "^3.9.0",
|
||||
"@typescript-eslint/parser": "^3.9.0",
|
||||
"axios": "^0.21.1",
|
||||
"eslint": "^7.7.0",
|
||||
"eslint-config-google": "^0.14.0",
|
||||
"typescript": "^3.8.3"
|
||||
"axios": "^0.27.2",
|
||||
"eslint": "^8.17.0",
|
||||
"typescript": "^4.7.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"js-yaml": "^3.13.1",
|
||||
"js-yaml": "^4.1.0",
|
||||
"qs": "^6.9.4"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,5 @@
|
|||
"outDir": "dist",
|
||||
"rootDir": "lib",
|
||||
"strict": true
|
||||
},
|
||||
"include": ["lib"]
|
||||
}
|
||||
}
|
||||
|
|
1
utils
1
utils
|
@ -1 +0,0 @@
|
|||
dist/utils
|
Reference in a new issue