From 894915e00d7996465cb402c4d3eb2c4ba32350c6 Mon Sep 17 00:00:00 2001 From: Antoine Arlaud Date: Thu, 4 Jul 2024 15:13:45 -0400 Subject: [PATCH] fix: allow for custom api hostname for regional instance --- src/lib/request/requestManager.ts | 10 +++++--- test/lib/request/request-proxy.test.ts | 34 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/lib/request/request-proxy.test.ts diff --git a/src/lib/request/requestManager.ts b/src/lib/request/requestManager.ts index 64033a4..2f684f0 100644 --- a/src/lib/request/requestManager.ts +++ b/src/lib/request/requestManager.ts @@ -37,9 +37,13 @@ interface RequestsManagerParams { function getRESTAPI(endpoint: string): string { // e.g 'https://api.snyk.io/rest/' - const apiData = new URL(endpoint.replace('app.', '')); - - return new URL(`${apiData.protocol}//api.${apiData.host}/rest`).toString(); + const apiData = new URL(endpoint); + if (!apiData.host.startsWith('api.')) { + console.warn( + `${apiData.host} seems invalid and should look like https://api.snyk.io or https://api..snyk.io.`, + ); + } + return new URL(`${apiData.protocol}//${apiData.host}/rest`).toString(); } const getConfig = (): { endpoint: string; token: string } => { diff --git a/test/lib/request/request-proxy.test.ts b/test/lib/request/request-proxy.test.ts new file mode 100644 index 0000000..21d1a47 --- /dev/null +++ b/test/lib/request/request-proxy.test.ts @@ -0,0 +1,34 @@ +import { makeSnykRequest } from '../../../src/lib/request/request'; +import * as fs from 'fs'; +import * as _ from 'lodash'; +import * as path from 'path'; +import { + NotFoundError, + ApiError, + ApiAuthenticationError, + GenericError, +} from '../../../src/lib/customErrors/apiError'; + +const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/'; + +const OLD_ENV = process.env; +beforeEach(() => { + jest.resetModules(); // this is important - it clears the cache + process.env = { ...OLD_ENV }; + delete process.env.SNYK_TOKEN; +}); + +afterEach(() => { + process.env = OLD_ENV; +}); + +describe('Test Snyk Utils make request properly', () => { + it('Test GET command on /', async () => { + const response = await makeSnykRequest( + { verb: 'GET', url: '/user/me' }, + process.env.SNYK_TOKEN, + ); + + expect(response.status).toEqual(200); + }); +});