Exemple : Plugin avec API REST
Plugin complet intégrant une API REST externe avec authentification et retry.
Structure
my-api-plugin/
├── plugin.ts # Définition du plugin
├── client.ts # Client API typé
├── schemas.ts # Schemas Zod
└── types.ts # Types TypeScript
Implémentation
// client.ts
export async function fetchFromApi<T>(path: string, token: string): Promise<T> {
const response = await fetch(`https://api.example.com${path}`, {
headers: { Authorization: `Bearer ${token}` },
});
if (!response.ok) throw new Error(`API error: ${response.status}`);
return response.json() as Promise<T>;
}
// plugin.ts
import { createPlugin } from "@plugin-factory/sdk";
import { fetchFromApi } from "./client";
import { CustomerSchema } from "./schemas";
export const apiPlugin = createPlugin({
name: "api-integration",
tools: {
getCustomer: async (ctx, { customerId }: { customerId: string }) => {
const token = process.env.API_TOKEN!;
const raw = await fetchFromApi(`/customers/${customerId}`, token);
return CustomerSchema.parse(raw);
},
},
});