diff --git a/packages/delegation-toolkit/src/experimental/delegationStorage.ts b/packages/delegation-toolkit/src/experimental/delegationStorage.ts index 1715f4ab..9add1d53 100644 --- a/packages/delegation-toolkit/src/experimental/delegationStorage.ts +++ b/packages/delegation-toolkit/src/experimental/delegationStorage.ts @@ -53,13 +53,16 @@ export class DelegationStorageClient { #apiUrl: string; constructor(config: DelegationStorageConfig) { - let apiUrl = config.environment.apiUrl.replace(/\/+$/u, ''); // Remove trailing slashes - if (!apiUrl.endsWith(this.#apiVersionPrefix)) { - apiUrl = `${apiUrl}/${this.#apiVersionPrefix}`; + const { apiUrl } = config.environment; + + if (apiUrl.endsWith(this.#apiVersionPrefix)) { + this.#apiUrl = apiUrl; + } else { + const separator = apiUrl.endsWith('/') ? '' : '/'; + this.#apiUrl = `${apiUrl}${separator}${this.#apiVersionPrefix}`; } this.#fetcher = this.#initializeFetcher(config); this.#config = config; - this.#apiUrl = apiUrl; } /** diff --git a/packages/delegation-toolkit/test/experimental/delegationStorage.test.ts b/packages/delegation-toolkit/test/experimental/delegationStorage.test.ts index 14065886..d4868513 100644 --- a/packages/delegation-toolkit/test/experimental/delegationStorage.test.ts +++ b/packages/delegation-toolkit/test/experimental/delegationStorage.test.ts @@ -1,4 +1,5 @@ import { stub } from 'sinon'; +import { zeroAddress } from 'viem'; import { beforeEach, describe, expect, it } from 'vitest'; import { @@ -56,6 +57,30 @@ describe('DelegationStorageClient', () => { expect(delegationStore).toBeInstanceOf(DelegationStorageClient); }); + + it('accepts an apiUrl with a trailing slash', async () => { + mockFetch.resolves({ + json: async () => Promise.resolve([]), + }); + + const delegationStore = new DelegationStorageClient({ + ...mockConfig, + environment: { + apiUrl: `${mockApiUrl}/`, + }, + fetcher: mockFetch, + }); + + await delegationStore.fetchDelegations(zeroAddress); + + const calledUrl = mockFetch.getCall(0).args[0]; + + const expectedUrlPrefix = `${mockApiUrl}/api/v0/delegation`; + + expect(calledUrl.slice(0, expectedUrlPrefix.length)).toStrictEqual( + expectedUrlPrefix, + ); + }); }); describe('getDelegationChain', () => {