diff options
| author | Li Zhineng <[email protected]> | 2025-07-24 09:07:10 +0800 |
|---|---|---|
| committer | Li Zhineng <[email protected]> | 2025-07-24 09:07:10 +0800 |
| commit | d4e676b0ea9a70ac8c11d55f486e3dc609325eee (patch) | |
| tree | df2019d94795b9dc1684c2233b7b4823e38d103a | |
| parent | 1feb4ec44ac4a06de90aa549fa96356a48079b7d (diff) | |
| download | wave-d4e676b0ea9a70ac8c11d55f486e3dc609325eee.tar.gz wave-d4e676b0ea9a70ac8c11d55f486e3dc609325eee.zip | |
improve README.md
| -rw-r--r-- | README.md | 79 |
1 files changed, 79 insertions, 0 deletions
@@ -17,6 +17,85 @@ npm i @beautiful-bubble/wave After installation, remember to run the "Build NPM Package" command in the WeChat Mini Program development tools. +## Sending HTTP Requests + +Wave features a set of intuitive APIs that help you quickly create HTTP +requests. + +```ts +import { Client } from '@beautiful-bubble/wave' + +Client.new().get('http://www.example.com') +Client.new().post('http://www.example.com') +Client.new().put('http://www.example.com') +Client.new().delete('http://www.example.com') +``` + +You can also pass an object as the second parameter along with the request to +specify the body data. The default content type for HTTP requests is set to +JSON. Since HTTP GET requests do not have a body, the object will be formatted +as a query string. + +```ts +Client.new().get('http://www.example.com/api/users', ['search' => 'Zhineng']) +``` + +When you're ready to get the response, you can `await` the invocation. Wave +also provides a comprehensive `Response` instance with intuitive APIs. + +```ts +const response = await Client.new().get('http://www.example.com') +console.log(response.data()) +``` + +We understand that you may need to abort outgoing HTTP requests in some +situations; in that case, you can simply retrieve the request task without +using the `await` operator. + +```ts +const { task } = Client.new().get('http://www.example.com') +task.abort() +``` + +## Configure base URL + +It can be tedious to specify the full endpoint in every HTTP request throughout +the application. You can set the base URL at the application level in the +`app.ts` or `app.js` file, and all subsequent HTTP requests will then respect +this endpoint. + +```ts +// app.ts + +Client.baseUrl('http://www.example.com') + +// page.ts + +Client.new().get('/api/users') +``` + +## Request timeout + +WeChat Mini Programs have a one-minute default timeout for HTTP requests. +You can shorten this by passing the desired time in milliseconds to the +timeout API. + +```ts +Client.new().timeout(3000).get('/') +``` + +## Middleware + +Wave streamlines the organization and reuse of code for HTTP requests. Through +its middleware, you can intercept or modify HTTP requests, such as by +configuring authentication tokens or recording outgoing requests. + +```ts +Client.use((request, next) => { + return next(request.withHeader('Authorization', 'Bearer ...')) +}) +``` + ## License The package is released under [the MIT license](LICENSE). |
