mirror of https://github.com/Seich/Beau.git
Replaced some promises with async/await.
This commit is contained in:
parent
23cedd5637
commit
10623f6625
21
request.js
21
request.js
|
|
@ -52,10 +52,11 @@ class Request {
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
|
|
||||||
exec(modifiers = []) {
|
async exec(modifiers = []) {
|
||||||
let dependencies = Array.from(this.DEPENDENCIES);
|
let dependencies = Array.from(this.DEPENDENCIES);
|
||||||
|
|
||||||
return this.list.fetchDependencies(dependencies).then(cache => {
|
let cache = await this.list.fetchDependencies(dependencies);
|
||||||
|
|
||||||
let settings = {
|
let settings = {
|
||||||
endpoint: cache.parse(this.ENDPOINT),
|
endpoint: cache.parse(this.ENDPOINT),
|
||||||
method: this.VERB,
|
method: this.VERB,
|
||||||
|
|
@ -70,7 +71,8 @@ class Request {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return request({
|
try {
|
||||||
|
let response = await request({
|
||||||
url: settings.endpoint,
|
url: settings.endpoint,
|
||||||
method: settings.method,
|
method: settings.method,
|
||||||
headers: settings.headers,
|
headers: settings.headers,
|
||||||
|
|
@ -80,9 +82,8 @@ class Request {
|
||||||
json: true,
|
json: true,
|
||||||
simple: false,
|
simple: false,
|
||||||
resolveWithFullResponse: true
|
resolveWithFullResponse: true
|
||||||
})
|
});
|
||||||
|
|
||||||
.then(response => {
|
|
||||||
let results = {
|
let results = {
|
||||||
request: {
|
request: {
|
||||||
headers: response.request.headers,
|
headers: response.request.headers,
|
||||||
|
|
@ -100,13 +101,9 @@ class Request {
|
||||||
cache.add(`$${this.ALIAS}`, results);
|
cache.add(`$${this.ALIAS}`, results);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
})
|
} catch({error}) {
|
||||||
|
throw new Error(error);
|
||||||
.catch(function({error}) {
|
}
|
||||||
throw new Error(error.message);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,39 +12,39 @@ class RequestList {
|
||||||
this.cache = new RequestCache();
|
this.cache = new RequestCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
execByAlias(alias) {
|
async execByAlias(alias) {
|
||||||
let request = this.list.find(r => r.ALIAS === alias);
|
let request = this.list.find(r => r.ALIAS === alias);
|
||||||
|
|
||||||
if (typeof request === 'undefined') {
|
if (typeof request === 'undefined') {
|
||||||
return Promise.reject(`${alias} not found among the requests.`);
|
return Promise.reject(`${alias} not found among the requests.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return request
|
try {
|
||||||
.exec(this.modifiers)
|
let response = await request.exec(this.modifiers);
|
||||||
.then(res => {
|
|
||||||
this.modifiers.forEach(mod => {
|
this.modifiers.forEach(mod => {
|
||||||
if (typeof mod.postResponse !== 'undefined') {
|
if (typeof mod.postResponse !== 'undefined') {
|
||||||
mod.postResponse(res);
|
mod.postResponse(response);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return res;
|
return response;
|
||||||
})
|
} catch (reason) {
|
||||||
.catch(reason => {
|
throw new Error(
|
||||||
return Promise
|
`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`
|
||||||
.reject(`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`);
|
);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchDependencies(dependencies) {
|
async fetchDependencies(dependencies) {
|
||||||
dependencies = dependencies.map(d => this.execByAlias(d));
|
dependencies = dependencies.map(d => this.execByAlias(d));
|
||||||
|
await Promise.all(dependencies);
|
||||||
|
|
||||||
return Promise.all(dependencies).then(() => this.cache);
|
return this.cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadRequests(doc) {
|
loadRequests(doc) {
|
||||||
let requestKeys = Object.keys(doc)
|
let requestKeys = Object.keys(doc).filter(key => {
|
||||||
.filter(key => {
|
|
||||||
let verb = key.split(' ')[0].toUpperCase();
|
let verb = key.split(' ')[0].toUpperCase();
|
||||||
return httpVerbs.indexOf(verb) > -1;
|
return httpVerbs.indexOf(verb) > -1;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue