Split tagsList and tagsStats methods in ShlinkApiClient for clarity

This commit is contained in:
Alejandro Celaya 2023-03-18 16:32:04 +01:00
parent a9af5163c1
commit 96c20b36a5
3 changed files with 18 additions and 13 deletions

View file

@ -86,14 +86,15 @@ export class ShlinkApiClient {
): Promise<ShortUrl> => ): Promise<ShortUrl> =>
this.performRequest<ShortUrl>(`/short-urls/${shortCode}`, 'PATCH', { domain }, edit); this.performRequest<ShortUrl>(`/short-urls/${shortCode}`, 'PATCH', { domain }, edit);
public readonly listTags = async (useTagsStatsEndpoint: boolean): Promise<ShlinkTags> => public readonly listTags = async (): Promise<ShlinkTags> =>
(useTagsStatsEndpoint this.performRequest<{ tags: ShlinkTagsResponse }>('/tags', 'GET', { withStats: 'true' })
? this.performRequest<{ tags: ShlinkTagsStatsResponse }>('/tags/stats', 'GET') .then(({ tags }) => tags)
.then(({ tags }) => tags) .then(({ data, stats }) => ({ tags: data, stats }));
.then(({ data }) => ({ tags: data.map(({ tag }) => tag), stats: data }))
: this.performRequest<{ tags: ShlinkTagsResponse }>('/tags', 'GET', { withStats: 'true' }) public readonly tagsStats = async (): Promise<ShlinkTags> =>
.then(({ tags }) => tags) this.performRequest<{ tags: ShlinkTagsStatsResponse }>('/tags/stats', 'GET')
.then(({ data, stats }) => ({ tags: data, stats }))); .then(({ tags }) => tags)
.then(({ data }) => ({ tags: data.map(({ tag }) => tag), stats: data }));
public readonly deleteTags = async (tags: string[]): Promise<{ tags: string[] }> => public readonly deleteTags = async (tags: string[]): Promise<{ tags: string[] }> =>
this.performEmptyRequest('/tags', 'DELETE', { tags }).then(() => ({ tags })); this.performEmptyRequest('/tags', 'DELETE', { tags }).then(() => ({ tags }));

View file

@ -77,8 +77,10 @@ export const listTags = (buildShlinkApiClient: ShlinkApiClientBuilder, force = t
return tagsList; return tagsList;
} }
const { listTags: shlinkListTags } = buildShlinkApiClient(getState); const { listTags: shlinkListTags, tagsStats } = buildShlinkApiClient(getState);
const { tags, stats = [] }: ShlinkTags = await shlinkListTags(supportedFeatures.tagsStats(selectedServer)); const { tags, stats = [] }: ShlinkTags = await (
supportedFeatures.tagsStats(selectedServer) ? tagsStats() : shlinkListTags()
);
const processedStats = stats.reduce<TagsStatsMap>((acc, { tag, shortUrlsCount, visitsCount }) => { const processedStats = stats.reduce<TagsStatsMap>((acc, { tag, shortUrlsCount, visitsCount }) => {
acc[tag] = { shortUrlsCount, visitsCount }; acc[tag] = { shortUrlsCount, visitsCount };

View file

@ -202,7 +202,7 @@ describe('ShlinkApiClient', () => {
}); });
const { listTags } = buildApiClient(); const { listTags } = buildApiClient();
const result = await listTags(false); const result = await listTags();
expect({ tags: expectedTags }).toEqual(result); expect({ tags: expectedTags }).toEqual(result);
expect(fetchJson).toHaveBeenCalledWith( expect(fetchJson).toHaveBeenCalledWith(
@ -210,7 +210,9 @@ describe('ShlinkApiClient', () => {
expect.objectContaining({ method: 'GET' }), expect.objectContaining({ method: 'GET' }),
); );
}); });
});
describe('tagsStats', () => {
it('can use /tags/stats endpoint', async () => { it('can use /tags/stats endpoint', async () => {
const expectedTags = ['foo', 'bar']; const expectedTags = ['foo', 'bar'];
const expectedStats = expectedTags.map((tag) => ({ tag, shortUrlsCount: 10, visitsCount: 10 })); const expectedStats = expectedTags.map((tag) => ({ tag, shortUrlsCount: 10, visitsCount: 10 }));
@ -220,9 +222,9 @@ describe('ShlinkApiClient', () => {
data: expectedStats, 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({ tags: expectedTags, stats: expectedStats }).toEqual(result);
expect(fetchJson).toHaveBeenCalledWith( expect(fetchJson).toHaveBeenCalledWith(