Skip to content
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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payamnaghdy/vue-api-manager",
"version": "0.0.6",
"version": "0.0.7",
"main": "index.js",
"keywords": [
"vue",
Expand Down
43 changes: 41 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const METHODS = {

class VueAPIManager {
constructor(config) {
this._responseParser = null;
this._httpErrorParser = null;
this._defaultErrorMessage = 'No message'
this._host = config.hasOwnProperty('host') ? config.host : "";
this._apiURL = `${this._host}${config.hasOwnProperty('rootURL') ? config.rootURL : "/"}`;
this._apis = config.hasOwnProperty('apis') ? config.apis : {};
Expand Down Expand Up @@ -47,6 +50,16 @@ class VueAPIManager {
this._authorizationHeader = authHeaderGetter;
}

setResponseParser(responseParser) {
this._responseParser = responseParser;
}

setHttpErrorParser(httpErrorParser) {
this._httpErrorParser = httpErrorParser;
}
setDefaultErrorMessage(message) {
this._defaultErrorMessage = message;
}
_getAuthorizationHeader() {
if (!this._authorizationHeader) {
throw new NullAuthorizationHeaderError()
Expand All @@ -70,8 +83,34 @@ class VueAPIManager {
if (requiresAuth) {
requestHeaders['Authorization'] = `${this._authorizationHeaderPrefix} ${this._getAuthorizationHeader()}`;
}
const requestParams ={...defaultParams, ...params}
return await getHTTPMethod(method)(url, requestParams, requestHeaders);
const requestParams = {...defaultParams, ...params}
try {
let response = await getHTTPMethod(method)(url, requestParams, requestHeaders);
if (this._responseParser)
response.parsedData = this._responseParser(response);
return response;
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
let response = error.response;
if (this._httpErrorParser)
response.parsedError = this._httpErrorParser(response);
else
response.parsedError = error.response.status;
return response;
} else {
// Something happened in setting up the request that triggered an Error
if (error.message)
return {
parsedError: error.message
}
else
return {
parsedError: this._defaultErrorMessage
}
}
}
}
}

Expand Down
103 changes: 103 additions & 0 deletions test/api-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,107 @@ describe('VueAPIManager', () => {
expect(mockAxios.delete).toBeCalledTimes(1);
expect(mockAxios.delete).toBeCalledWith("/deleteDataWithParams", {"headers": {}, data: {"id": 1}})
})

it('Parse response with 2xx status', async () => {
let apiManager = new VueAPIManager(API_MANAGER_CONFIG);

mockAxios.get.mockImplementationOnce(() =>
Promise.resolve({
data: {
result: {
status: 'OK'
}
}
})
);
const dataWithoutParser = await apiManager.getAll();
expect(dataWithoutParser.parsedData).toBe(undefined)

mockAxios.get.mockImplementationOnce(() =>
Promise.resolve({
data: {
result: {
status: 'OK'
}
}
})
);
const responseParser = (response) => {
return response.data.result.status
}
apiManager.setResponseParser(responseParser)
const data = await apiManager.getAll();
expect(data.parsedData).toBe('OK')
})

it('Parse response with status > 2xx', async () => {

mockAxios.get.mockImplementationOnce(() => {
throw {
response: {
status: 400,
data: {
result: {
status: 'NOK'
}
}
}
}
}
);

let apiManager = new VueAPIManager(API_MANAGER_CONFIG);

const dataWithoutParser = await apiManager.getAll();
expect(dataWithoutParser.parsedError).toBe(400);

mockAxios.get.mockImplementationOnce(() => {
throw {
response: {
status: 400,
data: {
result: {
status: 'NOK'
}
}
}
}
}
);
const httpErrorParser = (response) => {
return response.data.result.status;
}
apiManager.setHttpErrorParser(httpErrorParser);
const data = await apiManager.getAll();
expect(data.parsedError).toBe('NOK')
})
it('Error without response with message', async () => {
mockAxios.get.mockImplementationOnce(() => {
throw {
message: 'Error from tests'
}
}
);
let apiManager = new VueAPIManager(API_MANAGER_CONFIG);
const data = await apiManager.getAll();
expect(data.parsedError).toBe('Error from tests');
})
it('Error without response and message', async () => {
mockAxios.get.mockImplementationOnce(() => {
throw {}
}
);
let apiManager = new VueAPIManager(API_MANAGER_CONFIG);
const data = await apiManager.getAll();
expect(data.parsedError).toBe('No message');

mockAxios.get.mockImplementationOnce(() => {
throw {}
}
);
apiManager.setDefaultErrorMessage('New default message')
const dataNewDefault = await apiManager.getAll()
expect(dataNewDefault.parsedError).toBe('New default message');

})
})