Skip to content

FetchApiManager

katanakit-js / FetchApiManager

Defined in: core/services/http.service.ts:111

FetchApiManager - Singleton para el registro de APIs y peticiones HTTP.

Aplica el patrón Singleton: una única instancia mantiene el registro de APIs (ApisConfig) y centraliza la construcción de URLs y las peticiones fetch, devolviendo siempre Safe Results (nunca lanza en rutas async).

const fetchApi = FetchApiManager.getInstance();
fetchApi.useInit({ pokeapi: { baseUri: "https://pokeapi.co/api/v2", endpoints: { byId: "/pokemon/:id/" } } });
const result = await fetchApi.useFetch("pokeapi", "byId", { params: { id: 25 } });
useAddRequestInterceptor(interceptor): () => void;

Defined in: core/services/http.service.ts:185

Registers a request interceptor, which runs before fetch on every request. Interceptors run sequentially in registration order and can mutate url or init. The returned function unregisters this interceptor.

Parameter Type Description
interceptor RequestInterceptor The function to run before the request is sent.

A function that unregisters the interceptor.

() => void


useBuildUrl(
apiName,
endpointName,
options?
): string;

Defined in: core/services/http.service.ts:217

Returns the URL of a registered endpoint without calling fetch.

It builds the same URL useFetch would request - same registry, same :param substitution, same query merge with defaultQueryParams, same http(s) guard - then stops and hands you the string.

Parameter Type Description
apiName string Registered API key (from defineApiConfig).
endpointName string Endpoint key inside that API.
options? UrlOptions params fills :placeholders, query becomes the search string.

string

The fully built URL.

Unknown API, unknown endpoint, or a scheme that is not http(s).

FetchApiManager.getInstance().useBuildUrl("pokeapi", "pokemonById", { params: { id: 25 } });
// → "https://pokeapi.co/api/v2/pokemon/25"

Synchronous, so it throws instead of returning a Safe Result - Safe Result covers fallible async work, and this never leaves the process.


useDelete<T>(
apiName,
endpointName,
urlOptions?
): Promise<FetchResult<T>>;

Defined in: core/services/http.service.ts:496

DELETE helper over a registered API endpoint.

Type Parameter Default type
T unknown
Parameter Type Description
apiName string The registered API key.
endpointName string The endpoint key within the API.
urlOptions? UrlOptions Optional URL building options.

Promise<FetchResult<T>>

A FetchResult with data or error.


useFetch<T>(
apiName,
endpointName,
options?
): Promise<FetchResult<T>>;

Defined in: core/services/http.service.ts:280

Fetches a registered endpoint and returns a Safe Result.

URL options sit at the top level, next to method/headers/body - no nesting, same shape as useGet and Nuxt’s useFetch.

Type Parameter Default type
T unknown
Parameter Type Description
apiName string The registered API key.
endpointName string The endpoint key within the API.
options? FetchOptions<T> Fetch options: params, query, transform and any RequestInit field.

Promise<FetchResult<T>>

A FetchResult with data or error. Never throws - a failing transform comes back as an error result too.

const result = await FetchApiManager.getInstance().useFetch<{ results: unknown[] }>(
"pokeapi",
"pokemons",
{ method: "GET", query: { limit: 2 } },
);
if (result.ok) console.log(result.data.results);

useGet<T>(
apiName,
endpointName,
urlOptions?
): Promise<FetchResult<T>>;

Defined in: core/services/http.service.ts:411

GET helper over a registered API endpoint.

Type Parameter Default type
T unknown
Parameter Type Description
apiName string The registered API key.
endpointName string The endpoint key within the API.
urlOptions? UrlOptions Optional URL building options.

Promise<FetchResult<T>>

A FetchResult with data or error.


useGetApis(): ApisConfig;

Defined in: core/services/http.service.ts:173

Returns a shallow copy of the registered APIs (safe to mutate locally).

ApisConfig

A shallow copy of the current API registry.


useInit(apisConfig): void;

Defined in: core/services/http.service.ts:164

Registers (merges) API definitions into the client registry.

Parameter Type Description
apisConfig ApisConfig The API definitions to merge into the registry.

void

FetchApiManager.getInstance().useInit({
pokeapi: { baseUri: "https://pokeapi.co/api/v2", endpoints: { pokemonById: "/pokemon/:id/" } },
});

usePatch<T>(
apiName,
endpointName,
body?,
urlOptions?
): Promise<FetchResult<T>>;

Defined in: core/services/http.service.ts:475

PATCH helper over a registered API endpoint.

Type Parameter Default type
T unknown
Parameter Type Description
apiName string The registered API key.
endpointName string The endpoint key within the API.
body? unknown Optional request body (auto-serialized to JSON unless raw).
urlOptions? UrlOptions Optional URL building options.

Promise<FetchResult<T>>

A FetchResult with data or error.


usePost<T>(
apiName,
endpointName,
body?,
urlOptions?
): Promise<FetchResult<T>>;

Defined in: core/services/http.service.ts:431

POST helper over a registered API endpoint.

Type Parameter Default type
T unknown
Parameter Type Description
apiName string The registered API key.
endpointName string The endpoint key within the API.
body? unknown Optional request body (auto-serialized to JSON unless raw).
urlOptions? UrlOptions Optional URL building options.

Promise<FetchResult<T>>

A FetchResult with data or error.


usePut<T>(
apiName,
endpointName,
body?,
urlOptions?
): Promise<FetchResult<T>>;

Defined in: core/services/http.service.ts:453

PUT helper over a registered API endpoint.

Type Parameter Default type
T unknown
Parameter Type Description
apiName string The registered API key.
endpointName string The endpoint key within the API.
body? unknown Optional request body (auto-serialized to JSON unless raw).
urlOptions? UrlOptions Optional URL building options.

Promise<FetchResult<T>>

A FetchResult with data or error.


static getInstance(): FetchApiManager;

Defined in: core/services/http.service.ts:130

Obtiene la instancia única del FetchApiManager (Singleton).

FetchApiManager

La instancia única de FetchApiManager.