UNPKG

ch-chat-api-client-orval

Version:

TypeScript API client for CH Chat API with SWR hooks and Axios integration

194 lines (147 loc) 5.58 kB
# CH Chat API TypeScript Client TypeScript API client for CH Chat API with SWR hooks and Axios integration. This package provides type-safe API calls and React hooks for seamless integration with your frontend applications. ## Installation ```bash npm install @icloudhospital/chat-api-client ``` ## Prerequisites This package requires the following peer dependencies: ```bash npm install axios swr typescript zod ``` ## Usage ### Using SWR Hooks (Recommended) ```typescript import { useGetApiV1ChatTenantIdSessions, usePostApiV1ChatTenantIdSessions } from '@icloudhospital/chat-api-client/sessions'; import { ChatSessionModel, CreateChatSessionCommand } from '@icloudhospital/chat-api-client/models'; // In your React component function ChatSessions({ tenantId }: { tenantId: string }) { // Fetch sessions const { data: sessions, error, isLoading } = useGetApiV1ChatTenantIdSessions(tenantId); // Create session mutation const { trigger: createSession, isMutating } = usePostApiV1ChatTenantIdSessions(tenantId); const handleCreateSession = async () => { const newSession: CreateChatSessionCommand = { // ... session data }; try { const result = await createSession(newSession); console.log('Session created:', result.data); } catch (error) { console.error('Failed to create session:', error); } }; if (isLoading) return <div>Loading...</div>; if (error) return <div>Error: {error.message}</div>; return ( <div> {sessions?.data.map(session => ( <div key={session.id}>{session.name}</div> ))} <button onClick={handleCreateSession} disabled={isMutating}> {isMutating ? 'Creating...' : 'Create Session'} </button> </div> ); } ``` ### Using Direct API Calls ```typescript import { postApiV1ChatTenantIdSessions, getApiV1ChatTenantIdSessions } from '@icloudhospital/chat-api-client/sessions'; import { ChatSessionModel, CreateChatSessionCommand } from '@icloudhospital/chat-api-client/models'; // Direct API calls async function createSession(tenantId: string, sessionData: CreateChatSessionCommand) { try { const response = await postApiV1ChatTenantIdSessions(tenantId, sessionData); return response.data; } catch (error) { console.error('Failed to create session:', error); throw error; } } async function fetchSessions(tenantId: string) { try { const response = await getApiV1ChatTenantIdSessions(tenantId); return response.data; } catch (error) { console.error('Failed to fetch sessions:', error); throw error; } } ``` ### Using Specific Endpoints ```typescript // Import specific endpoint modules import { useGetApiV1ChatTenantIdSessions } from '@icloudhospital/chat-api-client/sessions'; import { useGetApiV1ChatMigrations } from '@icloudhospital/chat-api-client/migrations'; import { useGetApiV1ChatWhatsapps } from '@icloudhospital/chat-api-client/whats-apps'; // Import type definitions import { ChatSessionModel, ChatMessage, IntakeForm } from '@icloudhospital/chat-api-client/models'; ``` ## Configuration ### Base URL Configuration The generated code uses `http://localhost:3003` as the base URL. You can configure axios defaults to change this: ```typescript import axios from 'axios'; // Set default base URL axios.defaults.baseURL = 'https://your-api-endpoint.com'; // Or configure per request const response = await getApiV1ChatTenantIdSessions(tenantId, undefined, { baseURL: 'https://your-api-endpoint.com' }); ``` ### Authentication Configure authentication headers: ```typescript import axios from 'axios'; // Set default authorization header axios.defaults.headers.common['Authorization'] = `Bearer ${token}`; // Or configure per request const response = await getApiV1ChatTenantIdSessions(tenantId, undefined, { headers: { 'Authorization': `Bearer ${token}` } }); ``` ## TypeScript Configuration Make sure your `tsconfig.json` includes the following settings for optimal compatibility: ```json { "compilerOptions": { "moduleResolution": "node", "esModuleInterop": true, "allowSyntheticDefaultImports": true, "resolveJsonModule": true, "strict": true } } ``` ## Features - **TypeScript First**: Full type safety with generated TypeScript types - **SWR Integration**: React hooks for data fetching and caching - **Axios Based**: Built on top of axios for HTTP requests - **Modular Design**: Import only what you need - **OpenAPI Generated**: Automatically generated from OpenAPI specifications - **Mutation Support**: SWR mutations for POST, PUT, DELETE operations - **Error Handling**: Comprehensive error types for better error handling ## Available Hooks ### Sessions - `useGetApiV1ChatTenantIdSessions` - Fetch all sessions - `usePostApiV1ChatTenantIdSessions` - Create new session - `useGetApiV1ChatTenantIdSessionsSessionId` - Fetch specific session - `usePatchApiV1ChatTenantIdSessionsSessionId` - Update session - `useDeleteApiV1ChatTenantIdSessionsSessionId` - Delete session - `usePostApiV1ChatTenantIdSessionsSessionIdJoin` - Join session - `useGetApiV1ChatTenantIdSessionsSessionIdMessages` - Fetch session messages - `usePostApiV1ChatTenantIdSessionsSessionIdMessageSend` - Send message ### Migrations - `usePostApiV1ChatMigrations` - Run migrations ### WhatsApp - `usePostApiV1ChatWhatsappsMarketingTemplateMessagesSend` - Send marketing template messages ## Development This package is generated using [Orval](https://orval.dev/). To regenerate the client: ```bash npm run generate ``` ## License ISC