Saltar al contenido principal

Function: useBuildUrl()

useBuildUrl(apiName, endpointName, options?): string

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

Builds a safe http(s) URL from a registered API + endpoint without making a network request.

Use cases:

  • Navigation links — generate <a href> URLs for download links, pagination, etc.
  • Image/file sources — build <img src> or <video src> URLs from API endpoints.
  • Third-party libraries — pass URLs to charting, mapping, or analytics libraries that handle their own fetching.
  • Debugging — log the full URL before making a request to verify params and query strings.
  • SSR — construct URLs server-side for pre-rendering or metadata generation.

Parameters

apiName

string

The registered API key (from useInitApis).

endpointName

string

The endpoint key within that API.

options?

UrlOptions = {}

Optional URL building options (path params, query params, ignore defaults).

Returns

string

The fully constructed URL string.

Throws

If the API or endpoint is not registered, or the URL scheme is not http(s).

Example

import { useBuildUrl } from "katanakit-js";

// Basic URL with path params
const url = useBuildUrl("pokeapi", "pokemonById", { params: { id: 25 } });
// → "https://pokeapi.co/api/v2/pokemon/25"

// URL with query params
const download = useBuildUrl("myApi", "export", { query: { format: "pdf" } });
// → "https://api.myapp.com/v1/export?format=pdf"

// Use in a template
<a href={useBuildUrl("myApi", "download", { params: { id: 42 } })}>Download</a>

// Debug before fetching
console.log("Will fetch:", useBuildUrl("myApi", "users", { query: { page: 1 } }));