Skip to content

Commit

Permalink
fix: 同一个项目导出接口列表 API 应只请求一次
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 20, 2020
1 parent 9fc33da commit 9668a94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import JSON5 from 'json5'
import path from 'path'
import prettier from 'prettier'
import request from 'request-promise-native'
import {castArray, dedent, isArray, isEmpty, isFunction, omit, unique} from 'vtils'
import {castArray, dedent, isArray, isEmpty, isFunction, memoize, omit, unique} from 'vtils'
import {CategoryList, Config, ExtendedInterface, Interface, InterfaceList, Method, PropDefinition, RequestBodyType, RequestFormItemType, Required, ResponseBodyType, ServerConfig, SyntheticalConfig} from './types'
import {getNormalizedRelativePath, jsonSchemaStringToJsonSchema, jsonSchemaToType, jsonToJsonSchema, mockjsTemplateToJsonSchema, propDefinitionsToJsonSchema, throwError} from './utils'
import {JSONSchema4} from 'json-schema'
Expand All @@ -23,9 +23,6 @@ export class Generator {
/** 配置 */
private config: ServerConfig[] = []

/** { 项目标识: 分类列表 } */
private projectIdToCategoryList: Record<string, CategoryList | undefined> = Object.create(null)

constructor(
config: Config,
private options: { cwd: string } = {cwd: process.cwd()},
Expand Down Expand Up @@ -446,24 +443,23 @@ export class Generator {
return res.data || res
}

fetchExport = memoize(({serverUrl, token}: SyntheticalConfig) => {
return Generator.fetchApi<CategoryList>(
`${serverUrl}/api/plugin/export`,
{
type: 'json',
status: 'all',
isWiki: 'false',
token: token!,
},
)
}, {
serializer: ({serverUrl, token}) => `${serverUrl}|${token}`,
})

/** 获取分类的接口列表 */
async fetchInterfaceList({serverUrl, token, id}: SyntheticalConfig): Promise<InterfaceList> {
const projectId: string = `${serverUrl}|${token}`

if (!(projectId in this.projectIdToCategoryList)) {
const categoryList = await Generator.fetchApi<CategoryList>(
`${serverUrl}/api/plugin/export`,
{
type: 'json',
status: 'all',
isWiki: 'false',
token: token!,
},
)
this.projectIdToCategoryList[projectId] = categoryList
}

const category = (this.projectIdToCategoryList[projectId] || []).find(
const category = (await this.fetchExport({serverUrl, token}) || []).find(
cat => (
!isEmpty(cat)
&& !isEmpty(cat.list)
Expand Down
10 changes: 10 additions & 0 deletions tests/Generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import tempy from 'tempy'
import {forOwn, OneOrMore} from 'vtils'
import {Generator} from '../src/Generator'

afterEach(() => {
require('request-promise-native').resetExportCount()
})

const generatorFactory = (id: OneOrMore<0 | 82 | 87 | 151 | -82 | -87 | -151>, typesOnly: boolean, enableReactHooks: boolean = false) => {
const apiDir = tempy.directory()
return new Generator({
Expand Down Expand Up @@ -126,4 +130,10 @@ describe('Generator', () => {
expect(fs.readFileSync(requestHookMakerFilePath).toString()).toMatchSnapshot('Hook 生成文件')
})
})

test('同一个项目导出接口列表 API 应只请求一次', async () => {
const generator = generatorFactory(0, false)
await generator.generate()
expect(require('request-promise-native').getExportCount()).toEqual(1)
})
})
11 changes: 11 additions & 0 deletions tests/__mocks__/request-promise-native.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let exportCount: number = 0

const mockData: Record<string, any> = {
'/api/plugin/export': [
{
Expand Down Expand Up @@ -797,8 +799,17 @@ const mockData: Record<string, any> = {
const request = {
get: (url: string) => {
const path = Object.keys(mockData).find(path => url.endsWith(path))
if (path!.endsWith('/api/plugin/export')) {
exportCount++
}
return path && mockData[path]
},
resetExportCount() {
exportCount = 0
},
getExportCount() {
return exportCount
},
}

module.exports = request

0 comments on commit 9668a94

Please sign in to comment.