mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-10 18:27:25 +03:00
Updated ShlinkApiClient to support more filtering options for short URLs list
This commit is contained in:
parent
d6d237fc52
commit
a3bd10bc82
5 changed files with 44 additions and 8 deletions
|
@ -24,9 +24,14 @@ import { HttpClient } from '../../common/services/HttpClient';
|
||||||
|
|
||||||
const buildShlinkBaseUrl = (url: string, version: 2 | 3) => `${url}/rest/v${version}`;
|
const buildShlinkBaseUrl = (url: string, version: 2 | 3) => `${url}/rest/v${version}`;
|
||||||
const rejectNilProps = reject(isNil);
|
const rejectNilProps = reject(isNil);
|
||||||
const normalizeOrderByInParams = (
|
const normalizeListParams = (
|
||||||
{ orderBy = {}, ...rest }: ShlinkShortUrlsListParams,
|
{ orderBy = {}, excludeMaxVisitsReached, excludePastValidUntil, ...rest }: ShlinkShortUrlsListParams,
|
||||||
): ShlinkShortUrlsListNormalizedParams => ({ ...rest, orderBy: orderToString(orderBy) });
|
): ShlinkShortUrlsListNormalizedParams => ({
|
||||||
|
...rest,
|
||||||
|
excludeMaxVisitsReached: excludeMaxVisitsReached === true ? 'true' : undefined,
|
||||||
|
excludePastValidUntil: excludePastValidUntil === true ? 'true' : undefined,
|
||||||
|
orderBy: orderToString(orderBy),
|
||||||
|
});
|
||||||
|
|
||||||
export class ShlinkApiClient {
|
export class ShlinkApiClient {
|
||||||
private apiVersion: 2 | 3;
|
private apiVersion: 2 | 3;
|
||||||
|
@ -40,7 +45,7 @@ export class ShlinkApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly listShortUrls = async (params: ShlinkShortUrlsListParams = {}): Promise<ShlinkShortUrlsResponse> =>
|
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);
|
.then(({ shortUrls }) => shortUrls);
|
||||||
|
|
||||||
public readonly createShortUrl = async (options: ShortUrlData): Promise<ShortUrl> => {
|
public readonly createShortUrl = async (options: ShortUrlData): Promise<ShortUrl> => {
|
||||||
|
|
|
@ -96,14 +96,20 @@ export type ShlinkShortUrlsOrder = Order<ShlinkShortUrlsOrderableFields>;
|
||||||
export interface ShlinkShortUrlsListParams {
|
export interface ShlinkShortUrlsListParams {
|
||||||
page?: string;
|
page?: string;
|
||||||
itemsPerPage?: number;
|
itemsPerPage?: number;
|
||||||
tags?: string[];
|
|
||||||
searchTerm?: string;
|
searchTerm?: string;
|
||||||
|
tags?: string[];
|
||||||
|
tagsMode?: TagsFilteringMode;
|
||||||
|
orderBy?: ShlinkShortUrlsOrder;
|
||||||
startDate?: string;
|
startDate?: string;
|
||||||
endDate?: string;
|
endDate?: string;
|
||||||
orderBy?: ShlinkShortUrlsOrder;
|
excludeMaxVisitsReached?: boolean;
|
||||||
tagsMode?: TagsFilteringMode;
|
excludePastValidUntil?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ShlinkShortUrlsListNormalizedParams extends Omit<ShlinkShortUrlsListParams, 'orderBy'> {
|
export interface ShlinkShortUrlsListNormalizedParams extends
|
||||||
|
Omit<ShlinkShortUrlsListParams, 'orderBy' | 'excludeMaxVisitsReached' | 'excludePastValidUntil'>
|
||||||
|
{
|
||||||
orderBy?: string;
|
orderBy?: string;
|
||||||
|
excludeMaxVisitsReached?: 'true';
|
||||||
|
excludePastValidUntil?: 'true';
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,4 +83,6 @@ export interface ExportableShortUrl {
|
||||||
|
|
||||||
export interface ShortUrlsFilter {
|
export interface ShortUrlsFilter {
|
||||||
excludeBots?: boolean;
|
excludeBots?: boolean;
|
||||||
|
excludeMaxVisitsReached?: boolean;
|
||||||
|
excludePastValidUntil?: boolean;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,3 +11,4 @@ export const supportsNonOrphanVisits = serverMatchesMinVersion('3.0.0');
|
||||||
export const supportsAllTagsFiltering = supportsNonOrphanVisits;
|
export const supportsAllTagsFiltering = supportsNonOrphanVisits;
|
||||||
export const supportsDomainVisits = serverMatchesMinVersion('3.1.0');
|
export const supportsDomainVisits = serverMatchesMinVersion('3.1.0');
|
||||||
export const supportsExcludeBotsOnShortUrls = serverMatchesMinVersion('3.4.0');
|
export const supportsExcludeBotsOnShortUrls = serverMatchesMinVersion('3.4.0');
|
||||||
|
export const supportsFilterDisabledUrls = supportsExcludeBotsOnShortUrls;
|
||||||
|
|
|
@ -46,6 +46,28 @@ describe('ShlinkApiClient', () => {
|
||||||
expect.anything(),
|
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', () => {
|
describe('createShortUrl', () => {
|
||||||
|
|
Loading…
Reference in a new issue