1
- import PostgrestBuilder from './PostgrestBuilder'
2
1
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
3
2
import { GetResult } from './select-query-parser/result'
4
3
import { ClientServerOptions , Fetch , GenericSchema , GenericTable , GenericView } from './types'
@@ -11,7 +10,7 @@ export default class PostgrestQueryBuilder<
11
10
Relationships = Relation extends { Relationships : infer R } ? R : unknown
12
11
> {
13
12
url : URL
14
- headers : Record < string , string >
13
+ headers : Headers
15
14
schema ?: string
16
15
signal ?: AbortSignal
17
16
fetch ?: Fetch
@@ -23,13 +22,13 @@ export default class PostgrestQueryBuilder<
23
22
schema,
24
23
fetch,
25
24
} : {
26
- headers ?: Record < string , string >
25
+ headers ?: HeadersInit
27
26
schema ?: string
28
27
fetch ?: Fetch
29
28
}
30
29
) {
31
30
this . url = url
32
- this . headers = headers
31
+ this . headers = new Headers ( headers )
33
32
this . schema = schema
34
33
this . fetch = fetch
35
34
}
@@ -99,8 +98,9 @@ export default class PostgrestQueryBuilder<
99
98
} )
100
99
. join ( '' )
101
100
this . url . searchParams . set ( 'select' , cleanedColumns )
101
+
102
102
if ( count ) {
103
- this . headers [ 'Prefer' ] = `count=${ count } `
103
+ this . headers . append ( 'Prefer' , `count=${ count } ` )
104
104
}
105
105
106
106
return new PostgrestFilterBuilder ( {
@@ -109,8 +109,7 @@ export default class PostgrestQueryBuilder<
109
109
headers : this . headers ,
110
110
schema : this . schema ,
111
111
fetch : this . fetch ,
112
- allowEmpty : false ,
113
- } as unknown as PostgrestBuilder < ClientOptions , ResultOne [ ] > )
112
+ } )
114
113
}
115
114
116
115
// TODO(v3): Make `defaultToNull` consistent for both single & bulk inserts.
@@ -189,17 +188,12 @@ export default class PostgrestQueryBuilder<
189
188
> {
190
189
const method = 'POST'
191
190
192
- const prefersHeaders = [ ]
193
- if ( this . headers [ 'Prefer' ] ) {
194
- prefersHeaders . push ( this . headers [ 'Prefer' ] )
195
- }
196
191
if ( count ) {
197
- prefersHeaders . push ( `count=${ count } ` )
192
+ this . headers . append ( 'Prefer' , `count=${ count } ` )
198
193
}
199
194
if ( ! defaultToNull ) {
200
- prefersHeaders . push ( ' missing=default' )
195
+ this . headers . append ( 'Prefer' , ` missing=default` )
201
196
}
202
- this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
203
197
204
198
if ( Array . isArray ( values ) ) {
205
199
const columns = values . reduce ( ( acc , x ) => acc . concat ( Object . keys ( x ) ) , [ ] as string [ ] )
@@ -215,9 +209,8 @@ export default class PostgrestQueryBuilder<
215
209
headers : this . headers ,
216
210
schema : this . schema ,
217
211
body : values ,
218
- fetch : this . fetch ,
219
- allowEmpty : false ,
220
- } as unknown as PostgrestBuilder < ClientOptions , null > )
212
+ fetch : this . fetch ?? fetch ,
213
+ } )
221
214
}
222
215
223
216
// TODO(v3): Make `defaultToNull` consistent for both single & bulk upserts.
@@ -316,19 +309,15 @@ export default class PostgrestQueryBuilder<
316
309
> {
317
310
const method = 'POST'
318
311
319
- const prefersHeaders = [ `resolution=${ ignoreDuplicates ? 'ignore' : 'merge' } -duplicates` ]
312
+ this . headers . append ( 'Prefer' , `resolution=${ ignoreDuplicates ? 'ignore' : 'merge' } -duplicates` )
320
313
321
314
if ( onConflict !== undefined ) this . url . searchParams . set ( 'on_conflict' , onConflict )
322
- if ( this . headers [ 'Prefer' ] ) {
323
- prefersHeaders . push ( this . headers [ 'Prefer' ] )
324
- }
325
315
if ( count ) {
326
- prefersHeaders . push ( `count=${ count } ` )
316
+ this . headers . append ( 'Prefer' , `count=${ count } ` )
327
317
}
328
318
if ( ! defaultToNull ) {
329
- prefersHeaders . push ( 'missing=default' )
319
+ this . headers . append ( 'Prefer' , 'missing=default' )
330
320
}
331
- this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
332
321
333
322
if ( Array . isArray ( values ) ) {
334
323
const columns = values . reduce ( ( acc , x ) => acc . concat ( Object . keys ( x ) ) , [ ] as string [ ] )
@@ -344,9 +333,8 @@ export default class PostgrestQueryBuilder<
344
333
headers : this . headers ,
345
334
schema : this . schema ,
346
335
body : values ,
347
- fetch : this . fetch ,
348
- allowEmpty : false ,
349
- } as unknown as PostgrestBuilder < ClientOptions , null > )
336
+ fetch : this . fetch ?? fetch ,
337
+ } )
350
338
}
351
339
352
340
/**
@@ -387,24 +375,18 @@ export default class PostgrestQueryBuilder<
387
375
'PATCH'
388
376
> {
389
377
const method = 'PATCH'
390
- const prefersHeaders = [ ]
391
- if ( this . headers [ 'Prefer' ] ) {
392
- prefersHeaders . push ( this . headers [ 'Prefer' ] )
393
- }
394
378
if ( count ) {
395
- prefersHeaders . push ( `count=${ count } ` )
379
+ this . headers . append ( 'Prefer' , `count=${ count } ` )
396
380
}
397
- this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
398
381
399
382
return new PostgrestFilterBuilder ( {
400
383
method,
401
384
url : this . url ,
402
385
headers : this . headers ,
403
386
schema : this . schema ,
404
387
body : values ,
405
- fetch : this . fetch ,
406
- allowEmpty : false ,
407
- } as unknown as PostgrestBuilder < ClientOptions , null > )
388
+ fetch : this . fetch ?? fetch ,
389
+ } )
408
390
}
409
391
410
392
/**
@@ -440,22 +422,16 @@ export default class PostgrestQueryBuilder<
440
422
'DELETE'
441
423
> {
442
424
const method = 'DELETE'
443
- const prefersHeaders = [ ]
444
425
if ( count ) {
445
- prefersHeaders . push ( `count=${ count } ` )
426
+ this . headers . append ( 'Prefer' , `count=${ count } ` )
446
427
}
447
- if ( this . headers [ 'Prefer' ] ) {
448
- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
449
- }
450
- this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
451
428
452
429
return new PostgrestFilterBuilder ( {
453
430
method,
454
431
url : this . url ,
455
432
headers : this . headers ,
456
433
schema : this . schema ,
457
- fetch : this . fetch ,
458
- allowEmpty : false ,
459
- } as unknown as PostgrestBuilder < ClientOptions , null > )
434
+ fetch : this . fetch ?? fetch ,
435
+ } )
460
436
}
461
437
}
0 commit comments