-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
types.ts
137 lines (123 loc) · 3.92 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import fetch from 'cross-fetch'
/**
* Error format
*
* {@link https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes}
*/
type PostgrestError = {
message: string
details: string
hint: string
code: string
}
/**
* Response format
*
* {@link https://github.com/supabase/supabase-js/issues/32}
*/
interface PostgrestResponseBase {
status: number
statusText: string
}
interface PostgrestResponseSuccess<T> extends PostgrestResponseBase {
error: null
data: T[]
body: T[]
count: number | null
}
interface PostgrestResponseFailure extends PostgrestResponseBase {
error: PostgrestError
data: null
// For backward compatibility: body === data
body: null
count: null
}
export type PostgrestResponse<T> = PostgrestResponseSuccess<T> | PostgrestResponseFailure
interface PostgrestSingleResponseSuccess<T> extends PostgrestResponseBase {
error: null
data: T
// For backward compatibility: body === data
body: T
}
export type PostgrestSingleResponse<T> =
| PostgrestSingleResponseSuccess<T>
| PostgrestResponseFailure
export type PostgrestMaybeSingleResponse<T> = PostgrestSingleResponse<T | null>
export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestResponse<T>> {
protected method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
protected url!: URL
protected headers!: { [key: string]: string }
protected schema?: string
protected body?: Partial<T> | Partial<T>[]
protected shouldThrowOnError?: boolean
constructor(builder: PostgrestBuilder<T>) {
Object.assign(this, builder)
}
/**
* 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(): PostgrestBuilder<T> {
this.shouldThrowOnError = true
return this
}
then<TResult1 = PostgrestResponse<T>, TResult2 = never>(
onfulfilled?:
| ((value: PostgrestResponse<T>) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): PromiseLike<TResult1 | TResult2> {
// https://postgrest.org/en/stable/api.html#switching-schemas
if (typeof 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'
}
return fetch(this.url.toString(), {
method: this.method,
headers: this.headers,
body: JSON.stringify(this.body),
})
.then(async (res) => {
let error = null
let data = null
let count = null
if (res.ok) {
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
if (this.method !== 'HEAD' && !isReturnMinimal) {
const text = await res.text()
if (text && text !== '' && this.headers['Accept'] !== 'text/csv')
data = JSON.parse(text)
}
const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
const contentRange = res.headers.get('content-range')?.split('/')
if (countHeader && contentRange && contentRange.length > 1) {
count = parseInt(contentRange[1])
}
} else {
error = await res.json()
if (error && this.shouldThrowOnError) {
throw error
}
}
const postgrestResponse: PostgrestResponse<T> = {
error,
data,
count,
status: res.status,
statusText: res.statusText,
body: data,
}
return postgrestResponse
})
.then(onfulfilled, onrejected)
}
}