Skip to content
0

katanakit-js / FetchApiManager

Class: FetchApiManager ​

Defined in: src/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).

Example ​

ts
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 } });

Methods ​

useAddRequestInterceptor() ​

ts
useAddRequestInterceptor(interceptor): () => void;

Defined in: src/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.

Parameters ​

ParameterTypeDescription
interceptorRequestInterceptorThe function to run before the request is sent.

Returns ​

A function that unregisters the interceptor.

() => void


useBuildUrl() ​

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

Defined in: src/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.

Parameters ​

ParameterTypeDescription
apiNamestringRegistered API key (from defineApiConfig).
endpointNamestringEndpoint key inside that API.
optionsUrlOptionsparams fills :placeholders, query becomes the search string.

Returns ​

string

The fully built URL.

Throws ​

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

Example ​

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

Remarks ​

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


useDelete() ​

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

Defined in: src/core/services/http.service.ts:504

DELETE helper over a registered API endpoint.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterTypeDescription
apiNamestringThe registered API key.
endpointNamestringThe endpoint key within the API.
urlOptions?UrlOptionsOptional URL building options.

Returns ​

Promise<FetchResult<T>>

A FetchResult with data or error.


useFetch() ​

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

Defined in: src/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 Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterTypeDescription
apiNamestringThe registered API key.
endpointNamestringThe endpoint key within the API.
optionsFetchOptions<T>Fetch options: params, query, transform and any RequestInit field.

Returns ​

Promise<FetchResult<T>>

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

Example ​

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

useGet() ​

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

Defined in: src/core/services/http.service.ts:419

GET helper over a registered API endpoint.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterTypeDescription
apiNamestringThe registered API key.
endpointNamestringThe endpoint key within the API.
urlOptions?UrlOptionsOptional URL building options.

Returns ​

Promise<FetchResult<T>>

A FetchResult with data or error.


useGetApis() ​

ts
useGetApis(): ApisConfig;

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

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

Returns ​

ApisConfig

A shallow copy of the current API registry.


useInit() ​

ts
useInit(apisConfig): void;

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

Registers (merges) API definitions into the client registry.

Parameters ​

ParameterTypeDescription
apisConfigApisConfigThe API definitions to merge into the registry.

Returns ​

void

Example ​

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

usePatch() ​

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

Defined in: src/core/services/http.service.ts:483

PATCH helper over a registered API endpoint.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterTypeDescription
apiNamestringThe registered API key.
endpointNamestringThe endpoint key within the API.
body?unknownOptional request body (auto-serialized to JSON unless raw).
urlOptions?UrlOptionsOptional URL building options.

Returns ​

Promise<FetchResult<T>>

A FetchResult with data or error.


usePost() ​

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

Defined in: src/core/services/http.service.ts:439

POST helper over a registered API endpoint.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterTypeDescription
apiNamestringThe registered API key.
endpointNamestringThe endpoint key within the API.
body?unknownOptional request body (auto-serialized to JSON unless raw).
urlOptions?UrlOptionsOptional URL building options.

Returns ​

Promise<FetchResult<T>>

A FetchResult with data or error.


usePut() ​

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

Defined in: src/core/services/http.service.ts:461

PUT helper over a registered API endpoint.

Type Parameters ​

Type ParameterDefault type
Tunknown

Parameters ​

ParameterTypeDescription
apiNamestringThe registered API key.
endpointNamestringThe endpoint key within the API.
body?unknownOptional request body (auto-serialized to JSON unless raw).
urlOptions?UrlOptionsOptional URL building options.

Returns ​

Promise<FetchResult<T>>

A FetchResult with data or error.


getInstance() ​

ts
static getInstance(): FetchApiManager;

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

Obtiene la instancia única del FetchApiManager (Singleton).

Returns ​

FetchApiManager

La instancia única de FetchApiManager.

Released under the MIT License.