@miuiu/postgrest
Version:
Isomorphic PostgREST client
136 lines • 5.27 kB
JavaScript
export default class PostgrestBuilder {
constructor(builder) {
this.shouldThrowOnError = false;
this.fetch = fetch;
this.method = builder.method;
this.url = builder.url;
this.headers = builder.headers;
this.schema = builder.schema;
this.body = builder.body;
this.shouldThrowOnError = builder.shouldThrowOnError;
this.signal = builder.signal;
this.allowEmpty = builder.allowEmpty;
}
/**
* If there's an error with the query, throwOnError will reject the promise by
* throwing the error instead of returning it as part of a successful response.
*
* {@link https://github.com/supabase/supabase-js/issues/92}
*/
throwOnError() {
this.shouldThrowOnError = true;
return this;
}
then(onfulfilled, onrejected) {
// https://postgrest.org/en/stable/api.html#switching-schemas
if (this.schema === undefined) {
// skip
}
else if (["GET", "HEAD"].includes(this.method)) {
this.headers["Accept-Profile"] = this.schema;
}
else {
this.headers["Content-Profile"] = this.schema;
}
if (this.method !== "GET" && this.method !== "HEAD") {
this.headers["Content-Type"] = "application/json";
}
// NOTE: Invoke w/o `this` to avoid illegal invocation error.
// https://github.com/supabase/postgrest-js/pull/247
const _fetch = this.fetch;
let res = _fetch(this.url.toString(), {
method: this.method,
headers: this.headers,
body: JSON.stringify(this.body),
signal: this.signal,
}).then(async (res) => {
var _a, _b, _c, _d;
let error = null;
let data = null;
let count = null;
let status = res.status;
let statusText = res.statusText;
if (res.ok) {
if (this.method !== "HEAD") {
const body = await res.text();
if (body === "") {
// Prefer: return=minimal
}
else if (this.headers["Accept"] === "text/csv") {
data = body;
}
else if ((_a = this.headers["Accept"]) === null || _a === void 0 ? void 0 : _a.includes("application/vnd.pgrst.plan+text")) {
data = body;
}
else {
data = JSON.parse(body);
}
}
const countHeader = (_b = this.headers["Prefer"]) === null || _b === void 0 ? void 0 : _b.match(/count=(exact|planned|estimated)/);
const contentRange = (_c = res.headers.get("content-range")) === null || _c === void 0 ? void 0 : _c.split("/");
if (countHeader && contentRange && contentRange.length > 1) {
count = parseInt(contentRange[1]);
}
}
else {
const body = await res.text();
try {
error = JSON.parse(body);
// Workaround for https://github.com/supabase/postgrest-js/issues/295
if (Array.isArray(error) && res.status === 404) {
data = [];
error = null;
status = 200;
statusText = "OK";
}
}
catch (_e) {
// Workaround for https://github.com/supabase/postgrest-js/issues/295
if (res.status === 404 && body === "") {
status = 204;
statusText = "No Content";
}
else {
error = {
message: body,
};
}
}
if (error &&
this.allowEmpty &&
((_d = error === null || error === void 0 ? void 0 : error.details) === null || _d === void 0 ? void 0 : _d.includes("Results contain 0 rows"))) {
error = null;
status = 200;
statusText = "OK";
}
if (error && this.shouldThrowOnError) {
throw error;
}
}
const postgrestResponse = {
error,
data,
count,
status,
statusText,
};
return postgrestResponse;
});
if (!this.shouldThrowOnError) {
res = res.catch((fetchError) => ({
error: {
message: `FetchError: ${fetchError.message}`,
details: "",
hint: "",
code: fetchError.code || "",
},
data: null,
count: null,
status: 0,
statusText: "",
}));
}
return res.then(onfulfilled, onrejected);
}
}
//# sourceMappingURL=PostgrestBuilder.js.map