File tree 2 files changed +35
-1
lines changed
Filter options
2 files changed +35
-1
lines changed
Original file line number Diff line number Diff line change @@ -807,4 +807,32 @@ describe.skipIf(isBelowNode18)('useFetch', () => {
807
807
expect ( options ?. execute ) . toBeDefined ( )
808
808
} )
809
809
} )
810
+
811
+ it ( 'should partial overwrite when combination is overwrite' , async ( ) => {
812
+ const useMyFetch = createFetch ( {
813
+ baseUrl : 'https://example.com' ,
814
+ combination : 'overwrite' ,
815
+ options : {
816
+ beforeFetch ( { options } ) {
817
+ options . headers = { ...options . headers , before : 'Global' }
818
+ return { options }
819
+ } ,
820
+ afterFetch ( ctx ) {
821
+ ctx . data . after = 'Global'
822
+ return ctx
823
+ } ,
824
+ } ,
825
+ } )
826
+
827
+ const { data } = useMyFetch ( 'test' , {
828
+ beforeFetch ( { options } ) {
829
+ options . headers = { ...options . headers , before : 'Local' }
830
+ } ,
831
+ } ) . json ( )
832
+
833
+ await vi . waitFor ( ( ) => {
834
+ expect ( fetchSpyHeaders ( ) ) . toMatchObject ( { before : 'Local' } )
835
+ expect ( data . value ) . toEqual ( expect . objectContaining ( { after : 'Global' } ) )
836
+ } )
837
+ } )
810
838
} )
Original file line number Diff line number Diff line change @@ -247,7 +247,13 @@ function combineCallbacks<T = any>(combination: Combination, ...callbacks: (((ct
247
247
if ( combination === 'overwrite' ) {
248
248
// use last callback
249
249
return async ( ctx : T ) => {
250
- const callback = callbacks [ callbacks . length - 1 ]
250
+ let callback
251
+ for ( let i = callbacks . length - 1 ; i >= 0 ; i -- ) {
252
+ if ( callbacks [ i ] != null ) {
253
+ callback = callbacks [ i ]
254
+ break
255
+ }
256
+ }
251
257
if ( callback )
252
258
return { ...ctx , ...( await callback ( ctx ) ) }
253
259
You can’t perform that action at this time.
0 commit comments