Skip to content

Commit

Permalink
fix: allow for custom api hostname for regional instance
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed Jul 4, 2024
1 parent a13a0f4 commit 894915e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/lib/request/requestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.<REGION>.snyk.io.`,
);
}
return new URL(`${apiData.protocol}//${apiData.host}/rest`).toString();
}

const getConfig = (): { endpoint: string; token: string } => {
Expand Down
34 changes: 34 additions & 0 deletions test/lib/request/request-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 894915e

Please sign in to comment.