phx-react
Version:
PHX REACT
39 lines • 1.72 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import Cookies from 'js-cookie';
import { AUTH_TOKEN } from '../../utils/constants';
function PHXFetchAPI(url, options) {
return __awaiter(this, void 0, void 0, function () {
var authTokenInfo, method, body, response, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
authTokenInfo = Cookies.get(AUTH_TOKEN) && JSON.parse(Cookies.get(AUTH_TOKEN));
method = options.method, body = options.body;
return [4 /*yield*/, fetch(url, {
method: method,
headers: {
Authorization: 'Bearer' + ' ' + authTokenInfo.token,
'authorization-id': authTokenInfo.userInfo.id,
'Content-Type': 'application/json'
},
body: body
})];
case 1:
response = _a.sent();
if (!response.ok) {
throw new Error("HTTP error! Status: ".concat(response.status));
}
return [4 /*yield*/, response.json()];
case 2: return [2 /*return*/, _a.sent()];
case 3:
error_1 = _a.sent();
console.error('Error calling API:', error_1);
throw error_1;
case 4: return [2 /*return*/];
}
});
});
}
export default PHXFetchAPI;
//# sourceMappingURL=serverQuery.js.map