{"id":20359348,"url":"https://github.com/wubin1989/pullcode","last_synced_at":"2025-04-12T03:32:26.858Z","repository":{"id":41374378,"uuid":"509066883","full_name":"wubin1989/pullcode","owner":"wubin1989","description":"pullcode is a typescript http client code generation cli compatible with Swagger 2 and OpenAPI 3","archived":false,"fork":false,"pushed_at":"2023-08-20T17:15:13.000Z","size":618,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T23:07:17.854Z","etag":null,"topics":["axios","http","http-client","openapi","openapi3","rest-api","restful-api","swagger","swagger-codegen","swagger2","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/wubin1989/pullcode","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wubin1989.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-30T12:15:28.000Z","updated_at":"2024-02-13T15:05:27.000Z","dependencies_parsed_at":"2022-08-31T22:00:51.743Z","dependency_job_id":null,"html_url":"https://github.com/wubin1989/pullcode","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wubin1989%2Fpullcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wubin1989%2Fpullcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wubin1989%2Fpullcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wubin1989%2Fpullcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wubin1989","download_url":"https://codeload.github.com/wubin1989/pullcode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248512587,"owners_count":21116634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["axios","http","http-client","openapi","openapi3","rest-api","restful-api","swagger","swagger-codegen","swagger2","typescript"],"created_at":"2024-11-14T23:33:29.553Z","updated_at":"2025-04-12T03:32:26.825Z","avatar_url":"https://github.com/wubin1989.png","language":"TypeScript","readme":"# pullcode\n  \nPullcode 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.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n### TOC\n\n- [Features](#features)\n- [Credits](#credits)\n- [Installation](#installation)\n- [CLI Options](#cli-options)\n- [Generation Rule](#generation-rule)\n- [Usage](#usage)\n- [Quickstart](#quickstart)\n- [More Examples](#more-examples)\n  - [Response Error Interceptor](#response-error-interceptor)\n  - [Send Requests](#send-requests)\n- [Sister Projects](#sister-projects)\n- [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Features\n\n1. Totally typescript support. Pullcode generates service class, methods, request body and response body object types based on json formatted Swagger2/OpenAPI3 documentation.\n2. Rich and useful configuration options. Pullcode wrapps axios configuration options and add more options for you to elegantly customize requests.\n3. 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.\n4. Framework-agnostic. Pullcode can be used with any frontend frameworks.\n\n## Credits\n\n* [commander.js](https://github.com/tj/commander.js)：nodejs command line tool library\n* [swagger2openapi](https://github.com/Mermade/oas-kit/blob/main/packages/swagger2openapi/README.md)：convert swagger 2 to OpenAPI 3\n* [vue-vben-admin](https://github.com/vbenjs/vue-vben-admin): a modern vue3 admin\n\n## Installation\n\n```shell\nnpm install --save pullcode\n```\n\nNOTE: 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`.\n\n## CLI Options\n\n```shell\n➜  pullcode git:(master) ✗ pullcode -h                                           \nUsage: pullcode [options]\n\nOptions:\n  -o, --output \u003cvalue\u003e  code output path\n  -u, --url \u003cvalue\u003e     swagger 2.0 or openapi 3.0 json api document download url\n  -h, --help            display help for command\n```\n\n* output: directories will be recursively created if not exists.\n\n## Generation Rule\n\nPullcode will check if `BizService.ts` file exists, if exists, skip. Other files will always be overwritten.\n\n## Usage\n\n1. Configure pullcode command into `scripts` attribute in package.json. For example:\n\n```javascript\n\"scripts\": {\n  \"pull\": \"pullcode -u https://petstore3.swagger.io/api/v3/openapi.json -o src/api\"\n},\n```\n\n2. Run `npm run pull`\n\n3. Open `BizService.ts` file, fix `defaultOptions` parameters to fit your need.  \n\n```javascript\nconst defaultOptions: CreateAxiosOptions = {\n  requestOptions: {\n    apiUrl: '', // same as baseUrl\n    urlPrefix: '',\n  } as RequestOptions,\n}\n```\n\n- `apiUrl`: if configured proxy, `apiUrl` should be set to a prefix for matching the proxy.\n- `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).\n- `transform`: set your custom axios interceptors. Normally, you just need to customize response error interceptor.\n- `tokenGetter`: set auth token getter function. The function will be called in built-in request interceptor to put token into header.\n\nFor example: \n\n```javascript\nimport { merge } from 'lodash-es';\nimport { CreateAxiosOptions, VAxios } from '@/httputil/Axios';\nimport { useGlobSetting } from '/@/hooks/setting';\nimport { transform } from '/@/api/interceptor'\nimport { getToken } from '/@/utils/auth';\nimport { RequestOptions } from '@/types/axios';\n\nconst globSetting = useGlobSetting();\n\nconst defaultOptions: CreateAxiosOptions = {\n  transform,\n  requestOptions: {\n    apiUrl: globSetting.apiUrl, // same as baseUrl\n    urlPrefix: '/myservice',\n  } as RequestOptions,\n  tokenGetter: getToken as () =\u003e string\n}\n\nexport class BizService extends VAxios {\n  constructor(options?: Partial\u003cCreateAxiosOptions\u003e) {\n    super(merge(defaultOptions, options || {}));\n  }\n}\n\nexport default BizService;\n```\n\nFor other options, please read `CreateAxiosOptions` definition from source code.\n\n4. After above fix, you can import default singleton service instance to components to send http requests.\n\n## Quickstart\n\nPlease refer to this example app：[pullcode-quickstart](https://github.com/wubin1989/pullcode/tree/master/examples/pullcode-quickstart)\n\n![petstore](./petstore.png)\n\n## More Examples\n### Response Error Interceptor\n\n```javascript\nimport type { AxiosResponse } from 'axios';\nimport type { AxiosTransform } from '@/httputil/axiosTransform';\nimport { checkStatus } from './checkStatus';\nimport { useMessage } from '/@/hooks/web/useMessage';\nimport { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';\nimport { useI18n } from '/@/hooks/web/useI18n';\n\nconst { createMessage, createErrorModal } = useMessage();\n\nexport const transform: AxiosTransform = {\n  /**\n   * @description: Response Error Interceptor\n   */\n  responseInterceptorsCatch: (_: AxiosResponse, error: any) =\u003e {\n    const { t } = useI18n();\n    const errorLogStore = useErrorLogStoreWithOut();\n    errorLogStore.addAjaxErrorInfo(error);\n    const { response, code, message, config } = error || {};\n    const errorMessageMode = config?.requestOptions?.errorMessageMode || 'none';\n    const msg: string = response?.data?.error?.message ?? '';\n    const err: string = error?.toString?.() ?? '';\n    let errMessage = '';\n\n    try {\n      if (code === 'ECONNABORTED' \u0026\u0026 message.indexOf('timeout') !== -1) {\n        errMessage = t('sys.api.apiTimeoutMessage');\n      }\n      if (err?.includes('Network Error')) {\n        errMessage = t('sys.api.networkExceptionMsg');\n      }\n\n      if (errMessage) {\n        if (errorMessageMode === 'modal') {\n          createErrorModal({ title: t('sys.api.errorTip'), content: errMessage });\n        } else if (errorMessageMode === 'message') {\n          createMessage.error(errMessage);\n        }\n        return Promise.reject(error);\n      }\n    } catch (error) {\n      throw new Error(error as unknown as string);\n    }\n\n    checkStatus(error?.response?.status, msg, errorMessageMode);\n    return Promise.reject(error);\n  },\n};\n```\n\n### Send Requests\n\n```javascript\n\u003cscript setup lang=\"ts\"\u003e\nimport { petService } from '@/api/PetService';\nimport { Pet, PetStatusEnum } from '@/api/types';\nimport { ref } from 'vue';\n\nlet loading = ref(true);\nlet dataSource = ref([] as Pet[]);\n\npetService.getPetFindByStatus({\n  status: PetStatusEnum.AVAILABLE,\n}).then((resp: Pet[]) =\u003e {\n  dataSource.value = resp\n  loading.value = false\n})\n\u003c/script\u003e\n```\n\n## Sister Projects\n\n- [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.\n\n## License\n\nMIT","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwubin1989%2Fpullcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwubin1989%2Fpullcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwubin1989%2Fpullcode/lists"}