Skip to content

Commit a73e9a3

Browse files
committed
docs: update readme
1 parent 2785ac3 commit a73e9a3

File tree

1 file changed

+90
-28
lines changed

1 file changed

+90
-28
lines changed

README.md

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<p align="center">
1212
<a href="#about">About</a> •
1313
<a href="#disclaimer">Disclaimer</a> •
14-
<a href="#install">Install</a> •
14+
<a href="#getting-started">Getting Started</a> •
1515
<a href="#how-to-use">How To Use</a>
1616
</p>
1717
<p align="center">
@@ -30,9 +30,15 @@ HTTP Client is an expressive, minimal wrapper around the [Fetch()](https://devel
3030

3131
Until HTTP Client reaches a 1.0.0 release, breaking changes will be released with a new minor version.
3232

33-
## Install
33+
## Getting Started
3434

35-
You can install HTTP Client from npm registry and GitHub Packages.
35+
You will need to make sure your system meets the following prerequisites:
36+
37+
- Node.js >= 18.0.0
38+
39+
#### Package installation
40+
41+
You can install HTTP Client from npm registry or GitHub Packages.
3642

3743
```shell
3844
npm install @wolfpackthatcodes/http-client
@@ -44,7 +50,10 @@ To install HTTP Client from GitHub Packages, follow the steps in the GitHub docu
4450

4551
This project is a work-in-progress. Code and documentation are currently under development and are subject to change.
4652

47-
#### Making requests
53+
Documentation on the available functionality has been outlined below for you to get started:
54+
55+
<details>
56+
<summary><b>1. Making requests</b></summary>
4857

4958
Since the HTTP Client is a wrapper for Fetch() API, you can still make `HEAD`, `GET`, `POST`, `PUT`, `PATCH`, `DELETE` requests using the respectively helper methods provided.
5059

@@ -91,7 +100,10 @@ const response = new HttpClient()
91100
.get('https://api.example.local/users');
92101
```
93102

94-
#### Send request with data
103+
</details>
104+
105+
<details>
106+
<summary><b>2. Sending request with data</b></summary>
95107

96108
You can send additional data with your `POST`, `PUT`, and `PATCH` requests. These methods accept either a string or an object of data as their second argument.
97109

@@ -102,7 +114,7 @@ const response = new HttpClient()
102114
.post('https://api.example.local/users/1/notes', 'Example text.');
103115
```
104116

105-
By default, data will be sent with a header of `Content-Type: text/plain`.
117+
By default, data will be sent with a header of `Content-Type: text/plain`.
106118

107119
##### Send data as JSON
108120

@@ -140,22 +152,31 @@ const response = new HttpClient()
140152
.post('https://api.example.local/users', { first_name: 'Luis', last_name: 'Aveiro' });
141153
```
142154

143-
#### Headers
155+
</details>
144156

145-
You can add multiple headers to the request by using the `withHeaders` method. The `withHeaders` method accepts an object of key / value pairs:
157+
<details>
158+
<summary><b>3. Include headers</b></summary>
159+
160+
You can add multiple headers to the request by using the `withHeaders` method. The `withHeaders` method accepts an object of key / value pairs or Headers instance:
146161

147162
```typescript
148163
import { HttpClient } from '@wolfpackthatcodes/http-client';
149164

150-
const response = new HttpClient()
151-
.withHeaders({
165+
const headers = {
152166
'X-Header': 'value',
153167
'Y-Header': 'value',
154-
})
168+
};
169+
170+
const firstResponse = new HttpClient()
171+
.withHeaders(headers)
155172
.get('https://api.example.local/users');
173+
174+
const secondResponse = new HttpClient()
175+
.withHeaders(new Headers(headers))
176+
.get('https://api.example.local/posts');
156177
```
157178

158-
Alternatively, use `withHeader` method to include individual headers.
179+
Alternatively, use `withHeader` method to include an individual header.
159180

160181
```typescript
161182
import { HttpClient } from '@wolfpackthatcodes/http-client';
@@ -167,16 +188,25 @@ const response = new HttpClient()
167188

168189
##### Replace headers
169190

170-
The `replaceHeaders` method allow you to replace multiple headers. The `replaceHeaders` method accepts an object of key / value pairs:
191+
The `replaceHeaders` method allow you to replace multiple headers. The `replaceHeaders` method accepts an object of key / value pairs or Headers instance:
171192

172193
```typescript
173194
import { HttpClient } from '@wolfpackthatcodes/http-client';
174195

175196
const httpClient = new HttpClient()
176-
.withHeaders({ 'Content-Type': 'application/xml', Accept: 'application/html' })
197+
.withHeaders({ 'Content-Type': 'application/xml', Accept: 'application/html' });
177198

178-
const response = httpClient
179-
.replaceHeaders({ 'Content-Type': 'application/json', Accept: 'application/json' })
199+
const updatedHeaders = {
200+
'Content-Type': 'application/json',
201+
Accept: 'application/json'
202+
};
203+
204+
const firstResponse = httpClient
205+
.replaceHeaders(updatedHeaders)
206+
.get('https://api.example.local/users');
207+
208+
const secondResponse = httpClient
209+
.replaceHeaders(new Headers(updatedHeaders))
180210
.get('https://api.example.local/users');
181211
```
182212

@@ -186,7 +216,7 @@ Alternatively, use `replaceHeader` method to replace individual header.
186216
import { HttpClient } from '@wolfpackthatcodes/http-client';
187217

188218
const httpClient = new HttpClient()
189-
.withHeaders({ 'Content-Type': 'application/xml', Accept: 'application/html' })
219+
.withHeaders({ 'Content-Type': 'application/xml', Accept: 'application/html' });
190220

191221
const response = httpClient
192222
.replaceHeader('Content-Type', 'application/json')
@@ -215,16 +245,17 @@ const response = new HttpClient()
215245
.get('https://api.example.local/users');
216246
```
217247

218-
#### Authentication
248+
</details>
249+
250+
<details>
251+
<summary><b>4. Include authentication</b></summary>
219252

220253
You may specify basic authentication credentials using the `withBasicAuth` and method.
221254

222255
```typescript
223256
import { HttpClient } from '@wolfpackthatcodes/http-client';
224257

225-
const response = new HttpClient()
226-
.withBasicAuth('luisaveiro', 'secret')
227-
.get('https://api.example.local/settings');
258+
const response = new HttpClient().withBasicAuth('luisaveiro', 'secret').get('https://api.example.local/settings');
228259
```
229260

230261
##### Bearer Tokens
@@ -234,14 +265,43 @@ If you would like to quickly add a bearer token to the request's Authorization h
234265
```typescript
235266
import { HttpClient } from '@wolfpackthatcodes/http-client';
236267

268+
const response = new HttpClient().withToken('your-token').get('https://api.example.local/settings');
269+
```
270+
271+
</details>
272+
273+
<details>
274+
<summary><b>5. Include Fetch options</b></summary>
275+
276+
You may specify additional Fetch() API [Request options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) using the `withOptions` method. The `withOptions` method accepts an object of key / value pairs:
277+
278+
```typescript
279+
import { HttpClient } from '@wolfpackthatcodes/http-client';
280+
281+
const response = new HttpClient()
282+
.withOptions({
283+
credentials: 'same-origin',
284+
mode: 'same-origin'
285+
})
286+
.get('https://api.example.local/users');
287+
```
288+
289+
Alternatively, use `withOption` method to include an individual option.
290+
291+
```typescript
292+
import { HttpClient } from '@wolfpackthatcodes/http-client';
293+
237294
const response = new HttpClient()
238-
.withToken('token')
239-
.get('https://api.example.local/settings');
295+
.withOption('credentials', 'same-origin')
296+
.get('https://api.example.local/users');
240297
```
241298

242-
#### Testing
299+
</details>
243300

244-
The HTTP Client offers a `fake` method that allows you to instruct the HTTP Client to return mocked responses when requests are made.
301+
<details>
302+
<summary><b>6. Testing</b></summary>
303+
304+
The HTTP Client offers a `fake` method that allows you to instruct the HTTP Client to return mocked responses when requests are made. The `fake` method will prevent the HTTP Client to make a HTTP request.
245305

246306
```typescript
247307
import { HttpClient } from '@wolfpackthatcodes/http-client';
@@ -254,9 +314,9 @@ interface User {
254314

255315
function createUserArray(): User[] {
256316
const users: User[] = [
257-
{ id: 1, name: "John Doe", email: "john.doe@example.com" },
258-
{ id: 2, name: "Jane Smith", email: "jane.smith@example.com" },
259-
{ id: 3, name: "Alice Johnson", email: "alice.johnson@example.com" },
317+
{ id: 1, name: 'John Doe', email: 'john.doe@example.com' },
318+
{ id: 2, name: 'Jane Smith', email: 'jane.smith@example.com' },
319+
{ id: 3, name: 'Alice Johnson', email: 'alice.johnson@example.com' },
260320
// Add more user objects as needed
261321
];
262322

@@ -274,6 +334,8 @@ const response = new HttpClient()
274334
.get('https://api.example.local/users');
275335
```
276336

337+
</details>
338+
277339
## Changelog
278340

279341
Please see [CHANGELOG](https://github.com/wolfpackthatcodes/typescript-http-client/blob/main/CHANGELOG.md) for more information on what has changed recently.

0 commit comments

Comments
 (0)