@@ -24,23 +24,33 @@ export default abstract class PostgrestBuilder<
24
24
{
25
25
protected method : 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
26
26
protected url : URL
27
- protected headers : Record < string , string >
27
+ protected headers : Headers
28
28
protected schema ?: string
29
29
protected body ?: unknown
30
30
protected shouldThrowOnError = false
31
31
protected signal ?: AbortSignal
32
32
protected fetch : Fetch
33
33
protected isMaybeSingle : boolean
34
34
35
- constructor ( builder : PostgrestBuilder < ClientOptions , Result > ) {
35
+ constructor ( builder : {
36
+ method : 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
37
+ url : URL
38
+ headers : HeadersInit
39
+ schema ?: string
40
+ body ?: unknown
41
+ shouldThrowOnError ?: boolean
42
+ signal ?: AbortSignal
43
+ fetch ?: Fetch
44
+ isMaybeSingle ?: boolean
45
+ } ) {
36
46
this . method = builder . method
37
47
this . url = builder . url
38
- this . headers = builder . headers
48
+ this . headers = new Headers ( builder . headers )
39
49
this . schema = builder . schema
40
50
this . body = builder . body
41
- this . shouldThrowOnError = builder . shouldThrowOnError
51
+ this . shouldThrowOnError = builder . shouldThrowOnError ?? false
42
52
this . signal = builder . signal
43
- this . isMaybeSingle = builder . isMaybeSingle
53
+ this . isMaybeSingle = builder . isMaybeSingle ?? false
44
54
45
55
if ( builder . fetch ) {
46
56
this . fetch = builder . fetch
@@ -66,8 +76,8 @@ export default abstract class PostgrestBuilder<
66
76
* Set an HTTP header for the request.
67
77
*/
68
78
setHeader ( name : string , value : string ) : this {
69
- this . headers = { ... this . headers }
70
- this . headers [ name ] = value
79
+ this . headers = new Headers ( this . headers )
80
+ this . headers . set ( name , value )
71
81
return this
72
82
}
73
83
@@ -91,12 +101,12 @@ export default abstract class PostgrestBuilder<
91
101
if ( this . schema === undefined ) {
92
102
// skip
93
103
} else if ( [ 'GET' , 'HEAD' ] . includes ( this . method ) ) {
94
- this . headers [ 'Accept-Profile' ] = this . schema
104
+ this . headers . set ( 'Accept-Profile' , this . schema )
95
105
} else {
96
- this . headers [ 'Content-Profile' ] = this . schema
106
+ this . headers . set ( 'Content-Profile' , this . schema )
97
107
}
98
108
if ( this . method !== 'GET' && this . method !== 'HEAD' ) {
99
- this . headers [ 'Content-Type' ] = 'application/json'
109
+ this . headers . set ( 'Content-Type' , 'application/json' )
100
110
}
101
111
102
112
// NOTE: Invoke w/o `this` to avoid illegal invocation error.
@@ -119,19 +129,19 @@ export default abstract class PostgrestBuilder<
119
129
const body = await res . text ( )
120
130
if ( body === '' ) {
121
131
// Prefer: return=minimal
122
- } else if ( this . headers [ 'Accept' ] === 'text/csv' ) {
132
+ } else if ( this . headers . get ( 'Accept' ) === 'text/csv' ) {
123
133
data = body
124
134
} else if (
125
- this . headers [ 'Accept' ] &&
126
- this . headers [ 'Accept' ] . includes ( 'application/vnd.pgrst.plan+text' )
135
+ this . headers . get ( 'Accept' ) &&
136
+ this . headers . get ( 'Accept' ) ? .includes ( 'application/vnd.pgrst.plan+text' )
127
137
) {
128
138
data = body
129
139
} else {
130
140
data = JSON . parse ( body )
131
141
}
132
142
}
133
143
134
- const countHeader = this . headers [ 'Prefer' ] ?. match ( / c o u n t = ( e x a c t | p l a n n e d | e s t i m a t e d ) / )
144
+ const countHeader = this . headers . get ( 'Prefer' ) ?. match ( / c o u n t = ( e x a c t | p l a n n e d | e s t i m a t e d ) / )
135
145
const contentRange = res . headers . get ( 'content-range' ) ?. split ( '/' )
136
146
if ( countHeader && contentRange && contentRange . length > 1 ) {
137
147
count = parseInt ( contentRange [ 1 ] )
0 commit comments