diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 4c6ac30..83b8245 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -13,9 +13,9 @@ jobs: matrix: node: ["18", "20", "22"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} @@ -23,7 +23,7 @@ jobs: ${{ runner.os }}-node- - name: Setup node - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} @@ -42,9 +42,9 @@ jobs: name: "Linter" runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} @@ -52,7 +52,7 @@ jobs: ${{ runner.os }}-node- - name: Setup node - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: 22 @@ -64,9 +64,9 @@ jobs: name: "Build" runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} @@ -74,7 +74,7 @@ jobs: ${{ runner.os }}-node- - name: Setup node - uses: actions/setup-node@v2 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} diff --git a/src/normalizeEntityUri.ts b/src/normalizeEntityUri.ts index 76f0400..3c4a38d 100644 --- a/src/normalizeEntityUri.ts +++ b/src/normalizeEntityUri.ts @@ -51,20 +51,19 @@ function normalizeUri (uri: unknown, baseUrl: string): string | null { if (typeof uri !== 'string') return null const sorted = sortQueryParams(uri) const simpleReplace = sorted.replace(new RegExp(`^${baseUrl}`), '') - if (baseUrl && simpleReplace === uri) { - try { - const parsedBaseUrl = new URL(baseUrl) - const uriHasHost = getHostOfUri(uri) !== undefined - if (parsedBaseUrl.host && uriHasHost) { - return simpleReplace - } - const pathname = parsedBaseUrl.pathname.replace(/\/$/, '') - return sorted.replace(new RegExp(`^${pathname}`), '') - } catch (_) { + try { + const parsedBaseUrl = new URL(baseUrl) + const uriHasHost = getHostOfUri(uri) !== undefined + if (parsedBaseUrl.host && uriHasHost) { + return simpleReplace } + + const pathname = parsedBaseUrl.pathname.replace(/\/$/, '') + return sorted.replace(new RegExp(`^${pathname}`), '') + } catch (_) { + return simpleReplace } - return simpleReplace } /** diff --git a/tests/normalizeUri.spec.js b/tests/normalizeUri.spec.js index e7bf6d1..b66df4d 100644 --- a/tests/normalizeUri.spec.js +++ b/tests/normalizeUri.spec.js @@ -23,7 +23,8 @@ describe('URI normalizing', () => { '/camps?page=0&abc=123&page=1': '/camps?abc=123&page=0&page=1', '/camps?page=1&abc=123&page=0': '/camps?abc=123&page=1&page=0', '/camps?page=0&xyz=123&page=1': '/camps?page=0&page=1&xyz=123', - '/camps/?e[]=abc&a[]=123&a=test': '/camps/?a=test&a%5B%5D=123&e%5B%5D=abc' + '/camps/?e[]=abc&a[]=123&a=test': + '/camps/?a=test&a%5B%5D=123&e%5B%5D=abc' } Object.entries(examples).forEach(([example, expected]) => { @@ -45,7 +46,7 @@ describe('URI normalizing', () => { expect(result).toEqual(null) }) - it('treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get(\'\')', () => { + it("treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get('')", () => { // given // when @@ -55,7 +56,7 @@ describe('URI normalizing', () => { expect(result).toEqual('') }) - it('treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get(\'\')', () => { + it("treats undefined as root URI, to enable this.api.get() without parameters to be the same as this.api.get('')", () => { // given // when @@ -65,59 +66,68 @@ describe('URI normalizing', () => { expect(result).toEqual('') }) - const baseUrlParams = - [ - { - baseUrl: undefined, - uri: '/api/activities', - normalized: '/api/activities' - }, - { - baseUrl: null, - uri: '/api/activities', - normalized: '/api/activities' - }, - { - baseUrl: '', - uri: '/api/activities', - normalized: '/api/activities' - }, - { - baseUrl: '/api', - uri: '/api/activities', - normalized: '/activities' - }, - { - baseUrl: 'http://localhost:3000', - uri: 'http://localhost:3000/api/activities', - normalized: '/api/activities' - }, - { - baseUrl: 'http://localhost:3000', - uri: '/api/activities', - normalized: '/api/activities' - }, - { - baseUrl: 'http://localhost:3000/api', - uri: 'http://localhost:3000/api/activities', - normalized: '/activities' - }, - { - baseUrl: 'http://localhost:3000/api', - uri: '/api/activities', - normalized: '/activities' - }, - { - baseUrl: 'http://localhost:3000/api/', - uri: '/api/activities', - normalized: '/activities' - }, - { - baseUrl: 'http://localhost:3000/api/', - uri: 'http://localhost:3000/print/activities', - normalized: 'http://localhost:3000/print/activities' - } - ] + const baseUrlParams = [ + { + baseUrl: undefined, + uri: '/api/activities', + normalized: '/api/activities' + }, + { + baseUrl: null, + uri: '/api/activities', + normalized: '/api/activities' + }, + { + baseUrl: '', + uri: '/api/activities', + normalized: '/api/activities' + }, + { + baseUrl: '/api', + uri: '/api/activities', + normalized: '/activities' + }, + { + baseUrl: 'http://localhost:3000', + uri: 'http://localhost:3000/api/activities', + normalized: '/api/activities' + }, + { + baseUrl: 'http://localhost:3000', + uri: '/api/activities', + normalized: '/api/activities' + }, + { + baseUrl: 'http://localhost:3000/api', + uri: 'http://localhost:3000/api/activities', + normalized: '/activities' + }, + { + baseUrl: 'http://localhost:3000/api', + uri: '/api/activities', + normalized: '/activities' + }, + { + baseUrl: 'http://localhost:3000/api/', + uri: '/api/activities', + normalized: '/activities' + }, + { + baseUrl: 'http://localhost:3000/api/', + uri: 'http://localhost:3000/print/activities', + normalized: 'http://localhost:3000/print/activities' + }, + { + baseUrl: 'http://localhost:3000/api/', + uri: '/api/activities?page=0&abc=123', + normalized: '/activities?abc=123&page=0' + }, + { + baseUrl: '/api', + uri: '/api/activities?page=0&abc=123', + normalized: '/activities?abc=123&page=0' + } + ] baseUrlParams.forEach(({ baseUrl, uri, normalized }) => { it(`normalizes ${uri} when baseUrl is ${baseUrl} to ${normalized}`, () => {