# API Reference [← Back to README](../../README.md) ## Instance Methods | Method | Description | |--------|-------------| | `afetch.get(url, options?)` | GET request | | `afetch.post(url, data?, options?)` | POST request | | `afetch.put(url, data?, options?)` | PUT request | | `afetch.delete(url, options?)` | DELETE request | | `afetch.patch(url, data?, options?)` | PATCH request | | `afetch.head(url, options?)` | HEAD request | | `afetch.options(url, options?)` | OPTIONS request | | `afetch.request(url, options?)` | Custom method request | | `afetch.create(config?)` | Create a new instance | | `afetch.use(plugin)` | Install a plugin | | `afetch.task` | Task API for cancellable requests | | `afetch.defaults` | Default configuration | ## Response Object (`AResponse`) | Property | Type | Description | |----------|------|-------------| | `data` | `T` | Parsed response data | | `status` | `number` | HTTP status code | | `statusText` | `string` | HTTP status text | | `headers` | `Headers` | Response headers | | `config` | `ResolvedRequestConfig` | Request config | | `raw` | `Response` | Original Response object | | `ok` | `boolean` | `status >= 200 && status < 300` | ## Request Task (`RequestTask`) | Property / Method | Type | Description | |-------------------|------|-------------| | `abort()` | `() => void` | Cancel the request | | `wait()` | `() => Promise>` | Wait for the response (throws if aborted) | | `aborted` | `boolean` | Whether the request has been aborted | | `done` | `boolean` | Whether the request has completed | ## Error Types (`AFetchErrorType`) | Code | Description | |------|-------------| | `TIMEOUT` | Request timed out | | `NETWORK` | Network error | | `ABORT` | Request aborted | | `HTTP` | Non-2xx response | | `PARSE` | Response parse error | | `CONFIG` | Configuration error | ## SSE Module (`@ahriknow/afetch/sse`) ### Client Methods | Method | Description | |--------|-------------| | `createSSE(url, options?)` | Create an SSE client instance | | `createAutoReconnectPlugin(options?)` | Create an auto-reconnect plugin | | `sse.connect()` | Open the SSE connection | | `sse.close()` | Close the connection and stop reconnects | | `sse.use(plugin)` | Install a plugin | ### SSE Client Properties | Property | Type | Description | |----------|------|-------------| | `sse.state` | `SSEState` | Connection state: `CONNECTING`, `OPEN`, `CLOSED` | | `sse.url` | `string` | Full connection URL | | `sse.reconnectCount` | `number` | Number of successful reconnections | | `sse.defaults` | `SSEConfig` | Current configuration (readonly) | ### SSE Event (`SSEEvent`) | Property | Type | Description | |----------|------|-------------| | `event` | `string \| undefined` | Event type (default: `'message'`) | | `data` | `string` | Event data | | `id` | `string \| undefined` | Last event ID | | `retry` | `number \| undefined` | Reconnection time in ms | ### SSE Error Types (`SSEErrorType`) | Code | Description | |------|-------------| | `NETWORK` | Network or HTTP error | | `PARSE` | SSE data parsing error | | `TIMEOUT` | Connection timeout | | `CONFIG` | Configuration error | ### SSE Plugin Hooks | Hook | Context | Return | |------|---------|--------| | `connect` | `{ config, url }` | `void` | | `message` | `{ config, event }` | `void` | | `error` | `{ config, error, attempt }` | `false` to prevent reconnect, `void` to allow | | `close` | `{ config, reconnectCount }` | `void` | ## WebSocket Module (`@ahriknow/afetch/ws`) ### Client Methods | Method | Description | |--------|-------------| | `createWS(url, config?)` | Create a WebSocket client instance | | `createAutoReconnectPlugin(options?)` | Create an auto-reconnect plugin | | `createRequestSyncPlugin(options?)` | Create a request-sync plugin | | `ws.connect()` | Open the WebSocket connection | | `ws.close(code?, reason?)` | Close the connection and stop reconnects | | `ws.send(data)` | Send data through the WebSocket | | `ws.use(plugin)` | Install a plugin | ### WebSocket Client Properties | Property | Type | Description | |----------|------|-------------| | `ws.state` | `WSState` | Connection state: `CLOSED`, `CONNECTING`, `OPEN`, `CLOSING` | | `ws.url` | `string` | Full connection URL | | `ws.reconnectCount` | `number` | Number of successful reconnections | | `ws.defaults` | `WSConfig` | Current configuration (readonly) | ### WebSocket Message (`WSMessage`) | Property | Type | Description | |----------|------|-------------| | `data` | `unknown` | Parsed message data (if `autoParse` is enabled) | | `raw` | `string` | Raw message data string | | `origin` | `string \| undefined` | Origin of the message event | | `timestamp` | `number` | Timestamp when the message was received | ### WebSocket Error Types (`WSErrorType`) | Code | Description | |------|-------------| | `NETWORK` | Connection failed or lost | | `PARSE` | Message parse/serialization error | | `TIMEOUT` | Connection or send timeout | | `CONFIG` | URL or configuration error | ### WebSocket Plugin Hooks | Hook | Context | Return | |------|---------|--------| | `open` | `{ config, url }` | `void` | | `message` | `{ config, message }` | `void` | | `error` | `{ config, error, attempt }` | `false` to prevent reconnect, `void` to allow | | `close` | `{ config, code, reason, reconnectCount, wasClean }` | `void` | | `send` | `{ config, data }` | `void` |