Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit 772bb10

Browse files
author
Raphael Freitas
committed
Add documentation
1 parent f6da90e commit 772bb10

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ examples
22
tests
33
assets
44
api
5+
docs

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ A lightweight Promise based HTTP client, using Fetch API.
55
[![Build Status](https://travis-ci.org/raphaelpor/fitch.js.svg?branch=master)](https://travis-ci.org/raphaelpor/fitch.js)
66
[![license](https://img.shields.io/npm/l/fitch.svg)](https://github.com/raphaelpor/fitch.js/blob/master/LICENSE.md)
77

8+
* [Documentation](https://github.com/raphaelpor/fitch.js/blob/master/docs/Intro.md)
9+
* [License](https://github.com/raphaelpor/fitch.js/blob/master/LICENSE.md)
10+
811
## Get started
912
### Install
1013
```sh

docs/Config.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Configuration
2+
3+
Fitch.js allows you to use a custom configuration for your requests. You just need to pass a config object as second parameter of the Fitch.js methods.
4+
5+
## Objects available
6+
7+
**body**
8+
9+
Receives a json object, that is transformed to string by Fitch.js, before send the request.
10+
11+
**Note:** GET method doesn't allow `body` inside config object.
12+
13+
```js
14+
const req = { body: { name: 'Happy cat' } }
15+
16+
fitch.post(apiUrl, req)
17+
.then(data => console.log(data))
18+
```
19+
20+
**cache**
21+
22+
Same as in Fetch API. [See more at MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request/cache).
23+
24+
**headers**
25+
26+
Same as in Fetch API. [See more at MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request/headers).
27+
28+
**mode**
29+
30+
Same as in Fetch API. [See more at MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request/mode).
31+
32+
**params**
33+
34+
You don't need to pass your URL parameters as string. Fitch.js allows you to pass the parameters inside a object:
35+
36+
```js
37+
const config = {
38+
params: { // transform to '?test1=test-1&test2=test-2'
39+
test1: 'test-1',
40+
test2: 'test-2',
41+
},
42+
}
43+
44+
fitch.get(apiUrl, config)
45+
.then(data => console.log(data))
46+
```
47+
48+
**raw**
49+
50+
Returns the raw output of Fetch API, so you can work with headers and custom requests like files.
51+
52+
```js
53+
fitch.get('image.jpg', { raw: true })
54+
.then((response) => {
55+
if(response.ok) {
56+
return response.blob()
57+
} else {
58+
console.log('Network response was not ok.');
59+
}
60+
})
61+
.then(function(myBlob) {
62+
var objectURL = URL.createObjectURL(myBlob)
63+
myImage.src = objectURL
64+
})
65+
.catch(fitch.error)
66+
```

docs/Intro.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Get started
2+
3+
Fitch.js is a lightweight client HTTP that uses Fetch API and runs on Node.js and old browsers (with a polyfill).
4+
5+
## Install
6+
7+
You can install Fitch.js using NPM or Yarn:
8+
```sh
9+
npm i --save fitch
10+
# or:
11+
# yarn add fitch
12+
```
13+
14+
Now just import Fitch.js and you are ready to make your first request.
15+
16+
```js
17+
import fitch from 'fitch'
18+
// or:
19+
// const fitch = require('fitch')
20+
21+
const apiUrl = 'https://localhost:8080/'
22+
23+
fitch.get(apiUrl)
24+
.then(data => console.log(data))
25+
```
26+
27+
Next: [Learn about the methods available](https://github.com/raphaelpor/fitch.js/blob/master/docs/Methods.md).

docs/Methods.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Methods available:
2+
3+
**get(url, config)**
4+
5+
```js
6+
fitch.get(apiUrl, config)
7+
.then(data => console.log(data))
8+
```
9+
10+
**Note:** GET method doesn't allow `body` inside config object.
11+
12+
**post(url, config)**
13+
14+
```js
15+
const req = { body: { name: 'Happy cat' } }
16+
17+
fitch.post(apiUrl, req)
18+
.then(data => console.log(data))
19+
```
20+
21+
**put(url, config)**
22+
23+
```js
24+
const req = { body: { name: 'Happy cat' } }
25+
26+
fitch.put(apiUrl, req)
27+
.then(data => console.log(data))
28+
```
29+
30+
**patch(url, config)**
31+
32+
```js
33+
const req = { body: { name: 'Happy cat' } }
34+
35+
fitch.patch(apiUrl, req)
36+
.then(data => console.log(data))
37+
```
38+
39+
**delete(url, config)**
40+
41+
```js
42+
fitch.delete(apiUrl)
43+
.then(data => console.log(data))
44+
```
45+
46+
## Handling errors
47+
If Fetch throws an error, we can handle this using `catch()`:
48+
49+
```js
50+
fitch.get(apiUrl)
51+
.then(data => console.log(data))
52+
.catch(fitch.error) // log the error message on the console
53+
```
54+
55+
**Note:** Fitch.js allows you to use a custom configuration for your requests. [You can learn about it here](https://github.com/raphaelpor/fitch.js/blob/master/docs/Config.md).

0 commit comments

Comments
 (0)