From af43f681f888e056293fdb0474738eca267a5dc5 Mon Sep 17 00:00:00 2001 From: Voon Wong Date: Tue, 21 Mar 2023 09:22:25 +1100 Subject: [PATCH 1/2] fix: replace unmaintained dependency request for axios (merge from upstream) --- .gitignore | 1 + .../clients/http-client.ts | 63 ++++--------------- package.json | 3 +- test/e2e/cli.spec.ts | 2 +- 4 files changed, 16 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index 4185006..5060d5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .vs node_modules .idea +.npmrc build-output npm-debug.log package-lock.json diff --git a/lib/swagger-mock-validator/clients/http-client.ts b/lib/swagger-mock-validator/clients/http-client.ts index 48868a8..d420270 100644 --- a/lib/swagger-mock-validator/clients/http-client.ts +++ b/lib/swagger-mock-validator/clients/http-client.ts @@ -1,60 +1,23 @@ -import * as request from 'request'; - -type RequestOptions = request.Options; - -const hasHttp2xxStatusCode = (response: request.RequestResponse) => - response.statusCode && response.statusCode >= 200 && response.statusCode <= 299; +import axios, {AxiosRequestHeaders} from 'axios'; export class HttpClient { - private static getRequestOptions(url: string, auth?: string): RequestOptions { - let requestOptions: RequestOptions = { + public async get(url: string, auth?: string): Promise { + const headers: AxiosRequestHeaders = auth + ? {authorization: 'Basic ' + Buffer.from(auth).toString('base64')} + : {} + const response = await axios.get(url, { + headers, timeout: 30000, - url - }; - if (auth) { - requestOptions = {...requestOptions, headers: { - authorization: 'Basic ' + Buffer.from(auth).toString('base64') - }}; - } - - return requestOptions; - } - - public get(url: string, auth?: string): Promise { - return new Promise((resolve, reject) => { - const requestOptions = HttpClient.getRequestOptions(url, auth); - - request(requestOptions, (error, response, body) => { - if (error) { - reject(error); - } else if (response.statusCode !== 200) { - reject(new Error(`Expected 200 but received ${response.statusCode}`)); - } else { - resolve(body); - } - }); + transformResponse: (data) => data, + validateStatus: (status) => status === 200 }); + return response.data; } - public post(url: string, body: any): Promise { - const requestOptions = { - body, - json: true, - method: 'POST', + public async post(url: string, body: any): Promise { + await axios.post(url, body, { timeout: 5000, - url - }; - - return new Promise((resolve, reject) => { - request(requestOptions, (error, response) => { - if (error) { - reject(error); - } else if (!hasHttp2xxStatusCode(response)) { - reject(new Error(`Expected 2xx but received ${response.statusCode}}`)); - } else { - resolve(); - } - }); + validateStatus: (status) => status >= 200 && status <= 299 }); } } diff --git a/package.json b/package.json index 1ae79dc..a94f510 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "@types/lodash": "^4.14.126", "@types/node": "^14.14.23", "@types/q": "^1.0.0", - "@types/request": "^2.0.3", "@types/validator": "^13.1.0", "@types/verror": "^1.9.0", "ansi-colors": "^4.1.1", @@ -59,13 +58,13 @@ }, "dependencies": { "ajv": "^6.12.6", + "axios": "^0.27.2", "commander": "^7.0.0", "decimal.js": "^10.2.0", "js-yaml": "^4.0.0", "jsonpointer": "^5.0.0", "lodash": "^4.17.10", "openapi-types": "^7.0.1", - "request": "^2.87.0", "swagger-parser": "^10.0.2", "uuidjs": "^4.0.3", "validator": "^13.1.17", diff --git a/test/e2e/cli.spec.ts b/test/e2e/cli.spec.ts index dc32ec2..6f79770 100644 --- a/test/e2e/cli.spec.ts +++ b/test/e2e/cli.spec.ts @@ -310,7 +310,7 @@ describe('swagger-mock-validator/cli', () => { swagger: urlTo('test/e2e/fixtures/swagger-provider.json') })); - expect(error).toEqual(jasmine.stringMatching('Expected 200 but received 404')); + expect(error).toEqual(jasmine.stringMatching('Request failed with status code 404')); }, 30000); it('should succeed when a pact broker url and a swagger url are compatible', async () => { From 72dadebf1892f6819b71ec1b4b180455263c0e47 Mon Sep 17 00:00:00 2001 From: Voon Wong Date: Tue, 21 Mar 2023 10:05:38 +1100 Subject: [PATCH 2/2] chore: Remove redundant build step --- .github/workflows/build-and-test.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 1e6dc5a..44f4a91 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -34,8 +34,6 @@ jobs: - uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Set MSVS version - run: npm config set msvs_version 2017 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: @@ -62,4 +60,4 @@ jobs: # with: # node-version: ${{ matrix.node-version }} # - run: ./bin/build.sh - # shell: bash \ No newline at end of file + # shell: bash