@urland/api-generator

0.0.3-alpha.1 • Public • Published

根据 docs Api生成接口及TS类型脚步

稳定版0.0.1版本生成的API,暂不支持一个页面取多端接口前缀
内测版0.0.2-alpha.1 新增routingDynamicApi属性,支持生成api.get(xxx)一接口多端复用模式。

使用指南

 第一步:`npm i @urland/api-generator -D`
 第二步: 在项目根目录中添加`api-generator-config.ts`配置文件
            ```export default {
                    providers: {
                        'common-api': {
                            url: 'https://xxx/docs/common-api',
                        },
                        'admin-api': {
                             url: `https://xxx/docs/admin-api`,
                        },
                        'boss-api': {
                              url: `https://xxx/docs/boss-api`,
                        },
                    },
                };
            ```   
   第三步:在`package.json`文件`scripts中添加命令`           
            ``` 
              "scripts": {
                "sync-api": "sync-api && yarn run lint-fix-api",
                "lint-fix-api": "eslint --fix --ext .ts src/models src/services",
              }
            ```
   第四步:执行 npm run sync-api 即可生成代码  
lint-fix-api命令为eslint格式生成后的代码,可参考@urland/eslint-config包进行配置,也可自行安装eslint。

属性指南

providers:配置docsApi地址,如上图common-api作为key。根据key去生成目录及请求api接口类型。如以下:bossApi.get(xxxx)

        export const getUsersCellphoneNumber = async (cellphoneNumber: string, config: AxiosRequestConfig = {}): Promise<UrApiResponse<User>> => {
            const response = await bossApi.get<User>(`/users/cellphone-number/${cellphoneNumber}`, {}, config);
            return response;
        };

dynamicApi: true   //支持生成api.get(xxx)一接口多端复用模式,除commonApi以外,如api.post(xxxx)。   注:默认不填则按照providers的key为类型

        export const getUsersCellphoneNumber = async (cellphoneNumber: string, config: AxiosRequestConfig = {}): Promise<UrApiResponse<User>> => {
            const response = await api.get<User>(`/users/cellphone-number/${cellphoneNumber}`, {}, config);
            return response;
        };

Readme

Keywords

Package Sidebar

Install

npm i @urland/api-generator

Weekly Downloads

5

Version

0.0.3-alpha.1

License

ISC

Unpacked Size

17.8 kB

Total Files

11

Last publish

Collaborators

  • zhangcc123
  • tinpont