summaryrefslogtreecommitdiff
path: root/README.md
blob: 1aaa90728a700f933709c8df4cb2bd31621aeb6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# The HTTP Client for WeChat Mini Program

[![CI](https://github.com/Beautiful-Bubble/wave/actions/workflows/ci.yml/badge.svg)](https://github.com/Beautiful-Bubble/wave/actions/workflows/ci.yml)

Wave is an HTTP client designed for WeChat Mini Programs. It aims to enhance
the developer experience when sending HTTP requests.

## Installation

To install the package, navigate to the root directory of your WeChat Mini
Program. You can install it using NPM:

```bash
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).