Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import {
Subscriptions,
Templates,
} from './endpoints';
import type { HeadersMap } from './http';
import { createHttpClient } from './http';
import type { ApiError } from './http';
import { createHttpClient, type HeadersMap } from './http';

const DEFAULT_BASE_URL = 'https://api.prezly.com';

Expand All @@ -42,6 +42,7 @@ export interface ClientOptions {
baseUrl?: string;
headers?: HeadersMap;
fetch?: Fetch;
onError?: (error: ApiError) => void;
}

export interface Client {
Expand Down Expand Up @@ -83,9 +84,10 @@ export function createClient({
baseUrl = DEFAULT_BASE_URL,
headers = {},
fetch,
onError,
}: ClientOptions): Client {
const api = createDeferredJobsApiClient(
createApiClient(createHttpClient({ fetch, baseUrl }), {
createApiClient(createHttpClient({ fetch, baseUrl, onError }), {
accessToken,
headers,
}),
Expand Down
93 changes: 63 additions & 30 deletions src/http/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Fetch } from '../api';

import type { ApiError } from './ApiError';
import { createRequest } from './createRequest';
import type { ApiResponse, Params, ParamsWithPayload } from './types';
import { Method } from './types';
Expand All @@ -12,9 +13,16 @@ export interface HttpClient {
delete<V = any>(url: string, params?: ParamsWithPayload): Promise<ApiResponse<V>>;
}

export function createHttpClient(options: { baseUrl?: string; fetch?: Fetch } = {}): HttpClient {
interface Options {
baseUrl?: string;
fetch?: Fetch;
onError?: (error: ApiError) => void;
}

export function createHttpClient(options: Options = {}): HttpClient {
const baseUrl = options.baseUrl ?? null;
const fetchImpl = options.fetch ?? fetch;
const onError = options.onError;

function resolveUrl(url: string) {
if (baseUrl) {
Expand All @@ -25,47 +33,72 @@ export function createHttpClient(options: { baseUrl?: string; fetch?: Fetch } =

return {
get(url, { headers, query } = {}) {
return createRequest(fetchImpl, resolveUrl(url), {
headers,
method: Method.GET,
query,
});
return createRequest(
fetchImpl,
resolveUrl(url),
{
headers,
method: Method.GET,
query,
},
onError,
);
},

post(url: string, { headers, payload, query } = {}) {
return createRequest(fetchImpl, resolveUrl(url), {
headers,
method: Method.POST,
payload,
query,
});
return createRequest(
fetchImpl,
resolveUrl(url),
{
headers,
method: Method.POST,
payload,
query,
},
onError,
);
},

put(url, { headers, payload, query } = {}) {
return createRequest(fetchImpl, resolveUrl(url), {
headers,
method: Method.PUT,
payload,
query,
});
return createRequest(
fetchImpl,
resolveUrl(url),
{
headers,
method: Method.PUT,
payload,
query,
},
onError,
);
},

patch(url: string, { headers, payload, query } = {}) {
return createRequest(fetchImpl, resolveUrl(url), {
headers,
method: Method.PATCH,
payload,
query,
});
return createRequest(
fetchImpl,
resolveUrl(url),
{
headers,
method: Method.PATCH,
payload,
query,
},
onError,
);
},

delete(url: string, { headers, payload, query } = {}) {
return createRequest(fetchImpl, resolveUrl(url), {
headers,
method: Method.DELETE,
payload,
query,
});
return createRequest(
fetchImpl,
resolveUrl(url),
{
headers,
method: Method.DELETE,
payload,
query,
},
onError,
);
},
};
}
9 changes: 7 additions & 2 deletions src/http/createRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NETWORK_PROBLEM_ERROR_MESSAGE,
} from './constants';
import { createUrlWithQuery } from './lib';
import type { Method, HeadersMap, ApiResponse } from './types';
import type { ApiResponse, HeadersMap, Method } from './types';
import { HttpCodes } from './types';

function extractHeaders(headers: Headers): HeadersMap {
Expand Down Expand Up @@ -59,6 +59,7 @@ export async function createRequest<P = any>(
payload?: object;
query?: object;
},
onError?: (error: ApiError) => void,
): Promise<ApiResponse<P>> {
const { headers, method, payload, query } = options;
try {
Expand Down Expand Up @@ -91,10 +92,14 @@ export async function createRequest<P = any>(
responsePayload = createFakeErrorPayload(response);
}

throw new ApiError({
const error = new ApiError({
payload: responsePayload,
...extractResponse(response),
});

onError?.(error);

throw error;
}

const responsePayload =
Expand Down