Skip to content

Commit 752bf6b

Browse files
committed
💥 added Visits stats, docs, and releasing first full coverage version
1 parent e60f3f3 commit 752bf6b

File tree

7 files changed

+121
-2
lines changed

7 files changed

+121
-2
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,53 @@ interface QRCodeOptions {
300300
#### Response
301301

302302
An objet containing the MIME type of the QR Code, and the data in the requested format
303+
### client#countVisits()
304+
305+
Get general visits stats not linked to one specific short URL.
306+
307+
#### Response
308+
309+
Returns an object containing general data about link statistics
310+
311+
### client#getLinkVisits(shortCode, options)
312+
313+
Get the list of visits on the short URL behind provided short code.
314+
315+
#### Parameters
316+
317+
`shortCode`: The short code to which you want to get the visits stats.
318+
319+
`options`:
320+
```ts
321+
interface PaginationOptions {
322+
startDate?: string; // ISO-8601 date format
323+
endDate?: string; // ISO-8601 date format
324+
page?: number; // Page number, defaults to 1
325+
itemsPerPage?: number; // Items per page, defaults to all results
326+
}
327+
```
328+
329+
#### Response
330+
331+
A paginated response containing visits stats for the given short code.
332+
### client#getTagVisits(tag, options)
333+
334+
Get the list of visits on any short URL which is tagged with provided tag.
335+
336+
#### Parameters
337+
338+
`shortCode`: The short code to which you want to get the visits stats.
339+
340+
`options`:
341+
```ts
342+
interface PaginationOptions {
343+
startDate?: string; // ISO-8601 date format
344+
endDate?: string; // ISO-8601 date format
345+
page?: number; // Page number, defaults to 1
346+
itemsPerPage?: number; // Items per page, defaults to all results
347+
}
348+
```
349+
350+
#### Response
351+
352+
A paginated response containing visits stats for the given tag.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "shlink-client",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"author": {
55
"email": "[email protected]",
66
"name": "Neil Richter",

src/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const ROUTES = {
1212
/* Tags */
1313
TAGS: '/rest/v{:version}/tags',
1414

15+
/* Visits */
16+
VISITS: '/rest/v{:version}/visits',
17+
LINK_VISITS: '/rest/v{:version}/short-urls/{:shortCode}/visits',
18+
TAG_VISITS: '/rest/v{:version}/tags/{:tag}/visits',
19+
1520
/* Domains */
1621
DOMAINS: '/rest/v{:version}/domains',
1722

src/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
QRCodeOptions,
1111
QRCodeDefaultOptions,
1212
} from './types/endpoints/short-urls';
13-
import { DomainsListItem, DomainsListResponse, ListTagsOptions, TagsListResponse } from './types/endpoints';
13+
import { DomainsListItem, DomainsListResponse, ListTagsOptions, PaginationOptions, TagsListResponse, VisitData, VisitsPaginationOptions, VisitsStats } from './types/endpoints';
14+
import { Paginated } from './types/endpoints/utils';
1415
export class ShlinkClient {
1516
private url: string;
1617
private token: string;
@@ -87,6 +88,25 @@ export class ShlinkClient {
8788
.then(({ data }) => data);
8889
}
8990

91+
public countVisits(): Promise<VisitsStats> {
92+
return this.client.get<{ visits: VisitsStats }>({ url: 'VISITS' })
93+
.then(({ data }) => data.visits);
94+
}
95+
96+
public getLinkVisits(shortCode: string, options: VisitsPaginationOptions = {}): Promise<Paginated<VisitData>> {
97+
return this.client.get<{ visits: Paginated<VisitData> }>({ url: 'LINK_VISITS', params: { shortCode }}, {
98+
params: options,
99+
})
100+
.then(({ data }) => data.visits);
101+
}
102+
103+
public getTagVisits(tag: string, options: PaginationOptions = {}): Promise<Paginated<VisitData>> {
104+
return this.client.get<{ visits: Paginated<VisitData> }>({ url: 'TAG_VISITS', params: { tag }}, {
105+
params: options,
106+
})
107+
.then(({ data }) => data.visits);
108+
}
109+
90110
public listTags(options: ListTagsOptions<false>): Promise<TagsListResponse<ListTagsOptions<false>>>
91111
public listTags(options: ListTagsOptions<true>): Promise<TagsListResponse<ListTagsOptions<true>>>
92112
public listTags(options: ListTagsOptions = { withStats: false }): Promise<TagsListResponse<ListTagsOptions>> {

src/types/endpoints/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './monitoring';
22
export * from './tags';
33
export * from './domains';
4+
export * from './visits';
45

56
export interface GenericError {
67
type: string;

src/types/endpoints/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface Paginated<T> {
2+
data: T[];
3+
pagination: Pagination
4+
}
5+
6+
export interface Pagination {
7+
currentPAge: number;
8+
pagesCount: number;
9+
itemsPerPage: number;
10+
itemsInCurrentPage: number;
11+
totalItems: number;
12+
}

src/types/endpoints/visits.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export interface VisitsStats {
2+
visitsCounts: number;
3+
}
4+
5+
export interface VisitLocation {
6+
cityName: string;
7+
countryCode: string;
8+
countryName: string;
9+
latitude: number;
10+
longitude: number;
11+
regionName: string;
12+
timezone: string;
13+
}
14+
15+
export interface VisitData {
16+
referer: string;
17+
date: string;
18+
userAgent: string;
19+
visitLocation: VisitLocation | null;
20+
}
21+
22+
export type VisitsPaginationOptions = {
23+
domain?: string;
24+
} & PaginationOptions
25+
26+
export interface PaginationOptions {
27+
startDate?: string;
28+
endDate?: string;
29+
page?: number;
30+
itemsPerPage?: number;
31+
}

0 commit comments

Comments
 (0)