@@ -136,6 +136,71 @@ describe("PhantomApiClient", () => {
136136 } ) ;
137137 } ) ;
138138
139+ describe ( "setGetHeaders()" , ( ) => {
140+ it ( "includes dynamic header returned by the callback on GET" , async ( ) => {
141+ const dynamicClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
142+ dynamicClient . setGetHeaders ( ( ) => ( { authorization : "Bearer token-abc" } ) ) ;
143+ const spy = mockFetch ( 200 , { } ) ;
144+ await dynamicClient . get ( "/path" ) ;
145+ const calledHeaders = ( ( spy . mock . calls [ 0 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
146+ expect ( calledHeaders [ "authorization" ] ) . toBe ( "Bearer token-abc" ) ;
147+ } ) ;
148+
149+ it ( "includes dynamic header returned by the callback on POST" , async ( ) => {
150+ const dynamicClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
151+ dynamicClient . setGetHeaders ( ( ) => ( { authorization : "Bearer token-post" } ) ) ;
152+ const spy = mockFetch ( 200 , { } ) ;
153+ await dynamicClient . post ( "/path" , { } ) ;
154+ const calledHeaders = ( ( spy . mock . calls [ 0 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
155+ expect ( calledHeaders [ "authorization" ] ) . toBe ( "Bearer token-post" ) ;
156+ } ) ;
157+
158+ it ( "calls the callback on every request so token changes are reflected" , async ( ) => {
159+ const dynamicClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
160+ let token = "token-v1" ;
161+ dynamicClient . setGetHeaders ( ( ) => ( { authorization : `Bearer ${ token } ` } ) ) ;
162+
163+ const spy = mockFetch ( 200 , { } ) ;
164+ await dynamicClient . get ( "/path" ) ;
165+ const firstHeaders = ( ( spy . mock . calls [ 0 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
166+ expect ( firstHeaders [ "authorization" ] ) . toBe ( "Bearer token-v1" ) ;
167+
168+ token = "token-v2" ;
169+ await dynamicClient . get ( "/path" ) ;
170+ const secondHeaders = ( ( spy . mock . calls [ 1 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
171+ expect ( secondHeaders [ "authorization" ] ) . toBe ( "Bearer token-v2" ) ;
172+ } ) ;
173+
174+ it ( "omits dynamic header keys whose value is undefined" , async ( ) => {
175+ const dynamicClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
176+ dynamicClient . setGetHeaders ( ( ) => ( { authorization : "Bearer token" , "x-optional" : undefined } ) ) ;
177+ const spy = mockFetch ( 200 , { } ) ;
178+ await dynamicClient . get ( "/path" ) ;
179+ const calledHeaders = ( ( spy . mock . calls [ 0 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
180+ expect ( calledHeaders [ "authorization" ] ) . toBe ( "Bearer token" ) ;
181+ expect ( calledHeaders [ "x-optional" ] ) . toBeUndefined ( ) ;
182+ } ) ;
183+
184+ it ( "dynamic headers override static headers with the same key" , async ( ) => {
185+ const dynamicClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
186+ dynamicClient . setHeaders ( { authorization : "Bearer static-token" } ) ;
187+ dynamicClient . setGetHeaders ( ( ) => ( { authorization : "Bearer dynamic-token" } ) ) ;
188+ const spy = mockFetch ( 200 , { } ) ;
189+ await dynamicClient . get ( "/path" ) ;
190+ const calledHeaders = ( ( spy . mock . calls [ 0 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
191+ expect ( calledHeaders [ "authorization" ] ) . toBe ( "Bearer dynamic-token" ) ;
192+ } ) ;
193+
194+ it ( "per-request extra headers override dynamic headers" , async ( ) => {
195+ const dynamicClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
196+ dynamicClient . setGetHeaders ( ( ) => ( { "x-custom" : "from-dynamic" } ) ) ;
197+ const spy = mockFetch ( 200 , { } ) ;
198+ await dynamicClient . get ( "/path" , { headers : { "x-custom" : "from-extra" } } ) ;
199+ const calledHeaders = ( ( spy . mock . calls [ 0 ] as unknown [ ] ) [ 1 ] as RequestInit ) . headers as Record < string , string > ;
200+ expect ( calledHeaders [ "x-custom" ] ) . toBe ( "from-extra" ) ;
201+ } ) ;
202+ } ) ;
203+
139204 describe ( "setPaymentSignature()" , ( ) => {
140205 it ( "includes X-Payment header in subsequent requests after setPaymentSignature" , async ( ) => {
141206 const payingClient = new PhantomApiClient ( { baseUrl : "https://api.phantom.app" } ) ;
0 commit comments