/
swagger.config.js
138 lines (132 loc) · 5.34 KB
/
swagger.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const freeSwagger = require("free-swagger")
const { mock } = freeSwagger
const path = require("path")
freeSwagger('https://petstore.swagger.io/v2/swagger.json')
mock('https://petstore.swagger.io/v2/swagger.json')
// update typedef/interface
// freeSwagger({source:'https://petstore.swagger.io/v2/swagger.json',typeOnly:true})
// typescript
// freeSwagger({source:'https://petstore.swagger.io/v2/swagger.json', lang:'ts'})
// edit root
// freeSwagger({source: 'https://petstore.swagger.io/v2/swagger.json', root: path.resolve(__dirname, 'other/root')})
// custom code
// freeSwagger({
// source: 'https://petstore.swagger.io/v2/swagger.json',
// customImportCode: 'import customRequest from "./customRequest"',
// templateFunction: ({
// url, // 完整路径 {string}
// summary, // 注释 {string}
// method, // 请求方法 {string}
// name, // api 函数名 {string}
// responseType, // 响应值种类,同 axios {string}
// pathParams, // 路径参数 {Array<string>}
// IQueryParams, // 请求查询参数 ts 类型
// IBodyParams, // 请求体参数 ts 类型
// IPathParams // 请求路径参数 ts 类型
// }) => {
// /**
// * js 代码模版
// **/
//
// // debugger
// // 可通过 debugger 调试模版
//
// // 处理路径参数 `/pet/{id}` => `/pet/${id}`
// const parsedUrl = url.replace(/{(.*?)}/g, '${$1}');
//
// // 有 query 和 body 参数
// const multipleParamsCondition = ({IQueryParams, IBodyParams}) =>
// IQueryParams && IBodyParams
//
// const firstParamCodeMap = new Map()
// // 只有 query 参数,可能有 path 参数
// .set(
// ({IQueryParams, IBodyParams}) => IQueryParams && !IBodyParams,
// `params,`
// )
// // 只有 body 参数,可能有 path 参数
// .set(
// ({IQueryParams, IBodyParams}) => IBodyParams && !IQueryParams,
// `params,`
// )
// // 有 query 和 body 参数,可能有 path 参数
// .set(
// multipleParamsCondition,
// () => `queryParams,`
// )
// // 没有 query body 参数,有 path 参数
// .set(
// ({IQueryParams, pathParams, IBodyParams}) => !IBodyParams && !IQueryParams && pathParams.length,
// '_NOOP,'
// )
// // 只有 path 参数
// .set(
// ({pathParams}) => pathParams.length,
// ({pathParams}) =>
// `{${pathParams.join(',')}},`
// )
//
// const secondParamCodeMap = new Map()
// // 有 path 参数
// .set(
// ({pathParams}) => pathParams.length,
// ({pathParams}) =>
// `{${pathParams.join(',')}},`
// )
// // 有 query 和 body 参数,有 path 参数
// .set(multipleParamsCondition, `_NOOP,`)
//
// const thirdParamCodeMap = new Map()
// // 有 query 和 body 参数,有 path 参数
// .set(
// multipleParamsCondition,
// `bodyParams,`
// )
//
// const paramCodeMap = new Map()
// .set(multipleParamsCondition, 'queryParams,')
// .set(({IQueryParams}) => !!IQueryParams, 'params,')
//
// const dataCodeMap = new Map()
// .set(multipleParamsCondition, 'bodyParams,')
// .set(({IBodyParams}) => !!IBodyParams, 'params,')
//
// const createParamCode = (conditionMap, defaultCode = '') => {
// let code = defaultCode
// for (const [condition, codeFunction] of conditionMap.entries()) {
// const res = condition({
// IQueryParams,
// IBodyParams,
// pathParams,
// })
// if (res) {
// code =
// typeof codeFunction === 'string'
// ? codeFunction
// : codeFunction({
// IQueryParams,
// IBodyParams,
// IPathParams,
// pathParams,
// })
// break
// }
// }
// return code
// }
//
// return `
// ${summary ? `// ${summary}` : ""}
// export const ${name} = (
// ${createParamCode(firstParamCodeMap) /* query | body | NOOP */}
// ${createParamCode(secondParamCodeMap) /* path | null */}
// ${createParamCode(thirdParamCodeMap) /* body | null */}
// ) => customRequest({
// url: \`${parsedUrl}\`,
// method: "${method}",
// params:${createParamCode(paramCodeMap, '{},')}
// data:${createParamCode(dataCodeMap, '{},')}
// ${responseType === "json" ? "" : `responseType: ${responseType},`}
// })`
// }
// })