From 72cc88d07e627dd76ef1e5a51c740b234c1d5254 Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 22 Feb 2024 21:17:25 +0800 Subject: [PATCH] docs: add basic usage --- packages/core/readme.md | 95 ++++++++++++++++++++++++++++++++++++++ packages/core/src/index.ts | 2 +- 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/packages/core/readme.md b/packages/core/readme.md index b534702..6265e9f 100644 --- a/packages/core/readme.md +++ b/packages/core/readme.md @@ -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 { + (url: string, config: Config & { responseType: K }): Promise> + (url: string | URL, config?: Config): Promise> +} +``` + +Send a request. + +#### http.[get|delete|head](url, config?) + +```ts +interface HTTP { + get: Request1 + delete: Request1 + head(url: string, config?: Config): Promise +} + +interface Request1 { + (url: string, config: Config & { responseType: K }): Promise + (url: string, config?: Config): Promise +} +``` + +Send a GET / DELETE / HEAD request. + +#### http.[post|put|patch](url, data, config?) + +```ts +interface HTTP { + patch: Request2 + post: Request2 + put: Request2 +} + +interface Request2 { + (url: string, data: any, config: Config & { responseType: K }): Promise + (url: string, data?: any, config?: Config): Promise +} +``` + +#### 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`. diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 17d3cf5..a44871d 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -318,7 +318,7 @@ export class HTTP extends Service { } } - 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)