mirror of https://github.com/Seich/Beau.git
				
				
				
			Replaced some promises with async/await.
This commit is contained in:
		
							parent
							
								
									23cedd5637
								
							
						
					
					
						commit
						10623f6625
					
				
							
								
								
									
										77
									
								
								request.js
								
								
								
								
							
							
						
						
									
										77
									
								
								request.js
								
								
								
								
							|  | @ -52,25 +52,27 @@ 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 = { |  | ||||||
| 				endpoint: cache.parse(this.ENDPOINT), |  | ||||||
| 				method: this.VERB, |  | ||||||
| 				headers: cache.parse(this.HEADERS), |  | ||||||
| 				query: cache.parse(this.PARAMS), |  | ||||||
| 				payload: cache.parse(this.PAYLOAD) |  | ||||||
| 			}; |  | ||||||
| 
 | 
 | ||||||
| 			modifiers.forEach(mod => { | 		let settings = { | ||||||
| 				if (typeof mod.preRequest !== 'undefined') { | 			endpoint: cache.parse(this.ENDPOINT), | ||||||
| 					mod.preRequest(settings, this.originalRequest); | 			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, | 				url: settings.endpoint, | ||||||
| 				method: settings.method, | 				method: settings.method, | ||||||
| 				headers: settings.headers, | 				headers: settings.headers, | ||||||
|  | @ -80,33 +82,28 @@ class Request { | ||||||
| 				json: true, | 				json: true, | ||||||
| 				simple: false, | 				simple: false, | ||||||
| 				resolveWithFullResponse: true | 				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); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -12,42 +12,42 @@ 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 => { |  | ||||||
| 					if (typeof mod.postResponse !== 'undefined') { |  | ||||||
| 						mod.postResponse(res); |  | ||||||
| 					} |  | ||||||
| 				}); |  | ||||||
| 
 | 
 | ||||||
| 				return res; | 			this.modifiers.forEach(mod => { | ||||||
| 			}) | 				if (typeof mod.postResponse !== 'undefined') { | ||||||
| 			.catch(reason => { | 					mod.postResponse(response); | ||||||
| 				return Promise | 				} | ||||||
| 					.reject(`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`); |  | ||||||
| 			}); | 			}); | ||||||
|  | 
 | ||||||
|  | 			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)); | 		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; | 		}); | ||||||
| 			}); |  | ||||||
| 
 | 
 | ||||||
| 		return requestKeys.map(key => { | 		return requestKeys.map(key => { | ||||||
| 			doc[key] = doc[key] || {}; | 			doc[key] = doc[key] || {}; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue