Skip to content

Commit

Permalink
Merge pull request #49 from distributed-lab/fix/jax-create-link
Browse files Browse the repository at this point in the history
[jac] Fix createLink for root baseUrl
  • Loading branch information
napalmpapalam authored Mar 12, 2024
2 parents f0d4116 + dd57f78 commit a8b9c2e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- `@distributedlab/jac` - `JsonApiResponse.createLink` method to handle client `baseUrl` with root path

## [1.0.0-rc.13] - 2024-03-02
### Changed
Expand Down
18 changes: 15 additions & 3 deletions packages/jac/src/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,25 @@ describe('JsonApi response data parsing unit test', () => {
apiClient: api,
})

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(response.createLink(response.links.next)).toBe(
expect(response.createLink(response.links.next!)).toBe(
'/meta-buy-orders?filter%5Bowner%5D=3d38fff3-847f-45f2-a267-891ba90dac37&include=meta_sell_order%2Cmeta_sell_order.token%2Cmeta_sell_order.token.metadata&page%5Blimit%5D=5&page%5Bnumber%5D=1&page%5Border%5D=desc',
)
})

test('should create correct link from response if link has no match with base URL', () => {
const { JsonApiClient } = jest.requireActual('./json-api')

const response = parseJsonApiResponse({
raw: MockWrapper.makeFetcherResponse(RAW_RESPONSE),
isNeedRaw: false,
apiClient: new JsonApiClient({ baseUrl: 'https://localhost:8095' }),
})

expect(response.createLink(response.links.next!)).toBe(
'/core/meta-buy-orders?filter%5Bowner%5D=3d38fff3-847f-45f2-a267-891ba90dac37&include=meta_sell_order%2Cmeta_sell_order.token%2Cmeta_sell_order.token.metadata&page%5Blimit%5D=5&page%5Bnumber%5D=1&page%5Border%5D=desc',
)
})

test('should return raw response', () => {
const rawResponse = MockWrapper.makeFetcherResponse(RAW_RESPONSE)
const response = parseJsonApiResponse({
Expand Down
18 changes: 5 additions & 13 deletions packages/jac/src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,13 @@ export class JsonApiResponse<T, U = JsonApiDefaultMeta> {

public createLink(link: Endpoint): Endpoint {
const baseUrl = this.#apiClient?.baseUrl

if (!baseUrl) return link

let intersection = ''

for (const char of link) {
if (baseUrl.endsWith(intersection + char)) {
intersection += char
break
} else {
intersection += char
}
}

return link.replace(intersection, '')
const baseUrlPath = new URL(baseUrl).pathname
// Do not remove leading slash if baseUrl is root
return baseUrlPath === '/'
? link
: link.replace(new RegExp(`^${baseUrlPath}`), '')
}

public async fetchPage(
Expand Down

0 comments on commit a8b9c2e

Please sign in to comment.