definePlugin utility provides type-safe plugin creation for CallApi. It ensures your plugins have proper TypeScript types and helps you extend CallApi with reusable functionality.
Import
Signature
Parameters
Returns
Returns the plugin with deep writeable types, ensuring full type safety throughout your plugin implementation.Basic Usage
Simple Plugin
Plugin with Setup
Thesetup function runs before request processing and can modify the initial request configuration:
Plugin with Middleware
Plugins can wrap the fetch call using middleware:Plugin with Custom Options
Plugins can define custom options that users can pass to API calls:Advanced Examples
Authentication Plugin
Caching Plugin
Metrics Plugin
Plugin Interface
A CallApi plugin can include:Alternative: Using satisfies
You can also use TypeScript’s satisfies keyword instead of definePlugin:
definePlugin for consistency with CallApi’s other utilities, or use satisfies if you prefer TypeScript’s native type checking.
See Also
- Plugins Guide - Complete guide to creating and using plugins
- Logger Plugin - Built-in logger plugin example
- Custom Plugins - More examples of custom plugins
- Hooks - Available lifecycle hooks
- Middlewares - Working with middleware