1 | var __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 | };
|
10 | export class PostgrestBuilder {
|
11 | constructor(builder) {
|
12 | Object.assign(this, builder);
|
13 | let _fetch;
|
14 | if (builder.fetch) {
|
15 | _fetch = builder.fetch;
|
16 | }
|
17 | else if (typeof fetch === 'undefined') {
|
18 | _fetch = (...args) => __awaiter(this, void 0, void 0, function* () { return yield (yield import('cross-fetch')).fetch(...args); });
|
19 | }
|
20 | else {
|
21 | _fetch = fetch;
|
22 | }
|
23 | this.fetch = (...args) => _fetch(...args);
|
24 | this.shouldThrowOnError = builder.shouldThrowOnError || false;
|
25 | this.allowEmpty = builder.allowEmpty || false;
|
26 | }
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | throwOnError(throwOnError) {
|
34 | if (throwOnError === null || throwOnError === undefined) {
|
35 | throwOnError = true;
|
36 | }
|
37 | this.shouldThrowOnError = throwOnError;
|
38 | return this;
|
39 | }
|
40 | then(onfulfilled, onrejected) {
|
41 |
|
42 | if (typeof this.schema === 'undefined') {
|
43 |
|
44 | }
|
45 | else if (['GET', 'HEAD'].includes(this.method)) {
|
46 | this.headers['Accept-Profile'] = this.schema;
|
47 | }
|
48 | else {
|
49 | this.headers['Content-Profile'] = this.schema;
|
50 | }
|
51 | if (this.method !== 'GET' && this.method !== 'HEAD') {
|
52 | this.headers['Content-Type'] = 'application/json';
|
53 | }
|
54 | let res = this.fetch(this.url.toString(), {
|
55 | method: this.method,
|
56 | headers: this.headers,
|
57 | body: JSON.stringify(this.body),
|
58 | signal: this.signal,
|
59 | }).then((res) => __awaiter(this, void 0, void 0, function* () {
|
60 | var _a, _b, _c, _d;
|
61 | let error = null;
|
62 | let data = null;
|
63 | let count = null;
|
64 | let status = res.status;
|
65 | let statusText = res.statusText;
|
66 | if (res.ok) {
|
67 | const isReturnMinimal = (_a = this.headers['Prefer']) === null || _a === void 0 ? void 0 : _a.split(',').includes('return=minimal');
|
68 | if (this.method !== 'HEAD' && !isReturnMinimal) {
|
69 | const text = yield res.text();
|
70 | if (!text) {
|
71 |
|
72 | }
|
73 | else if (this.headers['Accept'] === 'text/csv') {
|
74 | data = text;
|
75 | }
|
76 | else {
|
77 | data = JSON.parse(text);
|
78 | }
|
79 | }
|
80 | const countHeader = (_b = this.headers['Prefer']) === null || _b === void 0 ? void 0 : _b.match(/count=(exact|planned|estimated)/);
|
81 | const contentRange = (_c = res.headers.get('content-range')) === null || _c === void 0 ? void 0 : _c.split('/');
|
82 | if (countHeader && contentRange && contentRange.length > 1) {
|
83 | count = parseInt(contentRange[1]);
|
84 | }
|
85 | }
|
86 | else {
|
87 | const body = yield res.text();
|
88 | try {
|
89 | error = JSON.parse(body);
|
90 | }
|
91 | catch (_e) {
|
92 | error = {
|
93 | message: body,
|
94 | };
|
95 | }
|
96 | 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'))) {
|
97 | error = null;
|
98 | status = 200;
|
99 | statusText = 'OK';
|
100 | }
|
101 | if (error && this.shouldThrowOnError) {
|
102 | throw error;
|
103 | }
|
104 | }
|
105 | const postgrestResponse = {
|
106 | error,
|
107 | data,
|
108 | count,
|
109 | status,
|
110 | statusText,
|
111 | body: data,
|
112 | };
|
113 | return postgrestResponse;
|
114 | }));
|
115 | if (!this.shouldThrowOnError) {
|
116 | res = res.catch((fetchError) => ({
|
117 | error: {
|
118 | message: `FetchError: ${fetchError.message}`,
|
119 | details: '',
|
120 | hint: '',
|
121 | code: fetchError.code || '',
|
122 | },
|
123 | data: null,
|
124 | body: null,
|
125 | count: null,
|
126 | status: 400,
|
127 | statusText: 'Bad Request',
|
128 | }));
|
129 | }
|
130 | return res.then(onfulfilled, onrejected);
|
131 | }
|
132 | }
|
133 |
|
\ | No newline at end of file |