Created
March 15, 2021 14:59
-
-
Save asim/8293a2cb96bd768cab0d675391cde8be to your computer and use it in GitHub Desktop.
A function to call the Micro API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export var BaseURL = 'http://localhost:8080' | |
if(process.env.MICRO_API_ENDPOINT?.length) { | |
BaseURL = process.env.MICRO_API_ENDPOINT | |
} | |
// call makes HTTP JSON calls to the Micro API. If the request succeeds (200), the response body is | |
// returned, otherwise the error is parsed from the response body and returned in the reject. If no | |
// error is returned in the response body, the response status text is returned. | |
export default function call(path: string, params?: any): Promise<any> { | |
return new Promise<any>((resolve: any, reject: any) => { | |
const body = JSON.stringify(params) | |
const headers = { 'Content-Type': 'application/json' } | |
console.log(`Calling Micro API ${path}`) | |
fetch(BaseURL + path, { method: 'POST', body , headers }) | |
.then((async (rsp) => { | |
try { | |
const data = await rsp.json() | |
rsp.status === 200 ? resolve(data) : reject({ error: data["Detail"] || rsp.statusText, code: rsp.status }) | |
} catch(error) { | |
console.error(`Error parsing response from Micro API: ${error}`) | |
rsp.status === 200 ? resolve({}) : reject({ error: rsp.statusText, code: rsp.status }) | |
} | |
})) | |
.catch((error: any) => { | |
console.error(`Error connecting to Micro API: ${error}`); | |
reject({ error, code: 500 }); | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment