Skip to content

📱 TypeScript Client SDK with React Hooks #29

Description

@VAIBHAVSING

Overview

Generate type-safe TypeScript client SDK from OpenAPI specs and gRPC protos, with React hooks for seamless frontend integration.

🎯 Goals

  • Type Safety: Full TypeScript support from API specs
  • React Integration: Custom hooks for data fetching and state management
  • Auto-Generation: Automated client generation from specs
  • Developer Experience: IntelliSense and error checking
  • Real-time: WebSocket/gRPC streaming support

📋 Implementation Tasks

Code Generation Setup

  • OpenAPI Client Generation

    • Configure openapi-generator for TypeScript
    • Generate REST API client with fetch support
    • Custom templates for better TypeScript support
    • Error handling and response typing
  • gRPC Client Generation

    • Generate TypeScript gRPC clients from proto files
    • gRPC-Web support for browser compatibility
    • Streaming support for real-time features
    • Interceptors for authentication and logging

React Hooks Package

// packages/api-client/src/hooks/useEnvironments.ts
export function useEnvironments() {
  const [environments, setEnvironments] = useState<Environment[]>([])
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<Error | null>(null)
  
  const { data, error: fetchError, mutate } = useSWR(
    '/api/v1/environments',
    apiClient.environments.listEnvironments
  )
  
  const createEnvironment = useCallback(async (request: CreateEnvironmentRequest) => {
    try {
      const environment = await apiClient.environments.createEnvironment(request)
      mutate() // Revalidate cache
      return environment
    } catch (err) {
      throw new APIError(err)
    }
  }, [mutate])
  
  return {
    environments: data?.environments || [],
    error: fetchError,
    createEnvironment,
    refreshEnvironments: mutate
  }
}

Real-time Integration

  • Environment Status Streaming

    • gRPC streaming for real-time status updates
    • React hooks for WebSocket connections
    • Automatic reconnection handling
    • State synchronization with server
  • Event-driven Updates

    • Server-sent events integration
    • Optimistic updates for better UX
    • Conflict resolution strategies
    • Offline support with retry logic

🏗️ Package Structure

packages/api-client/
├── src/
│   ├── generated/           # Auto-generated clients
│   │   ├── rest/           # OpenAPI generated
│   │   └── grpc/           # gRPC generated
│   ├── hooks/              # React hooks
│   │   ├── environments.ts
│   │   ├── templates.ts
│   │   └── users.ts
│   ├── providers/          # React context providers
│   │   ├── APIProvider.tsx
│   │   └── StreamProvider.tsx
│   ├── types/              # Custom types
│   └── utils/              # Utilities
├── scripts/
│   ├── generate-rest.sh    # OpenAPI generation
│   └── generate-grpc.sh    # gRPC generation
└── package.json

Time Estimate: 4-6 hours

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiAPI endpoint developmentenhancementNew feature or requestfrontendFrontend/Next.js related tasks

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions