@monokle/synchronizer
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -76,3 +76,4 @@ import { SuppressionStatus } from '@monokle/types'; | ||
private queryApi; | ||
private sendRequest; | ||
private formatAuthorizationHeader; | ||
} |
@@ -135,5 +135,25 @@ "use strict"; | ||
queryApi(query, tokenInfo, variables = {}) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const apiEndpointUrl = (0, normalize_url_1.default)(`${this.apiUrl}/graphql`); | ||
const response = yield (0, node_fetch_1.default)(apiEndpointUrl, { | ||
const response = yield this.sendRequest(apiEndpointUrl, tokenInfo, query, variables); | ||
if (!response.ok) { | ||
throw new Error(`Connection error. Cannot fetch data from ${apiEndpointUrl}. Error '${response.statusText}' (${response.status}).`); | ||
} | ||
const responseJson = yield response.json(); | ||
if (((_a = responseJson === null || responseJson === void 0 ? void 0 : responseJson.errors) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
const error = responseJson.errors[0]; | ||
const msg = error.message; | ||
const code = (_b = error.extensions) === null || _b === void 0 ? void 0 : _b.code; | ||
if (msg === 'Unauthorized' || code === 'UNAUTHENTICATED') { | ||
throw new Error(`Unauthorized error. Make sure that valid auth credentials were used. Cannot fetch data from ${apiEndpointUrl}.`); | ||
} | ||
throw new Error(`${msg} error (code: ${code}). Cannot fetch data from ${apiEndpointUrl}.`); | ||
} | ||
return responseJson; | ||
}); | ||
} | ||
sendRequest(apiEndpointUrl, tokenInfo, query, variables = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (0, node_fetch_1.default)(apiEndpointUrl, { | ||
method: 'POST', | ||
@@ -149,6 +169,2 @@ headers: { | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`Connection error. Cannot fetch data from ${apiEndpointUrl}. Error '${response.statusText}' (${response.status}).`); | ||
} | ||
return response.json(); | ||
}); | ||
@@ -155,0 +171,0 @@ } |
@@ -230,2 +230,86 @@ import { dirname, resolve } from 'path'; | ||
}); | ||
it('throws unauthorized error when invalid auth credentials passed', async () => { | ||
const storagePath = await createTmpConfigDir(); | ||
const synchronizer = createDefaultMonokleSynchronizer(new StorageHandlerPolicy(storagePath)); | ||
const sendRequestStub = sinon | ||
.stub(synchronizer._apiHandler, 'sendRequest') | ||
.callsFake(async (...args) => { | ||
return { | ||
ok: true, | ||
json: async () => { | ||
return { | ||
errors: [ | ||
{ | ||
message: 'Unauthorized', | ||
locations: [ | ||
{ | ||
line: 2, | ||
column: 3, | ||
}, | ||
], | ||
path: ['getProject'], | ||
extensions: { | ||
code: 'UNAUTHENTICATED', | ||
originalError: { | ||
statusCode: 401, | ||
message: 'Unauthorized', | ||
}, | ||
}, | ||
}, | ||
], | ||
data: null, | ||
}; | ||
}, | ||
}; | ||
}); | ||
stubs.push(sendRequestStub); | ||
const policyData = { | ||
slug: 'user6-proj-abc', | ||
}; | ||
try { | ||
await synchronizer.getPolicy(policyData, true, { | ||
accessToken: 'SAMPLE_ACCESS_TOKEN', | ||
tokenType: 'ApiKey', | ||
}); | ||
assert.fail('Should have thrown error.'); | ||
} | ||
catch (err) { | ||
assert.match(err.message, /Unauthorized error\. Make sure that valid auth/); | ||
} | ||
}); | ||
it('throws when there is no policy for project (project slug)', async () => { | ||
const storagePath = await createTmpConfigDir(); | ||
const synchronizer = createDefaultMonokleSynchronizer(new StorageHandlerPolicy(storagePath)); | ||
const sendRequestStub = sinon | ||
.stub(synchronizer._apiHandler, 'sendRequest') | ||
.callsFake(async (...args) => { | ||
return { | ||
ok: true, | ||
json: async () => { | ||
return { | ||
data: { | ||
getProject: { | ||
id: 6000, | ||
name: 'User6 Project', | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
}); | ||
stubs.push(sendRequestStub); | ||
const policyData = { | ||
slug: 'user6-proj-abc', | ||
}; | ||
try { | ||
await synchronizer.getPolicy(policyData, true, { | ||
accessToken: 'SAMPLE_ACCESS_TOKEN', | ||
tokenType: 'ApiKey', | ||
}); | ||
assert.fail('Should have thrown error.'); | ||
} | ||
catch (err) { | ||
assert.match(err.message, /project does not have policy defined\. Configure it/); | ||
} | ||
}); | ||
it('emits synchronize event after policy is fetched', async () => { | ||
@@ -232,0 +316,0 @@ const storagePath = await createTmpConfigDir(); |
@@ -76,3 +76,4 @@ import { SuppressionStatus } from '@monokle/types'; | ||
private queryApi; | ||
private sendRequest; | ||
private formatAuthorizationHeader; | ||
} |
@@ -114,3 +114,20 @@ import normalizeUrl from 'normalize-url'; | ||
const apiEndpointUrl = normalizeUrl(`${this.apiUrl}/graphql`); | ||
const response = await fetch(apiEndpointUrl, { | ||
const response = await this.sendRequest(apiEndpointUrl, tokenInfo, query, variables); | ||
if (!response.ok) { | ||
throw new Error(`Connection error. Cannot fetch data from ${apiEndpointUrl}. Error '${response.statusText}' (${response.status}).`); | ||
} | ||
const responseJson = await response.json(); | ||
if (responseJson?.errors?.length > 0) { | ||
const error = responseJson.errors[0]; | ||
const msg = error.message; | ||
const code = error.extensions?.code; | ||
if (msg === 'Unauthorized' || code === 'UNAUTHENTICATED') { | ||
throw new Error(`Unauthorized error. Make sure that valid auth credentials were used. Cannot fetch data from ${apiEndpointUrl}.`); | ||
} | ||
throw new Error(`${msg} error (code: ${code}). Cannot fetch data from ${apiEndpointUrl}.`); | ||
} | ||
return responseJson; | ||
} | ||
async sendRequest(apiEndpointUrl, tokenInfo, query, variables = {}) { | ||
return fetch(apiEndpointUrl, { | ||
method: 'POST', | ||
@@ -126,6 +143,2 @@ headers: { | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`Connection error. Cannot fetch data from ${apiEndpointUrl}. Error '${response.statusText}' (${response.status}).`); | ||
} | ||
return response.json(); | ||
} | ||
@@ -132,0 +145,0 @@ formatAuthorizationHeader(tokenInfo) { |
{ | ||
"name": "@monokle/synchronizer", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Monokle Cloud synchronizer", | ||
@@ -5,0 +5,0 @@ "author": "Kubeshop", |
153662
3398