Replaced some promises with async/await.

This commit is contained in:
David Diaz 2017-10-20 19:33:35 -06:00
parent 23cedd5637
commit 10623f6625
2 changed files with 58 additions and 61 deletions

View File

@ -52,25 +52,27 @@ class Request {
return set;
}
exec(modifiers = []) {
async exec(modifiers = []) {
let dependencies = Array.from(this.DEPENDENCIES);
return this.list.fetchDependencies(dependencies).then(cache => {
let settings = {
endpoint: cache.parse(this.ENDPOINT),
method: this.VERB,
headers: cache.parse(this.HEADERS),
query: cache.parse(this.PARAMS),
payload: cache.parse(this.PAYLOAD)
};
let cache = await this.list.fetchDependencies(dependencies);
modifiers.forEach(mod => {
if (typeof mod.preRequest !== 'undefined') {
mod.preRequest(settings, this.originalRequest);
}
});
let settings = {
endpoint: cache.parse(this.ENDPOINT),
method: this.VERB,
headers: cache.parse(this.HEADERS),
query: cache.parse(this.PARAMS),
payload: cache.parse(this.PAYLOAD)
};
return request({
modifiers.forEach(mod => {
if (typeof mod.preRequest !== 'undefined') {
mod.preRequest(settings, this.originalRequest);
}
});
try {
let response = await request({
url: settings.endpoint,
method: settings.method,
headers: settings.headers,
@ -80,33 +82,28 @@ class Request {
json: true,
simple: false,
resolveWithFullResponse: true
})
.then(response => {
let results = {
request: {
headers: response.request.headers,
body: response.request.body,
endpoint: response.request.uri.href
},
response: {
status: response.statusCode,
headers: response.headers,
body: response.body
},
body: response.body
};
cache.add(`$${this.ALIAS}`, results);
return results;
})
.catch(function({error}) {
throw new Error(error.message);
});
});
let results = {
request: {
headers: response.request.headers,
body: response.request.body,
endpoint: response.request.uri.href
},
response: {
status: response.statusCode,
headers: response.headers,
body: response.body
},
body: response.body
};
cache.add(`$${this.ALIAS}`, results);
return results;
} catch({error}) {
throw new Error(error);
}
}
}

View File

@ -12,42 +12,42 @@ class RequestList {
this.cache = new RequestCache();
}
execByAlias(alias) {
async execByAlias(alias) {
let request = this.list.find(r => r.ALIAS === alias);
if (typeof request === 'undefined') {
return Promise.reject(`${alias} not found among the requests.`);
}
return request
.exec(this.modifiers)
.then(res => {
this.modifiers.forEach(mod => {
if (typeof mod.postResponse !== 'undefined') {
mod.postResponse(res);
}
});
try {
let response = await request.exec(this.modifiers);
return res;
})
.catch(reason => {
return Promise
.reject(`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`);
this.modifiers.forEach(mod => {
if (typeof mod.postResponse !== 'undefined') {
mod.postResponse(response);
}
});
return response;
} catch (reason) {
throw new Error(
`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`
);
}
}
fetchDependencies(dependencies) {
async fetchDependencies(dependencies) {
dependencies = dependencies.map(d => this.execByAlias(d));
await Promise.all(dependencies);
return Promise.all(dependencies).then(() => this.cache);
return this.cache;
}
loadRequests(doc) {
let requestKeys = Object.keys(doc)
.filter(key => {
let verb = key.split(' ')[0].toUpperCase();
return httpVerbs.indexOf(verb) > -1;
});
let requestKeys = Object.keys(doc).filter(key => {
let verb = key.split(' ')[0].toUpperCase();
return httpVerbs.indexOf(verb) > -1;
});
return requestKeys.map(key => {
doc[key] = doc[key] || {};