Archived
1
0
Fork 0

[v2.1.0] simplify generated code of apiRouter; remove utils symlink

This commit is contained in:
sup39 2022-06-13 19:03:12 +09:00
parent 07c6bc4671
commit c73c5c7088
26 changed files with 790 additions and 645 deletions

View file

@ -1,3 +1,2 @@
dist/ dist/
./utils/
node_modules/ node_modules/

View file

@ -1,33 +1,20 @@
module.exports = { module.exports = {
'env': { env: {
'es6': true, 'es6': true,
'node': true 'node': true,
}, },
'extends': [ extends: [
'google' '@sup39/eslint-config-typescript',
], ],
'globals': { globals: {
'Atomics': 'readonly', 'Atomics': 'readonly',
'SharedArrayBuffer': 'readonly' 'SharedArrayBuffer': 'readonly',
}, },
'parser': '@typescript-eslint/parser', parserOptions: {
'parserOptions': {
'ecmaVersion': 2018, 'ecmaVersion': 2018,
'sourceType': 'module' 'sourceType': 'module',
}, },
'plugins': [ rules: {
'@typescript-eslint' 'no-prototype-builtins': 'off',
],
'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
}],
}, },
}; };

View file

@ -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. 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 ## 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 #### 2.0.6
- Change Full#advance and implement Full#advanced - Change Full#advance and implement Full#advanced
- Nest [object-type properties with id property] conversion in request function - Nest [object-type properties with id property] conversion in request function

View file

@ -65,9 +65,9 @@ function parseArgv(argv) {
for (const arg of argv) { for (const arg of argv) {
if (arg.startsWith('-')) { if (arg.startsWith('-')) {
if (arg.length == 1) { if (arg.length === 1) {
return badArgv(`Unexpected token: -`); return badArgv(`Unexpected token: -`);
} else if (arg[1] == '-') { } else if (arg[1] === '-') {
// flag name // flag name
setFlag(arg.substring(2)); setFlag(arg.substring(2));
} else { } else {

3
dist/CodePrinter.js vendored
View file

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.CodePrinter = exports.StringStream = void 0;
var StringStream = /** @class */ (function () { var StringStream = /** @class */ (function () {
function StringStream() { function StringStream() {
this.content = ''; this.content = '';
@ -29,7 +30,7 @@ var CodePrinter = /** @class */ (function () {
if (pretab === void 0) { pretab = dIndent < 0; } if (pretab === void 0) { pretab = dIndent < 0; }
if (pretab) if (pretab)
this.cIndent = Math.max(0, this.cIndent + dIndent); 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) if (!pretab)
this.cIndent = Math.max(0, this.cIndent + dIndent); this.cIndent = Math.max(0, this.cIndent + dIndent);
}; };

3
dist/Config.js vendored
View file

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.configDefault = void 0;
exports.configDefault = { exports.configDefault = {
// format // format
interfacePrefix: 'I', interfacePrefix: 'I',
@ -11,7 +12,7 @@ exports.configDefault = {
routerName: 'apiRouter', routerName: 'apiRouter',
// TS path // TS path
ServerAPITSPath: '#ServerAPI', ServerAPITSPath: '#ServerAPI',
utilsTSPath: '@sup39/api-ts-gen/utils', utilsTSPath: '@sup39/api-ts-gen/dist/utils',
// other // other
outputDir: 'api/generated', outputDir: 'api/generated',
validateStatus: function (status) { return /^2..$/.test(status); }, validateStatus: function (status) { return /^2..$/.test(status); },

51
dist/OpenAPI.js vendored
View file

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); 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 StrictTypeParser_1 = require("./utils/StrictTypeParser");
var warn = function (x) { return console.warn('\x1b[1;33mWarning: ' + x + '\x1b[0m'); }; var warn = function (x) { return console.warn('\x1b[1;33mWarning: ' + x + '\x1b[0m'); };
var ELMethod = ['get', 'put', 'post', 'delete', 'patch']; 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 name_1 = ref.substring(prefix.length + 1); // $prefix/
var obj0 = dict === null || dict === void 0 ? void 0 : dict[name_1]; var obj0 = dict === null || dict === void 0 ? void 0 : dict[name_1];
if (obj0 === undefined) { if (obj0 === undefined) {
console.error("ref not found: " + ref); console.error("ref not found: ".concat(ref));
return; return;
} }
obj = obj0; obj = obj0;
} }
else { else {
console.error("Invalid ref: " + ref + ", expect prefix " + prefix); console.error("Invalid ref: ".concat(ref, ", expect prefix ").concat(prefix));
return; return;
} }
} while (true); } while (true);
@ -73,31 +74,31 @@ var SchemaType = /** @class */ (function () {
var _a; var _a;
return (_a = this._typeName) !== null && _a !== void 0 ? _a : (this._typeName = SchemaType.typeNameOf(this.schema, this._sameFile)); return (_a = this._typeName) !== null && _a !== void 0 ? _a : (this._typeName = SchemaType.typeNameOf(this.schema, this._sameFile));
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
Object.defineProperty(SchemaType.prototype, "required", { Object.defineProperty(SchemaType.prototype, "required", {
get: function () { get: function () {
return this._required; return this._required;
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
Object.defineProperty(SchemaType.prototype, "maxSize", { Object.defineProperty(SchemaType.prototype, "maxSize", {
get: function () { get: function () {
return this.schema.maxSize; return this.schema.maxSize;
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
SchemaType.prototype.forProp = function (prop) { 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) { SchemaType.prototype.stp = function (prop, label, partial, sameFile) {
if (partial === void 0) { partial = false; } if (partial === void 0) { partial = false; }
if (sameFile === void 0) { sameFile = false; } if (sameFile === void 0) { sameFile = false; }
var stp = SchemaType.gcStp(prop, this.schema, label, partial, sameFile); 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) { SchemaType.typeNameOf = function (schema, sameFile) {
var _a; var _a;
@ -105,21 +106,21 @@ var SchemaType = /** @class */ (function () {
var $ref = schema.$ref; var $ref = schema.$ref;
var typeName = (_a = /^#\/components\/schemas\/(\w+)$/g.exec($ref)) === null || _a === void 0 ? void 0 : _a[1]; var typeName = (_a = /^#\/components\/schemas\/(\w+)$/g.exec($ref)) === null || _a === void 0 ? void 0 : _a[1];
if (typeName == null) { if (typeName == null) {
warn("Invalid $ref, use any instead: " + $ref); warn("Invalid $ref, use any instead: ".concat($ref));
return 'any'; 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 type = schema.type, format = schema.format, nullable = schema.nullable, readOnly = schema.readOnly;
var sType = type; var sType = type;
if (isArraySchema(schema)) { if (isArraySchema(schema)) {
sType = "Array<" + SchemaType.typeNameOf(schema.items, sameFile) + ">"; sType = "Array<".concat(SchemaType.typeNameOf(schema.items, sameFile), ">");
} }
else if (isObjectSchema(schema)) { else if (isObjectSchema(schema)) {
sType = '{'; sType = '{';
for (var _i = 0, _b = Object.entries(schema.properties); _i < _b.length; _i++) { for (var _i = 0, _b = Object.entries(schema.properties); _i < _b.length; _i++) {
var _c = _b[_i], name_2 = _c[0], sub = _c[1]; 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 += '}'; sType += '}';
} }
@ -133,14 +134,14 @@ var SchemaType = /** @class */ (function () {
else if (format === 'binary') else if (format === 'binary')
sType = 'string'; // TODO Buffer sType = 'string'; // TODO Buffer
else if (format) else if (format)
warn("Unknown format " + format + ", use string instead"); warn("Unknown format ".concat(format, ", use string instead"));
} }
else if (type === 'integer') else if (type === 'integer')
sType = 'number'; // TODO integer sType = 'number'; // TODO integer
if (nullable) if (nullable)
sType = sType + " | null"; sType = "".concat(sType, " | null");
if (readOnly) if (readOnly)
sType = "Readonly<" + sType + ">"; sType = "Readonly<".concat(sType, ">");
return sType; return sType;
}; };
SchemaType.gcStp = function (para, schema, label, partial, sameFile) { SchemaType.gcStp = function (para, schema, label, partial, sameFile) {
@ -148,7 +149,7 @@ var SchemaType = /** @class */ (function () {
// object // object
if (isReference(schema)) { if (isReference(schema)) {
var typeName = new SchemaType(schema, true, sameFile).typeName; var typeName = new SchemaType(schema, true, sameFile).typeName;
return typeName + "." + (partial ? 'Partial' : 'from') + "(" + para + ")"; return "".concat(typeName, ".").concat(partial ? 'Partial' : 'from', "(").concat(para, ")");
} }
// any // any
var type = schema.type, nullable = schema.nullable, format = schema.format; var type = schema.type, nullable = schema.nullable, format = schema.format;
@ -156,13 +157,13 @@ var SchemaType = /** @class */ (function () {
if (type === 'any') if (type === 'any')
return para; return para;
if (isArraySchema(schema)) { 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)) { else if (isObjectSchema(schema)) {
sStp = '()=>({'; sStp = '()=>({';
for (var _i = 0, _a = Object.entries(schema.properties); _i < _a.length; _i++) { for (var _i = 0, _a = Object.entries(schema.properties); _i < _a.length; _i++) {
var _b = _a[_i], name_3 = _b[0], sub = _b[1]; 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 += '})'; sStp += '})';
} }
@ -179,7 +180,7 @@ var SchemaType = /** @class */ (function () {
t = 'binary'; t = 'binary';
else { else {
if (format) { if (format) {
warn("Unknown string format " + format + ", use string instead"); warn("Unknown string format ".concat(format, ", use string instead"));
} }
t = 'string'; t = 'string';
} }
@ -188,8 +189,8 @@ var SchemaType = /** @class */ (function () {
if (format === 'int32') if (format === 'int32')
t = 'int32'; t = 'int32';
else { else {
if (format && format != 'int64') { if (format && format !== 'int64') {
warn("Unsupport integer format " + format + ", use number instead"); warn("Unsupport integer format ".concat(format, ", use number instead"));
} }
t = 'number'; // TODO int64 t = 'number'; // TODO int64
} }
@ -197,16 +198,16 @@ var SchemaType = /** @class */ (function () {
else else
t = type; t = type;
if (!StrictTypeParser_1.StrictTypeParser.supportTypes.includes(t)) { 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; return para;
} }
sStp = "STP._" + t; sStp = "STP._".concat(t);
} }
// nullable // nullable
var funcName = nullable ? 'nullableParse' : 'parse'; var funcName = nullable ? 'nullableParse' : 'parse';
// result // result
var sLabel = "'" + label.replace(/'/g, '\\\'') + "'"; // escape var sLabel = "'".concat(label.replace(/'/g, '\\\''), "'"); // escape
return "STP." + funcName + "(" + sStp + ", " + para + ", " + sLabel + ")"; return "STP.".concat(funcName, "(").concat(sStp, ", ").concat(para, ", ").concat(sLabel, ")");
}; };
return SchemaType; return SchemaType;
}()); }());
@ -225,7 +226,7 @@ function apiFunctionsOf(openAPI) {
// operationId // operationId
var operationId = op.operationId, parameters = op.parameters, requestBody = op.requestBody, responses = op.responses; var operationId = op.operationId, parameters = op.parameters, requestBody = op.requestBody, responses = op.responses;
if (operationId == null) { if (operationId == null) {
warn("ignore operation in " + method + " " + url + ": " + warn("ignore operation in ".concat(method, " ").concat(url, ": ") +
'operationId should be given'); 'operationId should be given');
continue; continue;
} }

134
dist/codegen.js vendored
View file

@ -8,9 +8,9 @@ var CodePrinter_1 = require("./CodePrinter");
function codegenIHandler(funcs, config, cp) { function codegenIHandler(funcs, config, cp) {
var schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, clientOnly = config.clientOnly; var schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, clientOnly = config.clientOnly;
// import // import
cp.writeln("import * as Schemas from './" + schemasName + "'"); cp.writeln("import * as Schemas from './".concat(schemasName, "'"));
cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' + cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' +
("from '" + utilsTSPath + "'")); "from '".concat(utilsTSPath, "'"));
if (!clientOnly) { if (!clientOnly) {
cp.writeln('import {RouterContext as CTX} from \'@koa/router\''); 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++) { for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) {
var _b = _a[_i], funcName = _b[0], func = _b[1]; var _b = _a[_i], funcName = _b[0], func = _b[1];
var reqTypes = func.reqTypes, resTypes = func.resTypes, method = func.method; var reqTypes = func.reqTypes, resTypes = func.resTypes, method = func.method;
cp.writeln(funcName + ": {", 1); cp.writeln("".concat(funcName, ": {"), 1);
// req // req
// req.path, ... // req.path, ...
cp.writeln("req: {", 1); cp.writeln("req: {", 1);
@ -29,7 +29,7 @@ function codegenIHandler(funcs, config, cp) {
var paras = reqTypes[_in]; var paras = reqTypes[_in];
if (paras == null) if (paras == null)
continue; continue;
cp.writeln(_in + ": {", 1); cp.writeln("".concat(_in, ": {"), 1);
for (var _d = 0, _e = Object.entries(paras); _d < _e.length; _d++) { for (var _d = 0, _e = Object.entries(paras); _d < _e.length; _d++) {
var _f = _e[_d], propName = _f[0], schemaType = _f[1]; var _f = _e[_d], propName = _f[0], schemaType = _f[1];
cp.writeln(schemaType.forProp(propName) + ';'); cp.writeln(schemaType.forProp(propName) + ';');
@ -41,9 +41,9 @@ function codegenIHandler(funcs, config, cp) {
if (body != null) { if (body != null) {
// PATCH's req body: Partial // PATCH's req body: Partial
var typeName = body.typeName; var typeName = body.typeName;
if (method == 'patch') if (method === 'patch')
typeName = "Partial<" + typeName + ">"; typeName = "Partial<".concat(typeName, ">");
cp.writeln("body" + (body.required ? '' : '?') + ": " + typeName + ";"); cp.writeln("body".concat(body.required ? '' : '?', ": ").concat(typeName, ";"));
} }
cp.writeln('}', -1); // req END cp.writeln('}', -1); // req END
// res // res
@ -51,7 +51,7 @@ function codegenIHandler(funcs, config, cp) {
for (var _g = 0, _h = Object.entries(resTypes); _g < _h.length; _g++) { for (var _g = 0, _h = Object.entries(resTypes); _g < _h.length; _g++) {
var _j = _h[_g], status_1 = _j[0], schema = _j[1]; var _j = _h[_g], status_1 = _j[0], schema = _j[1];
cp.writeln(schema.required ? 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 cp.writeln('}', -1); // res END
// operation END // operation END
@ -75,23 +75,25 @@ function codegenIHandler(funcs, config, cp) {
function codegenRouter(funcs, config, cp) { function codegenRouter(funcs, config, cp) {
var schemasName = config.schemasName, IHandlerName = config.IHandlerName, ServerAPITSPath = config.ServerAPITSPath, utilsTSPath = config.utilsTSPath; var schemasName = config.schemasName, IHandlerName = config.IHandlerName, ServerAPITSPath = config.ServerAPITSPath, utilsTSPath = config.utilsTSPath;
// import // import
cp.writeln("import * as Schemas from './" + schemasName + "'"); cp.writeln("import * as Schemas from './".concat(schemasName, "'"));
cp.writeln("import {IServerAPI} from './" + IHandlerName + "'"); cp.writeln("import {IServerAPI} from './".concat(IHandlerName, "'"));
cp.writeln("import * as Router from '@koa/router'"); 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'"); cp.writeln("import * as bodyParser from 'koa-body'");
// api // 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;"); cp.writeln("type IState = typeof api extends IServerAPI<infer T> ? T : any;");
// router // router
cp.writeln("type CTX = Router.RouterContext<IState>;"); cp.writeln("type CTX = Router.RouterContext<IState>;");
cp.writeln("\nconst router = new Router<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 // function
var gcGetParams = { var gcGetParams = {
path: function (attr) { return "ctx.params['" + attr + "']"; }, path: function (attr) { return "ctx.params['".concat(attr, "']"); },
query: function (attr) { return "ctx.query['" + attr + "']"; }, query: function (attr) { return "ctx.query['".concat(attr, "']"); },
header: function (attr) { return "ctx.headers['" + attr + "']"; }, header: function (attr) { return "ctx.headers['".concat(attr, "']"); },
cookie: function (attr) { return "ctx.cookies.get('" + attr + "')"; }, cookie: function (attr) { return "ctx.cookies.get('".concat(attr, "')"); },
}; };
// route // route
for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) { for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) {
@ -103,49 +105,41 @@ function codegenRouter(funcs, config, cp) {
var mid = ''; var mid = '';
if (reqTypes.body) { if (reqTypes.body) {
var maxSize = reqTypes.body.maxSize; // TODO doc var maxSize = reqTypes.body.maxSize; // TODO doc
var config_1 = maxSize == null ? '' : "{jsonLimit: '" + maxSize + "'}"; var config_1 = maxSize == null ? '' : "{jsonLimit: '".concat(maxSize, "'}");
mid = "bodyParser(" + config_1 + "), "; 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 // req
if (Object.keys(reqTypes).length === 0) { if (Object.keys(reqTypes).length === 0) {
cp.writeln('const req = {};'); cp.writeln('const req = {};');
} }
else { else {
cp.writeln('let req;'); cp.writeln('const req = {', 1);
cp.writeln('try {', 1);
cp.writeln('req = {', 1);
// paras // paras
for (var _c = 0, ELParameterIn_2 = OpenAPI_1.ELParameterIn; _c < ELParameterIn_2.length; _c++) { for (var _c = 0, ELParameterIn_2 = OpenAPI_1.ELParameterIn; _c < ELParameterIn_2.length; _c++) {
var _in = ELParameterIn_2[_c]; var _in = ELParameterIn_2[_c];
var paras = reqTypes[_in]; var paras = reqTypes[_in];
if (paras == null) if (paras == null)
continue; continue;
cp.writeln(_in + ": {", 1); cp.writeln("".concat(_in, ": {"), 1);
for (var _d = 0, _e = Object.entries(paras); _d < _e.length; _d++) { for (var _d = 0, _e = Object.entries(paras); _d < _e.length; _d++) {
var _f = _e[_d], name_1 = _f[0], schema = _f[1]; var _f = _e[_d], name_1 = _f[0], schema = _f[1];
var pn = gcGetParams[_in](name_1); var pn = gcGetParams[_in](name_1);
var label = "req." + _in; var label = "req.".concat(_in);
cp.writeln(name_1 + ": " + schema.stp(pn, label) + ","); cp.writeln("".concat(name_1, ": ").concat(schema.stp(pn, label), ","));
} }
cp.writeln('},', -1); cp.writeln('},', -1);
} }
// body // body
var body = reqTypes.body; var body = reqTypes.body;
if (body != null) { 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); // req END
cp.writeln('} catch(err) {', -1); cp.writeln('};', -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);
} }
// call // 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.status = r[0];");
cp.writeln("ctx.body = r[1] ?? '';"); cp.writeln("ctx.body = r[1] ?? '';");
// ctx END // ctx END
@ -157,10 +151,10 @@ function codegenRouter(funcs, config, cp) {
function codegenClientAPI(funcs, config, cp) { function codegenClientAPI(funcs, config, cp) {
var IHandlerName = config.IHandlerName, schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, validateStatus = config.validateStatus; var IHandlerName = config.IHandlerName, schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, validateStatus = config.validateStatus;
// import // import
cp.writeln("import {TAPI} from './" + IHandlerName + "'"); cp.writeln("import {TAPI} from './".concat(IHandlerName, "'"));
cp.writeln("import * as Schemas from './" + schemasName + "'"); cp.writeln("import * as Schemas from './".concat(schemasName, "'"));
cp.writeln("import {APIPromise, StrictTypeParser as STP, " + cp.writeln("import {APIPromise, StrictTypeParser as STP, " +
("qStringify} from '" + utilsTSPath + "'")); "qStringify} from '".concat(utilsTSPath, "'"));
cp.writeln("import axios from 'axios'"); cp.writeln("import axios from 'axios'");
cp.writeln(''); cp.writeln('');
// type // type
@ -192,38 +186,38 @@ function codegenClientAPI(funcs, config, cp) {
cp.writeln('}, err => Promise.reject(err));', -1); cp.writeln('}, err => Promise.reject(err));', -1);
cp.writeln('},', -1); cp.writeln('},', -1);
var _loop_1 = function (funcName, func) { 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 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 var query = reqTypes.query, header = reqTypes.header, path_1 = reqTypes.path, body = reqTypes.body; // TODO cookie
// name // name
cp.writeln(funcName + ": (", 1); cp.writeln("".concat(funcName, ": ("), 1);
// paras // paras
for (var _i = 0, ELParameterIn_3 = OpenAPI_1.ELParameterIn; _i < ELParameterIn_3.length; _i++) { for (var _c = 0, ELParameterIn_3 = OpenAPI_1.ELParameterIn; _c < ELParameterIn_3.length; _c++) {
var _in = ELParameterIn_3[_i]; var _in = ELParameterIn_3[_c];
var paras = reqTypes[_in]; var paras = reqTypes[_in];
if (paras == null) if (paras == null)
continue; continue;
var _required = false; var _required = false;
for (var _a = 0, _b = Object.values(paras); _a < _b.length; _a++) { for (var _d = 0, _e = Object.values(paras); _d < _e.length; _d++) {
var required = _b[_a].required; var required = _e[_d].required;
if (required) { if (required) {
_required = true; _required = true;
break; break;
} }
} }
cp.writeln(_in + ": " + gcReq(_in) + (_required ? '' : '={}') + ","); cp.writeln("".concat(_in, ": ").concat(gcReq(_in)).concat(_required ? '' : '={}', ","));
} }
// body // body
if (body != null) { if (body != null) {
cp.writeln("body" + (body.required ? '' : '?') + ": ExID<" + gcReq('body') + ">,"); cp.writeln("body".concat(body.required ? '' : '?', ": ExID<").concat(gcReq('body'), ">,"));
} }
// return value // return value
cp.tab(-1); cp.tab(-1);
cp.writeln(") => APIPromise.init($http({", 1); cp.writeln(") => APIPromise.init($http({", 1);
// req // req
cp.writeln("method: '" + method + "',"); cp.writeln("method: '".concat(method, "',"));
var sURL = "'" + url + "'"; var sURL = "'".concat(url, "'");
cp.writeln("url: " + (path_1 ? "urlReplacer(" + sURL + ", path)" : sURL) + ","); cp.writeln("url: ".concat(path_1 ? "urlReplacer(".concat(sURL, ", path)") : sURL, ","));
if (query) if (query)
cp.writeln('params: query,'); cp.writeln('params: query,');
if (header) if (header)
@ -233,15 +227,15 @@ function codegenClientAPI(funcs, config, cp) {
cp.writeln('}), {', -1); cp.writeln('}), {', -1);
cp.tab(1); cp.tab(1);
// stp // stp
for (var _c = 0, _d = Object.entries(resTypes); _c < _d.length; _c++) { for (var _f = 0, _g = Object.entries(resTypes); _f < _g.length; _f++) {
var _e = _d[_c], status_2 = _e[0], schema = _e[1]; var _h = _g[_f], status_2 = _h[0], schema = _h[1];
var label = "ClientAPI[" + funcName + "][" + status_2 + "]"; var label = "ClientAPI[".concat(funcName, "][").concat(status_2, "]");
cp.writeln(status_2 + ": x => " + schema.stp('x', label) + ","); 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); cp.tab(1);
// kRsv // kRsv
cp.writeln("[" + Object.keys(resTypes).filter(validateStatus).join(', ') + "]),", -1); cp.writeln("[".concat(Object.keys(resTypes).filter(validateStatus).join(', '), "]),"), -1);
}; };
// functions // functions
for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) { for (var _i = 0, _a = Object.entries(funcs); _i < _a.length; _i++) {
@ -255,17 +249,17 @@ function codegenSchemas(schemas, config, cp) {
var _a; var _a;
var utilsTSPath = config.utilsTSPath; var utilsTSPath = config.utilsTSPath;
// import // import
cp.writeln("import {FullDate, StrictTypeParser as STP} from '" + utilsTSPath + "'"); cp.writeln("import {FullDate, StrictTypeParser as STP} from '".concat(utilsTSPath, "'"));
// schema // schema
for (var _i = 0, _b = Object.entries(schemas); _i < _b.length; _i++) { for (var _i = 0, _b = Object.entries(schemas); _i < _b.length; _i++) {
var _c = _b[_i], typeName = _c[0], rSchema = _c[1]; 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) if (schema == null)
continue; continue;
cp.writeln(); cp.writeln();
if (OpenAPI_1.isObjectSchema(schema)) { if ((0, OpenAPI_1.isObjectSchema)(schema)) {
// interface // interface
cp.writeln("export interface " + typeName + " {", 1); cp.writeln("export interface ".concat(typeName, " {"), 1);
var propTypes = []; var propTypes = [];
var requireds = new Set((_a = schema.required) !== null && _a !== void 0 ? _a : []); 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++) { 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 cp.writeln('}', -1); // interface END
// const // const
cp.writeln("export const " + typeName + " = {", 1); cp.writeln("export const ".concat(typeName, " = {"), 1);
// .from // .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++) { for (var _g = 0, propTypes_1 = propTypes; _g < propTypes_1.length; _g++) {
var _h = propTypes_1[_g], n = _h[0], t = _h[1]; 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); cp.writeln('}),', -1);
// Partial // Partial
cp.writeln("Partial: (o: {[_: string]: any}): Partial<" + typeName + "> => {", 1); cp.writeln("Partial: (o: {[_: string]: any}): Partial<".concat(typeName, "> => {"), 1);
cp.writeln("const r: Partial<" + typeName + "> = {};"); cp.writeln("const r: Partial<".concat(typeName, "> = {};"));
var locPartial = "Partial<" + typeName + ">"; var locPartial = "Partial<".concat(typeName, ">");
for (var _j = 0, propTypes_2 = propTypes; _j < propTypes_2.length; _j++) { for (var _j = 0, propTypes_2 = propTypes; _j < propTypes_2.length; _j++) {
var _k = propTypes_2[_j], n = _k[0], t = _k[1]; 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('return r;');
cp.writeln('},', -1); cp.writeln('},', -1);
// fields // fields
cp.writeln("fields: [", 1); cp.writeln("fields: [", 1);
cp.writeln(propTypes.map(function (e) { return "'" + e[0] + "',"; }).join(' ')); cp.writeln(propTypes.map(function (e) { return "'".concat(e[0], "',"); }).join(' '));
cp.writeln("] as Array<keyof " + typeName + ">", -1); cp.writeln("] as Array<keyof ".concat(typeName, ">"), -1);
// end of const // end of const
cp.writeln('}', -1); cp.writeln('}', -1);
} }
else { else {
cp.writeln("export type " + typeName + " = " + cp.writeln("export type ".concat(typeName, " = ") +
OpenAPI_1.SchemaType.typeNameOf(schema, true)); OpenAPI_1.SchemaType.typeNameOf(schema, true));
} }
} }
@ -314,7 +308,7 @@ function codegen(openAPI, configUser) {
var config = Object.assign({}, Config_1.configDefault, configUser); var config = Object.assign({}, Config_1.configDefault, configUser);
// prepare // prepare
fs.mkdirSync(config.outputDir, { recursive: true }); 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 gCP = function (fn) { return new CodePrinter_1.CodePrinter(fs.createWriteStream(path.join(config.outputDir, fn + '.ts')), config.indentString); };
var ps = []; var ps = [];
// write files // write files

20
dist/index.js vendored
View file

@ -1,8 +1,20 @@
"use strict"; "use strict";
function __export(m) { var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; 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 }); Object.defineProperty(exports, "__esModule", { value: true });
exports.codegen = void 0;
var codegen_1 = require("./codegen"); var codegen_1 = require("./codegen");
exports.codegen = codegen_1.default; Object.defineProperty(exports, "codegen", { enumerable: true, get: function () { return codegen_1.default; } });
__export(require("./Config")); __exportStar(require("./Config"), exports);

View file

@ -3,16 +3,19 @@ var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) { var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf || extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || ({ __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 extendStatics(d, b);
}; };
return function (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); extendStatics(d, b);
function __() { this.constructor = d; } function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}; };
})(); })();
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.APIPromise = exports.BadResponseError = void 0;
function typeGuard(checker) { function typeGuard(checker) {
return function (x) { return function (x) {
return checker(x); return checker(x);
@ -21,7 +24,7 @@ function typeGuard(checker) {
var BadResponseError = /** @class */ (function (_super) { var BadResponseError = /** @class */ (function (_super) {
__extends(BadResponseError, _super); __extends(BadResponseError, _super);
function BadResponseError(res, label) { 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; _this.res = res;
Object.setPrototypeOf(_this, BadResponseError.prototype); Object.setPrototypeOf(_this, BadResponseError.prototype);
return _this; return _this;

View file

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.FullDate = void 0;
function removeTime(date) { function removeTime(date) {
return new Date(date.setHours(0, 0, 0, 0)); return new Date(date.setHours(0, 0, 0, 0));
} }
@ -11,9 +12,9 @@ var FullDate = /** @class */ (function () {
} }
this._date = removeTime((function () { this._date = removeTime((function () {
var _a, _b, _c; var _a, _b, _c;
if (argv.length == 0) if (argv.length === 0)
return new Date(); return new Date();
if (argv.length == 1) { if (argv.length === 1) {
var arg = argv[0]; var arg = argv[0];
if (arg instanceof FullDate) if (arg instanceof FullDate)
return new Date(+arg); return new Date(+arg);
@ -32,7 +33,7 @@ var FullDate = /** @class */ (function () {
FullDate.prototype.toString = function () { FullDate.prototype.toString = function () {
var d = this._date; var d = this._date;
var f = function (s) { return ('0' + s).slice(-2); }; 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 () { FullDate.prototype.toJSON = function () {
return this.toString(); return this.toString();
@ -45,7 +46,7 @@ var FullDate = /** @class */ (function () {
get: function () { get: function () {
return new Date(this._date); return new Date(this._date);
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
Object.defineProperty(FullDate.prototype, "year", { Object.defineProperty(FullDate.prototype, "year", {
@ -56,7 +57,7 @@ var FullDate = /** @class */ (function () {
set: function (val) { set: function (val) {
this._date.setFullYear(val); this._date.setFullYear(val);
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
Object.defineProperty(FullDate.prototype, "month", { Object.defineProperty(FullDate.prototype, "month", {
@ -66,7 +67,7 @@ var FullDate = /** @class */ (function () {
set: function (val) { set: function (val) {
this._date.setMonth(val - 1); this._date.setMonth(val - 1);
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
Object.defineProperty(FullDate.prototype, "day", { Object.defineProperty(FullDate.prototype, "day", {
@ -76,14 +77,14 @@ var FullDate = /** @class */ (function () {
set: function (val) { set: function (val) {
this._date.setDate(val); this._date.setDate(val);
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
Object.defineProperty(FullDate.prototype, "dayOfWeek", { Object.defineProperty(FullDate.prototype, "dayOfWeek", {
get: function () { get: function () {
return this._date.getDay(); return this._date.getDay();
}, },
enumerable: true, enumerable: false,
configurable: true configurable: true
}); });
// func // func

View file

@ -19,6 +19,7 @@ export declare module StrictTypeParser {
function _byte(x: any, label: string): string; function _byte(x: any, label: string): string;
function _binary(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 _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 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; function nullableParse<T>(stp: (val: any, label: string) => T, val: any, label: string): T | null;
const supportTypes: string[]; const supportTypes: string[];

View file

@ -3,16 +3,19 @@ var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) { var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf || extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || ({ __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 extendStatics(d, b);
}; };
return function (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); extendStatics(d, b);
function __() { this.constructor = d; } function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}; };
})(); })();
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.StrictTypeParser = void 0;
var FullDate_1 = require("./FullDate"); var FullDate_1 = require("./FullDate");
var StrictTypeParser; var StrictTypeParser;
(function (StrictTypeParser) { (function (StrictTypeParser) {
@ -31,8 +34,8 @@ var StrictTypeParser;
var BadTypeError = /** @class */ (function (_super) { var BadTypeError = /** @class */ (function (_super) {
__extends(BadTypeError, _super); __extends(BadTypeError, _super);
function BadTypeError(label, type, value) { function BadTypeError(label, type, value) {
var _this = _super.call(this, label, label + ": Can not convert `" + (['object', 'array'].includes(typeof value) ? var _this = _super.call(this, label, "".concat(label, ": Can not convert `").concat(['object', 'array'].includes(typeof value) ?
JSON.stringify(value) : "" + value) + "` to type " + type) || this; JSON.stringify(value) : "".concat(value), "` to type ").concat(type)) || this;
_this.label = label; _this.label = label;
_this.type = type; _this.type = type;
_this.value = value; _this.value = value;
@ -75,7 +78,7 @@ var StrictTypeParser;
if (typeof x === 'boolean') if (typeof x === 'boolean')
return x; return x;
if (typeof x === 'number') if (typeof x === 'number')
return x != 0; return x !== 0;
if (x === 'true') if (x === 'true')
return true; return true;
if (x === 'false') if (x === 'false')
@ -123,14 +126,15 @@ var StrictTypeParser;
StrictTypeParser._Array = _Array; StrictTypeParser._Array = _Array;
function undefinedCheck(val, label) { function undefinedCheck(val, label) {
if (val === undefined) { 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) { function parse(stp, val, label) {
// body // body
undefinedCheck(val, label); undefinedCheck(val, label);
if (val === null) { 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); return stp(val, label);
} }

23
dist/utils/index.js vendored
View file

@ -1,9 +1,20 @@
"use strict"; "use strict";
function __export(m) { var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; 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 }); Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./APIPromise")); __exportStar(require("./APIPromise"), exports);
__export(require("./FullDate")); __exportStar(require("./FullDate"), exports);
__export(require("./StrictTypeParser")); __exportStar(require("./StrictTypeParser"), exports);
__export(require("./qStringify")); __exportStar(require("./qStringify"), exports);

View file

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.qStringify = void 0;
var Qs0 = require("qs"); var Qs0 = require("qs");
var options0 = { var options0 = {
filter: function (prefix, value) { filter: function (prefix, value) {

View file

@ -5,7 +5,7 @@ interface WriteStream {
} }
export class StringStream implements WriteStream { export class StringStream implements WriteStream {
private content = '' private content = '';
write(s: string) { write(s: string) {
this.content += s; this.content += s;
} }

View file

@ -30,7 +30,7 @@ export const configDefault: ConfigOptional = {
routerName: 'apiRouter', routerName: 'apiRouter',
// TS path // TS path
ServerAPITSPath: '#ServerAPI', ServerAPITSPath: '#ServerAPI',
utilsTSPath: '@sup39/api-ts-gen/utils', utilsTSPath: '@sup39/api-ts-gen/dist/utils',
// other // other
outputDir: 'api/generated', outputDir: 'api/generated',
validateStatus: (status: string) => /^2..$/.test(status), validateStatus: (status: string) => /^2..$/.test(status),

View file

@ -257,7 +257,7 @@ export class SchemaType {
} else if (type === 'integer') { } else if (type === 'integer') {
if (format === 'int32') t = 'int32'; if (format === 'int32') t = 'int32';
else { else {
if (format && format != 'int64') { if (format && format !== 'int64') {
warn(`Unsupport integer format ${format}, use number instead`); warn(`Unsupport integer format ${format}, use number instead`);
} }
t = 'number'; // TODO int64 t = 'number'; // TODO int64

View file

@ -42,7 +42,7 @@ function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) {
if (body != null) { if (body != null) {
// PATCH's req body: Partial // PATCH's req body: Partial
let {typeName} = body; let {typeName} = body;
if (method == 'patch') typeName = `Partial<${typeName}>`; if (method === 'patch') typeName = `Partial<${typeName}>`;
cp.writeln(`body${body.required ? '' : '?'}: ${typeName};`); cp.writeln(`body${body.required ? '' : '?'}: ${typeName};`);
} }
cp.writeln('}', -1); // req END cp.writeln('}', -1); // req END
@ -89,6 +89,11 @@ function codegenRouter(funcs: APIFuncs, config: Config, cp: CodePrinter) {
// router // router
cp.writeln(`type CTX = Router.RouterContext<IState>;`); cp.writeln(`type CTX = Router.RouterContext<IState>;`);
cp.writeln(`\nconst router = new Router<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 // function
const gcGetParams = { const gcGetParams = {
path: (attr: string) => `ctx.params['${attr}']`, path: (attr: string) => `ctx.params['${attr}']`,
@ -115,9 +120,7 @@ function codegenRouter(funcs: APIFuncs, config: Config, cp: CodePrinter) {
if (Object.keys(reqTypes).length === 0) { if (Object.keys(reqTypes).length === 0) {
cp.writeln('const req = {};'); cp.writeln('const req = {};');
} else { } else {
cp.writeln('let req;'); cp.writeln('const req = {', 1);
cp.writeln('try {', 1);
cp.writeln('req = {', 1);
// paras // paras
for (const _in of ELParameterIn) { for (const _in of ELParameterIn) {
const paras = reqTypes[_in]; const paras = reqTypes[_in];
@ -136,12 +139,8 @@ function codegenRouter(funcs: APIFuncs, config: Config, cp: CodePrinter) {
cp.writeln( cp.writeln(
`body: ${body.stp('ctx.request.body', 'req.body', isPartial)}`); `body: ${body.stp('ctx.request.body', 'req.body', isPartial)}`);
} }
cp.writeln('}', -1); // req END
cp.writeln('} catch(err) {', -1); cp.tab(1); cp.writeln('};', -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);
} }
// call // call
cp.writeln(`const r = await api.${funcName}(req, ctx.state, ctx);`); cp.writeln(`const r = await api.${funcName}(req, ctx.state, ctx);`);

View file

@ -24,7 +24,7 @@ export class APIPromise<
THdl extends {[K in KRsv]: (data: TRes[K]) => any}, THdl extends {[K in KRsv]: (data: TRes[K]) => any},
KOn extends keyof TRes = keyof TRes, KOn extends keyof TRes = keyof TRes,
> implements PromiseLike<RHandler<THdl>> { > implements PromiseLike<RHandler<THdl>> {
private promise: Promise<RHandler<THdl>> private promise: Promise<RHandler<THdl>>;
constructor( constructor(
resPromise: Promise<AxiosResponse>, resPromise: Promise<AxiosResponse>,

View file

@ -12,8 +12,8 @@ export class FullDate {
constructor(y: number, m: number, d?: number); constructor(y: number, m: number, d?: number);
constructor(...argv: [any?, number?, number?]) { constructor(...argv: [any?, number?, number?]) {
this._date = removeTime((() => { this._date = removeTime((() => {
if (argv.length == 0) return new Date(); if (argv.length === 0) return new Date();
if (argv.length == 1) { if (argv.length === 1) {
const arg = argv[0]; const arg = argv[0];
if (arg instanceof FullDate) return new Date(+arg); if (arg instanceof FullDate) return new Date(+arg);
if (arg instanceof Date) return arg; if (arg instanceof Date) return arg;

View file

@ -40,7 +40,7 @@ export module StrictTypeParser {
} }
export function _boolean(x: any, label: string): boolean { export function _boolean(x: any, label: string): boolean {
if (typeof x === 'boolean') return x; 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==='true') return true;
if (x==='false') return false; if (x==='false') return false;
throw new BadTypeError(label, 'boolean', x); throw new BadTypeError(label, 'boolean', x);
@ -72,7 +72,7 @@ export module StrictTypeParser {
throw new BadTypeError(label, 'Array', x); throw new BadTypeError(label, 'Array', x);
} }
function undefinedCheck(val: any, label: string) { export function undefinedCheck(val: any, label: string) {
if (val === undefined) { if (val === undefined) {
throw new BadValueError(label, throw new BadValueError(label,
`${label} is required, but got undefined`); `${label} is required, but got undefined`);

View file

@ -1,9 +1,9 @@
{ {
"name": "@sup39/api-ts-gen", "name": "@sup39/api-ts-gen",
"version": "2.0.6-a", "version": "2.1.0",
"description": "OpenAPI code generator for TypeScript", "description": "OpenAPI code generator for TypeScript",
"main": "dist/index.js", "main": "dist/lib/index.js",
"types": "dist/index.d.ts", "types": "dist/lib/index.d.ts",
"scripts": { "scripts": {
"clean": "rm -rf dist", "clean": "rm -rf dist",
"build": "tsc", "build": "tsc",
@ -23,25 +23,22 @@
], ],
"files": [ "files": [
"dist/*", "dist/*",
"bin/*", "bin/*"
"utils"
], ],
"bin": { "bin": {
"api-codegen": "bin/api-codegen.js" "api-codegen": "bin/api-codegen.js"
}, },
"devDependencies": { "devDependencies": {
"@types/js-yaml": "^3.12.3", "@sup39/eslint-config-typescript": "^0.1.0",
"@types/node": "^14.0.27", "@types/js-yaml": "^4.0.5",
"@types/node": "^17.0.41",
"@types/qs": "^6.9.3", "@types/qs": "^6.9.3",
"@typescript-eslint/eslint-plugin": "^3.9.0", "axios": "^0.27.2",
"@typescript-eslint/parser": "^3.9.0", "eslint": "^8.17.0",
"axios": "^0.21.1", "typescript": "^4.7.3"
"eslint": "^7.7.0",
"eslint-config-google": "^0.14.0",
"typescript": "^3.8.3"
}, },
"dependencies": { "dependencies": {
"js-yaml": "^3.13.1", "js-yaml": "^4.1.0",
"qs": "^6.9.4" "qs": "^6.9.4"
} }
} }

View file

@ -10,6 +10,5 @@
"outDir": "dist", "outDir": "dist",
"rootDir": "lib", "rootDir": "lib",
"strict": true "strict": true
}, }
"include": ["lib"]
} }

1
utils
View file

@ -1 +0,0 @@
dist/utils

1050
yarn.lock

File diff suppressed because it is too large Load diff