# pullcode

> Pullcode is OpenAPI (aka Swagger) specification compatible typescript http client code generation tool relying on axios. You can configure it to npm scripts to directly generate client code to specified path to easily use. Support Swagger 2 and OpenAPI 3

Latest version **1.2.4** (published 2025-08-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install pullcode
pnpm add pullcode
yarn add pullcode
bun add pullcode
```

Provides the command `pullcode`.

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2025-08-07 |
| First published | 2022-07-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 20 |
| Unpacked size | 162.9 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 8 |
| Author | wubin1989 |
| Maintainers | wubin1989 |

## Links

- npm: https://www.npmjs.com/package/pullcode
- Repository: https://github.com/wubin1989/pullcode
- Issues: https://github.com/wubin1989/pullcode/issues
- npm.io page: https://npm.io/package/pullcode

## Dependencies (20)

- [ejs](https://npm.io/package/ejs.md) ^3.1.8
- [yaml](https://npm.io/package/yaml.md) ^1.10.2
- [axios](https://npm.io/package/axios.md) ^0.24.0
- [dotenv](https://npm.io/package/dotenv.md) ^10.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [prettier](https://npm.io/package/prettier.md) ^2.8.4
- [commander](https://npm.io/package/commander.md) ^8.0.0
- [form-data](https://npm.io/package/form-data.md) ^4.0.0
- [lodash-es](https://npm.io/package/lodash-es.md) ^4.17.21
- [@types/ejs](https://npm.io/package/@types/ejs.md) ^3.1.1
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [typescript](https://npm.io/package/typescript.md) ^4.3.4
- [@types/axios](https://npm.io/package/@types/axios.md) ^0.14.0
- [oas-validator](https://npm.io/package/oas-validator.md) ^5.0.8
- [openapi-types](https://npm.io/package/openapi-types.md) ^9.1.0
- [@types/prettier](https://npm.io/package/@types/prettier.md) ^2.7.2
- [swagger2openapi](https://npm.io/package/swagger2openapi.md) ^7.0.8
- [@types/lodash-es](https://npm.io/package/@types/lodash-es.md) ^4.17.6
- [@apidevtools/swagger-parser](https://npm.io/package/@apidevtools/swagger-parser.md) ^10.0.2
- [prettier-plugin-organize-imports](https://npm.io/package/prettier-plugin-organize-imports.md) ^3.2.2

## Recent versions

- 1.2.4 (latest) — 2025-08-07
- 1.2.3 — 2023-08-20
- 1.2.2 — 2023-08-20
- 1.2.1 — 2023-03-13
- 1.2.0 — 2023-02-15
- 1.1.9 — 2022-07-25
- 1.1.8 — 2022-07-05
- 1.1.7 — 2022-07-04
- 1.1.6 — 2022-07-04
- 1.1.5 — 2022-07-03
- 1.1.4 — 2022-07-03
- 1.1.3 — 2022-07-01
- 1.1.2 — 2022-07-01

## README

# pullcode
  
Pullcode is OpenAPI (aka Swagger) specification compatible typescript http client code generation tool relying on axios. You can configure it to npm scripts to directly generate client code to specified path to easily use. Support Swagger 2 and OpenAPI 3 (aka Swagger 3) in json format.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
### TOC

- [Features](#features)
- [Credits](#credits)
- [Installation](#installation)
- [CLI Options](#cli-options)
- [Generation Rule](#generation-rule)
- [Usage](#usage)
- [Quickstart](#quickstart)
- [More Examples](#more-examples)
  - [Response Error Interceptor](#response-error-interceptor)
  - [Send Requests](#send-requests)
- [Sister Projects](#sister-projects)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Features

1. Totally typescript support. Pullcode generates service class, methods, request body and response body object types based on json formatted Swagger2/OpenAPI3 documentation.
2. Rich and useful configuration options. Pullcode wrapps axios configuration options and add more options for you to elegantly customize requests.
3. Built-in axios request and response interceptors. Request interceptor handles request url rewrite and adds Authorization header. Response interceptor extracts data attribute from raw response. Users can configure additional custom interceptors to do further process. There is no built-in request error and response error interceptors, users should pass their own implementations if necessary.
4. Framework-agnostic. Pullcode can be used with any frontend frameworks.

## Credits

* [commander.js](https://github.com/tj/commander.js)：nodejs command line tool library
* [swagger2openapi](https://github.com/Mermade/oas-kit/blob/main/packages/swagger2openapi/README.md)：convert swagger 2 to OpenAPI 3
* [vue-vben-admin](https://github.com/vbenjs/vue-vben-admin): a modern vue3 admin

## Installation

```shell
npm install --save pullcode
```

NOTE: generated code imports code from pullcode, so pullcode is not only a CLI, but also a npm package, you should use `--save` instead of `--save-dev`.

## CLI Options

```shell
➜  pullcode git:(master) ✗ pullcode -h                                           
Usage: pullcode [options]

Options:
  -o, --output <value>  code output path
  -u, --url <value>     swagger 2.0 or openapi 3.0 json api document download url
  -h, --help            display help for command
```

* output: directories will be recursively created if not exists.

## Generation Rule

Pullcode will check if `BizService.ts` file exists, if exists, skip. Other files will always be overwritten.

## Usage

1. Configure pullcode command into `scripts` attribute in package.json. For example:

```javascript
"scripts": {
  "pull": "pullcode -u https://petstore3.swagger.io/api/v3/openapi.json -o src/api"
},
```

2. Run `npm run pull`

3. Open `BizService.ts` file, fix `defaultOptions` parameters to fit your need.  

```javascript
const defaultOptions: CreateAxiosOptions = {
  requestOptions: {
    apiUrl: '', // same as baseUrl
    urlPrefix: '',
  } as RequestOptions,
}
```

- `apiUrl`: if configured proxy, `apiUrl` should be set to a prefix for matching the proxy.
- `urlPrefix`: should be confirmed with your backend colleagues if there is url prefix to match service name (if the service is behind a gateway) or servlet contxt path (if the service is based on spring boot framework).
- `transform`: set your custom axios interceptors. Normally, you just need to customize response error interceptor.
- `tokenGetter`: set auth token getter function. The function will be called in built-in request interceptor to put token into header.

For example: 

```javascript
import { merge } from 'lodash-es';
import { CreateAxiosOptions, VAxios } from '@/httputil/Axios';
import { useGlobSetting } from '/@/hooks/setting';
import { transform } from '/@/api/interceptor'
import { getToken } from '/@/utils/auth';
import { RequestOptions } from '@/types/axios';

const globSetting = useGlobSetting();

const defaultOptions: CreateAxiosOptions = {
  transform,
  requestOptions: {
    apiUrl: globSetting.apiUrl, // same as baseUrl
    urlPrefix: '/myservice',
  } as RequestOptions,
  tokenGetter: getToken as () => string
}

export class BizService extends VAxios {
  constructor(options?: Partial<CreateAxiosOptions>) {
    super(merge(defaultOptions, options || {}));
  }
}

export default BizService;
```

For other options, please read `CreateAxiosOptions` definition from source code.

4. After above fix, you can import default singleton service instance to components to send http requests.

## Quickstart

Please refer to this example app：[pullcode-quickstart](https://github.com/wubin1989/pullcode/tree/master/examples/pullcode-quickstart)

![petstore](./petstore.png)

## More Examples
### Response Error Interceptor

```javascript
import type { AxiosResponse } from 'axios';
import type { AxiosTransform } from '@/httputil/axiosTransform';
import { checkStatus } from './checkStatus';
import { useMessage } from '/@/hooks/web/useMessage';
import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';
import { useI18n } from '/@/hooks/web/useI18n';

const { createMessage, createErrorModal } = useMessage();

export const transform: AxiosTransform = {
  /**
   * @description: Response Error Interceptor
   */
  responseInterceptorsCatch: (_: AxiosResponse, error: any) => {
    const { t } = useI18n();
    const errorLogStore = useErrorLogStoreWithOut();
    errorLogStore.addAjaxErrorInfo(error);
    const { response, code, message, config } = error || {};
    const errorMessageMode = config?.requestOptions?.errorMessageMode || 'none';
    const msg: string = response?.data?.error?.message ?? '';
    const err: string = error?.toString?.() ?? '';
    let errMessage = '';

    try {
      if (code === 'ECONNABORTED' && message.indexOf('timeout') !== -1) {
        errMessage = t('sys.api.apiTimeoutMessage');
      }
      if (err?.includes('Network Error')) {
        errMessage = t('sys.api.networkExceptionMsg');
      }

      if (errMessage) {
        if (errorMessageMode === 'modal') {
          createErrorModal({ title: t('sys.api.errorTip'), content: errMessage });
        } else if (errorMessageMode === 'message') {
          createMessage.error(errMessage);
        }
        return Promise.reject(error);
      }
    } catch (error) {
      throw new Error(error as unknown as string);
    }

    checkStatus(error?.response?.status, msg, errorMessageMode);
    return Promise.reject(error);
  },
};
```

### Send Requests

```javascript
<script setup lang="ts">
import { petService } from '@/api/PetService';
import { Pet, PetStatusEnum } from '@/api/types';
import { ref } from 'vue';

let loading = ref(true);
let dataSource = ref([] as Pet[]);

petService.getPetFindByStatus({
  status: PetStatusEnum.AVAILABLE,
}).then((resp: Pet[]) => {
  dataSource.value = resp
  loading.value = false
})
</script>
```

## Sister Projects

- [go-doudou](https://github.com/unionj-cloud/go-doudou): OpenAPI 3.0 spec based lightweight microservice framework written in Go. It supports monolith service application as well. Currently, it supports RESTful service only.

## License

MIT

---
_Source: https://npm.io/package/pullcode · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
