# API Reference URL: /docs/guide/api-reference Reference documentation for the @docyrus/api-client — type-safe REST client with streaming, token management, and error handling. The `@docyrus/api-client` is a modern, type-safe API client for JavaScript and TypeScript. It works across Web, React Native, and Node.js. ## Installation ```bash tab="pnpm" pnpm add @docyrus/api-client ``` ```bash tab="npm" npm install @docyrus/api-client ``` ## Quick Start ```tsx import { RestApiClient } from '@docyrus/api-client'; const client = new RestApiClient({ baseUrl: 'https://api.example.com', getAccessToken: () => getToken(), }); // GET request const users = await client.get('/v1/users'); // POST with body const user = await client.post('/v1/users', { body: { name: 'Ali', email: 'ali@example.com' }, }); ``` ## Client Configuration ```tsx const client = new RestApiClient({ baseUrl: string; // API base URL (required) getAccessToken: () => Promise; // Token provider refreshToken?: () => Promise; // Auto-refresh on 401 onUnauthorized?: () => void; // Callback when auth fails headers?: Record; // Default headers timeout?: number; // Request timeout (ms) }); ``` | Option | Type | Required | Description | |--------|------|----------|-------------| | `baseUrl` | `string` | Yes | API base URL | | `getAccessToken` | `() => Promise` | Yes | Returns current access token | | `refreshToken` | `() => Promise` | No | Called on 401 — refreshes token and retries | | `onUnauthorized` | `() => void` | No | Called when both token and refresh fail | | `headers` | `Record` | No | Default headers for every request | | `timeout` | `number` | No | Request timeout in milliseconds | ## HTTP Methods ### GET ```tsx // Simple GET const data = await client.get('/v1/users'); // With query parameters const data = await client.get('/v1/users', { params: { page: 1, limit: 20, search: 'Ali' }, }); // With type safety interface User { id: string; name: string; email: string } const users = await client.get('/v1/users'); ``` ### POST ```tsx const user = await client.post('/v1/users', { body: { name: 'Ali', email: 'ali@example.com' }, }); ``` ### PUT / PATCH ```tsx await client.put('/v1/users/123', { body: { name: 'Updated Name' }, }); await client.patch('/v1/users/123', { body: { email: 'new@example.com' }, }); ``` ### DELETE ```tsx await client.delete('/v1/users/123'); ``` ## Streaming For real-time data and server-sent events: ```tsx const stream = client.stream('/v1/chat/completions', { method: 'POST', body: { message: 'Hello', model: 'gpt-4' }, }); for await (const chunk of stream) { console.log(chunk); // Process each chunk as it arrives } ``` ## Error Handling ```tsx import { ApiError } from '@docyrus/api-client'; try { await client.post('/v1/users', { body: userData }); } catch (error) { if (error instanceof ApiError) { console.log(error.status); // HTTP status code console.log(error.message); // Error message console.log(error.data); // Response body } } ``` | Property | Type | Description | |----------|------|-------------| | `status` | `number` | HTTP status code (400, 401, 404, 500, etc.) | | `message` | `string` | Error message from the server | | `data` | `unknown` | Full response body | ## Token Management The client handles token refresh automatically: 1. Request fails with `401 Unauthorized` 2. Client calls `refreshToken()` to get a new token 3. Original request is retried with the new token 4. If refresh also fails, `onUnauthorized()` is called ```tsx const client = new RestApiClient({ baseUrl: 'https://api.example.com', getAccessToken: () => tokenStore.getAccessToken(), refreshToken: async () => { const newToken = await authService.refresh(); tokenStore.setAccessToken(newToken); return newToken; }, onUnauthorized: () => { // Redirect to login router.push('/login'); }, }); ``` ## Usage with React ### With TanStack Query ```tsx import { useQuery, useMutation } from '@tanstack/react-query'; function useUsers() { const client = useApiClient(); // Your custom hook return useQuery({ queryKey: ['users'], queryFn: () => client.get('/v1/users'), }); } function useCreateUser() { const client = useApiClient(); return useMutation({ mutationFn: (data: CreateUserInput) => client.post('/v1/users', { body: data }), }); } ``` ### With React Native The API client works identically in React Native — no additional configuration needed: ```tsx import { RestApiClient } from '@docyrus/api-client'; import * as SecureStore from 'expo-secure-store'; const client = new RestApiClient({ baseUrl: 'https://api.example.com', getAccessToken: () => SecureStore.getItemAsync('access_token'), }); ``` ## Related - [Packages](/docs/guide/packages) — All `@docyrus/*` NPM packages - [CLI](/docs/guide/cli) — Authentication and project setup commands --- # Changelog URL: /docs/guide/changelog Latest releases and updates for the Docyrus CLI. Track the latest changes, features, and bug fixes for the `@docyrus/cli` command-line tool. For component library releases, see [Web Releases](/docs/web/guide/releases) and [Native Releases](/docs/native/guide/releases). --- # CLI URL: /docs/guide/cli The Docyrus CLI for authentication, project scaffolding, and component management. The `@docyrus/cli` is a command-line tool that handles authentication, project creation, component installation, theme management, and code generation from the Docyrus registry. [npm](https://www.npmjs.com/package/@docyrus/cli) ## Installation You can use the CLI directly with `npx` without installing it globally: ```bash npx @docyrus/cli ``` Or install it globally: ```bash tab="pnpm" pnpm add -g @docyrus/cli ``` ```bash tab="npm" npm install -g @docyrus/cli ``` ```bash tab="yarn" yarn global add @docyrus/cli ``` ```bash tab="bun" bun add -g @docyrus/cli ``` Or install it globally for quicker access: Once installed globally, you can use the `docyrus` command directly: ```bash docyrus add button ``` ## Commands ### Authentication | Command | Description | |---------|-------------| | `docyrus login` | Log in with browser-based SSO | | `docyrus login --email` | Log in with email and password | | `docyrus login -e user@example.com` | Log in with pre-filled email | | `docyrus logout` | Clear stored auth tokens | | `docyrus whoami` | Display current logged-in user info | ### Components | Command | Description | |---------|-------------| | `docyrus add [items...]` | Add components, hooks, or utilities from the registry | | `docyrus add button dialog` | Add multiple components at once | | `docyrus add --all web` | Install all web components | | `docyrus add --all react-native` | Install all React Native components | | `docyrus add --all-web` | Shortcut for `--all web` | | `docyrus add --all-rn` | Shortcut for `--all react-native` | | `docyrus add --overwrite` | Skip file conflict prompts | | `docyrus add --dry-run` | Preview installation without writing files | | `docyrus add --path ./src/ui` | Custom target path for installation | | `docyrus update` | Update all installed Docyrus components to latest | | `docyrus update button toast` | Update specific components only | | `docyrus update rn-card rn-button` | Update specific native components | | `docyrus update --prune` | Update and remove components deleted from registry | | `docyrus update --dry-run` | Preview what would be updated | | `docyrus list` | List available components, hooks, and utilities | | `docyrus list --packages` | List published @docyrus npm packages | ### Themes | Command | Description | |---------|-------------| | `docyrus themes list` | List available theme presets | | `docyrus themes add [name]` | Apply a theme preset to your project | | `docyrus themes add --url ` | Apply theme from a share URL | | `docyrus themes add --file ` | Apply theme from a local JSON file | | `docyrus themes current` | Show current `:root` and `.dark` CSS variables | ### Project Creation | Command | Description | |---------|-------------| | `docyrus create [name]` | Create a new project from a template (interactive) | | `docyrus create my-app --nextjs` | Create with a specific framework | | `docyrus create my-app --react --shadcn --zustand` | Create with full stack flags | **Framework flags:** `--nextjs`, `--react`, `--vue` **UI library flags:** `--shadcn` **State management flags:** `--zustand`, `--tanstack-query`, `--tanstack-vue-query` **Linter flags:** `--eslint`, `--biome`, `--no-linter` **Package manager flags:** `--pnpm`, `--npm`, `--yarn`, `--bun` **Styling options (shadcn):** | Option | Description | |--------|-------------| | `--style