@@ -24,23 +24,33 @@ export default abstract class PostgrestBuilder<
2424{
2525 protected method : 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
2626 protected url : URL
27- protected headers : Record < string , string >
27+ protected headers : Headers
2828 protected schema ?: string
2929 protected body ?: unknown
3030 protected shouldThrowOnError = false
3131 protected signal ?: AbortSignal
3232 protected fetch : Fetch
3333 protected isMaybeSingle : boolean
3434
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+ } ) {
3646 this . method = builder . method
3747 this . url = builder . url
38- this . headers = builder . headers
48+ this . headers = new Headers ( builder . headers )
3949 this . schema = builder . schema
4050 this . body = builder . body
41- this . shouldThrowOnError = builder . shouldThrowOnError
51+ this . shouldThrowOnError = builder . shouldThrowOnError ?? false
4252 this . signal = builder . signal
43- this . isMaybeSingle = builder . isMaybeSingle
53+ this . isMaybeSingle = builder . isMaybeSingle ?? false
4454
4555 if ( builder . fetch ) {
4656 this . fetch = builder . fetch
@@ -66,8 +76,8 @@ export default abstract class PostgrestBuilder<
6676 * Set an HTTP header for the request.
6777 */
6878 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 )
7181 return this
7282 }
7383
@@ -91,12 +101,12 @@ export default abstract class PostgrestBuilder<
91101 if ( this . schema === undefined ) {
92102 // skip
93103 } else if ( [ 'GET' , 'HEAD' ] . includes ( this . method ) ) {
94- this . headers [ 'Accept-Profile' ] = this . schema
104+ this . headers . set ( 'Accept-Profile' , this . schema )
95105 } else {
96- this . headers [ 'Content-Profile' ] = this . schema
106+ this . headers . set ( 'Content-Profile' , this . schema )
97107 }
98108 if ( this . method !== 'GET' && this . method !== 'HEAD' ) {
99- this . headers [ 'Content-Type' ] = 'application/json'
109+ this . headers . set ( 'Content-Type' , 'application/json' )
100110 }
101111
102112 // NOTE: Invoke w/o `this` to avoid illegal invocation error.
@@ -119,19 +129,19 @@ export default abstract class PostgrestBuilder<
119129 const body = await res . text ( )
120130 if ( body === '' ) {
121131 // Prefer: return=minimal
122- } else if ( this . headers [ 'Accept' ] === 'text/csv' ) {
132+ } else if ( this . headers . get ( 'Accept' ) === 'text/csv' ) {
123133 data = body
124134 } 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' )
127137 ) {
128138 data = body
129139 } else {
130140 data = JSON . parse ( body )
131141 }
132142 }
133143
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 ) / )
135145 const contentRange = res . headers . get ( 'content-range' ) ?. split ( '/' )
136146 if ( countHeader && contentRange && contentRange . length > 1 ) {
137147 count = parseInt ( contentRange [ 1 ] )
0 commit comments