From 4d42ae9becba0a7eed8b56ddc694452d4a9a4f6f Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 22 Feb 2024 21:28:58 +0800 Subject: [PATCH] docs: config and response --- packages/core/readme.md | 112 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 7 deletions(-) diff --git a/packages/core/readme.md b/packages/core/readme.md index 6265e9f..260c88d 100644 --- a/packages/core/readme.md +++ b/packages/core/readme.md @@ -26,10 +26,6 @@ const { status, data } = await http('https://example.com', { method: 'GET' }) ## API -### Static Methods - -### new Undios(config?) - ### Instance Methods #### http(url, config?) @@ -85,23 +81,125 @@ interface HTTP { 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. +> [!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 +```ts +interface Config { + baseURL?: string + method?: Method + headers?: Record + redirect?: RequestRedirect + keepAlive?: boolean + params?: Record + data?: any + responseType?: keyof ResponseTypes + timeout?: number +} +``` + +#### config.baseURL + +The base URL of the request. If it is set, the `url` will be resolved against it. + +See [URL#base](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#base). + #### config.method +See [fetch#method](https://developer.mozilla.org/en-US/docs/Web/API/fetch#method). + #### config.headers +See [fetch#headers](https://developer.mozilla.org/en-US/docs/Web/API/fetch#headers). + +#### config.redirect + +See [fetch#redirect](https://developer.mozilla.org/en-US/docs/Web/API/fetch#redirect). + +#### config.keepAlive + +See [fetch#keepalive](https://developer.mozilla.org/en-US/docs/Web/API/fetch#keepalive). + #### config.params +Additional query parameters. They will be appended to the URL. + #### config.data +The request body. Currently support below types: + +- string +- URLSearchParams +- ArrayBuffer / ArrayBufferView +- Blob +- FormData +- Object (will be serialized to JSON) + #### config.responseType +Supported response types: + +```ts +interface ResponseTypes { + json: any + text: string + stream: ReadableStream + blob: Blob + formdata: FormData + arraybuffer: ArrayBuffer +} +``` + #### config.timeout +The request timeout in milliseconds. + #### config.proxyAgent -> ![NOTE] In order to use a proxy agent, you need to install `undios-proxy-agent`. +> [!NOTE] In order to use a proxy agent, you need to install `undios-proxy-agent`. + +### Response + +```ts +interface Response { + status: number + statusText: string + headers: Headers + data: T +} +``` + +#### response.status + +See [Response#status](https://developer.mozilla.org/en-US/docs/Web/API/Response/status). + +#### response.statusText + +See [Response#statusText](https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText). + +#### response.headers + +See [Response#headers](https://developer.mozilla.org/en-US/docs/Web/API/Response/headers). + +#### response.data + +The decoded response body. + +### Static Methods + +```ts +class Undios { + constructor(config?: Config) +} +``` + +#### Undios.Error.is(error) + +```ts +function is(error: any): error is Undios.Error +```