Skip to content

Commit a3aa314

Browse files
feat: support docs authorization
1 parent 40dc1b4 commit a3aa314

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

.changeset/good-olives-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': minor
3+
---
4+
5+
feat: support docs authorization

README-en_US.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ $ openapi --help
135135
-i, --input <string> OpenAPI specification, can be a path, url (required)
136136
-o, --output <string> output directory (required)
137137
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
138-
--allowedTags <string[]> generate results from allowed tags
138+
--allowedTags <string[]> generate code from allowed tags
139139
--requestOptionsType <string> custom request method options parameter type (default: "{ [key:
140140
string]: unknown }")
141141
--requestImportStatement <string> custom request import statement, for example: "const request =
@@ -145,6 +145,7 @@ $ openapi --help
145145
--isDisplayTypeLabel <boolean> generate label matching type field (default: false)
146146
--isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
147147
--mockFolder <string> mock file path
148+
--authorization <string> docs authorization
148149
--nullable <boolean> null instead of optional (default: false)
149150
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
150151
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
@@ -164,13 +165,14 @@ openapi -i ./spec.json -o ./apis
164165
| schemaPath | yes | string | - | Swagger2/OpenAPI3 URL |
165166
| serversPath | no | string | './src/apis' | the folder path for the generated results |
166167
| requestLibPath | no | string | 'axios' | custom request lib path, for example: '@/request', 'node-fetch' |
167-
| allowedTags | no | string[] | - | generate results from allowed tags |
168+
| allowedTags | no | string[] | - | generate code from allowed tags |
168169
| requestOptionsType | no | string | '{ [key: string]: unknown }' | custom request method options parameter type |
169170
| requestImportStatement | no | string | - | custom request import statement, for example: "const request = require('@/request')" |
170171
| apiPrefix | no | string | - | custom the prefix of the api path, for example: 'api'(variable), "'api'"(string) |
171172
| isDisplayTypeLabel | no | boolean | false | generate label matching type field |
172173
| isGenJsonSchemas | no | boolean | false | generate JSON Schemas |
173174
| mockFolder | no | string | './mocks' | mock file path |
175+
| authorization || string | - | docs authorization |
174176
| nullable | no | boolean | false | null instead of optional |
175177
| isTranslateToEnglishTag | no | boolean | false | translate chinese tag name to english tag name |
176178
| isCamelCase | no | boolean | true | camelCase naming of controller files and request client |

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ $ openapi --help
135135
-i, --input <string> OpenAPI specification, can be a path, url (required)
136136
-o, --output <string> output directory (required)
137137
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
138-
--allowedTags <string[]> generate results from allowed tags
138+
--allowedTags <string[]> generate code from allowed tags
139139
--requestOptionsType <string> custom request method options parameter type (default: "{ [key:
140140
string]: unknown }")
141141
--requestImportStatement <string> custom request import statement, for example: "const request =
@@ -145,6 +145,7 @@ $ openapi --help
145145
--isDisplayTypeLabel <boolean> generate label matching type field (default: false)
146146
--isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
147147
--mockFolder <string> mock file path
148+
--authorization <string> docs authorization
148149
--nullable <boolean> null instead of optional (default: false)
149150
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
150151
--isCamelCase <boolean> camelCase naming of controller files and request client (default: true)
@@ -164,13 +165,14 @@ openapi --i ./spec.json --o ./apis
164165
| schemaPath || string | - | Swagger2/OpenAPI3 地址 |
165166
| serversPath || string | './src/apis' | 生成结果的文件夹路径 |
166167
| requestLibPath || string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
167-
| allowedTags || string[] | - | 根据指定的 tags 生成结果 |
168+
| allowedTags || string[] | - | 根据指定的 tags 生成代码 |
168169
| requestOptionsType || string | '{ [key: string]: unknown }' | 自定义请求方法 options 参数类型 |
169170
| requestImportStatement || string | - | 自定义请求方法表达式,例如:"const request = require('@/request')" |
170171
| apiPrefix || string | - | api path的前缀,例如:'api'(动态变量), "'api'"(字符串) |
171172
| isDisplayTypeLabel || boolean | false | 是否生成 type 对应的label |
172173
| isGenJsonSchemas || boolean | false | 是否生成 JSON Schemas |
173174
| mockFolder || string | './mocks' | mock文件路径 |
175+
| authorization || string | - | 文档权限凭证 |
174176
| nullable || boolean | false | 使用 null 代替可选 |
175177
| isTranslateToEnglishTag || boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |
176178
| isCamelCase || boolean | true | 小驼峰命名文件和请求函数 |

src/bin/openapi.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const params = program
3838
)
3939
.option('--isGenJsonSchemas <boolean>', 'generate JSON Schemas', false)
4040
.option('--mockFolder <string>', 'mock file path')
41+
.option('--authorization <string>', 'docs authorization')
4142
.option('--nullable <boolean>', 'null instead of optional', false)
4243
.option(
4344
'--isTranslateToEnglishTag <boolean>',
@@ -78,6 +79,7 @@ async function run() {
7879
JSON.parse(params.isDisplayTypeLabel as string) === true,
7980
isGenJsonSchemas: JSON.parse(params.isGenJsonSchemas as string) === true,
8081
mockFolder: params.mockFolder as string,
82+
authorization: params.authorization as string,
8183
nullable: JSON.parse(params.nullable as string) === true,
8284
isTranslateToEnglishTag:
8385
JSON.parse(params.isTranslateToEnglishTag as string) === true,

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export type GenerateServiceProps = {
7575
* mock目录
7676
*/
7777
mockFolder?: string;
78+
/**
79+
* 文档权限凭证
80+
*/
81+
authorization?: string;
7882
/**
7983
* 默认为false,true时使用null代替可选值
8084
*/
@@ -171,14 +175,18 @@ export async function generateService({
171175
schemaPath,
172176
mockFolder,
173177
allowedTags,
178+
authorization,
174179
isTranslateToEnglishTag,
175180
...rest
176181
}: GenerateServiceProps) {
177182
if (!schemaPath) {
178183
return;
179184
}
180185

181-
const openAPI = (await getOpenAPIConfig(schemaPath)) as OpenAPIObject;
186+
const openAPI = (await getOpenAPIConfig(
187+
schemaPath,
188+
authorization
189+
)) as OpenAPIObject;
182190

183191
if (isEmpty(openAPI)) {
184192
return;

src/util.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const getImportStatement = (requestLibPath: string) => {
2323
return `import request from 'axios';`;
2424
};
2525

26-
async function getSchema(schemaPath: string) {
26+
async function getSchema(schemaPath: string, authorization?: string) {
2727
if (schemaPath.startsWith('http')) {
2828
const isHttps = schemaPath.startsWith('https:');
2929
const protocol = isHttps ? https : http;
@@ -34,7 +34,10 @@ async function getSchema(schemaPath: string) {
3434
});
3535
const config = isHttps ? { httpsAgent: agent } : { httpAgent: agent };
3636
const json = await axios
37-
.get(schemaPath, config)
37+
.get(schemaPath, {
38+
...config,
39+
headers: { authorization },
40+
})
3841
.then((res) => res.data as OpenAPI.Document);
3942

4043
return json;
@@ -90,8 +93,11 @@ function converterSwaggerToOpenApi(swagger: OpenAPI.Document) {
9093
});
9194
}
9295

93-
export const getOpenAPIConfig = async (schemaPath: string) => {
94-
const schema = await getSchema(schemaPath);
96+
export const getOpenAPIConfig = async (
97+
schemaPath: string,
98+
authorization?: string
99+
) => {
100+
const schema = await getSchema(schemaPath, authorization);
95101

96102
if (!schema) {
97103
return;

0 commit comments

Comments
 (0)