diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index fe53ab1f2..000000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,32 +0,0 @@ -version: 2 -jobs: - build: - docker: - - image: cimg/node:lts-browsers - resource_class: large - working_directory: ~/repo - steps: - - checkout - - restore_cache: - keys: - - v1-dependencies-{{ checksum "package-lock.json" }} - - v1-dependencies- - - run: - name: Install dependencies - command: npm install - - save_cache: - key: v1-dependencies-{{ checksum "package-lock.json" }} - paths: - - node_modules - - run: - name: Build library - command: npm run release - - run: - name: Run unit tests - command: npm run test -# - run: -# name: Run e2e tests -# command: npm run test:e2e - - run: - name: Submit to Codecov - command: npm run codecov diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 456d1d9ed..000000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve -title: '' -labels: '' -assignees: ferdikoomen - ---- - -**Describe the bug** -A clear and concise description of what the bug is. Ideally with a screenshot of the result or a link to a small example. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index 902bbe4b2..000000000 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project -title: '' -labels: '' -assignees: '' - ---- - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. Ideally with a small example of the proposed changes. diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 77d50f37b..000000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,22 +0,0 @@ -version: 2 -updates: -- package-ecosystem: npm - directory: "/" - schedule: - interval: "daily" - ignore: - - dependency-name: "@types/node-fetch" - - dependency-name: "node-fetch" - - dependency-name: "camelcase" - - dependency-name: "@angular-devkit/build-angular" - - dependency-name: "@angular/animations" - - dependency-name: "@angular/cli" - - dependency-name: "@angular/common" - - dependency-name: "@angular/compiler" - - dependency-name: "@angular/compiler-cli" - - dependency-name: "@angular/core" - - dependency-name: "@angular/forms" - - dependency-name: "@angular/platform-browser" - - dependency-name: "@angular/platform-browser-dynamic" - - dependency-name: "@angular/router" - - dependency-name: "typescript" \ No newline at end of file diff --git a/.github/workflows/auto-merge.yml b/.github/workflows/auto-merge.yml deleted file mode 100644 index 5d5d5301d..000000000 --- a/.github/workflows/auto-merge.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: auto-merge - -on: pull_request_target - -permissions: - pull-requests: write - contents: write - -jobs: - dependabot: - runs-on: ubuntu-latest - if: ${{ github.actor == 'dependabot[bot]' }} - steps: - - name: Fetch Dependabot metadata - id: dependabot-metadata - uses: dependabot/fetch-metadata@v1 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - - name: Approve PR - run: gh pr review --approve "$PR_URL" - env: - PR_URL: ${{ github.event.pull_request.html_url }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Merge PR - if: ${{ steps.dependabot-metadata.outputs.update-type != 'version-update:semver-major' }} - run: gh pr merge --auto --squash "$PR_URL" - env: - PR_URL: ${{ github.event.pull_request.html_url }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/publish-packages.yml b/.github/workflows/publish-packages.yml new file mode 100644 index 000000000..2d5f91fd7 --- /dev/null +++ b/.github/workflows/publish-packages.yml @@ -0,0 +1,37 @@ +name: Publish Packages + +on: + workflow_dispatch: {} + # inputs: + # version: + # description: 'major | minor | patch' + # required: true + # default: 'patch' + +jobs: + publish-packages: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: '18' + + - name: Install Deps + run: | + npm ci + + - name: Creating .npmrc + run: | + cat << EOF > "$HOME/.npmrc" + //registry.npmjs.org/:_authToken=$NPM_TOKEN + EOF + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Publish Packages + run: | + npm publish --access restricted diff --git a/build-and-test.cmd b/build-and-test.cmd new file mode 100644 index 000000000..bb6d3fa50 --- /dev/null +++ b/build-and-test.cmd @@ -0,0 +1 @@ +npm run build && node ./bin --input ./samples/spec/v3.json --useOptions --client axios --name VerseAxiosClient --output ./.tmp/generated-axios-client diff --git a/package-lock.json b/package-lock.json index ee56b63da..e04e4cc3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "openapi-typescript-codegen", - "version": "0.23.0", + "name": "@powdergg/openapi-typescript-codegen", + "version": "0.4.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "openapi-typescript-codegen", - "version": "0.23.0", + "name": "@powdergg/openapi-typescript-codegen", + "version": "0.4.1", "license": "MIT", "dependencies": { "camelcase": "^6.3.0", diff --git a/package.json b/package.json index f801755be..37b2a5f07 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { - "name": "openapi-typescript-codegen", - "version": "0.23.0", + "name": "@powdergg/openapi-typescript-codegen", + "version": "0.4.1", "description": "Library that generates Typescript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", - "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", + "homepage": "https://github.com/gustvision/openapi-typescript-codegen", "repository": { "type": "git", - "url": "git+https://github.com/ferdikoomen/openapi-typescript-codegen.git" + "url": "git+https://github.com/gustvision/openapi-typescript-codegen.git" }, "bugs": { - "url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues" + "url": "https://github.com/gustvision/openapi-typescript-codegen/issues" }, "license": "MIT", "keywords": [ @@ -55,8 +55,7 @@ "test:e2e": "jest --selectProjects E2E --runInBand --verbose", "eslint": "eslint .", "eslint:fix": "eslint . --fix", - "prepublishOnly": "npm run clean && npm run release", - "codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b" + "prepublishOnly": "npm run clean && npm run release" }, "dependencies": { "camelcase": "^6.3.0", @@ -120,7 +119,7 @@ "typescript": "4.8.4", "zone.js": "0.11.8" }, - "overrides" : { + "overrides": { "rollup": "3.2.3" } } diff --git a/src/templates/core/ApiRequestAdditionalOptions.hbs b/src/templates/core/ApiRequestAdditionalOptions.hbs new file mode 100644 index 000000000..d1a7a016c --- /dev/null +++ b/src/templates/core/ApiRequestAdditionalOptions.hbs @@ -0,0 +1,5 @@ +{{>header}} + +export type ApiRequestAdditionalOptions = { + readonly headers?: Record; +}; diff --git a/src/templates/core/axios/request.hbs b/src/templates/core/axios/request.hbs index 19ebd5fb9..6612f1614 100644 --- a/src/templates/core/axios/request.hbs +++ b/src/templates/core/axios/request.hbs @@ -1,7 +1,7 @@ {{>header}} import axios from 'axios'; -import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; +import type { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios'; import FormData from 'form-data'; import { ApiError } from './ApiError'; @@ -66,10 +66,11 @@ import type { OpenAPIConfig } from './OpenAPI'; * Request method * @param config The OpenAPI configuration object * @param options The request options from the service + * @param axiosClient The axios client instance to use * @returns CancelablePromise * @throws ApiError */ -export const request = (config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise => { +export const request = (config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); @@ -78,7 +79,7 @@ export const request = (config: OpenAPIConfig, options: ApiRequestOptions): C const headers = await getHeaders(config, options, formData); if (!onCancel.isCancelled) { - const response = await sendRequest(config, options, url, body, formData, headers, onCancel); + const response = await sendRequest(config, options, url, body, formData, headers, onCancel, axiosClient); const responseBody = getResponseBody(response); const responseHeader = getResponseHeader(response, options.responseHeader); diff --git a/src/templates/core/axios/sendRequest.hbs b/src/templates/core/axios/sendRequest.hbs index 0b871ea36..4d3c0f402 100644 --- a/src/templates/core/axios/sendRequest.hbs +++ b/src/templates/core/axios/sendRequest.hbs @@ -5,7 +5,8 @@ const sendRequest = async ( body: any, formData: FormData | undefined, headers: Record, - onCancel: OnCancel + onCancel: OnCancel, + axiosClient: AxiosInstance ): Promise> => { const source = axios.CancelToken.source(); @@ -21,7 +22,7 @@ const sendRequest = async ( onCancel(() => source.cancel('The user aborted a request.')); try { - return await axios.request(requestConfig); + return await axiosClient.request(requestConfig); } catch (error) { const axiosError = error as AxiosError; if (axiosError.response) { diff --git a/src/templates/core/functions/getFormData.hbs b/src/templates/core/functions/getFormData.hbs index 3a74b8f3a..b0b4f3abb 100644 --- a/src/templates/core/functions/getFormData.hbs +++ b/src/templates/core/functions/getFormData.hbs @@ -1,4 +1,7 @@ const getFormData = (options: ApiRequestOptions): FormData | undefined => { + if (options.formData instanceof FormData) { + return options.formData + } if (options.formData) { const formData = new FormData(); diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index d6bccbbeb..fe162ff2f 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -30,6 +30,7 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest'; import { OpenAPI } from '../core/OpenAPI'; import { request as __request } from '../core/request'; {{/if}} +import type { ApiRequestAdditionalOptions } from '../core/ApiRequestAdditionalOptions'; {{#equals @root.httpClient 'angular'}} @Injectable({ @@ -103,13 +104,12 @@ export class {{{name}}}{{{@root.postfix}}} { {{/each}} }, {{/if}} - {{#if parametersHeader}} headers: { + ...opts?.headers, {{#each parametersHeader}} '{{{prop}}}': {{{name}}}, {{/each}} }, - {{/if}} {{#if parametersQuery}} query: { {{#each parametersQuery}} diff --git a/src/templates/partials/parameters.hbs b/src/templates/partials/parameters.hbs index 57ab5a7d1..6314ed148 100644 --- a/src/templates/partials/parameters.hbs +++ b/src/templates/partials/parameters.hbs @@ -25,4 +25,6 @@ {{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}}, {{/each}} {{/if}} +, {{/if}} +opts?: ApiRequestAdditionalOptions diff --git a/src/utils/registerHandlebarTemplates.ts b/src/utils/registerHandlebarTemplates.ts index bf77cbdc1..167a18101 100644 --- a/src/utils/registerHandlebarTemplates.ts +++ b/src/utils/registerHandlebarTemplates.ts @@ -11,6 +11,7 @@ import angularSendRequest from '../templates/core/angular/sendRequest.hbs'; import templateCoreApiError from '../templates/core/ApiError.hbs'; import templateCoreApiRequestOptions from '../templates/core/ApiRequestOptions.hbs'; import templateCoreApiResult from '../templates/core/ApiResult.hbs'; +import templateCoreApiRequestAdditionalOptions from '../templates/core/ApiRequestAdditionalOptions.hbs'; import axiosGetHeaders from '../templates/core/axios/getHeaders.hbs'; import axiosGetRequestBody from '../templates/core/axios/getRequestBody.hbs'; import axiosGetResponseBody from '../templates/core/axios/getResponseBody.hbs'; @@ -102,6 +103,7 @@ export interface Templates { request: Handlebars.TemplateDelegate; baseHttpRequest: Handlebars.TemplateDelegate; httpRequest: Handlebars.TemplateDelegate; + apiRequestAdditionalOptions: Handlebars.TemplateDelegate; }; } @@ -129,6 +131,7 @@ export const registerHandlebarTemplates = (root: { settings: Handlebars.template(templateCoreSettings), apiError: Handlebars.template(templateCoreApiError), apiRequestOptions: Handlebars.template(templateCoreApiRequestOptions), + apiRequestAdditionalOptions: Handlebars.template(templateCoreApiRequestAdditionalOptions), apiResult: Handlebars.template(templateCoreApiResult), cancelablePromise: Handlebars.template(templateCancelablePromise), request: Handlebars.template(templateCoreRequest), diff --git a/src/utils/writeClient.spec.ts b/src/utils/writeClient.spec.ts index 3c06a95a5..45ab7b5ce 100644 --- a/src/utils/writeClient.spec.ts +++ b/src/utils/writeClient.spec.ts @@ -28,6 +28,7 @@ describe('writeClient', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/src/utils/writeClientClass.spec.ts b/src/utils/writeClientClass.spec.ts index 102f2eb57..9e43831c3 100644 --- a/src/utils/writeClientClass.spec.ts +++ b/src/utils/writeClientClass.spec.ts @@ -28,6 +28,7 @@ describe('writeClientClass', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/src/utils/writeClientCore.spec.ts b/src/utils/writeClientCore.spec.ts index 36990054e..e73b4acf8 100644 --- a/src/utils/writeClientCore.spec.ts +++ b/src/utils/writeClientCore.spec.ts @@ -30,6 +30,7 @@ describe('writeClientCore', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/src/utils/writeClientCore.ts b/src/utils/writeClientCore.ts index 6d35849d2..748ab54ae 100644 --- a/src/utils/writeClientCore.ts +++ b/src/utils/writeClientCore.ts @@ -40,6 +40,7 @@ export const writeClientCore = async ( await writeFile(resolve(outputPath, 'OpenAPI.ts'), i(templates.core.settings(context), indent)); await writeFile(resolve(outputPath, 'ApiError.ts'), i(templates.core.apiError(context), indent)); await writeFile(resolve(outputPath, 'ApiRequestOptions.ts'), i(templates.core.apiRequestOptions(context), indent)); + await writeFile(resolve(outputPath, 'ApiRequestAdditionalOptions.ts'), i(templates.core.apiRequestAdditionalOptions(context), indent)); await writeFile(resolve(outputPath, 'ApiResult.ts'), i(templates.core.apiResult(context), indent)); await writeFile(resolve(outputPath, 'CancelablePromise.ts'), i(templates.core.cancelablePromise(context), indent)); await writeFile(resolve(outputPath, 'request.ts'), i(templates.core.request(context), indent)); diff --git a/src/utils/writeClientIndex.spec.ts b/src/utils/writeClientIndex.spec.ts index a74421115..bd9420fc6 100644 --- a/src/utils/writeClientIndex.spec.ts +++ b/src/utils/writeClientIndex.spec.ts @@ -26,6 +26,7 @@ describe('writeClientIndex', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/src/utils/writeClientModels.spec.ts b/src/utils/writeClientModels.spec.ts index e147c8e73..c436dee9f 100644 --- a/src/utils/writeClientModels.spec.ts +++ b/src/utils/writeClientModels.spec.ts @@ -43,6 +43,7 @@ describe('writeClientModels', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/src/utils/writeClientSchemas.spec.ts b/src/utils/writeClientSchemas.spec.ts index f71286232..4a362d4a7 100644 --- a/src/utils/writeClientSchemas.spec.ts +++ b/src/utils/writeClientSchemas.spec.ts @@ -43,6 +43,7 @@ describe('writeClientSchemas', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/src/utils/writeClientServices.spec.ts b/src/utils/writeClientServices.spec.ts index b7ebbfe6c..df66cef98 100644 --- a/src/utils/writeClientServices.spec.ts +++ b/src/utils/writeClientServices.spec.ts @@ -31,6 +31,7 @@ describe('writeClientServices', () => { settings: () => 'settings', apiError: () => 'apiError', apiRequestOptions: () => 'apiRequestOptions', + apiRequestAdditionalOptions: () => 'apiRequestAdditionalOptions', apiResult: () => 'apiResult', cancelablePromise: () => 'cancelablePromise', request: () => 'request', diff --git a/tsconfig.json b/tsconfig.json index 8d27e49a8..9727ec856 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,4 +1,5 @@ { + "$schema": "https://json.schemastore.org/tsconfig", "compilerOptions": { "outDir": "./dist", "target": "es2019",