Allow heallth request to be performed for a different domain

This commit is contained in:
Alejandro Celaya 2023-07-24 10:47:40 +02:00
parent 2ac5236cc7
commit 3fe48779be
4 changed files with 62 additions and 52 deletions

View file

@ -3,6 +3,7 @@ import type { HttpClient } from '../../common/services/HttpClient';
import type { ShortUrl, ShortUrlData } from '../../shlink-web-component/short-urls/data'; import type { ShortUrl, ShortUrlData } from '../../shlink-web-component/short-urls/data';
import { orderToString } from '../../utils/helpers/ordering'; import { orderToString } from '../../utils/helpers/ordering';
import { stringifyQuery } from '../../utils/helpers/query'; import { stringifyQuery } from '../../utils/helpers/query';
import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
import type { OptionalString } from '../../utils/utils'; import type { OptionalString } from '../../utils/utils';
import type { import type {
ShlinkDomainRedirects, ShlinkDomainRedirects,
@ -23,7 +24,17 @@ import type {
} from '../types'; } from '../types';
import { isRegularNotFound, parseApiError } from '../utils'; import { isRegularNotFound, parseApiError } from '../utils';
const buildShlinkBaseUrl = (url: string, version: 2 | 3) => `${url}/rest/v${version}`; type ApiVersion = 2 | 3;
type RequestOptions = {
url: string;
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
query?: object;
body?: object;
domain?: string;
};
const buildShlinkBaseUrl = (url: string, version: ApiVersion) => `${url}/rest/v${version}`;
const rejectNilProps = reject(isNil); const rejectNilProps = reject(isNil);
const normalizeListParams = ( const normalizeListParams = (
{ orderBy = {}, excludeMaxVisitsReached, excludePastValidUntil, ...rest }: ShlinkShortUrlsListParams, { orderBy = {}, excludeMaxVisitsReached, excludePastValidUntil, ...rest }: ShlinkShortUrlsListParams,
@ -35,101 +46,115 @@ const normalizeListParams = (
}); });
export class ShlinkApiClient { export class ShlinkApiClient {
private apiVersion: 2 | 3; private apiVersion: ApiVersion;
public constructor( public constructor(
private readonly httpClient: HttpClient, private readonly httpClient: HttpClient,
private readonly baseUrl: string, public readonly baseUrl: string,
private readonly apiKey: string, public readonly apiKey: string,
) { ) {
this.apiVersion = 3; this.apiVersion = 3;
} }
public readonly listShortUrls = async (params: ShlinkShortUrlsListParams = {}): Promise<ShlinkShortUrlsResponse> => public readonly listShortUrls = async (params: ShlinkShortUrlsListParams = {}): Promise<ShlinkShortUrlsResponse> =>
this.performRequest<{ shortUrls: ShlinkShortUrlsResponse }>('/short-urls', 'GET', normalizeListParams(params)) this.performRequest<{ shortUrls: ShlinkShortUrlsResponse }>(
.then(({ shortUrls }) => shortUrls); { url: '/short-urls', query: normalizeListParams(params) },
).then(({ shortUrls }) => shortUrls);
public readonly createShortUrl = async (options: ShortUrlData): Promise<ShortUrl> => { public readonly createShortUrl = async (options: ShortUrlData): Promise<ShortUrl> => {
const filteredOptions = reject((value) => isEmpty(value) || isNil(value), options as any); const body = reject((value) => isEmpty(value) || isNil(value), options as any);
return this.performRequest<ShortUrl>('/short-urls', 'POST', {}, filteredOptions); return this.performRequest<ShortUrl>({ url: '/short-urls', method: 'POST', body });
}; };
public readonly getShortUrlVisits = async (shortCode: string, query?: ShlinkVisitsParams): Promise<ShlinkVisits> => public readonly getShortUrlVisits = async (shortCode: string, query?: ShlinkVisitsParams): Promise<ShlinkVisits> =>
this.performRequest<{ visits: ShlinkVisits }>(`/short-urls/${shortCode}/visits`, 'GET', query) this.performRequest<{ visits: ShlinkVisits }>({ url: `/short-urls/${shortCode}/visits`, query })
.then(({ visits }) => visits); .then(({ visits }) => visits);
public readonly getTagVisits = async (tag: string, query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> => public readonly getTagVisits = async (tag: string, query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> =>
this.performRequest<{ visits: ShlinkVisits }>(`/tags/${tag}/visits`, 'GET', query).then(({ visits }) => visits); this.performRequest<{ visits: ShlinkVisits }>({ url: `/tags/${tag}/visits`, query }).then(({ visits }) => visits);
public readonly getDomainVisits = async (domain: string, query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> => public readonly getDomainVisits = async (domain: string, query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> =>
this.performRequest<{ visits: ShlinkVisits }>(`/domains/${domain}/visits`, 'GET', query).then(({ visits }) => visits); this.performRequest<{ visits: ShlinkVisits }>({ url: `/domains/${domain}/visits`, query }).then(({ visits }) => visits);
public readonly getOrphanVisits = async (query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> => public readonly getOrphanVisits = async (query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> =>
this.performRequest<{ visits: ShlinkVisits }>('/visits/orphan', 'GET', query).then(({ visits }) => visits); this.performRequest<{ visits: ShlinkVisits }>({ url: '/visits/orphan', query }).then(({ visits }) => visits);
public readonly getNonOrphanVisits = async (query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> => public readonly getNonOrphanVisits = async (query?: Omit<ShlinkVisitsParams, 'domain'>): Promise<ShlinkVisits> =>
this.performRequest<{ visits: ShlinkVisits }>('/visits/non-orphan', 'GET', query).then(({ visits }) => visits); this.performRequest<{ visits: ShlinkVisits }>({ url: '/visits/non-orphan', query }).then(({ visits }) => visits);
public readonly getVisitsOverview = async (): Promise<ShlinkVisitsOverview> => public readonly getVisitsOverview = async (): Promise<ShlinkVisitsOverview> =>
this.performRequest<{ visits: ShlinkVisitsOverview }>('/visits').then(({ visits }) => visits); this.performRequest<{ visits: ShlinkVisitsOverview }>({ url: '/visits' }).then(({ visits }) => visits);
public readonly getShortUrl = async (shortCode: string, domain?: OptionalString): Promise<ShortUrl> => public readonly getShortUrl = async (shortCode: string, domain?: OptionalString): Promise<ShortUrl> =>
this.performRequest<ShortUrl>(`/short-urls/${shortCode}`, 'GET', { domain }); this.performRequest<ShortUrl>({ url: `/short-urls/${shortCode}`, query: { domain } });
public readonly deleteShortUrl = async (shortCode: string, domain?: OptionalString): Promise<void> => public readonly deleteShortUrl = async (shortCode: string, domain?: OptionalString): Promise<void> =>
this.performEmptyRequest(`/short-urls/${shortCode}`, 'DELETE', { domain }); this.performEmptyRequest({ url: `/short-urls/${shortCode}`, method: 'DELETE', query: { domain } });
public readonly updateShortUrl = async ( public readonly updateShortUrl = async (
shortCode: string, shortCode: string,
domain: OptionalString, domain: OptionalString,
edit: ShlinkShortUrlData, body: ShlinkShortUrlData,
): Promise<ShortUrl> => ): Promise<ShortUrl> =>
this.performRequest<ShortUrl>(`/short-urls/${shortCode}`, 'PATCH', { domain }, edit); this.performRequest<ShortUrl>({ url: `/short-urls/${shortCode}`, method: 'PATCH', query: { domain }, body });
public readonly listTags = async (): Promise<ShlinkTags> => public readonly listTags = async (): Promise<ShlinkTags> =>
this.performRequest<{ tags: ShlinkTagsResponse }>('/tags', 'GET', { withStats: 'true' }) this.performRequest<{ tags: ShlinkTagsResponse }>({ url: '/tags', query: { withStats: 'true' } })
.then(({ tags }) => tags) .then(({ tags }) => tags)
.then(({ data, stats }) => ({ tags: data, stats })); .then(({ data, stats }) => ({ tags: data, stats }));
public readonly tagsStats = async (): Promise<ShlinkTags> => public readonly tagsStats = async (): Promise<ShlinkTags> =>
this.performRequest<{ tags: ShlinkTagsStatsResponse }>('/tags/stats', 'GET') this.performRequest<{ tags: ShlinkTagsStatsResponse }>({ url: '/tags/stats' })
.then(({ tags }) => tags) .then(({ tags }) => tags)
.then(({ data }) => ({ tags: data.map(({ tag }) => tag), stats: data })); .then(({ data }) => ({ tags: data.map(({ tag }) => tag), stats: data }));
public readonly deleteTags = async (tags: string[]): Promise<{ tags: string[] }> => public readonly deleteTags = async (tags: string[]): Promise<{ tags: string[] }> =>
this.performEmptyRequest('/tags', 'DELETE', { tags }).then(() => ({ tags })); this.performEmptyRequest({ url: '/tags', method: 'DELETE', body: { tags } }).then(() => ({ tags }));
public readonly editTag = async (oldName: string, newName: string): Promise<{ oldName: string; newName: string }> => public readonly editTag = async (oldName: string, newName: string): Promise<{ oldName: string; newName: string }> =>
this.performEmptyRequest('/tags', 'PUT', {}, { oldName, newName }).then(() => ({ oldName, newName })); this.performEmptyRequest({
url: '/tags',
method: 'PUT',
body: { oldName, newName },
}).then(() => ({ oldName, newName }));
public readonly health = async (): Promise<ShlinkHealth> => this.performRequest<ShlinkHealth>('/health', 'GET'); public readonly health = async (domain?: string): Promise<ShlinkHealth> => this.performRequest<ShlinkHealth>(
{ url: '/health', domain },
);
public readonly mercureInfo = async (): Promise<ShlinkMercureInfo> => public readonly mercureInfo = async (): Promise<ShlinkMercureInfo> =>
this.performRequest<ShlinkMercureInfo>('/mercure-info', 'GET'); this.performRequest<ShlinkMercureInfo>({ url: '/mercure-info' });
public readonly listDomains = async (): Promise<ShlinkDomainsResponse> => public readonly listDomains = async (): Promise<ShlinkDomainsResponse> =>
this.performRequest<{ domains: ShlinkDomainsResponse }>('/domains').then(({ domains }) => domains); this.performRequest<{ domains: ShlinkDomainsResponse }>({ url: '/domains' }).then(({ domains }) => domains);
public readonly editDomainRedirects = async ( public readonly editDomainRedirects = async (
domainRedirects: ShlinkEditDomainRedirects, domainRedirects: ShlinkEditDomainRedirects,
): Promise<ShlinkDomainRedirects> => ): Promise<ShlinkDomainRedirects> =>
this.performRequest<ShlinkDomainRedirects>('/domains/redirects', 'PATCH', {}, domainRedirects); this.performRequest<ShlinkDomainRedirects>({ url: '/domains/redirects', method: 'PATCH', body: domainRedirects });
private readonly performRequest = async <T>(url: string, method = 'GET', query = {}, body?: object): Promise<T> => private readonly performRequest = async <T>(requestOptions: RequestOptions): Promise<T> =>
this.httpClient.fetchJson<T>(...this.toFetchParams(url, method, query, body)).catch( this.httpClient.fetchJson<T>(...this.toFetchParams(requestOptions)).catch(
this.handleFetchError(() => this.httpClient.fetchJson<T>(...this.toFetchParams(url, method, query, body))), this.handleFetchError(() => this.httpClient.fetchJson<T>(...this.toFetchParams(requestOptions))),
); );
private readonly performEmptyRequest = async (url: string, method = 'GET', query = {}, body?: object): Promise<void> => private readonly performEmptyRequest = async (requestOptions: RequestOptions): Promise<void> =>
this.httpClient.fetchEmpty(...this.toFetchParams(url, method, query, body)).catch( this.httpClient.fetchEmpty(...this.toFetchParams(requestOptions)).catch(
this.handleFetchError(() => this.httpClient.fetchEmpty(...this.toFetchParams(url, method, query, body))), this.handleFetchError(() => this.httpClient.fetchEmpty(...this.toFetchParams(requestOptions))),
); );
private readonly toFetchParams = (url: string, method: string, query = {}, body?: object): [string, RequestInit] => { private readonly toFetchParams = ({
url,
method = 'GET',
query = {},
body,
domain,
}: RequestOptions): [string, RequestInit] => {
const normalizedQuery = stringifyQuery(rejectNilProps(query)); const normalizedQuery = stringifyQuery(rejectNilProps(query));
const stringifiedQuery = isEmpty(normalizedQuery) ? '' : `?${normalizedQuery}`; const stringifiedQuery = isEmpty(normalizedQuery) ? '' : `?${normalizedQuery}`;
const baseUrl = domain ? replaceAuthorityFromUri(this.baseUrl, domain) : this.baseUrl;
return [`${buildShlinkBaseUrl(this.baseUrl, this.apiVersion)}${url}${stringifiedQuery}`, { return [`${buildShlinkBaseUrl(baseUrl, this.apiVersion)}${url}${stringifiedQuery}`, {
method, method,
body: body && JSON.stringify(body), body: body && JSON.stringify(body),
headers: { 'X-Api-Key': this.apiKey }, headers: { 'X-Api-Key': this.apiKey },

View file

@ -4,7 +4,6 @@ import type { ShlinkApiClient } from '../../../api/services/ShlinkApiClient';
import type { ShlinkDomainRedirects } from '../../../api/types'; import type { ShlinkDomainRedirects } from '../../../api/types';
import type { ProblemDetailsError } from '../../../api/types/errors'; import type { ProblemDetailsError } from '../../../api/types/errors';
import { parseApiError } from '../../../api/utils'; import { parseApiError } from '../../../api/utils';
import { hasServerData } from '../../../servers/data';
import { createAsyncThunk } from '../../../utils/helpers/redux'; import { createAsyncThunk } from '../../../utils/helpers/redux';
import type { Domain, DomainStatus } from '../data'; import type { Domain, DomainStatus } from '../data';
import type { EditDomainRedirects } from './domainRedirects'; import type { EditDomainRedirects } from './domainRedirects';
@ -58,22 +57,9 @@ export const domainsListReducerCreator = (
const checkDomainHealth = createAsyncThunk( const checkDomainHealth = createAsyncThunk(
`${REDUCER_PREFIX}/checkDomainHealth`, `${REDUCER_PREFIX}/checkDomainHealth`,
async (domain: string, { getState }): Promise<ValidateDomain> => { async (domain: string): Promise<ValidateDomain> => {
const { selectedServer } = getState();
if (!hasServerData(selectedServer)) {
return { domain, status: 'invalid' };
}
try { try {
// FIXME This should call different domains const { status } = await apiClient.health(domain);
// const { url, ...rest } = selectedServer;
// const { health } = buildShlinkApiClient({
// ...rest,
// url: replaceAuthorityFromUri(url, domain),
// });
const { status } = await apiClient.health();
return { domain, status: status === 'pass' ? 'valid' : 'invalid' }; return { domain, status: status === 'pass' ? 'valid' : 'invalid' };
} catch (e) { } catch (e) {
return { domain, status: 'invalid' }; return { domain, status: 'invalid' };

View file

@ -31,7 +31,7 @@ const initialState: ShortUrlEdition = {
export const editShortUrl = (apiClient: ShlinkApiClient) => createAsyncThunk( export const editShortUrl = (apiClient: ShlinkApiClient) => createAsyncThunk(
`${REDUCER_PREFIX}/editShortUrl`, `${REDUCER_PREFIX}/editShortUrl`,
({ shortCode, domain, data }: EditShortUrl): Promise<ShortUrl> => ({ shortCode, domain, data }: EditShortUrl): Promise<ShortUrl> =>
apiClient.updateShortUrl(shortCode, domain, data as any) // FIXME parse dates apiClient.updateShortUrl(shortCode, domain, data as any) // TODO parse dates
, ,
); );

View file

@ -1,5 +1,4 @@
import type { ShlinkApiClient } from '../../../api/services/ShlinkApiClient'; import type { ShlinkApiClient } from '../../../api/services/ShlinkApiClient';
import type { ShlinkApiClientBuilder } from '../../../api/services/ShlinkApiClientBuilder';
import { isBetween } from '../../../utils/helpers/date'; import { isBetween } from '../../../utils/helpers/date';
import { createVisitsAsyncThunk, createVisitsReducer, lastVisitLoaderForLoader } from './common'; import { createVisitsAsyncThunk, createVisitsReducer, lastVisitLoaderForLoader } from './common';
import type { VisitsInfo } from './types'; import type { VisitsInfo } from './types';