npm package: @godgpt/api
Description: HTTP client module for GodGPT SDK with auth integration
Author: GodGPT
Latest version: 0.1.1

# @godgpt/api

HTTP API module for the GodGPT SDK with automatic auth integration.

## Installation

```bash
npm install @godgpt/core @godgpt/api
# or
pnpm add @godgpt/core @godgpt/api
# or
yarn add @godgpt/core @godgpt/api
```

## Usage

### Basic Setup

```typescript
import { createSDK } from "@godgpt/core";
import { ApiModule } from "@godgpt/api";

const sdk = createSDK();
const api = sdk.use(
  new ApiModule({
    baseUrl: "https://api.example.com",
    timeout: 10000,
    headers: {
      "X-App-Version": "1.0.0",
    },
  })
);

await sdk.init();
```

### Making Requests

```typescript
// GET request
const users = await api.get<User[]>("/users");

// GET with query parameters
const posts = await api.get("/posts", {
  params: { page: 1, limit: 10 },
});

// POST request
const newUser = await api.post("/users", {
  name: "John",
  email: "john@example.com",
});

// PUT request
await api.put("/users/1", { name: "Updated Name" });

// PATCH request
await api.patch("/users/1", { name: "Patched Name" });

// DELETE request
await api.delete("/users/1");
```

### Auth Integration

When used with `@godgpt/auth`, the API module automatically includes auth tokens:

```typescript
import { AuthModule } from "@godgpt/auth";
import { ApiModule } from "@godgpt/api";

const auth = sdk.use(new AuthModule());
const api = sdk.use(new ApiModule({ baseUrl: "https://api.example.com" }));

await sdk.init();
await auth.signIn("provider");

// Requests automatically include Authorization header
const profile = await api.get("/me");

// Skip auth for public endpoints
const publicData = await api.get("/public", { skipAuth: true });
```

### Request Interceptors

```typescript
// Add custom headers or modify requests
const unsubscribe = api.addRequestInterceptor((url, init) => {
  return {
    ...init,
    headers: {
      ...init.headers,
      "X-Request-ID": generateRequestId(),
    },
  };
});

// Remove interceptor when done
unsubscribe();
```

### Error Handling

```typescript
import { ApiError } from "@godgpt/api";

try {
  await api.get("/users/999");
} catch (error) {
  if (error instanceof ApiError) {
    console.log("Status:", error.status); // 404
    console.log("Message:", error.message);
    console.log("Data:", error.data); // Response body
  }
}
```

### API Events

```typescript
sdk.on("api:requestStart", ({ method, url }) => {
  console.log(`${method} ${url}`);
});

sdk.on("api:requestComplete", ({ method, url, status, duration }) => {
  console.log(`${method} ${url} - ${status} (${duration}ms)`);
});

sdk.on("api:requestError", ({ method, url, error }) => {
  console.error(`${method} ${url} failed:`, error);
});
```

## API Reference

### ApiModule Constructor Options

- `baseUrl` (string): Base URL for all requests
- `timeout` (number): Request timeout in milliseconds
- `headers` (object): Default headers for all requests

### Methods

- `get<T>(path, options?)`: Make a GET request
- `post<T>(path, body?, options?)`: Make a POST request
- `put<T>(path, body?, options?)`: Make a PUT request
- `patch<T>(path, body?, options?)`: Make a PATCH request
- `delete<T>(path, options?)`: Make a DELETE request
- `addRequestInterceptor(fn)`: Add a request interceptor

### Request Options

- `params` (object): Query parameters
- `headers` (object): Additional headers
- `skipAuth` (boolean): Skip automatic auth header

## License

MIT