Skip to content

Commit b216857

Browse files
committed
index.ts: fix double appending baseUrl when baseUrl does not contain host
By using axios.defaults.baseUrl correctly: The idea is that you don't have to add it yourself anymore. But for the empty root endpoint, we need to append a / because axios supports the baseUrl, but not using and empty string as the url for a request...?
1 parent 2608139 commit b216857

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
7676
}
7777
}
7878

79-
return axios.post(axios.defaults.baseURL + uri, data).then(({ data, status }) => {
79+
return axios.post(uri || '/', data).then(({ data, status }) => {
8080
if (status === 204) {
8181
return null
8282
}
@@ -226,7 +226,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
226226
* rejects when the API request fails
227227
*/
228228
function loadFromApi (uri: string, operation: string): Promise<StoreData> {
229-
return axios.get(axios.defaults.baseURL + uri).then(({ data }) => {
229+
return axios.get(uri || '/').then(({ data }) => {
230230
if (opts.forceRequestedSelfLink) {
231231
data._links.self.href = uri
232232
}
@@ -280,7 +280,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
280280
store.commit('addEmpty', uri)
281281
}
282282

283-
const returnedResource = axios.patch(axios.defaults.baseURL + uri, data).then(({ data }) => {
283+
const returnedResource = axios.patch(uri || '/', data).then(({ data }) => {
284284
if (opts.forceRequestedSelfLink) {
285285
data._links.self.href = uri
286286
}
@@ -343,7 +343,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
343343
}
344344

345345
store.commit('deleting', uri)
346-
return axios.delete(axios.defaults.baseURL + uri).then(
346+
return axios.delete(uri || '/').then(
347347
() => deleted(uri),
348348
(error) => {
349349
store.commit('deletingFailed', uri)

tests/axios.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('When using baseUrl with axios', () => {
4646
}
4747
},
4848
expectedFetches: [
49-
'http://localhost:3000',
49+
'http://localhost:3000/',
5050
'http://localhost:3000/entities'
5151
]
5252
},
@@ -63,7 +63,7 @@ describe('When using baseUrl with axios', () => {
6363
}
6464
},
6565
expectedFetches: [
66-
'http://localhost:3000/api',
66+
'http://localhost:3000/api/',
6767
'http://localhost:3000/api/api/entities'
6868
]
6969
},
@@ -80,8 +80,8 @@ describe('When using baseUrl with axios', () => {
8080
}
8181
},
8282
expectedFetches: [
83-
'/api/api',
84-
'/api/api/entities'
83+
'/api/',
84+
'/api/entities'
8585
]
8686
}
8787
]

0 commit comments

Comments
 (0)