Skip to content

Commit 94ba90b

Browse files
authored
Merge pull request #305 from usu/fix/normalize-uri
fix normalizeUri for sorted query params
2 parents 70bfe40 + b6c280f commit 94ba90b

File tree

3 files changed

+85
-76
lines changed

3 files changed

+85
-76
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ jobs:
1313
matrix:
1414
node: ["18", "20", "22"]
1515
steps:
16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v4
1717

18-
- uses: actions/cache@v2
18+
- uses: actions/cache@v4
1919
with:
2020
path: ~/.npm
2121
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
2222
restore-keys: |
2323
${{ runner.os }}-node-
2424
2525
- name: Setup node
26-
uses: actions/setup-node@v2
26+
uses: actions/setup-node@v4
2727
with:
2828
node-version: ${{ matrix.node }}
2929

@@ -42,17 +42,17 @@ jobs:
4242
name: "Linter"
4343
runs-on: ubuntu-latest
4444
steps:
45-
- uses: actions/checkout@v2
45+
- uses: actions/checkout@v4
4646

47-
- uses: actions/cache@v2
47+
- uses: actions/cache@v4
4848
with:
4949
path: ~/.npm
5050
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
5151
restore-keys: |
5252
${{ runner.os }}-node-
5353
5454
- name: Setup node
55-
uses: actions/setup-node@v2
55+
uses: actions/setup-node@v4
5656
with:
5757
node-version: 22
5858

@@ -64,17 +64,17 @@ jobs:
6464
name: "Build"
6565
runs-on: ubuntu-latest
6666
steps:
67-
- uses: actions/checkout@v2
67+
- uses: actions/checkout@v4
6868

69-
- uses: actions/cache@v2
69+
- uses: actions/cache@v4
7070
with:
7171
path: ~/.npm
7272
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
7373
restore-keys: |
7474
${{ runner.os }}-node-
7575
7676
- name: Setup node
77-
uses: actions/setup-node@v2
77+
uses: actions/setup-node@v4
7878
with:
7979
node-version: ${{ matrix.node }}
8080

src/normalizeEntityUri.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,19 @@ function normalizeUri (uri: unknown, baseUrl: string): string | null {
5151
if (typeof uri !== 'string') return null
5252
const sorted = sortQueryParams(uri)
5353
const simpleReplace = sorted.replace(new RegExp(`^${baseUrl}`), '')
54-
if (baseUrl && simpleReplace === uri) {
55-
try {
56-
const parsedBaseUrl = new URL(baseUrl)
57-
const uriHasHost = getHostOfUri(uri) !== undefined
58-
if (parsedBaseUrl.host && uriHasHost) {
59-
return simpleReplace
60-
}
6154

62-
const pathname = parsedBaseUrl.pathname.replace(/\/$/, '')
63-
return sorted.replace(new RegExp(`^${pathname}`), '')
64-
} catch (_) {
55+
try {
56+
const parsedBaseUrl = new URL(baseUrl)
57+
const uriHasHost = getHostOfUri(uri) !== undefined
58+
if (parsedBaseUrl.host && uriHasHost) {
59+
return simpleReplace
6560
}
61+
62+
const pathname = parsedBaseUrl.pathname.replace(/\/$/, '')
63+
return sorted.replace(new RegExp(`^${pathname}`), '')
64+
} catch (_) {
65+
return simpleReplace
6666
}
67-
return simpleReplace
6867
}
6968

7069
/**

tests/normalizeUri.spec.js

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe('URI normalizing', () => {
2323
'/camps?page=0&abc=123&page=1': '/camps?abc=123&page=0&page=1',
2424
'/camps?page=1&abc=123&page=0': '/camps?abc=123&page=1&page=0',
2525
'/camps?page=0&xyz=123&page=1': '/camps?page=0&page=1&xyz=123',
26-
'/camps/?e[]=abc&a[]=123&a=test': '/camps/?a=test&a%5B%5D=123&e%5B%5D=abc'
26+
'/camps/?e[]=abc&a[]=123&a=test':
27+
'/camps/?a=test&a%5B%5D=123&e%5B%5D=abc'
2728
}
2829

2930
Object.entries(examples).forEach(([example, expected]) => {
@@ -45,7 +46,7 @@ describe('URI normalizing', () => {
4546
expect(result).toEqual(null)
4647
})
4748

48-
it('treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get(\'\')', () => {
49+
it("treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get('')", () => {
4950
// given
5051

5152
// when
@@ -55,7 +56,7 @@ describe('URI normalizing', () => {
5556
expect(result).toEqual('')
5657
})
5758

58-
it('treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get(\'\')', () => {
59+
it("treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get('')", () => {
5960
// given
6061

6162
// when
@@ -65,59 +66,68 @@ describe('URI normalizing', () => {
6566
expect(result).toEqual('')
6667
})
6768

68-
const baseUrlParams =
69-
[
70-
{
71-
baseUrl: undefined,
72-
uri: '/api/activities',
73-
normalized: '/api/activities'
74-
},
75-
{
76-
baseUrl: null,
77-
uri: '/api/activities',
78-
normalized: '/api/activities'
79-
},
80-
{
81-
baseUrl: '',
82-
uri: '/api/activities',
83-
normalized: '/api/activities'
84-
},
85-
{
86-
baseUrl: '/api',
87-
uri: '/api/activities',
88-
normalized: '/activities'
89-
},
90-
{
91-
baseUrl: 'http://localhost:3000',
92-
uri: 'http://localhost:3000/api/activities',
93-
normalized: '/api/activities'
94-
},
95-
{
96-
baseUrl: 'http://localhost:3000',
97-
uri: '/api/activities',
98-
normalized: '/api/activities'
99-
},
100-
{
101-
baseUrl: 'http://localhost:3000/api',
102-
uri: 'http://localhost:3000/api/activities',
103-
normalized: '/activities'
104-
},
105-
{
106-
baseUrl: 'http://localhost:3000/api',
107-
uri: '/api/activities',
108-
normalized: '/activities'
109-
},
110-
{
111-
baseUrl: 'http://localhost:3000/api/',
112-
uri: '/api/activities',
113-
normalized: '/activities'
114-
},
115-
{
116-
baseUrl: 'http://localhost:3000/api/',
117-
uri: 'http://localhost:3000/print/activities',
118-
normalized: 'http://localhost:3000/print/activities'
119-
}
120-
]
69+
const baseUrlParams = [
70+
{
71+
baseUrl: undefined,
72+
uri: '/api/activities',
73+
normalized: '/api/activities'
74+
},
75+
{
76+
baseUrl: null,
77+
uri: '/api/activities',
78+
normalized: '/api/activities'
79+
},
80+
{
81+
baseUrl: '',
82+
uri: '/api/activities',
83+
normalized: '/api/activities'
84+
},
85+
{
86+
baseUrl: '/api',
87+
uri: '/api/activities',
88+
normalized: '/activities'
89+
},
90+
{
91+
baseUrl: 'http://localhost:3000',
92+
uri: 'http://localhost:3000/api/activities',
93+
normalized: '/api/activities'
94+
},
95+
{
96+
baseUrl: 'http://localhost:3000',
97+
uri: '/api/activities',
98+
normalized: '/api/activities'
99+
},
100+
{
101+
baseUrl: 'http://localhost:3000/api',
102+
uri: 'http://localhost:3000/api/activities',
103+
normalized: '/activities'
104+
},
105+
{
106+
baseUrl: 'http://localhost:3000/api',
107+
uri: '/api/activities',
108+
normalized: '/activities'
109+
},
110+
{
111+
baseUrl: 'http://localhost:3000/api/',
112+
uri: '/api/activities',
113+
normalized: '/activities'
114+
},
115+
{
116+
baseUrl: 'http://localhost:3000/api/',
117+
uri: 'http://localhost:3000/print/activities',
118+
normalized: 'http://localhost:3000/print/activities'
119+
},
120+
{
121+
baseUrl: 'http://localhost:3000/api/',
122+
uri: '/api/activities?page=0&abc=123',
123+
normalized: '/activities?abc=123&page=0'
124+
},
125+
{
126+
baseUrl: '/api',
127+
uri: '/api/activities?page=0&abc=123',
128+
normalized: '/activities?abc=123&page=0'
129+
}
130+
]
121131

122132
baseUrlParams.forEach(({ baseUrl, uri, normalized }) => {
123133
it(`normalizes ${uri} when baseUrl is ${baseUrl} to ${normalized}`, () => {

0 commit comments

Comments
 (0)