@instantdb/core
Version:
Instant's core local abstraction
41 lines • 1.88 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export class InstantAPIError extends Error {
constructor(error) {
var _a;
// Create a descriptive message based on the error
const message = ((_a = error.body) === null || _a === void 0 ? void 0 : _a.message) || `API Error (${error.status})`;
super(message);
const actualProto = new.target.prototype;
if (Object.setPrototypeOf) {
Object.setPrototypeOf(this, actualProto);
}
// Maintain proper stack trace for where our error was thrown
if (Error.captureStackTrace) {
Error.captureStackTrace(this, InstantAPIError);
}
this.name = 'InstantAPIError';
this.status = error.status;
this.body = error.body;
}
get [Symbol.toStringTag]() {
return 'InstantAPIError';
}
}
export function jsonFetch(input, init) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield fetch(input, init);
const json = yield res.json();
return res.status === 200
? Promise.resolve(json)
: Promise.reject(new InstantAPIError({ status: res.status, body: json }));
});
}
//# sourceMappingURL=fetch.js.map