From ed584b1f110af78b39292662517d78f13bdc309b Mon Sep 17 00:00:00 2001 From: sup39 Date: Wed, 20 May 2020 05:54:19 +0900 Subject: [PATCH] fix clientOnly code: remove CTX --- dist/codegen.js | 27 ++++++++++++++++----------- lib/codegen.ts | 29 +++++++++++++++++------------ package.json | 2 +- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/dist/codegen.js b/dist/codegen.js index 64e8763..0aa18f9 100644 --- a/dist/codegen.js +++ b/dist/codegen.js @@ -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[keyof T];'); - cp.writeln('type RServerAPI = ValueOf<', 1); - cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', -1, false); - cp.writeln('export type IServerAPI = {[K in keyof TAPI]:', 1); - cp.writeln("(req: TAPI[K]['req'], state: IState, ctx: CTX) =>", 1); - cp.writeln("Promise>}", -2, false); + if (!clientOnly) { + cp.writeln(''); + cp.writeln('type ValueOf = T[keyof T];'); + cp.writeln('type RServerAPI = ValueOf<', 1); + cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', -1, false); + cp.writeln('export type IServerAPI = {[K in keyof TAPI]:', 1); + cp.writeln("(req: TAPI[K]['req'], state: IState, ctx: CTX) =>", 1); + cp.writeln("Promise>}", -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,"); + cp.writeln("} as TSTP,", -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) { diff --git a/lib/codegen.ts b/lib/codegen.ts index 6fd080e..f0cb960 100644 --- a/lib/codegen.ts +++ b/lib/codegen.ts @@ -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[keyof T];'); - cp.writeln('type RServerAPI = ValueOf<', 1); - cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', - -1, false); - cp.writeln('export type IServerAPI = {[K in keyof TAPI]:', 1); - cp.writeln(`(req: TAPI[K]['req'], state: IState, ctx: CTX) =>`, 1); - cp.writeln(`Promise>}`, -2, false); + if (!clientOnly) { + cp.writeln(''); + cp.writeln('type ValueOf = T[keyof T];'); + cp.writeln('type RServerAPI = ValueOf<', 1); + cp.writeln('{[K in keyof T]: T[K] extends void ? [K, any?] : [K, T[K]]}>;', + -1, false); + cp.writeln('export type IServerAPI = {[K in keyof TAPI]:', 1); + cp.writeln(`(req: TAPI[K]['req'], state: IState, ctx: CTX) =>`, 1); + cp.writeln(`Promise>}`, -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,`); + cp.writeln(`} as TSTP,`, -1); + cp.tab(1); // kRsv cp.writeln(`[${ Object.keys(resTypes).filter(validateStatus).join(', ') }]),`, -1); } - cp.writeln('}'); + cp.writeln('}', -1); return cp.end(); } diff --git a/package.json b/package.json index 95c082b..933b407 100644 --- a/package.json +++ b/package.json @@ -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",