FetchApiManager
katanakit-js / FetchApiManager
Class: FetchApiManager
Section titled “Class: 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).
Example
Section titled “Example”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
Section titled “Methods”useAddRequestInterceptor()
Section titled “useAddRequestInterceptor()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
interceptor |
RequestInterceptor |
The function to run before the request is sent. |
Returns
Section titled “Returns”A function that unregisters the interceptor.
() => void
useBuildUrl()
Section titled “useBuildUrl()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”string
The fully built URL.
Throws
Section titled “Throws”Unknown API, unknown endpoint, or a scheme that is not http(s).
Example
Section titled “Example”FetchApiManager.getInstance().useBuildUrl("pokeapi", "pokemonById", { params: { id: 25 } });// → "https://pokeapi.co/api/v2/pokemon/25"Remarks
Section titled “Remarks”Synchronous, so it throws instead of returning a Safe Result - Safe Result covers fallible async work, and this never leaves the process.
useDelete()
Section titled “useDelete()”useDelete<T>( apiName, endpointName, urlOptions?): Promise<FetchResult<T>>;Defined in: core/services/http.service.ts:496
DELETE helper over a registered API endpoint.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
apiName |
string |
The registered API key. |
endpointName |
string |
The endpoint key within the API. |
urlOptions? |
UrlOptions |
Optional URL building options. |
Returns
Section titled “Returns”Promise<FetchResult<T>>
A FetchResult with data or error.
useFetch()
Section titled “useFetch()”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<FetchResult<T>>
A FetchResult with data or error. Never throws - a failing
transform comes back as an error result too.
Example
Section titled “Example”const result = await FetchApiManager.getInstance().useFetch<{ results: unknown[] }>( "pokeapi", "pokemons", { method: "GET", query: { limit: 2 } },);if (result.ok) console.log(result.data.results);useGet()
Section titled “useGet()”useGet<T>( apiName, endpointName, urlOptions?): Promise<FetchResult<T>>;Defined in: core/services/http.service.ts:411
GET helper over a registered API endpoint.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
apiName |
string |
The registered API key. |
endpointName |
string |
The endpoint key within the API. |
urlOptions? |
UrlOptions |
Optional URL building options. |
Returns
Section titled “Returns”Promise<FetchResult<T>>
A FetchResult with data or error.
useGetApis()
Section titled “useGetApis()”useGetApis(): ApisConfig;Defined in: core/services/http.service.ts:173
Returns a shallow copy of the registered APIs (safe to mutate locally).
Returns
Section titled “Returns”A shallow copy of the current API registry.
useInit()
Section titled “useInit()”useInit(apisConfig): void;Defined in: core/services/http.service.ts:164
Registers (merges) API definitions into the client registry.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
apisConfig |
ApisConfig |
The API definitions to merge into the registry. |
Returns
Section titled “Returns”void
Example
Section titled “Example”FetchApiManager.getInstance().useInit({ pokeapi: { baseUri: "https://pokeapi.co/api/v2", endpoints: { pokemonById: "/pokemon/:id/" } },});usePatch()
Section titled “usePatch()”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<FetchResult<T>>
A FetchResult with data or error.
usePost()
Section titled “usePost()”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<FetchResult<T>>
A FetchResult with data or error.
usePut()
Section titled “usePut()”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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<FetchResult<T>>
A FetchResult with data or error.
getInstance()
Section titled “getInstance()”static getInstance(): FetchApiManager;Defined in: core/services/http.service.ts:130
Obtiene la instancia única del FetchApiManager (Singleton).
Returns
Section titled “Returns”FetchApiManager
La instancia única de FetchApiManager.