UNPKG

5.36 kBJavaScriptView Raw
1var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10import crossFetch from 'cross-fetch';
11export class PostgrestBuilder {
12 constructor(builder) {
13 Object.assign(this, builder);
14 let _fetch;
15 if (builder.fetch) {
16 _fetch = builder.fetch;
17 }
18 else if (typeof fetch === 'undefined') {
19 _fetch = crossFetch;
20 }
21 else {
22 _fetch = fetch;
23 }
24 this.fetch = (...args) => _fetch(...args);
25 this.shouldThrowOnError = builder.shouldThrowOnError || false;
26 this.allowEmpty = builder.allowEmpty || false;
27 }
28 /**
29 * If there's an error with the query, throwOnError will reject the promise by
30 * throwing the error instead of returning it as part of a successful response.
31 *
32 * {@link https://github.com/supabase/supabase-js/issues/92}
33 */
34 throwOnError(throwOnError) {
35 if (throwOnError === null || throwOnError === undefined) {
36 throwOnError = true;
37 }
38 this.shouldThrowOnError = throwOnError;
39 return this;
40 }
41 then(onfulfilled, onrejected) {
42 // https://postgrest.org/en/stable/api.html#switching-schemas
43 if (typeof this.schema === 'undefined') {
44 // skip
45 }
46 else if (['GET', 'HEAD'].includes(this.method)) {
47 this.headers['Accept-Profile'] = this.schema;
48 }
49 else {
50 this.headers['Content-Profile'] = this.schema;
51 }
52 if (this.method !== 'GET' && this.method !== 'HEAD') {
53 this.headers['Content-Type'] = 'application/json';
54 }
55 let res = this.fetch(this.url.toString(), {
56 method: this.method,
57 headers: this.headers,
58 body: JSON.stringify(this.body),
59 signal: this.signal,
60 }).then((res) => __awaiter(this, void 0, void 0, function* () {
61 var _a, _b, _c, _d;
62 let error = null;
63 let data = null;
64 let count = null;
65 let status = res.status;
66 let statusText = res.statusText;
67 if (res.ok) {
68 const isReturnMinimal = (_a = this.headers['Prefer']) === null || _a === void 0 ? void 0 : _a.split(',').includes('return=minimal');
69 if (this.method !== 'HEAD' && !isReturnMinimal) {
70 const text = yield res.text();
71 if (!text) {
72 // discard `text`
73 }
74 else if (this.headers['Accept'] === 'text/csv') {
75 data = text;
76 }
77 else {
78 data = JSON.parse(text);
79 }
80 }
81 const countHeader = (_b = this.headers['Prefer']) === null || _b === void 0 ? void 0 : _b.match(/count=(exact|planned|estimated)/);
82 const contentRange = (_c = res.headers.get('content-range')) === null || _c === void 0 ? void 0 : _c.split('/');
83 if (countHeader && contentRange && contentRange.length > 1) {
84 count = parseInt(contentRange[1]);
85 }
86 }
87 else {
88 const body = yield res.text();
89 try {
90 error = JSON.parse(body);
91 }
92 catch (_e) {
93 error = {
94 message: body,
95 };
96 }
97 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'))) {
98 error = null;
99 status = 200;
100 statusText = 'OK';
101 }
102 if (error && this.shouldThrowOnError) {
103 throw error;
104 }
105 }
106 const postgrestResponse = {
107 error,
108 data,
109 count,
110 status,
111 statusText,
112 body: data,
113 };
114 return postgrestResponse;
115 }));
116 if (!this.shouldThrowOnError) {
117 res = res.catch((fetchError) => ({
118 error: {
119 message: `FetchError: ${fetchError.message}`,
120 details: '',
121 hint: '',
122 code: fetchError.code || '',
123 },
124 data: null,
125 body: null,
126 count: null,
127 status: 400,
128 statusText: 'Bad Request',
129 }));
130 }
131 return res.then(onfulfilled, onrejected);
132 }
133}
134//# sourceMappingURL=types.js.map
\No newline at end of file