@@ -7,8 +7,10 @@ import LoadingResource from './LoadingResource'
77import storeModule , { State } from './storeModule'
88import ServerException from './ServerException'
99import { ExternalConfig } from './interfaces/Config'
10+ import Options from './interfaces/Options'
1011import { Store } from 'vuex/types'
11- import { AxiosInstance , AxiosError } from 'axios'
12+ import AxiosCreator , { AxiosInstance , AxiosError } from 'axios'
13+ import mergeAxiosConfig from 'axios/lib/core/mergeConfig'
1214import ResourceInterface from './interfaces/ResourceInterface'
1315import StoreData , { Link , SerializablePromise } from './interfaces/StoreData'
1416import ApiActions from './interfaces/ApiActions'
@@ -109,11 +111,12 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
109111 * }
110112 *
111113 * @param uriOrEntity URI (or instance) of an entity to load from the store or API. If omitted, the root resource of the API is returned.
114+ * @param options Options for this single request
112115 * @returns entity Entity from the store. Note that when fetching an object for the first time, a reactive
113116 * dummy is returned, which will be replaced with the true data through Vue's reactivity
114117 * system as soon as the API request finishes.
115118 */
116- function get ( uriOrEntity : string | ResourceInterface = '' ) : ResourceInterface {
119+ function get ( uriOrEntity : string | ResourceInterface = '' , options : Options = { } ) : ResourceInterface {
117120 const uri = normalizeEntityUri ( uriOrEntity , axios . defaults . baseURL )
118121
119122 if ( uri === null ) {
@@ -125,7 +128,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
125128 throw new Error ( `Could not perform GET, "${ uriOrEntity } " is not an entity or URI` )
126129 }
127130
128- setLoadPromiseOnStore ( uri , load ( uri , false ) )
131+ setLoadPromiseOnStore ( uri , load ( uri , false , options ) )
129132 return resourceCreator . wrap ( store . state [ opts . apiName ] [ uri ] )
130133 }
131134
@@ -184,10 +187,11 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
184187 * sets the load promise on the entity in the Vuex store.
185188 * @param uri URI of the entity to load
186189 * @param forceReload If true, the entity will be fetched from the API even if it is already in the Vuex store.
190+ * @param options Options for this single request
187191 * @returns entity the current entity data from the Vuex store. Note: This may be a reactive dummy if the
188192 * API request is still ongoing.
189193 */
190- function load ( uri : string , forceReload : boolean ) : Promise < StoreData > {
194+ function load ( uri : string , forceReload : boolean , options : Options = { } ) : Promise < StoreData > {
191195 const existsInStore = ! isUnknown ( uri )
192196
193197 const isAlreadyLoading = existsInStore && ( store . state [ opts . apiName ] [ uri ] . _meta || { } ) . loading
@@ -204,9 +208,9 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
204208 }
205209
206210 if ( ! existsInStore ) {
207- return loadFromApi ( uri , 'fetch' )
211+ return loadFromApi ( uri , 'fetch' , options )
208212 } else if ( forceReload ) {
209- return loadFromApi ( uri , 'reload' ) . catch ( error => {
213+ return loadFromApi ( uri , 'reload' , options ) . catch ( error => {
210214 store . commit ( 'reloadingFailed' , uri )
211215 throw error
212216 } )
@@ -222,11 +226,12 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
222226 * being usable in Vue components).
223227 * @param uri URI of the entity to load from the API
224228 * @param operation description of the operation triggering this load, e.g. fetch or reload, for error reporting
229+ * @param options Options for this single request
225230 * @returns Promise resolves to the raw data stored in the Vuex store after the API request completes, or
226231 * rejects when the API request fails
227232 */
228- function loadFromApi ( uri : string , operation : string ) : Promise < StoreData > {
229- return axios . get ( axios . defaults . baseURL + uri ) . then ( ( { data } ) => {
233+ function loadFromApi ( uri : string , operation : string , options : Options = { } ) : Promise < StoreData > {
234+ return axiosWith ( options ) . get ( axios . defaults . baseURL + uri ) . then ( ( { data } ) => {
230235 if ( opts . forceRequestedSelfLink ) {
231236 data . _links . self . href = uri
232237 }
@@ -237,6 +242,12 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
237242 } )
238243 }
239244
245+ function axiosWith ( options ) {
246+ const instance = AxiosCreator . create ( mergeAxiosConfig ( axios . defaults , { } ) )
247+ instance . interceptors . request . use ( options . axiosRequestInterceptor )
248+ return instance
249+ }
250+
240251 /**
241252 * Loads the URI of a related entity from the store, or the API in case it is not already fetched.
242253 *
@@ -280,7 +291,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
280291 store . commit ( 'addEmpty' , uri )
281292 }
282293
283- const returnedResource = axios . patch ( axios . defaults . baseURL + uri , data ) . then ( ( { data } ) => {
294+ return axios . patch ( axios . defaults . baseURL + uri , data ) . then ( ( { data } ) => {
284295 if ( opts . forceRequestedSelfLink ) {
285296 data . _links . self . href = uri
286297 }
@@ -289,8 +300,6 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
289300 } , ( error ) => {
290301 throw handleAxiosError ( 'patch' , uri , error )
291302 } )
292-
293- return returnedResource
294303 }
295304
296305 /**
0 commit comments