Skip to content

Commit 837b459

Browse files
committed
docs(common): CHECKOUT-2738 Update README with usage instructions
1 parent 9208683 commit 837b459

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# RequestSender
2+
3+
A simple library for sending HTTP requests.
4+
5+
## Usage
6+
7+
To send a HTTP request.
8+
9+
```js
10+
import { createRequestSender } from '@bigcommerce/request-sender';
11+
12+
const requestSender = createRequestSender();
13+
14+
// GET request
15+
requestSender.get('/foobars')
16+
.then(({ body }) => console.log(response.body));
17+
18+
// POST request
19+
requestSender.post('/foobars', { body: { name: 'Foobar' } })
20+
.then(({ body }) => console.log(response.body));
21+
```
22+
23+
To cancel a pending request
24+
25+
```js
26+
import { createRequestSender, createTimeout } from '@bigcommerce/request-sender';
27+
28+
const timeout = createTimeout(100);
29+
const requestSender = createRequestSender();
30+
31+
requestSender.get('/foobars', { timeout })
32+
.catch(({ status }) => console.log(status));
33+
34+
timeout.cancel();
35+
```
36+
37+
## API
38+
39+
### createRequestSender()
40+
41+
To create a new instance of `RequestSender`.
42+
43+
### createTimeout(delay: number?)
44+
45+
To create a new instance of `Timeout`. If `delay` is defined, the instance will automatically timeout after the specified period. Otherwise, it remains inactive until `complete()` is called.
46+
47+
### RequestSender
48+
#### sendRequest(url: string, options: RequestOptions): Promise<Response>
49+
#### get(url: string, options: RequestOptions): Promise<Response>
50+
#### post(url: string, options: RequestOptions): Promise<Response>
51+
#### put(url: string, options: RequestOptions): Promise<Response>
52+
#### patch(url: string, options: RequestOptions): Promise<Response>
53+
#### delete(url: string, options: RequestOptions): Promise<Response>
54+
55+
To submit a HTTP request using `GET`, `POST`, `PUT`, `PATCH` or `DELETE` method. Alternatively, you can call `sendRequest` and specify the request method as an argument.
56+
57+
### Timeout
58+
#### complete(): void;
59+
60+
To manually complete a timeout.
61+
62+
### RequestOptions
63+
#### body: any?
64+
Request payload.
65+
Default: `null`
66+
67+
#### headers: Object?
68+
Request headers.
69+
Default: `{
70+
'Accept': 'application/json, text/plain, */*',
71+
'Content-Type': 'application/json',
72+
}`
73+
74+
#### params: Object?
75+
URL parameters. They get serialized as a query string.
76+
Default: `null`
77+
78+
#### method: string?
79+
Request method. It's ignored if calling one of the convenience methods (`get`, `post` etc...).
80+
Default: `GET`
81+
82+
#### credentials: boolean?
83+
Same as `XMLHttpRequest.withCredentials`.
84+
Default: `true`
85+
86+
#### timeout: Timeout?
87+
Define if wish to timeout a request.
88+
Default: `null`
89+
90+
### Response
91+
92+
#### body: any
93+
Response body.
94+
Default: `null`
95+
96+
#### headers: Object
97+
Response headers.
98+
Default: `{}`
99+
100+
#### status: number?
101+
Response status code. Return `0` if the request is cancelled.
102+
Default: `undefined`
103+
104+
#### statusText: string?
105+
Response status text.
106+
Default: `undefined`
107+
108+
## Development
109+
110+
Some useful commands
111+
112+
```sh
113+
# To test
114+
yarn test
115+
116+
# To lint
117+
yarn lint
118+
119+
# To release
120+
yarn release
121+
```
122+
123+
For more commands, please see `package.json`

0 commit comments

Comments
 (0)