Skip to content

Commit

Permalink
feat: 将接口请求头加入请求配置
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 26, 2021
1 parent 3a0754a commit 67f6ce0
Show file tree
Hide file tree
Showing 8 changed files with 18,434 additions and 15,227 deletions.
8 changes: 8 additions & 0 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,14 @@ export class Generator {
prodUrl: prodUrl${categoryUID},
path: ${JSON.stringify(extendedInterfaceInfo.path)},
method: Method.${extendedInterfaceInfo.method},
requestHeaders: ${JSON.stringify(
(extendedInterfaceInfo.req_headers || [])
.filter(item => item.name.toLowerCase() !== 'content-type')
.reduce<Record<string, string>>((res, item) => {
res[item.name] = item.value
return res
}, {}),
)},
requestBodyType: RequestBodyType.${
extendedInterfaceInfo.method === Method.GET
? RequestBodyType.query
Expand Down
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ export interface Interface {
catid: number
/** 标签列表 */
tag: string[]
/** 请求头 */
req_headers: Array<{
/** 名称 */
name: string
/** 值 */
value: string
/** 备注 */
desc: string
/** 示例 */
example: string
/** 是否必需 */
required: Required
}>
/** 路径参数 */
req_params: Array<{
/** 名称 */
Expand Down Expand Up @@ -609,6 +622,8 @@ export interface RequestConfig<
path: Path
/** 请求方法 */
method: Method
/** 请求头,除了 Content-Type 的所有头 */
requestHeaders: Record<string, string>
/** 请求数据类型 */
requestBodyType: RequestBodyType
/** 返回数据类型 */
Expand Down
45 changes: 27 additions & 18 deletions tests/Generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra'
import path from 'path'
import tempy from 'tempy'
import { CatId } from './consts'
import { forOwn } from 'vtils'
import { Generator } from '../src/Generator'
import { OneOrMore } from 'vtils/types'
Expand All @@ -20,7 +21,15 @@ const generatorFactory = ({
onlyMatchPath,
comment,
}: {
id: OneOrMore<0 | 82 | 87 | 151 | -82 | -87 | -151>
id: OneOrMore<
| 0
| CatId.test
| CatId.test2
| CatId.issues
| CatId._test
| CatId._test2
| CatId._issues
>
typesOnly?: boolean
enableReactHooks?: boolean
target?: ServerConfig['target']
Expand Down Expand Up @@ -75,7 +84,7 @@ const generatorFactory = ({
describe('Generator', () => {
test('正确生成代码并写入文件 - 单分类', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
})
await generator.prepare()
const output = await generator.generate()
Expand All @@ -96,7 +105,7 @@ describe('Generator', () => {

test('正确生成代码并写入文件 - 多分类', async () => {
const generator = generatorFactory({
id: [82, 87],
id: [CatId.test, CatId.test2],
})
await generator.prepare()
const output = await generator.generate()
Expand Down Expand Up @@ -138,7 +147,7 @@ describe('Generator', () => {

test('正确生成代码并写入文件 - 排除分类', async () => {
const generator = generatorFactory({
id: [0, -82],
id: [0, CatId._test],
})
await generator.prepare()
const output = await generator.generate()
Expand All @@ -159,7 +168,7 @@ describe('Generator', () => {

test('只生成类型代码并写入文件', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
typesOnly: true,
})
await generator.prepare()
Expand All @@ -179,7 +188,7 @@ describe('Generator', () => {

test('生成 React Hooks 代码', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
enableReactHooks: true,
})
await generator.prepare()
Expand Down Expand Up @@ -258,7 +267,7 @@ describe('Generator', () => {

test('支持项目设置里的接口基本路径', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
token: 'with-basepath',
})
await generator.prepare()
Expand All @@ -280,7 +289,7 @@ describe('Generator', () => {

test('支持将 token 设为数组', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
token: ['projectA', 'projectB'],
})
await generator.prepare()
Expand All @@ -302,7 +311,7 @@ describe('Generator', () => {

test('生成请求数据和返回数据的 JSON Schema', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
jsonSchema: {
enabled: true,
},
Expand All @@ -323,7 +332,7 @@ describe('Generator', () => {

test('只生成请求数据的 JSON Schema', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
jsonSchema: {
enabled: true,
responseData: false,
Expand All @@ -345,7 +354,7 @@ describe('Generator', () => {

test('只生成返回数据的 JSON Schema', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
jsonSchema: {
enabled: true,
requestData: false,
Expand Down Expand Up @@ -386,7 +395,7 @@ describe('Generator', () => {

test('无注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
enabled: false,
},
Expand All @@ -407,7 +416,7 @@ describe('Generator', () => {

test('无更新时间注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
updateTime: false,
},
Expand All @@ -428,7 +437,7 @@ describe('Generator', () => {

test('无标题注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
title: false,
},
Expand All @@ -449,7 +458,7 @@ describe('Generator', () => {

test('无分类注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
category: false,
},
Expand All @@ -470,7 +479,7 @@ describe('Generator', () => {

test('无标签注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
tag: false,
},
Expand All @@ -491,7 +500,7 @@ describe('Generator', () => {

test('无请求头注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
requestHeader: false,
},
Expand All @@ -512,7 +521,7 @@ describe('Generator', () => {

test('无链接注释', async () => {
const generator = generatorFactory({
id: 82,
id: CatId.test,
comment: {
link: false,
},
Expand Down
Loading

0 comments on commit 67f6ce0

Please sign in to comment.