Skip to content

Commit 08f5d70

Browse files
committed
chore: remove regex stripping trailing slash from configured url
1 parent 34fd9c9 commit 08f5d70

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

packages/delegation-toolkit/src/experimental/delegationStorage.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ export class DelegationStorageClient {
5353
#apiUrl: string;
5454

5555
constructor(config: DelegationStorageConfig) {
56-
let apiUrl = config.environment.apiUrl.replace(/\/+$/u, ''); // Remove trailing slashes
56+
const apiUrl = config.environment.apiUrl;
57+
5758
if (!apiUrl.endsWith(this.#apiVersionPrefix)) {
58-
apiUrl = `${apiUrl}/${this.#apiVersionPrefix}`;
59+
const separator = apiUrl.endsWith('/') ? '' : '/';
60+
this.#apiUrl = `${apiUrl}${separator}${this.#apiVersionPrefix}`;
61+
} else {
62+
this.#apiUrl = apiUrl;
5963
}
6064
this.#fetcher = this.#initializeFetcher(config);
6165
this.#config = config;
62-
this.#apiUrl = apiUrl;
6366
}
6467

6568
/**

packages/delegation-toolkit/test/experimental/delegationStorage.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
DelegationStorageEnvironment,
1515
DelegationStoreFilter,
1616
} from '../../src/experimental/delegationStorage';
17+
import { zeroAddress } from 'viem';
1718

1819
const mockAPIKey = 'mock-api-key-mock-api-key';
1920
const mockAPIKeyId = 'mock-api-key-id-mock-api-key-id';
@@ -56,6 +57,30 @@ describe('DelegationStorageClient', () => {
5657

5758
expect(delegationStore).toBeInstanceOf(DelegationStorageClient);
5859
});
60+
61+
it('accepts an apiUrl with a trailing slash', async () => {
62+
mockFetch.resolves({
63+
json: () => Promise.resolve([]),
64+
});
65+
66+
const delegationStore = new DelegationStorageClient({
67+
...mockConfig,
68+
environment: {
69+
apiUrl: `${mockApiUrl}/`,
70+
},
71+
fetcher: mockFetch,
72+
});
73+
74+
await delegationStore.fetchDelegations(zeroAddress);
75+
76+
const calledUrl = mockFetch.getCall(0).args[0];
77+
78+
const expectedUrlPrefix = `${mockApiUrl}/api/v0/delegation`;
79+
80+
expect(calledUrl.slice(0, expectedUrlPrefix.length)).toStrictEqual(
81+
expectedUrlPrefix,
82+
);
83+
});
5984
});
6085

6186
describe('getDelegationChain', () => {

0 commit comments

Comments
 (0)