Gathering detailed insights and metrics for x-sm2tsservice
Gathering detailed insights and metrics for x-sm2tsservice
Gathering detailed insights and metrics for x-sm2tsservice
Gathering detailed insights and metrics for x-sm2tsservice
npm install x-sm2tsservice
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
11
25
推荐替换为 tkit-service npm i -D tkit-service
1 cd yourProjectName 2 java -version 3 npm i sm2tsservice -D
edit json2service.json
,也可用 xxx.js
,然后配置 -c xxx.js
参数 | 值 | 说明 |
---|---|---|
url | url 或者文件地址 | swagger、yapi 文档 url 地址或者文件目录,注意:如果是本地文件,文件名不能以 http 开头 |
requestConfig | 拉取远程文档配置 | |
url | 同 url | |
headers | 请求头配置,见 headers | |
type | yapi、swagger | 标记类型,默认是 swagger |
swaggerParser | swagger-code-gen 配置 | |
-o | 输出 typescript 代码目录,默认是当前 src/services | |
-t | 模板目录,默认是工具内置模板目录 plugins/typescript-tkit/,避免修改 | |
-l | 模板目录,默认是 typescript-angularjs,避免修改 | |
validateResponse | boolean | 是否生成校验逻辑,默认 false,详细文档 |
yapiConfig | yapi 相关配置 | |
required | 当直接使用 yapi json 定义返回数据格式的时候,生成的 typescript 文件,默认情况下,所有字段都是可选的,配置成 true,则所有字段都是不可缺省的 | |
bodyJsonRequired | 当直接使用 yapi json 定义 json 格式 body 参数的时候,生成的 typescript 文件,默认情况下,所有字段都是可选的,配置成 true,则所有字段都是不可缺省的 | |
categoryMap | 对象,yapi 项目接口分类中英文映射,如 { "公共分类": "Common" } | |
guardConfig | mode | 缺省, safe, strict |
methodUrl2OperationIdMap | 对象,http method + url => operationId 映射,如 {"get /api/xxx/xxx": "operationId"} | |
badParamsReg | 非法参数格式校验正则,默认 /[^a-z0-9_.[]$]/gi ,仅配置文件是 *.js 可用 | |
prefixReg | 生成 url + Using + http method 时,需移除 url 前缀正则,默认是 /^(\/)?api\//g ,仅配置文件是 *.js 可用 |
1{ 2 "url": "./api.json", // 文件路径或url 3 "requestConfig": { 4 "url": "./api.json" // 文件路径或url 5 // 以下所有 request 支持的参数 6 // headers?: Headers; 7 // baseUrl?: string; 8 // callback?: RequestCallback; 9 // jar?: CookieJar | boolean; 10 // formData?: { [key: string]: any }; 11 // form?: { [key: string]: any } | string; 12 // auth?: AuthOptions; 13 // oauth?: OAuthOptions; 14 // aws?: AWSOptions; 15 // hawk?: HawkOptions; 16 // qs?: any; 17 // qsStringifyOptions?: any; 18 // qsParseOptions?: any; 19 // json?: any; 20 // jsonReviver?: (key: string, value: any) => any; 21 // jsonReplacer?: (key: string, value: any) => any; 22 // multipart?: RequestPart[] | Multipart; 23 // agent?: http.Agent | https.Agent; 24 // agentOptions?: http.AgentOptions | https.AgentOptions; 25 // agentClass?: any; 26 // forever?: any; 27 // host?: string; 28 // port?: number; 29 // method?: string; 30 // body?: any; 31 // family?: 4 | 6; 32 // followRedirect?: boolean | ((response: http.IncomingMessage) => boolean); 33 // followAllRedirects?: boolean; 34 // followOriginalHttpMethod?: boolean; 35 // maxRedirects?: number; 36 // removeRefererHeader?: boolean; 37 // encoding?: string | null; 38 // pool?: any; 39 // timeout?: number; 40 // localAddress?: string; 41 // proxy?: any; 42 // tunnel?: boolean; 43 // strictSSL?: boolean; 44 // rejectUnauthorized?: boolean; 45 // time?: boolean; 46 // gzip?: boolean; 47 // preambleCRLF?: boolean; 48 // postambleCRLF?: boolean; 49 // withCredentials?: boolean; 50 // key?: Buffer; 51 // cert?: Buffer; 52 // passphrase?: string; 53 // ca?: string | Buffer | string[] | Buffer[]; 54 // har?: HttpArchiveRequest; 55 // useQuerystring?: boolean; 56 }, 57 "type": "yapi", 58 "yapiConfig": { 59 "required": false, 60 "bodyJsonRequired": false, 61 "categoryMap": { 62 "中文": "English" // yapi 接口分类中英文映射 63 } 64 }, 65 "swaggerParser": { 66 "-o": "tmp/services" 67 }, 68 "validateResponse": false, // 是否生成校验逻辑 69 "guardConfig": { 70 // + strict 严格模式 71 // - 校验 swagger tags【yapi 接口分类】是否是纯英文 72 // - 方法名使用 http method + url 驼峰形式 73 // - 新项目采用 74 // + safe 安全模式 75 // - 方法名使用 http method + url 驼峰形式 76 // - 老项目升级,不会校验 tags,会生成方法调用替换映射表 77 // + 默认 78 // - http method + url => operationId 映射锁定 79 // - 老项目维持现状 80 "mode": "strict", 81 // swagger 处理重复 operationId 逻辑有风险,因此需要锁定映射关系 82 "methodUrl2OperationIdMap": { 83 "get /api/xxx/xxx": "operationId" 84 } 85 } 86}
参考下方代码,实现 ajax 类【如果使用的 axios,且后端返回数据结构遵循 { code?:number;message?:string;result: any }
,可直接复制使用】
推荐使用 tkit-ajax npm i tkit-ajax
1import axios, { AxiosError } from 'axios'; 2import qs from 'qs'; 3 4const inst = axios.create({ 5 timeout: 2000, 6 withCredentials: true, 7 headers: {} 8}); 9 10// @cc: 检测 axios 响应状态 11function onStatusError(error: AxiosError | Error) { 12 const err = 13 'response' in error && error.response 14 ? { 15 code: error.response.status, 16 message: error.response.statusText 17 } 18 : { code: 10001, message: error.message }; 19 if (err.code === 401 || err.code === 403) { 20 // @todo 未登录未授权 21 // EventCenter.emit('common.user.status', err); 22 } 23 return err; 24} 25 26export type AjaxPromise<R> = Promise<R>; 27 28export interface ExtraFetchParams { 29 extra?: any; 30} 31 32export interface WrappedFetchParams extends ExtraFetchParams { 33 method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH' | 'HEAD'; 34 url: string; 35 data?: any; // post json 36 form?: any; // post form 37 query?: any; 38 header?: any; 39 path?: any; 40} 41 42export class WrappedFetch { 43 /** 44 * @description ajax 方法 45 */ 46 public ajax( 47 { method, url, data, form, query, header, extra }: WrappedFetchParams, 48 path?: string, 49 basePath?: string 50 ) { 51 let config = { 52 ...extra, 53 method: method.toLocaleLowerCase(), 54 headers: { ...header } 55 }; 56 // json 57 if (data) { 58 config = { 59 ...config, 60 headers: { 61 ...config.headers, 62 'Content-Type': 'application/json' 63 }, 64 data 65 }; 66 } 67 // form 68 if (form) { 69 config = { 70 ...config, 71 headers: { 72 ...config.headers, 73 'Content-Type': 'application/x-www-form-urlencoded' 74 }, 75 data: qs.stringify(form) 76 }; 77 } 78 return inst 79 .request({ ...config, url, params: query }) 80 .then(res => res.data) 81 .catch(onStatusError); 82 } 83 84 /** 85 * @description 接口传参校验 86 */ 87 public check<V>(value: V, name: string) { 88 if (value === null || value === undefined) { 89 const msg = `[ERROR PARAMS]: ${name} can't be null or undefined`; 90 // 非生产环境,直接抛出错误 91 if (process.env.NODE_ENV === 'development') { 92 throw Error(msg); 93 } 94 } 95 } 96} 97 98export default new WrappedFetch();
配置 tsconfig.json[如未使用 ts-loader,还需要配置 webpack alias]
1{ 2 "paths": { 3 "@ajax": ["你的实现文件地址"] 4 } 5}
1 ./node_modules/.bin/service # 使用默认配置 2 ./node_modules/.bin/service -c config.json # 指定配置文件 3 ./node_modules/.bin/service --clear # 清空上次生成产物
或可以写入 package.json
,通过 npm run api
使用
1{ 2 "scripts": { 3 "api": "service --clear" 4 } 5}
No vulnerabilities found.
No security vulnerabilities found.