@supabase/postgrest-js 0.33.2 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -15
- package/dist/main/PostgrestClient.d.ts +7 -2
- package/dist/main/PostgrestClient.d.ts.map +1 -1
- package/dist/main/PostgrestClient.js +13 -5
- package/dist/main/PostgrestClient.js.map +1 -1
- package/dist/main/lib/PostgrestFilterBuilder.d.ts +1 -1
- package/dist/main/lib/PostgrestFilterBuilder.d.ts.map +1 -1
- package/dist/main/lib/PostgrestFilterBuilder.js.map +1 -1
- package/dist/main/lib/PostgrestQueryBuilder.d.ts +3 -2
- package/dist/main/lib/PostgrestQueryBuilder.d.ts.map +1 -1
- package/dist/main/lib/PostgrestQueryBuilder.js +2 -2
- package/dist/main/lib/PostgrestQueryBuilder.js.map +1 -1
- package/dist/main/lib/PostgrestRpcBuilder.d.ts +5 -3
- package/dist/main/lib/PostgrestRpcBuilder.d.ts.map +1 -1
- package/dist/main/lib/PostgrestRpcBuilder.js +15 -5
- package/dist/main/lib/PostgrestRpcBuilder.js.map +1 -1
- package/dist/main/lib/PostgrestTransformBuilder.d.ts +4 -0
- package/dist/main/lib/PostgrestTransformBuilder.d.ts.map +1 -1
- package/dist/main/lib/PostgrestTransformBuilder.js +7 -0
- package/dist/main/lib/PostgrestTransformBuilder.js.map +1 -1
- package/dist/main/lib/constants.d.ts +4 -0
- package/dist/main/lib/constants.d.ts.map +1 -0
- package/dist/main/lib/constants.js +6 -0
- package/dist/main/lib/constants.js.map +1 -0
- package/dist/main/lib/types.d.ts +4 -1
- package/dist/main/lib/types.d.ts.map +1 -1
- package/dist/main/lib/types.js +22 -5
- package/dist/main/lib/types.js.map +1 -1
- package/dist/main/lib/version.d.ts +2 -0
- package/dist/main/lib/version.d.ts.map +1 -0
- package/dist/main/lib/version.js +6 -0
- package/dist/main/lib/version.js.map +1 -0
- package/dist/module/PostgrestClient.d.ts +7 -2
- package/dist/module/PostgrestClient.d.ts.map +1 -1
- package/dist/module/PostgrestClient.js +13 -5
- package/dist/module/PostgrestClient.js.map +1 -1
- package/dist/module/lib/PostgrestFilterBuilder.d.ts +1 -1
- package/dist/module/lib/PostgrestFilterBuilder.d.ts.map +1 -1
- package/dist/module/lib/PostgrestFilterBuilder.js.map +1 -1
- package/dist/module/lib/PostgrestQueryBuilder.d.ts +3 -2
- package/dist/module/lib/PostgrestQueryBuilder.d.ts.map +1 -1
- package/dist/module/lib/PostgrestQueryBuilder.js +2 -2
- package/dist/module/lib/PostgrestQueryBuilder.js.map +1 -1
- package/dist/module/lib/PostgrestRpcBuilder.d.ts +5 -3
- package/dist/module/lib/PostgrestRpcBuilder.d.ts.map +1 -1
- package/dist/module/lib/PostgrestRpcBuilder.js +15 -5
- package/dist/module/lib/PostgrestRpcBuilder.js.map +1 -1
- package/dist/module/lib/PostgrestTransformBuilder.d.ts +4 -0
- package/dist/module/lib/PostgrestTransformBuilder.d.ts.map +1 -1
- package/dist/module/lib/PostgrestTransformBuilder.js +7 -0
- package/dist/module/lib/PostgrestTransformBuilder.js.map +1 -1
- package/dist/module/lib/constants.d.ts +4 -0
- package/dist/module/lib/constants.d.ts.map +1 -0
- package/dist/module/lib/constants.js +3 -0
- package/dist/module/lib/constants.js.map +1 -0
- package/dist/module/lib/types.d.ts +4 -1
- package/dist/module/lib/types.d.ts.map +1 -1
- package/dist/module/lib/types.js +23 -6
- package/dist/module/lib/types.js.map +1 -1
- package/dist/module/lib/version.d.ts +2 -0
- package/dist/module/lib/version.d.ts.map +1 -0
- package/dist/module/lib/version.js +3 -0
- package/dist/module/lib/version.js.map +1 -0
- package/package.json +5 -2
- package/src/PostgrestClient.ts +20 -4
- package/src/lib/PostgrestFilterBuilder.ts +22 -0
- package/src/lib/PostgrestQueryBuilder.ts +7 -3
- package/src/lib/PostgrestRpcBuilder.ts +21 -5
- package/src/lib/PostgrestTransformBuilder.ts +8 -0
- package/src/lib/constants.ts +2 -0
- package/src/lib/types.ts +61 -40
- package/src/lib/version.ts +2 -0
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { PostgrestBuilder } from './types'
|
|
1
|
+
import { Fetch, PostgrestBuilder } from './types'
|
|
2
2
|
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
|
|
3
3
|
|
|
4
4
|
export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
|
|
5
5
|
constructor(
|
|
6
6
|
url: string,
|
|
7
|
-
{
|
|
7
|
+
{
|
|
8
|
+
headers = {},
|
|
9
|
+
schema,
|
|
10
|
+
fetch,
|
|
11
|
+
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
|
|
8
12
|
) {
|
|
9
|
-
super({} as PostgrestBuilder<T>)
|
|
13
|
+
super(({ fetch } as unknown) as PostgrestBuilder<T>)
|
|
10
14
|
this.url = new URL(url)
|
|
11
15
|
this.headers = { ...headers }
|
|
12
16
|
this.schema = schema
|
|
@@ -18,13 +22,25 @@ export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
|
|
|
18
22
|
rpc(
|
|
19
23
|
params?: object,
|
|
20
24
|
{
|
|
25
|
+
head = false,
|
|
21
26
|
count = null,
|
|
22
27
|
}: {
|
|
28
|
+
head?: boolean
|
|
23
29
|
count?: null | 'exact' | 'planned' | 'estimated'
|
|
24
30
|
} = {}
|
|
25
31
|
): PostgrestFilterBuilder<T> {
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
if (head) {
|
|
33
|
+
this.method = 'HEAD'
|
|
34
|
+
|
|
35
|
+
if (params) {
|
|
36
|
+
Object.entries(params).forEach(([name, value]) => {
|
|
37
|
+
this.url.searchParams.append(name, value)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
this.method = 'POST'
|
|
42
|
+
this.body = params
|
|
43
|
+
}
|
|
28
44
|
|
|
29
45
|
if (count) {
|
|
30
46
|
if (this.headers['Prefer'] !== undefined) this.headers['Prefer'] += `,count=${count}`
|
|
@@ -85,6 +85,14 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
|
|
|
85
85
|
return this
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Sets the AbortSignal for the fetch request.
|
|
90
|
+
*/
|
|
91
|
+
abortSignal(signal: AbortSignal): this {
|
|
92
|
+
this.signal = signal
|
|
93
|
+
return this
|
|
94
|
+
}
|
|
95
|
+
|
|
88
96
|
/**
|
|
89
97
|
* Retrieves only one row from the result. Result must be one row (e.g. using
|
|
90
98
|
* `limit`), otherwise this will result in an error.
|
package/src/lib/types.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import crossFetch from 'cross-fetch'
|
|
2
|
+
|
|
3
|
+
export type Fetch = typeof fetch
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Error format
|
|
@@ -54,10 +56,13 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
|
|
|
54
56
|
protected headers!: { [key: string]: string }
|
|
55
57
|
protected schema?: string
|
|
56
58
|
protected body?: Partial<T> | Partial<T>[]
|
|
57
|
-
protected shouldThrowOnError
|
|
59
|
+
protected shouldThrowOnError = false
|
|
60
|
+
protected signal?: AbortSignal
|
|
61
|
+
protected fetch: Fetch
|
|
58
62
|
|
|
59
63
|
constructor(builder: PostgrestBuilder<T>) {
|
|
60
64
|
Object.assign(this, builder)
|
|
65
|
+
this.fetch = builder.fetch || crossFetch
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
/**
|
|
@@ -90,53 +95,69 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
|
|
|
90
95
|
this.headers['Content-Type'] = 'application/json'
|
|
91
96
|
}
|
|
92
97
|
|
|
93
|
-
|
|
98
|
+
let res = this.fetch(this.url.toString(), {
|
|
94
99
|
method: this.method,
|
|
95
100
|
headers: this.headers,
|
|
96
101
|
body: JSON.stringify(this.body),
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (res.ok) {
|
|
104
|
-
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
|
|
105
|
-
if (this.method !== 'HEAD' && !isReturnMinimal) {
|
|
106
|
-
const text = await res.text()
|
|
107
|
-
if (!text) {
|
|
108
|
-
// discard `text`
|
|
109
|
-
} else if (this.headers['Accept'] === 'text/csv') {
|
|
110
|
-
data = text
|
|
111
|
-
} else {
|
|
112
|
-
data = JSON.parse(text)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
102
|
+
signal: this.signal,
|
|
103
|
+
}).then(async (res) => {
|
|
104
|
+
let error = null
|
|
105
|
+
let data = null
|
|
106
|
+
let count = null
|
|
115
107
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
108
|
+
if (res.ok) {
|
|
109
|
+
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
|
|
110
|
+
if (this.method !== 'HEAD' && !isReturnMinimal) {
|
|
111
|
+
const text = await res.text()
|
|
112
|
+
if (!text) {
|
|
113
|
+
// discard `text`
|
|
114
|
+
} else if (this.headers['Accept'] === 'text/csv') {
|
|
115
|
+
data = text
|
|
116
|
+
} else {
|
|
117
|
+
data = JSON.parse(text)
|
|
120
118
|
}
|
|
121
|
-
}
|
|
122
|
-
error = await res.json()
|
|
119
|
+
}
|
|
123
120
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
121
|
+
const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
|
|
122
|
+
const contentRange = res.headers.get('content-range')?.split('/')
|
|
123
|
+
if (countHeader && contentRange && contentRange.length > 1) {
|
|
124
|
+
count = parseInt(contentRange[1])
|
|
127
125
|
}
|
|
126
|
+
} else {
|
|
127
|
+
error = await res.json()
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
error
|
|
131
|
-
data,
|
|
132
|
-
count,
|
|
133
|
-
status: res.status,
|
|
134
|
-
statusText: res.statusText,
|
|
135
|
-
body: data,
|
|
129
|
+
if (error && this.shouldThrowOnError) {
|
|
130
|
+
throw error
|
|
136
131
|
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const postgrestResponse = {
|
|
135
|
+
error,
|
|
136
|
+
data,
|
|
137
|
+
count,
|
|
138
|
+
status: res.status,
|
|
139
|
+
statusText: res.statusText,
|
|
140
|
+
body: data,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return postgrestResponse
|
|
144
|
+
})
|
|
145
|
+
if (!this.shouldThrowOnError) {
|
|
146
|
+
res = res.catch((fetchError) => ({
|
|
147
|
+
error: {
|
|
148
|
+
message: `FetchError: ${fetchError.message}`,
|
|
149
|
+
details: '',
|
|
150
|
+
hint: '',
|
|
151
|
+
code: fetchError.code || '',
|
|
152
|
+
},
|
|
153
|
+
data: null,
|
|
154
|
+
body: null,
|
|
155
|
+
count: null,
|
|
156
|
+
status: 400,
|
|
157
|
+
statusText: 'Bad Request',
|
|
158
|
+
}))
|
|
159
|
+
}
|
|
137
160
|
|
|
138
|
-
|
|
139
|
-
})
|
|
140
|
-
.then(onfulfilled, onrejected)
|
|
161
|
+
return res.then(onfulfilled, onrejected)
|
|
141
162
|
}
|
|
142
163
|
}
|