File tree Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Expand file tree Collapse file tree 3 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,15 @@ export default abstract class PostgrestBuilder<Result>
47
47
return this
48
48
}
49
49
50
+ /**
51
+ * Set an HTTP header for the request.
52
+ */
53
+ setHeader ( name : string , value : string ) : this {
54
+ this . headers = { ...this . headers }
55
+ this . headers [ name ] = value
56
+ return this
57
+ }
58
+
50
59
then < TResult1 = PostgrestSingleResponse < Result > , TResult2 = never > (
51
60
onfulfilled ?:
52
61
| ( ( value : PostgrestSingleResponse < Result > ) => TResult1 | PromiseLike < TResult1 > )
Original file line number Diff line number Diff line change @@ -107,6 +107,15 @@ test('custom headers', async () => {
107
107
expect ( ( postgrest . from ( 'users' ) . select ( ) as any ) . headers [ 'apikey' ] ) . toEqual ( 'foo' )
108
108
} )
109
109
110
+ test ( 'custom headers on a per-call basis' , async ( ) => {
111
+ const postgrest1 = new PostgrestClient < Database > ( REST_URL , { headers : { apikey : 'foo' } } )
112
+ const postgrest2 = postgrest1 . rpc ( 'void_func' ) . setHeader ( 'apikey' , 'bar' )
113
+ // Original client object isn't affected
114
+ expect ( ( postgrest1 . from ( 'users' ) . select ( ) as any ) . headers [ 'apikey' ] ) . toEqual ( 'foo' )
115
+ // Derived client object uses new header value
116
+ expect ( ( postgrest2 as any ) . headers [ 'apikey' ] ) . toEqual ( 'bar' )
117
+ } )
118
+
110
119
describe ( 'custom prefer headers with ' , ( ) => {
111
120
test ( 'insert' , async ( ) => {
112
121
const postgrest = new PostgrestClient < Database > ( REST_URL , {
Original file line number Diff line number Diff line change @@ -196,3 +196,12 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
196
196
channels . channel_details
197
197
)
198
198
}
199
+
200
+ // PostgrestBuilder's children retains class when using inherited methods
201
+ {
202
+ const x = postgrest . from ( 'channels' ) . select ( )
203
+ const y = x . throwOnError ( )
204
+ const z = x . setHeader ( '' , '' )
205
+ expectType < typeof x > ( y )
206
+ expectType < typeof x > ( z )
207
+ }
You can’t perform that action at this time.
0 commit comments