fix clientOnly code: remove CTX
This commit is contained in:
parent
3d2fea95cb
commit
ed584b1f11
3 changed files with 34 additions and 24 deletions
27
dist/codegen.js
vendored
27
dist/codegen.js
vendored
|
@ -6,12 +6,14 @@ var Config_1 = require("./Config");
|
|||
var OpenAPI_1 = require("./OpenAPI");
|
||||
var CodePrinter_1 = require("./CodePrinter");
|
||||
function codegenIHandler(funcs, config, cp) {
|
||||
var schemasName = config.schemasName, utilsTSPath = config.utilsTSPath;
|
||||
var schemasName = config.schemasName, utilsTSPath = config.utilsTSPath, clientOnly = config.clientOnly;
|
||||
// import
|
||||
cp.writeln("import * as Schemas from './" + schemasName + "'");
|
||||
cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' +
|
||||
("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\'');
|
||||
// api req, res types
|
||||
cp.writeln("export type TAPI = {", 1);
|
||||
|
@ -58,13 +60,15 @@ function codegenIHandler(funcs, config, cp) {
|
|||
// TAPI END
|
||||
cp.writeln('}', -1);
|
||||
// export IServerAPI
|
||||
cp.writeln('');
|
||||
cp.writeln('type ValueOf<T> = T[keyof T];');
|
||||
cp.writeln('type RServerAPI<T> = ValueOf<', 1);
|
||||
cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', -1, false);
|
||||
cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1);
|
||||
cp.writeln("(req: TAPI[K]['req'], state: IState, ctx: CTX) =>", 1);
|
||||
cp.writeln("Promise<RServerAPI<TAPI[K]['res']>>}", -2, false);
|
||||
if (!clientOnly) {
|
||||
cp.writeln('');
|
||||
cp.writeln('type ValueOf<T> = T[keyof T];');
|
||||
cp.writeln('type RServerAPI<T> = ValueOf<', 1);
|
||||
cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', -1, false);
|
||||
cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1);
|
||||
cp.writeln("(req: TAPI[K]['req'], state: IState, ctx: CTX) =>", 1);
|
||||
cp.writeln("Promise<RServerAPI<TAPI[K]['res']>>}", -2, false);
|
||||
}
|
||||
// return
|
||||
return cp.end();
|
||||
}
|
||||
|
@ -229,7 +233,8 @@ function codegenClientAPI(funcs, config, cp) {
|
|||
var label = "ClientAPI[" + funcName + "][" + status_2 + "]";
|
||||
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
|
||||
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];
|
||||
_loop_1(funcName, func);
|
||||
}
|
||||
cp.writeln('}');
|
||||
cp.writeln('}', -1);
|
||||
return cp.end();
|
||||
}
|
||||
function codegenSchemas(schemas, config, cp) {
|
||||
|
|
|
@ -9,13 +9,15 @@ import {CodePrinter} from './CodePrinter';
|
|||
|
||||
function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
||||
const {
|
||||
schemasName, utilsTSPath,
|
||||
schemasName, utilsTSPath, clientOnly,
|
||||
} = config;
|
||||
// import
|
||||
cp.writeln(`import * as Schemas from './${schemasName}'`);
|
||||
cp.writeln('import {FullDate, StrictTypeParser as STP, APIPromise} ' +
|
||||
`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\'');
|
||||
// api req, res types
|
||||
cp.writeln(`export type TAPI = {`, 1);
|
||||
|
@ -56,14 +58,16 @@ function codegenIHandler(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
|||
// TAPI END
|
||||
cp.writeln('}', -1);
|
||||
// export IServerAPI
|
||||
cp.writeln('');
|
||||
cp.writeln('type ValueOf<T> = T[keyof T];');
|
||||
cp.writeln('type RServerAPI<T> = ValueOf<', 1);
|
||||
cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;',
|
||||
-1, false);
|
||||
cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1);
|
||||
cp.writeln(`(req: TAPI[K]['req'], state: IState, ctx: CTX) =>`, 1);
|
||||
cp.writeln(`Promise<RServerAPI<TAPI[K]['res']>>}`, -2, false);
|
||||
if (!clientOnly) {
|
||||
cp.writeln('');
|
||||
cp.writeln('type ValueOf<T> = T[keyof T];');
|
||||
cp.writeln('type RServerAPI<T> = ValueOf<', 1);
|
||||
cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;',
|
||||
-1, false);
|
||||
cp.writeln('export type IServerAPI<IState=any> = {[K in keyof TAPI]:', 1);
|
||||
cp.writeln(`(req: TAPI[K]['req'], state: IState, ctx: CTX) =>`, 1);
|
||||
cp.writeln(`Promise<RServerAPI<TAPI[K]['res']>>}`, -2, false);
|
||||
}
|
||||
// return
|
||||
return cp.end();
|
||||
}
|
||||
|
@ -223,13 +227,14 @@ function codegenClientAPI(funcs: APIFuncs, config: Config, cp: CodePrinter) {
|
|||
const label = `ClientAPI[${funcName}][${status}]`;
|
||||
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
|
||||
cp.writeln(`[${
|
||||
Object.keys(resTypes).filter(validateStatus).join(', ')
|
||||
}]),`, -1);
|
||||
}
|
||||
cp.writeln('}');
|
||||
cp.writeln('}', -1);
|
||||
return cp.end();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@sup39/api-ts-gen",
|
||||
"version": "2.0.3",
|
||||
"version": "2.0.3-a",
|
||||
"description": "OpenAPI code generator for TypeScript",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
|
|
Reference in a new issue