Skip to content

Commit f23bf9c

Browse files
committed
feat: 暴露些常用的 API
1 parent 92e0298 commit f23bf9c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22
import fs from 'fs-extra'
3-
import { pascalCase } from 'change-case'
4-
import { Config, ApiList, InterfaceType } from './types'
3+
import * as changeCase from 'change-case'
4+
import { Config, ApiList, InterfaceType, ExtendedApi } from './types'
55
import fetchApiCollection from './fetchApiCollection'
66
import generateRequestPayloadType from './generateRequestPayloadType'
77
import generateResponsePayloadType from './generateResponsePayloadType'
@@ -20,14 +20,19 @@ export default async (config: Config): Promise<void> => {
2020
const { getRequestFunctionName, getInterfaceName } = config.categories[categoryId]
2121
return Promise.all(
2222
categoryIdToApiList[categoryId].map(async api => {
23-
const requestDataInterfaceName = pascalCase(getInterfaceName(api, InterfaceType.Request))
24-
const responseDataInterfaceName = pascalCase(getInterfaceName(api, InterfaceType.Response))
23+
const extendedApi: ExtendedApi = {
24+
...api,
25+
parsedPath: path.parse(api.path),
26+
changeCase: changeCase,
27+
}
28+
const requestDataInterfaceName = changeCase.pascalCase(getInterfaceName(extendedApi, InterfaceType.Request))
29+
const responseDataInterfaceName = changeCase.pascalCase(getInterfaceName(extendedApi, InterfaceType.Response))
2530
const requestPayloadType = (await generateRequestPayloadType(api, requestDataInterfaceName)).trim()
2631
const responsePayloadType = (await generateResponsePayloadType(api, responseDataInterfaceName, config.dataKey)).trim()
2732
return [
2833
`/**\n * **请求类型**:${api.title}\n */\n${requestPayloadType}`,
2934
`/**\n * **响应类型**:${api.title}\n */\n${responsePayloadType}`,
30-
`/**\n * ${api.title}\n */\nexport function ${getRequestFunctionName(api)}(data${/(\{\}|any)$/s.test(requestPayloadType) ? '?' : ''}: ${requestDataInterfaceName}): Promise<${responseDataInterfaceName}> {\n${
35+
`/**\n * ${api.title}\n */\nexport function ${getRequestFunctionName(extendedApi)}(data${/(\{\}|any)$/s.test(requestPayloadType) ? '?' : ''}: ${requestDataInterfaceName}): Promise<${responseDataInterfaceName}> {\n${
3136
[
3237
` return request({`,
3338
` path: '${api.path}',`,

src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { ParsedPath } from 'path'
12
import { JSONSchema4 } from 'json-schema'
3+
import * as changeCase from 'change-case'
24

35
// 参考:https://github.com/YMFE/yapi/blob/master/server/models/interface.js#L9
46

@@ -147,9 +149,9 @@ export interface Config {
147149
/** 分类 id */
148150
[key: number]: {
149151
/** 获取发起请求函数的名称 */
150-
getRequestFunctionName: (api: Api) => string,
152+
getRequestFunctionName: (api: ExtendedApi) => string,
151153
/** 获取 ts 接口的名称 */
152-
getInterfaceName: (api: Api, interfaceType: InterfaceType) => string,
154+
getInterfaceName: (api: ExtendedApi, interfaceType: InterfaceType) => string,
153155
},
154156
},
155157
}
@@ -170,3 +172,9 @@ export interface RequestPayload {
170172

171173
/** 请求,应返回包含结果的 Promise */
172174
export type Request = (payload: RequestPayload) => Promise<any>
175+
176+
/** 扩展 API */
177+
export type ExtendedApi = Api & {
178+
parsedPath: ParsedPath,
179+
changeCase: typeof changeCase,
180+
}

0 commit comments

Comments
 (0)