From 17b278bc3a74f2a8be4e8ef14de24a633af50c16 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Mar 2024 13:03:11 +0100 Subject: [PATCH] Allow type param to be passed to getOrphanVisits() --- CHANGELOG.md | 4 +++- src/api-contract/ShlinkApiClient.ts | 3 ++- src/api-contract/types.ts | 5 +++++ src/api/ShlinkApiClient.ts | 23 +++++++++++------------ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a76ab..fc5f483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] ### Added -* *Nothing* +* [#72](https://github.com/shlinkio/shlink-js-sdk/issues/72) Add support for Shlink 4.0.0 + + * Add `type` param to `ShlinkApiClient.getOrphanVisits`. ### Changed * Update dependencies. diff --git a/src/api-contract/ShlinkApiClient.ts b/src/api-contract/ShlinkApiClient.ts index 4b7a5e0..bf5d13a 100644 --- a/src/api-contract/ShlinkApiClient.ts +++ b/src/api-contract/ShlinkApiClient.ts @@ -7,6 +7,7 @@ import type { ShlinkEditShortUrlData, ShlinkHealth, ShlinkMercureInfo, + ShlinkOrphanVisitsParams, ShlinkShortUrl, ShlinkShortUrlsList, ShlinkShortUrlsListParams, @@ -45,7 +46,7 @@ export type ShlinkApiClient = { getDomainVisits(domain: string, params?: ShlinkVisitsParams): Promise; - getOrphanVisits(params?: ShlinkVisitsParams): Promise; + getOrphanVisits(params?: ShlinkOrphanVisitsParams): Promise; getNonOrphanVisits(params?: ShlinkVisitsParams): Promise; diff --git a/src/api-contract/types.ts b/src/api-contract/types.ts index c338ff7..bd50b03 100644 --- a/src/api-contract/types.ts +++ b/src/api-contract/types.ts @@ -179,6 +179,11 @@ export type ShlinkShortUrlVisitsParams = ShlinkVisitsParams & { domain?: string | null; }; +export type ShlinkOrphanVisitsParams = ShlinkVisitsParams & { + /** Ignored by Shlink older than v4.0.0 */ + type?: ShlinkOrphanVisitType; +}; + export type ShlinkDomainRedirects = { baseUrlRedirect: string | null; regular404Redirect: string | null; diff --git a/src/api/ShlinkApiClient.ts b/src/api/ShlinkApiClient.ts index 2a640ce..5f77646 100644 --- a/src/api/ShlinkApiClient.ts +++ b/src/api/ShlinkApiClient.ts @@ -8,6 +8,7 @@ import type { ShlinkEditShortUrlData, ShlinkHealth, ShlinkMercureInfo, + ShlinkOrphanVisitsParams, ShlinkShortUrl, ShlinkShortUrlsList, ShlinkShortUrlsListParams, @@ -94,29 +95,27 @@ export class ShlinkApiClient implements BaseShlinkApiClient { } public async getShortUrlVisits(shortCode: string, params?: ShlinkShortUrlVisitsParams): Promise { - return this.performRequest<{ visits: ShlinkVisitsList }>({ url: `/short-urls/${shortCode}/visits`, query: params }) - .then(({ visits }) => visits); + return this.performVisitsRequest({ url: `/short-urls/${shortCode}/visits`, query: params }); } public async getTagVisits(tag: string, params?: ShlinkVisitsParams): Promise { - return this.performRequest<{ visits: ShlinkVisitsList }>({ url: `/tags/${tag}/visits`, query: params }) - .then(({ visits }) => visits); + return this.performVisitsRequest({ url: `/tags/${tag}/visits`, query: params }); } public async getDomainVisits(domain: string, params?: ShlinkVisitsParams): Promise { - return this.performRequest<{ visits: ShlinkVisitsList }>({ url: `/domains/${domain}/visits`, query: params }) - .then(({ visits }) => visits); + return this.performVisitsRequest({ url: `/domains/${domain}/visits`, query: params }); } - public async getOrphanVisits(params?: ShlinkVisitsParams): Promise { - return this.performRequest<{ visits: ShlinkVisitsList }>({ url: '/visits/orphan', query: params }).then( - ({ visits }) => visits, - ); + public async getOrphanVisits(params?: ShlinkOrphanVisitsParams): Promise { + return this.performVisitsRequest({ url: '/visits/orphan', query: params }); } public async getNonOrphanVisits(params?: ShlinkVisitsParams): Promise { - return this.performRequest<{ visits: ShlinkVisitsList }>({ url: '/visits/non-orphan', query: params }) - .then(({ visits }) => visits); + return this.performVisitsRequest({ url: '/visits/non-orphan', query: params }); + } + + private async performVisitsRequest(options: ShlinkRequestOptions): Promise { + return this.performRequest<{ visits: ShlinkVisitsList }>(options).then(({ visits }) => visits); } public async deleteShortUrlVisits(shortCode: string, domain?: string | null): Promise {