Skip to content

Commit 22eaccc

Browse files
authored
Merge pull request #83 from snyk-labs/develop
release changes
2 parents ef38c31 + 211169c commit 22eaccc

File tree

7 files changed

+41
-37
lines changed

7 files changed

+41
-37
lines changed

src/lib/request/request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as Error from '../customErrors/apiError';
66
import { bootstrap } from 'global-agent';
77
import { getProxyForUrl } from 'proxy-from-env';
88

9-
const DEFAULT_API = 'https://snyk.io/api/v1';
9+
const DEFAULT_API = 'https://api.snyk.io/v1';
1010
const DEFAULT_REST_API = 'https://api.snyk.io/rest/';
1111
interface SnykRequest {
1212
verb: string;

src/lib/request/requestManager.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ interface RequestsManagerParams {
3737

3838
function getRESTAPI(endpoint: string): string {
3939
// e.g 'https://api.snyk.io/rest/'
40-
const apiData = new URL(endpoint.replace('app.', ''));
41-
42-
return new URL(`${apiData.protocol}//api.${apiData.host}/rest`).toString();
40+
const apiData = new URL(endpoint);
41+
if (!apiData.host.startsWith('api.') && process.env.NODE_ENV != 'test') {
42+
console.warn(
43+
`${apiData.host} seems invalid and should look like https://api.snyk.io or https://api.<REGION>.snyk.io.`,
44+
);
45+
}
46+
return new URL(`${apiData.protocol}//${apiData.host}/rest`).toString();
4347
}
4448

4549
const getConfig = (): { endpoint: string; token: string } => {
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"what orgs can the current token access?": "https://snyk.io/api/v1/orgs",
3-
"what projects are owned by this org?": "https://snyk.io/api/v1/org/:id/projects",
4-
"test a package for issues": "https://snyk.io/api/v1/test/:packageManager/:packageName/:packageVersion"
2+
"what orgs can the current token access?": "https://api.snyk.io/v1/orgs",
3+
"what projects are owned by this org?": "https://api.snyk.io/v1/org/:id/projects",
4+
"test a package for issues": "https://api.snyk.io/v1/test/:packageManager/:packageName/:packageVersion"
55
}

test/lib/request/request.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/';
1414
beforeEach(() => {
15-
return nock('https://snyk.io')
15+
return nock('https://api.snyk.io')
1616
.persist()
1717
.get(/\/xyz/)
1818
.reply(404, '404')
@@ -40,7 +40,7 @@ beforeEach(() => {
4040
.post(/^(?!.*xyz).*$/)
4141
.reply(200, (uri, requestBody) => {
4242
switch (uri) {
43-
case '/api/v1/':
43+
case '/v1/':
4444
return requestBody;
4545
break;
4646
default:
@@ -49,7 +49,7 @@ beforeEach(() => {
4949
.get(/^(?!.*xyz).*$/)
5050
.reply(200, (uri) => {
5151
switch (uri) {
52-
case '/api/v1/':
52+
case '/v1/':
5353
return fs.readFileSync(
5454
fixturesFolderPath + 'apiResponses/general-doc.json',
5555
);

test/lib/requestManager/normal-flows.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { RequestsManagerNotFoundError } from '../../../src/lib/customErrors/requ
1010

1111
const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/';
1212
beforeAll(() => {
13-
return nock('https://snyk.io')
13+
return nock('https://api.snyk.io')
1414
.persist()
1515
.get(/\/customtoken/)
1616
.reply(200, function() {
@@ -35,24 +35,24 @@ beforeAll(() => {
3535
.post(/^(?!.*xyz).*$/)
3636
.reply(200, (uri, requestBody) => {
3737
switch (uri) {
38-
case '/api/v1/':
38+
case '/v1/':
3939
return requestBody;
40-
case '/api/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
40+
case '/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
4141
return fs.readFileSync(
4242
fixturesFolderPath + 'apiResponses/projectIssues.json',
4343
);
4444
default:
4545
}
4646
})
47-
.get(/\/api\/v1\/dummypath/)
47+
.get(/\/v1\/dummypath/)
4848
.delay(1000)
4949
.reply(200, () => {
5050
return 'dummypath slowed down';
5151
})
5252
.get(/^(?!.*xyz).*$/)
5353
.reply(200, (uri) => {
5454
switch (uri) {
55-
case '/api/v1/':
55+
case '/v1/':
5656
return fs.readFileSync(
5757
fixturesFolderPath + 'apiResponses/general-doc.json',
5858
);
@@ -188,11 +188,11 @@ describe('Testing Request Flows', () => {
188188
const expectedResponse = [
189189
{
190190
'what orgs can the current token access?':
191-
'https://snyk.io/api/v1/orgs',
191+
'https://api.snyk.io/v1/orgs',
192192
'what projects are owned by this org?':
193-
'https://snyk.io/api/v1/org/:id/projects',
193+
'https://api.snyk.io/v1/org/:id/projects',
194194
'test a package for issues':
195-
'https://snyk.io/api/v1/test/:packageManager/:packageName/:packageVersion',
195+
'https://api.snyk.io/v1/test/:packageManager/:packageName/:packageVersion',
196196
},
197197

198198
'dummypath slowed down',
@@ -300,7 +300,7 @@ describe('Test getConfig function', () => {
300300
});
301301

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

306306
it('Get snyk api endpoint via env var', async () => {

test/lib/requestManager/rate-limits.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from 'path';
55

66
const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/';
77
beforeAll(() => {
8-
return nock('https://snyk.io')
8+
return nock('https://api.snyk.io')
99
.persist()
1010
.get(/\/xyz/)
1111
.reply(404, '404')
@@ -26,24 +26,24 @@ beforeAll(() => {
2626
.post(/^(?!.*xyz).*$/)
2727
.reply(200, (uri, requestBody) => {
2828
switch (uri) {
29-
case '/api/v1/':
29+
case '/v1/':
3030
return requestBody;
31-
case '/api/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
31+
case '/v1/org/334e0c45-5d3d-40f6-b882-ae82a164b317/project/0bbbfee1-2138-4322-80d4-4166d1259ae5/issues':
3232
return fs.readFileSync(
3333
fixturesFolderPath + 'apiResponses/projectIssues.json',
3434
);
3535
default:
3636
}
3737
})
38-
.get(/\/api\/v1\/dummypath/)
38+
.get(/\/v1\/dummypath/)
3939
.delay(1000)
4040
.reply(200, () => {
4141
return 'dummypath slowed down';
4242
})
4343
.get(/^(?!.*xyz).*$/)
4444
.reply(200, (uri) => {
4545
switch (uri) {
46-
case '/api/v1/':
46+
case '/v1/':
4747
return fs.readFileSync(
4848
fixturesFolderPath + 'apiResponses/general-doc.json',
4949
);

test/lib/requestManager/retries.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const requestManager = new requestsManager();
1010

1111
describe('Testing Request Retries', () => {
1212
it('Retry on 500 - success after 1 retry', async () => {
13-
nock('https://snyk.io')
13+
nock('https://api.snyk.io')
1414
.post(/\/apierror/)
1515
.reply(500, '500');
16-
nock('https://snyk.io')
16+
nock('https://api.snyk.io')
1717
.post(/\/apierror/)
1818
.reply(200, () => {
1919
return fs.readFileSync(
@@ -39,19 +39,19 @@ describe('Testing Request Retries', () => {
3939
});
4040

4141
it('Retry on 500 - success after 4 retries', async () => {
42-
nock('https://snyk.io')
42+
nock('https://api.snyk.io')
4343
.post(/\/apierror/)
4444
.reply(500, '500');
45-
nock('https://snyk.io')
45+
nock('https://api.snyk.io')
4646
.post(/\/apierror/)
4747
.reply(500, '500');
48-
nock('https://snyk.io')
48+
nock('https://api.snyk.io')
4949
.post(/\/apierror/)
5050
.reply(500, '500');
51-
nock('https://snyk.io')
51+
nock('https://api.snyk.io')
5252
.post(/\/apierror/)
5353
.reply(500, '500');
54-
nock('https://snyk.io')
54+
nock('https://api.snyk.io')
5555
.post(/\/apierror/)
5656
.reply(200, () => {
5757
return fs.readFileSync(
@@ -78,22 +78,22 @@ describe('Testing Request Retries', () => {
7878

7979
it('Retry on 500 - fail after 5 retries', async () => {
8080
let hasReached5thTime = false;
81-
nock('https://snyk.io')
81+
nock('https://api.snyk.io')
8282
.post(/\/apierror/)
8383
.reply(500, '500');
84-
nock('https://snyk.io')
84+
nock('https://api.snyk.io')
8585
.post(/\/apierror/)
8686
.reply(500, '500');
87-
nock('https://snyk.io')
87+
nock('https://api.snyk.io')
8888
.post(/\/apierror/)
8989
.reply(500, '500');
90-
nock('https://snyk.io')
90+
nock('https://api.snyk.io')
9191
.post(/\/apierror/)
9292
.reply(500, '500');
93-
nock('https://snyk.io')
93+
nock('https://api.snyk.io')
9494
.post(/\/apierror/)
9595
.reply(500, '500');
96-
nock('https://snyk.io')
96+
nock('https://api.snyk.io')
9797
.post(/\/apierror/)
9898
.reply(500, () => {
9999
hasReached5thTime = true;

0 commit comments

Comments
 (0)