From f67af6e4b0756c3eb796e55415fe99340b55fec6 Mon Sep 17 00:00:00 2001 From: yangqianjun Date: Thu, 6 May 2021 15:44:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BD=93=E9=AA=8C,=E6=94=AF=E6=8C=81=E6=96=B0?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GettingStarted.mdx | 24 +++++- package.json | 2 +- plugins/typescript-tkit-autos/api.mustache | 63 ++++++++++++++ plugins/typescript-tkit-autos/apis.mustache | 0 .../git_push.sh.mustache | 0 plugins/typescript-tkit-autos/gitignore | 3 + plugins/typescript-tkit-autos/index.mustache | 2 + .../licenseInfo.mustache | 11 +++ plugins/typescript-tkit-autos/model.mustache | 46 ++++++++++ plugins/typescript-tkit-autos/models.mustache | 8 ++ plugins/typescript-tkit-autos/params.mustache | 85 +++++++++++++++++++ src/cli.ts | 18 ++-- src/consts.ts | 6 +- src/index.ts | 18 ++-- static/service.css | 4 + static/service.ts | 30 +++---- 16 files changed, 289 insertions(+), 31 deletions(-) create mode 100644 plugins/typescript-tkit-autos/api.mustache create mode 100644 plugins/typescript-tkit-autos/apis.mustache create mode 100755 plugins/typescript-tkit-autos/git_push.sh.mustache create mode 100644 plugins/typescript-tkit-autos/gitignore create mode 100644 plugins/typescript-tkit-autos/index.mustache create mode 100644 plugins/typescript-tkit-autos/licenseInfo.mustache create mode 100644 plugins/typescript-tkit-autos/model.mustache create mode 100644 plugins/typescript-tkit-autos/models.mustache create mode 100644 plugins/typescript-tkit-autos/params.mustache diff --git a/GettingStarted.mdx b/GettingStarted.mdx index 8bd6751..f1b6058 100644 --- a/GettingStarted.mdx +++ b/GettingStarted.mdx @@ -32,7 +32,7 @@ $ npx autos --help ``` -V, --version output the version number - -c, --config [path] config file (default: "json2service.json") + -c, --config [path] config file (default: "json2service.json" or "json2service.js") --clear rm typescript service before gen --quiet auto merge without popup --apis [boolean] 是否生成 apis,如果未指定 models 且未指定 typeScriptDataFile 则会生成 apis @@ -284,6 +284,28 @@ export declare class Ajax { ): Promise; /** 接口传参校验 */ check(value: V, name: string): void; + /** 新 fetch 接口,>= 3.5.4 */ + public autosFetch( + source: S, + ...args: AutosAPIS[S][0] + ): Promise { + const { isFile, opt, ...config } = (args[0] || {}) as AutosParams; + let { headers = {} } = opt || {}; + if (isFile) { + headers = { + ...headers, + 'Content-Type': 'multipart/form-data' + }; + } + const [method, url] = source.split(' ') as [WrappedFetchParams['method'], string]; + return this.ajax({ + ...config, + ...opt, + method, + url, + headers + }); + } } declare const _default: Ajax; export default _default; diff --git a/package.json b/package.json index 5509be1..411f500 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auto-service", - "version": "3.5.2", + "version": "3.5.3", "description": "auto generate swagger or yapi mock to typescript services and models", "repository": "git@github.com:gogoyqj/auto-service.git", "author": "杨乾军 ", diff --git a/plugins/typescript-tkit-autos/api.mustache b/plugins/typescript-tkit-autos/api.mustache new file mode 100644 index 0000000..13bfa33 --- /dev/null +++ b/plugins/typescript-tkit-autos/api.mustache @@ -0,0 +1,63 @@ +{{>licenseInfo}} +import { ExtraFetchParams } from '@ajax'; +{{^models.isEmpty}} +import * as models from '../model/models'; +{{/models.isEmpty}} + +/* tslint:disable:no-unused-variable member-ordering object-literal-shorthand */ + +{{#operations}} +{{#description}} +/** @description {{&description}} */ +{{/description}} + +declare global { + interface AutosAPIS { +{{#operation}} + {{#description}}/** {{&description}} */{{/description}} + '{{httpMethod}} {{{contextPath}}}{{{path}}}': [[ + { + opt?: {{^headerParams.0}}ExtraFetchParams{{/headerParams.0}}{{#headerParams.0}}{ headers: ParamsHeader{{nickname}} } & Omit {{/headerParams.0}} + {{#queryParams.0}} + query: { + {{#queryParams}} + '{{paramName}}'{{^required}}?{{/required}}: {{&dataType}}; + {{/queryParams}} + }; + {{/queryParams.0}} + {{#pathParams.0}} + path: { + {{#pathParams}} + '{{paramName}}'{{^required}}?{{/required}}: {{&dataType}}; + {{/pathParams}} + }; + {{/pathParams.0}} + {{#formParams.0}} + {{#isFile}} + form: { + {{#formParams}} + '{{baseName}}'{{^required}}?{{/required}}: {{&dataType}}; + {{/formParams}} + }; + isFile: true; + {{/isFile}} + {{^isFile}} + form: { + {{#formParams}} + '{{paramName}}'{{^required}}?{{/required}}: {{&dataType}}; + {{/formParams}} + }; + {{/isFile}} + {{/formParams.0}} + {{#bodyParam.0}} + data: { + {{#bodyParam}} + '{{paramName}}'{{^required}}?{{/required}}: {{&dataType}}; + {{/bodyParam}} + }; + {{/bodyParam.0}} + }], {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}]; +{{/operation}} + } +} +{{/operations}} diff --git a/plugins/typescript-tkit-autos/apis.mustache b/plugins/typescript-tkit-autos/apis.mustache new file mode 100644 index 0000000..e69de29 diff --git a/plugins/typescript-tkit-autos/git_push.sh.mustache b/plugins/typescript-tkit-autos/git_push.sh.mustache new file mode 100755 index 0000000..e69de29 diff --git a/plugins/typescript-tkit-autos/gitignore b/plugins/typescript-tkit-autos/gitignore new file mode 100644 index 0000000..35e2fb2 --- /dev/null +++ b/plugins/typescript-tkit-autos/gitignore @@ -0,0 +1,3 @@ +wwwroot/*.js +node_modules +typings diff --git a/plugins/typescript-tkit-autos/index.mustache b/plugins/typescript-tkit-autos/index.mustache new file mode 100644 index 0000000..5573655 --- /dev/null +++ b/plugins/typescript-tkit-autos/index.mustache @@ -0,0 +1,2 @@ +export * from './api/api'; +export * from './model/models'; \ No newline at end of file diff --git a/plugins/typescript-tkit-autos/licenseInfo.mustache b/plugins/typescript-tkit-autos/licenseInfo.mustache new file mode 100644 index 0000000..bbd8742 --- /dev/null +++ b/plugins/typescript-tkit-autos/licenseInfo.mustache @@ -0,0 +1,11 @@ +/** + * {{{appName}}} + * {{{appDescription}}} + * + * {{#version}}OpenAPI spec version: {{{version}}}{{/version}} + * {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}} + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ diff --git a/plugins/typescript-tkit-autos/model.mustache b/plugins/typescript-tkit-autos/model.mustache new file mode 100644 index 0000000..1231f45 --- /dev/null +++ b/plugins/typescript-tkit-autos/model.mustache @@ -0,0 +1,46 @@ +{{>licenseInfo}} +import * as models from './models'; + +{{#models}} +{{#model}} +/** +{{#description}} + * @description {{&description}} +{{/description}} +{{#vars}} + * @property `{{^required}}[{{/required}}{{name}}{{^required}}]{{/required}}` {{&description}} +{{/vars}} + */ +export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{ +{{#vars}} + {{#description}} + /** + * {{&description}} + */ + {{/description}} + "{{name}}"{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}; +{{/vars}} +} + +{{#hasEnums}} +export namespace {{classname}} { +{{#vars}} +{{#isEnum}} + export enum {{enumName}} { + {{#allowableValues}} + {{#enumVars}} + {{#description}} + /** + * `{{name}}` {{&description}} + */ + {{/description}} + {{{name}}} = {{{value}}} as any{{^-last}},{{/-last}} + {{/enumVars}} + {{/allowableValues}} + } +{{/isEnum}} +{{/vars}} +} +{{/hasEnums}} +{{/model}} +{{/models}} diff --git a/plugins/typescript-tkit-autos/models.mustache b/plugins/typescript-tkit-autos/models.mustache new file mode 100644 index 0000000..22df1ab --- /dev/null +++ b/plugins/typescript-tkit-autos/models.mustache @@ -0,0 +1,8 @@ +{{#models}} +{{#model}} +export * from './{{{ classFilename }}}'; +{{/model}} +{{/models}} +export type ModelInteger = number; +export type ModelFloat = number; +export type LocalDate = Date; \ No newline at end of file diff --git a/plugins/typescript-tkit-autos/params.mustache b/plugins/typescript-tkit-autos/params.mustache new file mode 100644 index 0000000..2828509 --- /dev/null +++ b/plugins/typescript-tkit-autos/params.mustache @@ -0,0 +1,85 @@ +{{#operations}} +{{#operation}} +{{#allParams.0}} +{{^hasOnlyBodyParams}} +{{#headerParams.0}} +/**{{#headerParams}} + * @description {{nickname}} headers 参数 + * @property `{{^required}}[{{/required}}{{baseName}}{{^required}}]{{/required}}` {{&description}} + {{/headerParams}}*/ +export interface ParamsHeader{{nickname}} { + {{#headerParams}} + {{#description}} + /** {{&description}} */ + {{/description}} + '{{baseName}}'{{^required}}?{{/required}}: {{&dataType}}; + {{/headerParams}} +} +{{/headerParams.0}} +/** + {{#description}} + * @description {{nickname}}参数 + {{/description}} +{{#pathParams.0}} + {{#pathParams}} + * @property `{{^required}}[{{/required}}{{paramName}}{{^required}}]{{/required}}` {{&description}} + {{/pathParams}} +{{/pathParams.0}} +{{#queryParams.0}} + {{#queryParams}} + * @property `{{^required}}[{{/required}}{{paramName}}{{^required}}]{{/required}}` {{&description}} + {{/queryParams}} +{{/queryParams.0}} +{{#formParams.0}} + {{#formParams}} + * @property `{{^required}}[{{/required}}{{paramName}}{{^required}}]{{/required}}` {{&description}} + {{/formParams}} +{{/formParams.0}} + */ +export interface Params{{nickname}} { +{{#pathParams.0}} + // pathParams + {{#pathParams}} + {{#description}} + /** + * {{&description}} + */ + {{/description}} + {{paramName}}{{^required}}?{{/required}}: {{&dataType}}; + {{/pathParams}} +{{/pathParams.0}} +{{#queryParams.0}} + // queryParams + {{#queryParams}} + {{#description}} + /** + * {{&description}} + */ + {{/description}} + {{paramName}}{{^required}}?{{/required}}: {{{dataType}}}; + {{/queryParams}} +{{/queryParams.0}} +{{#formParams.0}} + // formParams + {{#formParams}} + {{#description}} + /** + * {{&description}} + */ + {{/description}} + {{paramName}}{{^required}}?{{/required}}: {{&dataType}}; + {{/formParams}} +{{/formParams.0}} +} +{{/hasOnlyBodyParams}} +{{#bodyParam}} +/** + {{#description}} + * {{&description}} + {{/description}} + */ +export type ParamsBody{{nickname}} = {{&dataType}}; +{{/bodyParam}} +{{/allParams.0}} +{{/operation}} +{{/operations}} \ No newline at end of file diff --git a/src/cli.ts b/src/cli.ts index 44339c4..ea54649 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,14 +3,14 @@ import * as fs from 'fs'; import * as path from 'path'; import commander from 'commander'; // @fix no import * https://github.com/microsoft/tslib/issues/58 import chalk from 'chalk'; -import { Json2Service } from './consts'; +import { Json2Service, ProjectDir } from './consts'; import gen from './index'; -const CD = process.cwd(); +const defaultConfig = 'json2service.json'; commander .version(require('../package.json').version) - .option('-c, --config [path]', 'config file', 'json2service.json') + .option('-c, --config [path]', 'config file', defaultConfig) .option('--clear', 'rm typescript service before gen', false) .option('--quiet', 'auto merge without popup', false) .option( @@ -31,10 +31,16 @@ commander .parse(process.argv); const Config = commander.config as string; -const ConfigFile = path.join(CD, Config); - +let ConfigFile = path.join(ProjectDir, Config); +if (!fs.existsSync(ConfigFile)) { + ConfigFile = ConfigFile.replace(/\.json/g, '.js'); +} if (!fs.existsSync(ConfigFile)) { - console.log(chalk.red(`[ERROR]: ${Config} not found in ${CD}`)); + console.log( + chalk.red( + `[ERROR]: ${Config} or ${Config.replace(/\.json/g, '.js')} not found in ${ProjectDir}` + ) + ); } else { // eslint-disable-next-line @typescript-eslint/no-var-requires const config: Json2Service = require(ConfigFile); diff --git a/src/consts.ts b/src/consts.ts index 459420b..91d7ad6 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -180,7 +180,7 @@ export interface Json2Service { export interface SwaggerParser { /** 输出 typescript 代码目录,默认是当前 src/services */ '-o'?: string; - /** 模板目录,默认是 plugins/typescript-tkit,避免修改;配置成 plugins/types-only 仅输出类型 */ + /** 模板目录,默认是 plugins/typescript-tkit,避免修改;配置成 plugins/types-only 仅输出类型;配置成 plugins/typescript-tkit-autos 输出新调用方式格式接口 */ '-t'?: string; /** language,默认是 typescript-angularjs,避免修改 */ '-l'?: string; @@ -191,5 +191,7 @@ export interface SwaggerParser { /** 项目目录 */ export const ProjectDir = process.cwd(); export const RemoteUrlReg = /^http/; +/** 模块目录 */ +export const ModuleDir = path.join(__dirname, '..'); /** 放置依赖 web 静态文件目录 */ -export const StaticDir = path.join(__dirname, '..', 'static'); +export const StaticDir = path.join(ModuleDir, 'static'); diff --git a/src/index.ts b/src/index.ts index 7623813..4a9c010 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,6 +78,12 @@ export default async function gen( }); }; const swagger2tsConfig = { ...defaultParseConfig, ...swaggerParser }; + if ( + swagger2tsConfig['-t'] === 'plugins/types-only' || + swagger2tsConfig['-t'] === 'plugins/typescript-tkit-autos' + ) { + swagger2tsConfig['-t'] = path.join(pluginsPath, '..', swagger2tsConfig['-t']); + } const servicesPath = swagger2tsConfig['-o'] || ''; // IMP: 加载新版 const code: number = await new Promise(rs => { @@ -85,12 +91,12 @@ export default async function gen( remoteSwaggerUrl ? remoteSwaggerUrl.match(RemoteUrlReg) ? request.get( - { - ...requestConfig, - url: remoteSwaggerUrl - }, - (err, res) => cb(err, { body: JSON.parse(res.body) }) - ) + { + ...requestConfig, + url: remoteSwaggerUrl + }, + (err, res) => cb(err, { body: JSON.parse(res.body) }) + ) : cb(undefined, { body: require(remoteSwaggerUrl) as SwaggerJson }) : cb(undefined, {}); }; diff --git a/static/service.css b/static/service.css index 8cc9dad..0a5e116 100644 --- a/static/service.css +++ b/static/service.css @@ -16,6 +16,10 @@ .select-count { float: left; padding: 0 5px; + position: fixed; + right: 0; + top: 0; + background: lightgreen; } .select-change { diff --git a/static/service.ts b/static/service.ts index 0263bd8..a76657b 100644 --- a/static/service.ts +++ b/static/service.ts @@ -5,7 +5,7 @@ // @ts-ignore service.ts 编译规则和 src 不一致 // eslint-disable-next-line prettier/prettier -declare let jsondiffpatch: any; +declare let jsondiffpatch: any; /** 差异 */ declare let delta: any; /** 当前 swagger 版本 */ @@ -78,7 +78,7 @@ declare const Autos: { /** 仅不选择删除 */ // eslint-disable-next-line @typescript-eslint/no-unused-vars function excludeDelete(me: HTMLInputElement) { - setTimeout(function () { + setTimeout(function() { const all = document.getElementById('全选/取消全选') as HTMLInputElement; all.checked = true; toggleSelectAll(all); @@ -88,7 +88,7 @@ declare const Autos: { /** 仅选择新增 */ // eslint-disable-next-line @typescript-eslint/no-unused-vars function onlyAdd(me: HTMLInputElement) { - setTimeout(function () { + setTimeout(function() { const all = document.getElementById('全选/取消全选') as HTMLInputElement; all.checked = true; toggleSelectAll(all); @@ -119,13 +119,13 @@ declare const Autos: { modified ? 'show-modified-only canvas' : addedAndDeleted - ? 'show-added-deleted-only canvas' - : added - ? 'show-added-only canvas' - : deleted - ? 'show-add-deleted canvas' - : 'canvas' - }${propertyNameOnly ? ' show-only-property-name' : ''}`; + ? 'show-added-deleted-only canvas' + : added + ? 'show-added-only canvas' + : deleted + ? 'show-add-deleted canvas' + : 'canvas' + }${propertyNameOnly ? ' show-only-property-name' : ''}`; } const help = `