Skip to content

Commit

Permalink
Merge pull request #83 from snyk-labs/develop
Browse files Browse the repository at this point in the history
release changes
  • Loading branch information
aarlaud authored Jul 5, 2024
2 parents ef38c31 + 211169c commit 22eaccc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Error from '../customErrors/apiError';
import { bootstrap } from 'global-agent';
import { getProxyForUrl } from 'proxy-from-env';

const DEFAULT_API = 'https://snyk.io/api/v1';
const DEFAULT_API = 'https://api.snyk.io/v1';
const DEFAULT_REST_API = 'https://api.snyk.io/rest/';
interface SnykRequest {
verb: string;
Expand Down
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.') && process.env.NODE_ENV != 'test') {
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
6 changes: 3 additions & 3 deletions test/fixtures/apiResponses/general-doc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"what orgs can the current token access?": "https://snyk.io/api/v1/orgs",
"what projects are owned by this org?": "https://snyk.io/api/v1/org/:id/projects",
"test a package for issues": "https://snyk.io/api/v1/test/:packageManager/:packageName/:packageVersion"
"what orgs can the current token access?": "https://api.snyk.io/v1/orgs",
"what projects are owned by this org?": "https://api.snyk.io/v1/org/:id/projects",
"test a package for issues": "https://api.snyk.io/v1/test/:packageManager/:packageName/:packageVersion"
}
6 changes: 3 additions & 3 deletions test/lib/request/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/';
beforeEach(() => {
return nock('https://snyk.io')
return nock('https://api.snyk.io')
.persist()
.get(/\/xyz/)
.reply(404, '404')
Expand Down Expand Up @@ -40,7 +40,7 @@ beforeEach(() => {
.post(/^(?!.*xyz).*$/)
.reply(200, (uri, requestBody) => {
switch (uri) {
case '/api/v1/':
case '/v1/':
return requestBody;
break;
default:
Expand All @@ -49,7 +49,7 @@ beforeEach(() => {
.get(/^(?!.*xyz).*$/)
.reply(200, (uri) => {
switch (uri) {
case '/api/v1/':
case '/v1/':
return fs.readFileSync(
fixturesFolderPath + 'apiResponses/general-doc.json',
);
Expand Down
18 changes: 9 additions & 9 deletions test/lib/requestManager/normal-flows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RequestsManagerNotFoundError } from '../../../src/lib/customErrors/requ

const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/';
beforeAll(() => {
return nock('https://snyk.io')
return nock('https://api.snyk.io')
.persist()
.get(/\/customtoken/)
.reply(200, function() {
Expand All @@ -35,24 +35,24 @@ beforeAll(() => {
.post(/^(?!.*xyz).*$/)
.reply(200, (uri, requestBody) => {
switch (uri) {
case '/api/v1/':
case '/v1/':
return requestBody;
case '/api/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
case '/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
return fs.readFileSync(
fixturesFolderPath + 'apiResponses/projectIssues.json',
);
default:
}
})
.get(/\/api\/v1\/dummypath/)
.get(/\/v1\/dummypath/)
.delay(1000)
.reply(200, () => {
return 'dummypath slowed down';
})
.get(/^(?!.*xyz).*$/)
.reply(200, (uri) => {
switch (uri) {
case '/api/v1/':
case '/v1/':
return fs.readFileSync(
fixturesFolderPath + 'apiResponses/general-doc.json',
);
Expand Down Expand Up @@ -188,11 +188,11 @@ describe('Testing Request Flows', () => {
const expectedResponse = [
{
'what orgs can the current token access?':
'https://snyk.io/api/v1/orgs',
'https://api.snyk.io/v1/orgs',
'what projects are owned by this org?':
'https://snyk.io/api/v1/org/:id/projects',
'https://api.snyk.io/v1/org/:id/projects',
'test a package for issues':
'https://snyk.io/api/v1/test/:packageManager/:packageName/:packageVersion',
'https://api.snyk.io/v1/test/:packageManager/:packageName/:packageVersion',
},

'dummypath slowed down',
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('Test getConfig function', () => {
});

it('Get snyk.io api endpoint default', async () => {
expect(getConfig().endpoint).toEqual('https://snyk.io/api/v1');
expect(getConfig().endpoint).toEqual('https://api.snyk.io/v1');
});

it('Get snyk api endpoint via env var', async () => {
Expand Down
10 changes: 5 additions & 5 deletions test/lib/requestManager/rate-limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';

const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/';
beforeAll(() => {
return nock('https://snyk.io')
return nock('https://api.snyk.io')
.persist()
.get(/\/xyz/)
.reply(404, '404')
Expand All @@ -26,24 +26,24 @@ beforeAll(() => {
.post(/^(?!.*xyz).*$/)
.reply(200, (uri, requestBody) => {
switch (uri) {
case '/api/v1/':
case '/v1/':
return requestBody;
case '/api/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
case '/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
return fs.readFileSync(
fixturesFolderPath + 'apiResponses/projectIssues.json',
);
default:
}
})
.get(/\/api\/v1\/dummypath/)
.get(/\/v1\/dummypath/)
.delay(1000)
.reply(200, () => {
return 'dummypath slowed down';
})
.get(/^(?!.*xyz).*$/)
.reply(200, (uri) => {
switch (uri) {
case '/api/v1/':
case '/v1/':
return fs.readFileSync(
fixturesFolderPath + 'apiResponses/general-doc.json',
);
Expand Down
26 changes: 13 additions & 13 deletions test/lib/requestManager/retries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const requestManager = new requestsManager();

describe('Testing Request Retries', () => {
it('Retry on 500 - success after 1 retry', async () => {
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(200, () => {
return fs.readFileSync(
Expand All @@ -39,19 +39,19 @@ describe('Testing Request Retries', () => {
});

it('Retry on 500 - success after 4 retries', async () => {
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(200, () => {
return fs.readFileSync(
Expand All @@ -78,22 +78,22 @@ describe('Testing Request Retries', () => {

it('Retry on 500 - fail after 5 retries', async () => {
let hasReached5thTime = false;
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, '500');
nock('https://snyk.io')
nock('https://api.snyk.io')
.post(/\/apierror/)
.reply(500, () => {
hasReached5thTime = true;
Expand Down

0 comments on commit 22eaccc

Please sign in to comment.