allow makeAPIRequest() to handle JSON requests
This commit is contained in:
		
							parent
							
								
									e1ea41b6d1
								
							
						
					
					
						commit
						25eeae34ba
					
				| @ -6,13 +6,23 @@ | ||||
|  * @returns a Promise, which resolves when the server can be reached and responds without an error | ||||
|  * and rejects otherwise | ||||
|  */ | ||||
| async function makeAPIRequest(endpoint, body) { | ||||
|     return new Promise((resolve, reject) => { | ||||
|         const fetchOptions = { | ||||
| async function makeAPIRequest(endpoint, body, options={}) { | ||||
|     let fetchOptions = { | ||||
|         method: "POST", | ||||
|             body: new URLSearchParams(body) | ||||
|         headers: { | ||||
|             'Accept': 'application/json' | ||||
|         } | ||||
|     }; | ||||
| 
 | ||||
|     if (options["content"] === 'json') { | ||||
|         fetchOptions["headers"]["Content-Type"] = 'application/json' | ||||
|         fetchOptions["body"] = JSON.stringify(body) | ||||
|     } else { | ||||
|         fetchOptions["body"] = new URLSearchParams(body); | ||||
|     } | ||||
| 
 | ||||
|     return new Promise((resolve, reject) => { | ||||
| 
 | ||||
|         fetch("/api/v1/" + endpoint, fetchOptions).then(response => { | ||||
|             const responseCode = response.status; | ||||
|             if (responseCode >= 500) { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user