Skip to content

Commit 0a2c012

Browse files
authored
Merge pull request #280 from ecamp/fix-base-url
Fix baseUrl usage of axios
2 parents 0e348e2 + 4f06dbc commit 0a2c012

File tree

8 files changed

+375
-9
lines changed

8 files changed

+375
-9
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
node: [ '12', '13', '14' ]
14+
node: [ '14', '16', '18' ]
1515
steps:
1616

1717
- uses: actions/checkout@v2

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module.exports = {
44
transform: {
55
'^.+\\.ts?$': 'ts-jest',
66
'^.+\\.js?$': 'babel-jest'
7-
}
7+
},
8+
transformIgnorePatterns: [
9+
'node_modules/(?!(mock-xmlhttprequest))'
10+
]
811
}

package-lock.json

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"version": "npm run build"
1515
},
1616
"engines": {
17-
"node": ">=0.12"
17+
"node": ">=14.0.0 <19.0.0"
1818
},
1919
"files": [
2020
"/dist",
@@ -51,6 +51,7 @@
5151
"@babel/preset-env": "7.16.11",
5252
"@babel/preset-typescript": "7.16.7",
5353
"@nuxt/types": "2.15.8",
54+
"@types/jest": "27.5.1",
5455
"@typescript-eslint/eslint-plugin": "5.10.1",
5556
"@typescript-eslint/parser": "5.10.1",
5657
"@vue/eslint-config-standard": "6.1.0",
@@ -70,6 +71,7 @@
7071
"eslint-plugin-vue": "7.20.0",
7172
"jest": "27.5.1",
7273
"lodash": "4.17.21",
74+
"mock-xmlhttprequest": "8.1.0",
7375
"rimraf": "3.0.2",
7476
"source-map-loader": "3.0.1",
7577
"ts-jest": "27.1.3",

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)

src/normalizeEntityUri.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,34 @@ function normalizeEntityUri (uriOrEntity: string | ResourceInterface | null = ''
5555
*/
5656
function normalizeUri (uri: unknown, baseUrl: string): string | null {
5757
if (typeof uri !== 'string') return null
58-
return sortQueryParams(uri).replace(new RegExp(`^${baseUrl}`), '')
58+
const sorted = sortQueryParams(uri)
59+
const simpleReplace = sorted.replace(new RegExp(`^${baseUrl}`), '')
60+
if (baseUrl && simpleReplace === uri) {
61+
try {
62+
const parsedBaseUrl = new URL(baseUrl)
63+
const uriHasHost = getHostOfUri(uri) !== undefined
64+
if (parsedBaseUrl.host && uriHasHost) {
65+
return simpleReplace
66+
}
67+
68+
const pathname = parsedBaseUrl.pathname.replace(/\/$/, '')
69+
return sorted.replace(new RegExp(`^${pathname}`), '')
70+
} catch (_) {
71+
}
72+
}
73+
return simpleReplace
74+
}
75+
76+
/**
77+
* returns the host of uri if present, or undefined if new URL throws exception.
78+
* @param uri
79+
*/
80+
function getHostOfUri (uri: string): string | undefined {
81+
try {
82+
return new URL(uri).host
83+
} catch (_) {
84+
return undefined
85+
}
5986
}
6087

6188
export default normalizeEntityUri

0 commit comments

Comments
 (0)