Skip to content

Commit b082f61

Browse files
authored
chore: remove regex stripping trailing slash from configured url (#29)
1 parent 1f764e3 commit b082f61

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 4 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
57-
if (!apiUrl.endsWith(this.#apiVersionPrefix)) {
58-
apiUrl = `${apiUrl}/${this.#apiVersionPrefix}`;
56+
const { apiUrl } = config.environment;
57+
58+
if (apiUrl.endsWith(this.#apiVersionPrefix)) {
59+
this.#apiUrl = apiUrl;
60+
} else {
61+
const separator = apiUrl.endsWith('/') ? '' : '/';
62+
this.#apiUrl = `${apiUrl}${separator}${this.#apiVersionPrefix}`;
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
@@ -1,4 +1,5 @@
11
import { stub } from 'sinon';
2+
import { zeroAddress } from 'viem';
23
import { beforeEach, describe, expect, it } from 'vitest';
34

45
import {
@@ -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: async () => 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)