@custonomy/community-sdk
Version:
Custonomy Lib for Community
40 lines (35 loc) • 1.37 kB
text/typescript
import fetch from 'node-fetch';
function safeParseJSON(response: any) {
return response.text()
.then ((body : any) => {
// console.log({body});
if (typeof body == 'object') {
// if (body.data) {
// return { body: body.data, status: response.status };
// } else {
if (response.status == 404) return null;
return { body: body, status: response.status };
// }
} else {
// console.log(body)
if (response.status == 404 || body == null) return null;
let _body = JSON.parse(body)
return { body: _body, status: response.status };
// return { body: _body.data ? _body.data : _body, status: response.status };
}
}).catch ((err: Error) =>{
// console.error("Error:", err);
// console.error("Response body:", body);
// throw err;
// return ReE(response, err.message, 500)
return { body: { type: 'error', code: 999, message: err?.message ?? 'unknown error' }};
})
}
export const fetchURL = (url: string, option: any) => {
return fetch(url, option)
.then(function (response : any) {
return safeParseJSON(response);
}).then(function (ret: any) {
return ret;
})
}