Archived
1
0
Fork 0

fix clientOnly code: remove CTX

This commit is contained in:
sup39 2020-05-20 05:54:19 +09:00
parent 3d2fea95cb
commit ed584b1f11
3 changed files with 34 additions and 24 deletions

27
dist/codegen.js vendored
View file

@ -6,12 +6,14 @@ var Config_1 = require("./Config");
var OpenAPI_1 = require("./OpenAPI"); var OpenAPI_1 = require("./OpenAPI");
var CodePrinter_1 = require("./CodePrinter"); var CodePrinter_1 = require("./CodePrinter");
function codegenIHandler(funcs, config, cp) { function codegenIHandler(funcs, config, cp) {
var schemasName = config.schemasName, utilsTSPath = config.utilsTSPath; 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 './" + schemasName + "'");
cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' + cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' +
("from '" + utilsTSPath + "'")); ("from '" + utilsTSPath + "'"));
cp.writeln('import {RouterContext as CTX} from \'@koa/router\''); if (!clientOnly) {
cp.writeln('import {RouterContext as CTX} from \'@koa/router\'');
}
cp.writeln('import {AxiosResponse} from \'axios\''); cp.writeln('import {AxiosResponse} from \'axios\'');
// api req, res types // api req, res types
cp.writeln("export type TAPI = {", 1); cp.writeln("export type TAPI = {", 1);
@ -58,13 +60,15 @@ function codegenIHandler(funcs, config, cp) {
// TAPI END // TAPI END
cp.writeln('}', -1); cp.writeln('}', -1);
// export IServerAPI // export IServerAPI
cp.writeln(''); if (!clientOnly) {
cp.writeln('type ValueOf<T> = T[keyof T];'); cp.writeln('');
cp.writeln('type RServerAPI<T> = ValueOf<', 1); cp.writeln('type ValueOf<T> = T[keyof T];');
cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', -1, false); cp.writeln('type RServerAPI<T> = ValueOf<', 1);
cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1); cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', -1, false);
cp.writeln("(req: TAPI[K]['req'], state: IState, ctx: CTX) =>", 1); cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1);
cp.writeln("Promise<RServerAPI<TAPI[K]['res']>>}", -2, false); cp.writeln("(req: TAPI[K]['req'], state: IState, ctx: CTX) =>", 1);
cp.writeln("Promise<RServerAPI<TAPI[K]['res']>>}", -2, false);
}
// return // return
return cp.end(); return cp.end();
} }
@ -229,7 +233,8 @@ function codegenClientAPI(funcs, config, cp) {
var label = "ClientAPI[" + funcName + "][" + status_2 + "]"; var label = "ClientAPI[" + funcName + "][" + status_2 + "]";
cp.writeln(status_2 + ": x => " + schema.stp('x', label) + ","); cp.writeln(status_2 + ": x => " + schema.stp('x', label) + ",");
} }
cp.writeln("} as TSTP<TAPI['" + funcName + "']['res']>,"); cp.writeln("} as TSTP<TAPI['" + funcName + "']['res']>,", -1);
cp.tab(1);
// kRsv // kRsv
cp.writeln("[" + Object.keys(resTypes).filter(validateStatus).join(', ') + "]),", -1); cp.writeln("[" + Object.keys(resTypes).filter(validateStatus).join(', ') + "]),", -1);
}; };
@ -238,7 +243,7 @@ function codegenClientAPI(funcs, config, cp) {
var _b = _a[_i], funcName = _b[0], func = _b[1]; var _b = _a[_i], funcName = _b[0], func = _b[1];
_loop_1(funcName, func); _loop_1(funcName, func);
} }
cp.writeln('}'); cp.writeln('}', -1);
return cp.end(); return cp.end();
} }
function codegenSchemas(schemas, config, cp) { function codegenSchemas(schemas, config, cp) {

View file

@ -9,13 +9,15 @@ import {CodePrinter} from './CodePrinter';
function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) { function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) {
const { const {
schemasName, utilsTSPath, schemasName, utilsTSPath, clientOnly,
} = config; } = config;
// import // import
cp.writeln(`import * as Schemas from './${schemasName}'`); cp.writeln(`import * as Schemas from './${schemasName}'`);
cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' + cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' +
`from '${utilsTSPath}'`); `from '${utilsTSPath}'`);
cp.writeln('import {RouterContext as CTX} from \'@koa/router\''); if (!clientOnly) {
cp.writeln('import {RouterContext as CTX} from \'@koa/router\'');
}
cp.writeln('import {AxiosResponse} from \'axios\''); cp.writeln('import {AxiosResponse} from \'axios\'');
// api req, res types // api req, res types
cp.writeln(`export type TAPI = {`, 1); cp.writeln(`export type TAPI = {`, 1);
@ -56,14 +58,16 @@ function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) {
// TAPI END // TAPI END
cp.writeln('}', -1); cp.writeln('}', -1);
// export IServerAPI // export IServerAPI
cp.writeln(''); if (!clientOnly) {
cp.writeln('type ValueOf<T> = T[keyof T];'); cp.writeln('');
cp.writeln('type RServerAPI<T> = ValueOf<', 1); cp.writeln('type ValueOf<T> = T[keyof T];');
cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', cp.writeln('type RServerAPI<T> = ValueOf<', 1);
-1, false); cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;',
cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1); -1, false);
cp.writeln(`(req: TAPI[K]['req'], state: IState, ctx: CTX) =>`, 1); cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1);
cp.writeln(`Promise<RServerAPI<TAPI[K]['res']>>}`, -2, false); cp.writeln(`(req: TAPI[K]['req'], state: IState, ctx: CTX) =>`, 1);
cp.writeln(`Promise<RServerAPI<TAPI[K]['res']>>}`, -2, false);
}
// return // return
return cp.end(); return cp.end();
} }
@ -223,13 +227,14 @@ function codegenClientAPI(funcs: APIFuncs, config: Config, cp: CodePrinter) {
const label = `ClientAPI[${funcName}][${status}]`; const label = `ClientAPI[${funcName}][${status}]`;
cp.writeln(`${status}: x => ${schema.stp('x', label)},`); cp.writeln(`${status}: x => ${schema.stp('x', label)},`);
} }
cp.writeln(`} as TSTP<TAPI['${funcName}']['res']>,`); cp.writeln(`} as TSTP<TAPI['${funcName}']['res']>,`, -1);
cp.tab(1);
// kRsv // kRsv
cp.writeln(`[${ cp.writeln(`[${
Object.keys(resTypes).filter(validateStatus).join(', ') Object.keys(resTypes).filter(validateStatus).join(', ')
}]),`, -1); }]),`, -1);
} }
cp.writeln('}'); cp.writeln('}', -1);
return cp.end(); return cp.end();
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "@sup39/api-ts-gen", "name": "@sup39/api-ts-gen",
"version": "2.0.3", "version": "2.0.3-a",
"description": "OpenAPI code generator for TypeScript", "description": "OpenAPI code generator for TypeScript",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",