npm package: @hoangkhang12789/sdk
Description: Type-safe SDK for Hono API
Latest version: 1.0.8

# @hoangkhang12789/sdk

Type-safe SDK client for Social API Services using Hono RPC. This SDK provides full type safety and IntelliSense support for your API endpoints.

## Installation

```bash
npm install @hoangkhang12789/sdk
# or
pnpm add @hoangkhang12789/sdk
# or
yarn add @hoangkhang12789/sdk
```

## Features

- 🔒 **Full Type Safety**: Leverages Hono's RPC to provide end-to-end type safety
- 🚀 **Easy to Use**: Simple and intuitive API
- 📝 **TypeScript First**: Built with TypeScript for the best developer experience
- 🎯 **IntelliSense Support**: Auto-completion for all API endpoints and parameters
- ⚡ **Lightweight**: Minimal dependencies

## Usage

### Basic Setup

```typescript
import { createClient } from '@hoangkhang12789/sdk';

// Create a client instance
const client = createClient('http://localhost:3000');

// Now you can use the client with full type safety
const response = await client.communities.me.$get({
  query: {
    page: '1',
    limit: '10',
  },
});
const data = await response.json();
```

### With Custom Options

You can pass custom request options as the second parameter to `createClient`, which will be used for all requests:

```typescript
import { createClient } from '@hoangkhang12789/sdk';

const client = createClient('http://localhost:3000', {
  headers: {
    'Authorization': 'Bearer your-token'
  },
});
```

### Advanced Usage

#### Communities

```typescript
// Create a new community
const createResponse = await client.communities.$post({
  form: {
    name: 'My Community',
    description: 'A place for developers',
    privacy: 'PUBLIC',
    type: 'General',
    address: 'Internet',
    catalogs: ['Tech'],
    avatar: someFile, // File object
    coverImage: someFile, // File object
  },
});
const newCommunity = await createResponse.json();

// Get community details
const communityResponse = await client.communities[':communityId'].$get({
  param: { communityId: '01HQ...' },
});
const community = await communityResponse.json();
```

#### Posts

```typescript
// Create a post in a community
const postResponse = await client.communities[':communityId'].posts.$post({
  param: { communityId: '01HQ...' },
  form: {
    title: 'Hello World',
    content: 'This is my first post!',
  },
});
const post = await postResponse.json();
```

### Error Handling

```typescript
try {
  const response = await client.communities[':communityId'].$get({
    param: { communityId: 'invalid-id' },
  });

  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }

  const data = await response.json();
} catch (error) {
  console.error('Failed to fetch community:', error);
}
```

## Type Definitions

The SDK automatically infers types from your Hono API server. The `ApiClient` type is exported for use in your application:

```typescript
import { createClient, type ApiClient } from '@hoangkhang12789/sdk';

const client = createClient('http://localhost:3000');

// Use ApiClient type for dependency injection
function communityService(client: ApiClient) {
  return {
    async getCommunity(id: string) {
      const response = await client.communities[':communityId'].$get({
        param: { communityId: id },
      });
      return response.json();
    },
  };
}
```

## Requirements

- Node.js >= 18
- TypeScript >= 5.0
- Hono ^4.0.0 (peer dependency)

## License

MIT