Updated ShlinkApiClient to support more filtering options for short URLs list

This commit is contained in:
Alejandro Celaya 2022-12-28 22:58:47 +01:00
parent d6d237fc52
commit a3bd10bc82
5 changed files with 44 additions and 8 deletions

View file

@ -24,9 +24,14 @@ import { HttpClient } from '../../common/services/HttpClient';
const buildShlinkBaseUrl = (url: string, version: 2 | 3) => `${url}/rest/v${version}`;
const rejectNilProps = reject(isNil);
const normalizeOrderByInParams = (
{ orderBy = {}, ...rest }: ShlinkShortUrlsListParams,
): ShlinkShortUrlsListNormalizedParams => ({ ...rest, orderBy: orderToString(orderBy) });
const normalizeListParams = (
{ orderBy = {}, excludeMaxVisitsReached, excludePastValidUntil, ...rest }: ShlinkShortUrlsListParams,
): ShlinkShortUrlsListNormalizedParams => ({
...rest,
excludeMaxVisitsReached: excludeMaxVisitsReached === true ? 'true' : undefined,
excludePastValidUntil: excludePastValidUntil === true ? 'true' : undefined,
orderBy: orderToString(orderBy),
});
export class ShlinkApiClient {
private apiVersion: 2 | 3;
@ -40,7 +45,7 @@ export class ShlinkApiClient {
}
public readonly listShortUrls = async (params: ShlinkShortUrlsListParams = {}): Promise<ShlinkShortUrlsResponse> =>
this.performRequest<{ shortUrls: ShlinkShortUrlsResponse }>('/short-urls', 'GET', normalizeOrderByInParams(params))
this.performRequest<{ shortUrls: ShlinkShortUrlsResponse }>('/short-urls', 'GET', normalizeListParams(params))
.then(({ shortUrls }) => shortUrls);
public readonly createShortUrl = async (options: ShortUrlData): Promise<ShortUrl> => {

View file

@ -96,14 +96,20 @@ export type ShlinkShortUrlsOrder = Order<ShlinkShortUrlsOrderableFields>;
export interface ShlinkShortUrlsListParams {
page?: string;
itemsPerPage?: number;
tags?: string[];
searchTerm?: string;
tags?: string[];
tagsMode?: TagsFilteringMode;
orderBy?: ShlinkShortUrlsOrder;
startDate?: string;
endDate?: string;
orderBy?: ShlinkShortUrlsOrder;
tagsMode?: TagsFilteringMode;
excludeMaxVisitsReached?: boolean;
excludePastValidUntil?: boolean;
}
export interface ShlinkShortUrlsListNormalizedParams extends Omit<ShlinkShortUrlsListParams, 'orderBy'> {
export interface ShlinkShortUrlsListNormalizedParams extends
Omit<ShlinkShortUrlsListParams, 'orderBy' | 'excludeMaxVisitsReached' | 'excludePastValidUntil'>
{
orderBy?: string;
excludeMaxVisitsReached?: 'true';
excludePastValidUntil?: 'true';
}

View file

@ -83,4 +83,6 @@ export interface ExportableShortUrl {
export interface ShortUrlsFilter {
excludeBots?: boolean;
excludeMaxVisitsReached?: boolean;
excludePastValidUntil?: boolean;
}

View file

@ -11,3 +11,4 @@ export const supportsNonOrphanVisits = serverMatchesMinVersion('3.0.0');
export const supportsAllTagsFiltering = supportsNonOrphanVisits;
export const supportsDomainVisits = serverMatchesMinVersion('3.1.0');
export const supportsExcludeBotsOnShortUrls = serverMatchesMinVersion('3.4.0');
export const supportsFilterDisabledUrls = supportsExcludeBotsOnShortUrls;

View file

@ -46,6 +46,28 @@ describe('ShlinkApiClient', () => {
expect.anything(),
);
});
it.each([
[{}, ''],
[{ excludeMaxVisitsReached: false }, ''],
[{ excludeMaxVisitsReached: true }, '?excludeMaxVisitsReached=true'],
[{ excludePastValidUntil: false }, ''],
[{ excludePastValidUntil: true }, '?excludePastValidUntil=true'],
[
{ excludePastValidUntil: true, excludeMaxVisitsReached: true },
'?excludeMaxVisitsReached=true&excludePastValidUntil=true',
],
])('parses disabled URLs params', async (params, expectedQuery) => {
fetchJson.mockResolvedValue({ data: expectedList });
const { listShortUrls } = buildApiClient();
await listShortUrls(params);
expect(fetchJson).toHaveBeenCalledWith(
expect.stringContaining(`/short-urls${expectedQuery}`),
expect.anything(),
);
});
});
describe('createShortUrl', () => {