useApiClient Composable¶
The useApiClient
composable provides a standardized way to access API clients in VC-Shell applications. It serves as a factory for creating authenticated API client instances that can be used to make HTTP requests to backend services.
The useApiClient
composable simplifies the process of creating and using API clients in your application by handling common concerns such as authentication token management and base URL configuration. It's particularly useful for accessing platform APIs and custom API endpoints in a consistent manner.
API reference¶
Parameters¶
Parameter | Type | Description |
---|---|---|
clientClass |
constructor |
A class constructor for the API client type that extends IAuthApiBase . |
Return value¶
The useApiClient
composable returns an object with the following methods:
interface UseApiClient<ApiClient extends IAuthApiBase> {
getApiClient: () => Promise<ApiClient>; // Returns a promise that resolves to an authenticated API client instance
}
IAuthApiBase interface¶
API client classes used with this composable must implement the IAuthApiBase
interface:
interface IAuthApiBase {
authToken: string; // The authentication token
setAuthToken(token: string): void; // Method to set the auth token
getBaseUrl(defaultUrl: string, baseUrl: string): string; // Method to resolve the base URL
}
Usage¶
Basic usage¶
import { useApiClient } from '@vc-shell/framework';
import { ProductsClient } from '@your-api-package';
const { getApiClient } = useApiClient(ProductsClient);
Requirements¶
API client class requirements¶
API client classes must implement the IAuthApiBase
interface to be compatible with useApiClient
:
- Must extend or implement
IAuthApiBase
- Must have proper authentication token handling
- Must support base URL configuration
- Should be generated from OpenAPI/Swagger specifications
Generated API clients¶
VC-Shell provides tools for generating compatible API client code:
npm run generate-api-client --APP_PLATFORM_URL=https://api.example.com/ --APP_PLATFORM_MODULES=Platform,Cart,Orders --APP_API_CLIENT_DIRECTORY=src/api-clients
Related Resources¶
- API Client Integration - Guide for generating API clients
- Connecting to Platform - Guide for connecting to VirtoCommerce Platform