Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly set rest api hostname #82

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
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;
url: string;
body?: string;
headers?: Record<string, any>;

Check warning on line 15 in src/lib/request/request.ts

View workflow job for this annotation

GitHub Actions / build-test

Unexpected any. Specify a different type
requestId?: string;
useRESTApi?: boolean;
}
Expand Down Expand Up @@ -45,19 +45,19 @@
apiUrl = DEFAULT_API,
apiUrlREST = DEFAULT_REST_API,
userAgentPrefix = '',
): Promise<AxiosResponse<any>> => {

Check warning on line 48 in src/lib/request/request.ts

View workflow job for this annotation

GitHub Actions / build-test

Unexpected any. Specify a different type
const proxyUri = getProxyForUrl(request.useRESTApi ? apiUrlREST : apiUrl);
if (proxyUri) {
bootstrap({
environmentVariableNamespace: '',
});
}
const topParentModuleName = getTopParentModuleName(module.parent as any);

Check warning on line 55 in src/lib/request/request.ts

View workflow job for this annotation

GitHub Actions / build-test

Unexpected any. Specify a different type
const userAgentPrefixChecked =
userAgentPrefix != '' && !userAgentPrefix.endsWith('/')
? userAgentPrefix + '/'
: userAgentPrefix;
const requestHeaders: Record<string, any> = {

Check warning on line 60 in src/lib/request/request.ts

View workflow job for this annotation

GitHub Actions / build-test

Unexpected any. Specify a different type
'Content-Type':
request.useRESTApi && request.body
? 'application/vnd.api+json'
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 @@ -13,7 +13,7 @@
snykRequest: SnykRequest;
}
interface QueueCallbackListenerBundle {
callback(requestId: string, data: any): void;

Check warning on line 16 in src/lib/request/requestManager.ts

View workflow job for this annotation

GitHub Actions / build-test

Unexpected any. Specify a different type
channel?: string;
}
enum eventType {
Expand All @@ -37,9 +37,13 @@

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
Loading