Skip to content

Commit

Permalink
docs: add basic usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 22, 2024
1 parent f94a7fb commit 72cc88d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
95 changes: 95 additions & 0 deletions packages/core/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,98 @@ Fetch-based axios-style HTTP client.

- Browser and Node.js support
- Proxy agents (HTTP / HTTPS / SOCKS)
- WebSocket

## Basic Usage

```ts
import Undios from 'undios'

const http = new Undios()

const data = await http.get('https://example.com')
const data = await http.post('https://example.com', body)
const { status, data } = await http('https://example.com', { method: 'GET' })
```

## API

### Static Methods

### new Undios(config?)

### Instance Methods

#### http(url, config?)

```ts
interface HTTP {
<K extends keyof ResponseTypes>(url: string, config: Config & { responseType: K }): Promise<Response<ResponseTypes[K]>>
<T = any>(url: string | URL, config?: Config): Promise<Response<T>>
}
```

Send a request.

#### http.[get|delete|head](url, config?)

```ts
interface HTTP {
get: Request1
delete: Request1
head(url: string, config?: Config): Promise<Headers>
}

interface Request1 {
<K extends keyof ResponseTypes>(url: string, config: Config & { responseType: K }): Promise<ResponseTypes[K]>
<T = any>(url: string, config?: Config): Promise<T>
}
```

Send a GET / DELETE / HEAD request.

#### http.[post|put|patch](url, data, config?)

```ts
interface HTTP {
patch: Request2
post: Request2
put: Request2
}

interface Request2 {
<K extends keyof ResponseTypes>(url: string, data: any, config: Config & { responseType: K }): Promise<ResponseTypes[K]>
<T = any>(url: string, data?: any, config?: Config): Promise<T>
}
```

#### http.ws(url, config?)

```ts
interface HTTP {
ws(url: string | URL, config?: Config): WebSocket
}
```

Open a WebSocket connection.

> ![NOTE]
> Currently we will use [`ws`](https://github.com/websockets/ws) package to polyfill `WebSocket` in Node.js. Once Node.js has a stable WebSocket API, we will switch to it.
### Config

#### config.method

#### config.headers

#### config.params

#### config.data

#### config.responseType

#### config.timeout

#### config.proxyAgent

> ![NOTE] In order to use a proxy agent, you need to install `undios-proxy-agent`.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export class HTTP extends Service<HTTP.Config> {
}
}

ws(this: HTTP, url: string | URL, init?: HTTP.Config) {
ws(url: string | URL, init?: HTTP.Config) {
const caller = this[Context.trace]
const config = this.resolveConfig(init)
url = this.resolveURL(url, config)
Expand Down

0 comments on commit 72cc88d

Please sign in to comment.