Skip to main content

API Fetch

@konomi/api-fetch is WordPress' apiFetch with Konomi's error handling already wired in. Import it instead of @wordpress/api-fetch and every request routes through Konomi's middleware — you configure nothing.

Concept

Pre-wired client. The module re-exports @wordpress/api-fetch with a catchErrors middleware applied. That middleware forwards each request and, on rejection, logs the error to the console — but only when Konomi's isDebugMode config flag is on. You get consistent, debug-aware error handling for free.

Using it

Import it in place of @wordpress/api-fetch:

import { apiFetch } from '@konomi/api-fetch';

export async function toggleFavorite( postId: number ) {
return apiFetch( { path: `/konomi/v1/favorites/${ postId }`, method: 'POST' } );
}

Register your script module against the @konomi/api-fetch id so WordPress resolves the import at runtime:

wp_register_script_module(
'@my-plugin/favorites',
plugins_url('build/index.js', __FILE__),
['@konomi/api-fetch'], // dependency id
'1.0.0'
);

API

ExportModule idDescription
apiFetch@konomi/api-fetch@wordpress/api-fetch with Konomi's catchErrors middleware pre-applied.
  • Icons — the other client building block, konomi-icons.
  • Configuration — the isDebugMode flag catchErrors reads.
  • Functions — the add_action_on_module_import() helper used to load this module's dependency lazily.