@@ -7,8 +7,10 @@ import LoadingResource from './LoadingResource'
7
7
import storeModule , { State } from './storeModule'
8
8
import ServerException from './ServerException'
9
9
import { ExternalConfig } from './interfaces/Config'
10
+ import Options from './interfaces/Options'
10
11
import { 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'
12
14
import ResourceInterface from './interfaces/ResourceInterface'
13
15
import StoreData , { Link , SerializablePromise } from './interfaces/StoreData'
14
16
import ApiActions from './interfaces/ApiActions'
@@ -109,11 +111,12 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
109
111
* }
110
112
*
111
113
* @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
112
115
* @returns entity Entity from the store. Note that when fetching an object for the first time, a reactive
113
116
* dummy is returned, which will be replaced with the true data through Vue's reactivity
114
117
* system as soon as the API request finishes.
115
118
*/
116
- function get ( uriOrEntity : string | ResourceInterface = '' ) : ResourceInterface {
119
+ function get ( uriOrEntity : string | ResourceInterface = '' , options : Options = { } ) : ResourceInterface {
117
120
const uri = normalizeEntityUri ( uriOrEntity , axios . defaults . baseURL )
118
121
119
122
if ( uri === null ) {
@@ -125,7 +128,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
125
128
throw new Error ( `Could not perform GET, "${ uriOrEntity } " is not an entity or URI` )
126
129
}
127
130
128
- setLoadPromiseOnStore ( uri , load ( uri , false ) )
131
+ setLoadPromiseOnStore ( uri , load ( uri , false , options ) )
129
132
return resourceCreator . wrap ( store . state [ opts . apiName ] [ uri ] )
130
133
}
131
134
@@ -184,10 +187,11 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
184
187
* sets the load promise on the entity in the Vuex store.
185
188
* @param uri URI of the entity to load
186
189
* @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
187
191
* @returns entity the current entity data from the Vuex store. Note: This may be a reactive dummy if the
188
192
* API request is still ongoing.
189
193
*/
190
- function load ( uri : string , forceReload : boolean ) : Promise < StoreData > {
194
+ function load ( uri : string , forceReload : boolean , options : Options = { } ) : Promise < StoreData > {
191
195
const existsInStore = ! isUnknown ( uri )
192
196
193
197
const isAlreadyLoading = existsInStore && ( store . state [ opts . apiName ] [ uri ] . _meta || { } ) . loading
@@ -204,9 +208,9 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
204
208
}
205
209
206
210
if ( ! existsInStore ) {
207
- return loadFromApi ( uri , 'fetch' )
211
+ return loadFromApi ( uri , 'fetch' , options )
208
212
} else if ( forceReload ) {
209
- return loadFromApi ( uri , 'reload' ) . catch ( error => {
213
+ return loadFromApi ( uri , 'reload' , options ) . catch ( error => {
210
214
store . commit ( 'reloadingFailed' , uri )
211
215
throw error
212
216
} )
@@ -222,11 +226,12 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
222
226
* being usable in Vue components).
223
227
* @param uri URI of the entity to load from the API
224
228
* @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
225
230
* @returns Promise resolves to the raw data stored in the Vuex store after the API request completes, or
226
231
* rejects when the API request fails
227
232
*/
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 } ) => {
230
235
if ( opts . forceRequestedSelfLink ) {
231
236
data . _links . self . href = uri
232
237
}
@@ -237,6 +242,12 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
237
242
} )
238
243
}
239
244
245
+ function axiosWith ( options ) {
246
+ const instance = AxiosCreator . create ( mergeAxiosConfig ( axios . defaults , { } ) )
247
+ instance . interceptors . request . use ( options . axiosRequestInterceptor )
248
+ return instance
249
+ }
250
+
240
251
/**
241
252
* Loads the URI of a related entity from the store, or the API in case it is not already fetched.
242
253
*
@@ -280,7 +291,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
280
291
store . commit ( 'addEmpty' , uri )
281
292
}
282
293
283
- const returnedResource = axios . patch ( axios . defaults . baseURL + uri , data ) . then ( ( { data } ) => {
294
+ return axios . patch ( axios . defaults . baseURL + uri , data ) . then ( ( { data } ) => {
284
295
if ( opts . forceRequestedSelfLink ) {
285
296
data . _links . self . href = uri
286
297
}
@@ -289,8 +300,6 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
289
300
} , ( error ) => {
290
301
throw handleAxiosError ( 'patch' , uri , error )
291
302
} )
292
-
293
- return returnedResource
294
303
}
295
304
296
305
/**
0 commit comments