Skip to content

Commit

Permalink
Add support for short URL redirect conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Mar 5, 2024
1 parent 0713535 commit edf3709
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added
* [#72](https://github.com/shlinkio/shlink-js-sdk/issues/72) Add support for Shlink 4.0.0

* Add support to list and set short URL redirect conditions.
* Add `type` param to `ShlinkApiClient.getOrphanVisits`.

### Changed
Expand Down
12 changes: 12 additions & 0 deletions src/api-contract/ShlinkApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
ShlinkHealth,
ShlinkMercureInfo,
ShlinkOrphanVisitsParams,
ShlinkRedirectRulesList,
ShlinkSetRedirectRulesData,
ShlinkShortUrl,
ShlinkShortUrlsList,
ShlinkShortUrlsListParams,
Expand Down Expand Up @@ -36,6 +38,16 @@ export type ShlinkApiClient = {
data: ShlinkEditShortUrlData,
): Promise<ShlinkShortUrl>;

// Short URL redirect rules

getShortUrlRedirectRules(shortCode: string, domain?: string | null): Promise<ShlinkRedirectRulesList>;

setShortUrlRedirectRules(
shortCode: string,
domain: string | null | undefined,
data: ShlinkSetRedirectRulesData,
): Promise<ShlinkRedirectRulesList>;

// Visits

getVisitsOverview(): Promise<ShlinkVisitsOverview>;
Expand Down
26 changes: 26 additions & 0 deletions src/api-contract/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,29 @@ export type ShlinkShortUrlsListParams = {
excludeMaxVisitsReached?: boolean;
excludePastValidUntil?: boolean;
};

export type ShlinkRedirectConditionType = 'device' | 'language' | 'query-param';

export type ShlinkRedirectCondition = {
type: ShlinkRedirectConditionType;
matchKey: string | null;
matchValue: string;
};

export type ShlinkRedirectRuleData = {
longUrl: string;
conditions: ShlinkRedirectCondition[];
};

export type ShlinkRedirectRule = ShlinkRedirectRuleData & {
priority: number;
};

export type ShlinkRedirectRulesList = {
defaultLongUrl: string;
redirectRules: ShlinkRedirectRule[];
};

export type ShlinkSetRedirectRulesData = {
redirectRules: ShlinkRedirectRuleData[];
};
20 changes: 20 additions & 0 deletions src/api/ShlinkApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
ShlinkHealth,
ShlinkMercureInfo,
ShlinkOrphanVisitsParams,
ShlinkRedirectRulesList,
ShlinkSetRedirectRulesData,
ShlinkShortUrl,
ShlinkShortUrlsList,
ShlinkShortUrlsListParams,
Expand Down Expand Up @@ -88,6 +90,24 @@ export class ShlinkApiClient implements BaseShlinkApiClient {
);
}

// Short URL redirect rules

public async getShortUrlRedirectRules(shortCode: string, domain?: string | null): Promise<ShlinkRedirectRulesList> {
return this.performRequest<ShlinkRedirectRulesList>(
{ url: `/short-urls/${shortCode}/redirect-rules`, method: 'GET', query: { domain } },
);
}

Check warning on line 99 in src/api/ShlinkApiClient.ts

View check run for this annotation

Codecov / codecov/patch

src/api/ShlinkApiClient.ts#L96-L99

Added lines #L96 - L99 were not covered by tests

public async setShortUrlRedirectRules(
shortCode: string,
domain: string | null | undefined,
data: ShlinkSetRedirectRulesData,
): Promise<ShlinkRedirectRulesList> {
return this.performRequest<ShlinkRedirectRulesList>(
{ url: `/short-urls/${shortCode}/redirect-rules`, method: 'POST', query: { domain }, body: data },
);
}

Check warning on line 109 in src/api/ShlinkApiClient.ts

View check run for this annotation

Codecov / codecov/patch

src/api/ShlinkApiClient.ts#L102-L109

Added lines #L102 - L109 were not covered by tests

// Visits

public async getVisitsOverview(): Promise<ShlinkVisitsOverview> {
Expand Down

0 comments on commit edf3709

Please sign in to comment.