diff --git a/src/api/services/ShlinkApiClient.ts b/src/api/services/ShlinkApiClient.ts index 99161c47..97b6774a 100644 --- a/src/api/services/ShlinkApiClient.ts +++ b/src/api/services/ShlinkApiClient.ts @@ -86,14 +86,15 @@ export class ShlinkApiClient { ): Promise => this.performRequest(`/short-urls/${shortCode}`, 'PATCH', { domain }, edit); - public readonly listTags = async (useTagsStatsEndpoint: boolean): Promise => - (useTagsStatsEndpoint - ? this.performRequest<{ tags: ShlinkTagsStatsResponse }>('/tags/stats', 'GET') - .then(({ tags }) => tags) - .then(({ data }) => ({ tags: data.map(({ tag }) => tag), stats: data })) - : this.performRequest<{ tags: ShlinkTagsResponse }>('/tags', 'GET', { withStats: 'true' }) - .then(({ tags }) => tags) - .then(({ data, stats }) => ({ tags: data, stats }))); + public readonly listTags = async (): Promise => + this.performRequest<{ tags: ShlinkTagsResponse }>('/tags', 'GET', { withStats: 'true' }) + .then(({ tags }) => tags) + .then(({ data, stats }) => ({ tags: data, stats })); + + public readonly tagsStats = async (): Promise => + this.performRequest<{ tags: ShlinkTagsStatsResponse }>('/tags/stats', 'GET') + .then(({ tags }) => tags) + .then(({ data }) => ({ tags: data.map(({ tag }) => tag), stats: data })); public readonly deleteTags = async (tags: string[]): Promise<{ tags: string[] }> => this.performEmptyRequest('/tags', 'DELETE', { tags }).then(() => ({ tags })); diff --git a/src/tags/reducers/tagsList.ts b/src/tags/reducers/tagsList.ts index b29a8264..e96c8a23 100644 --- a/src/tags/reducers/tagsList.ts +++ b/src/tags/reducers/tagsList.ts @@ -77,8 +77,10 @@ export const listTags = (buildShlinkApiClient: ShlinkApiClientBuilder, force = t return tagsList; } - const { listTags: shlinkListTags } = buildShlinkApiClient(getState); - const { tags, stats = [] }: ShlinkTags = await shlinkListTags(supportedFeatures.tagsStats(selectedServer)); + const { listTags: shlinkListTags, tagsStats } = buildShlinkApiClient(getState); + const { tags, stats = [] }: ShlinkTags = await ( + supportedFeatures.tagsStats(selectedServer) ? tagsStats() : shlinkListTags() + ); const processedStats = stats.reduce((acc, { tag, shortUrlsCount, visitsCount }) => { acc[tag] = { shortUrlsCount, visitsCount }; diff --git a/test/api/services/ShlinkApiClient.test.ts b/test/api/services/ShlinkApiClient.test.ts index 5599fa3c..408f6903 100644 --- a/test/api/services/ShlinkApiClient.test.ts +++ b/test/api/services/ShlinkApiClient.test.ts @@ -202,7 +202,7 @@ describe('ShlinkApiClient', () => { }); const { listTags } = buildApiClient(); - const result = await listTags(false); + const result = await listTags(); expect({ tags: expectedTags }).toEqual(result); expect(fetchJson).toHaveBeenCalledWith( @@ -210,7 +210,9 @@ describe('ShlinkApiClient', () => { expect.objectContaining({ method: 'GET' }), ); }); + }); + describe('tagsStats', () => { it('can use /tags/stats endpoint', async () => { const expectedTags = ['foo', 'bar']; const expectedStats = expectedTags.map((tag) => ({ tag, shortUrlsCount: 10, visitsCount: 10 })); @@ -220,9 +222,9 @@ describe('ShlinkApiClient', () => { data: expectedStats, }, }); - const { listTags } = buildApiClient(); + const { tagsStats } = buildApiClient(); - const result = await listTags(true); + const result = await tagsStats(); expect({ tags: expectedTags, stats: expectedStats }).toEqual(result); expect(fetchJson).toHaveBeenCalledWith(