Skip to content

Commit

Permalink
📝 Readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 3, 2017
1 parent f054836 commit 3cb3b23
Showing 1 changed file with 97 additions and 20 deletions.
117 changes: 97 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
<i>f[ETCH] [WR]apper</i>
</h6>

<br>

# Table of Contents

* [Motivation](#motivation)
* [Installation](#installation)
* [Compatibility](#compatibility)
* [Usage](#usage)
* [Api](#api)
* [License](#license)

# Motivation

#### Because having to write two callbacks for a simple request is awkward.
Expand Down Expand Up @@ -84,14 +95,14 @@ fetch("endpoint", {
// Wretch

wretch("endpoint")
.json({ "hello": "world"})
.json({ "hello": "world" })
.post()
.res(response => /* ... */)
```

# Installation

**Wretch bundled using the UMD format (@`dist/bundle/wretch.js`).**
**Wretch is bundled using the UMD format (@`dist/bundle/wretch.js`).**

## Npm

Expand Down Expand Up @@ -178,6 +189,14 @@ wretch(url, options)

# API

* [Helper Methods](#helper-methods)
* [Body Types](#body-types)
* [Http Methods](#http-methods)
* [Catchers](#catchers)
* [Response Types](#response-types)

------

#### wretcher(url = "", opts = {})

Create a new Wretcher object with an url and [vanilla fetch options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch).
Expand All @@ -202,12 +221,37 @@ wretch("...", { headers: { "X-Custom": "Header" }}).get()

Mix in (instead of simply overwriting) the current default options with the new ones.

```js
wretch().defaults({ headers: { "Accept": "application/json" }})

wretch().mixdefaults({ encoding: "same-origin", headers: { "X-Custom": "Header" } })

/* The new options are :
{
headers: { "Accept": "application/json", "X-Custom": "Header" },
encoding: "same-origin"
}
*/
```

#### errorType(method: "text" | "json" = "text")

Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.

Persists for every subsequent requests.

```js
wretch().errorType("json")

wretch("http://server/which/returns/an/error/with/a/json/body")
.get()
.res()
.catch(error => {
// error.message contains the parsed body
console.log(error.message))
}
```
#### options(options: Object)
Set the fetch options.
Expand All @@ -216,6 +260,15 @@ Set the fetch options.
wretch("...").options({ credentials: "same-origin" })
```
Wretch being immutable, you can store the object for later use.
```js
const corsWretch = wretch().options({ credentials: "include", mode: "cors" })

corsWretch.url("http://endpoint1").get()
corsWretch.url("http://endpoint2").get()
```
#### url(url: string)
Set the url.
Expand Down Expand Up @@ -280,28 +333,40 @@ wretch("...").formData(form).post()
Perform a get request.
```js
wretch("...").get({ credentials: "same-origin" })
```
#### delete(opts = {})
Perform a delete request.
```js
wretch("...").delete({ credentials: "same-origin" })
```
#### put(opts = {})
Perform a put request.
```js
wretch("...").json({...}).put({ credentials: "same-origin" })
```
#### patch(opts = {})
Perform a patch request.
```js
wretch("...").json({...}).patch({ credentials: "same-origin" })
```
#### post(opts = {})
Perform a post request.
```js
wretch("...").get({ credentials: "same-origin" })
wretch("...").delete({ credentials: "same-origin" })
wretch("...").json({...}).put(({ credentials: "same-origin" })
wretch("...").json({...}).patch(({ credentials: "same-origin" })
wretch("...").json({...}).post(({ credentials: "same-origin" })
wretch("...").json({...}).post({ credentials: "same-origin" })
```
## Catchers
Expand All @@ -314,6 +379,19 @@ wretch("...").json({...}).post(({ credentials: "same-origin" })
type WretcherError = Error & { status: number, response: Response, text?: string, json?: Object }
```
```js
wretch("...")
.get()
.badRequest(err => console.log(err.status))
.unauthorized(err => console.log(err.status))
.forbidden(err => console.log(err.status))
.notFound(err => console.log(err.status))
.timeout(err => console.log(err.status))
.internalError(err => console.log(err.status))
.error(418, err => console.log(err.status))
.res()
```
#### badRequest(cb: (error: WretcherError) => any)
Syntactic sugar for `error(400, cb)`.
Expand Down Expand Up @@ -342,19 +420,6 @@ Syntactic sugar for `error(500, cb)`.
Catch a specific error and perform the callback.
```js
wretch("...")
.get()
.badRequest(err => console.log(err.status))
.unauthorized(err => console.log(err.status))
.forbidden(err => console.log(err.status))
.notFound(err => console.log(err.status))
.timeout(err => console.log(err.status))
.internalError(err => console.log(err.status))
.error(418, err => console.log(err.status))
.res()
```
## Response Types
**Required**
Expand All @@ -381,14 +446,26 @@ wretch("...").get().json(json => console.log(Object.keys(json)))
Blob handler.
```js
wretch("...").get().blob(blob => /* ... */)
```
#### formData(cb: (fd : FormData) => any)
FormData handler.
```js
wretch("...").get().formData(formData => /* ... */)
```
#### arrayBuffer(cb: (ab : ArrayBuffer) => any)
ArrayBuffer handler.
```js
wretch("...").get().arrayBuffer(arrayBuffer => /* ... */)
```
#### text(cb: (text : string) => any)
Text handler.
Expand Down

0 comments on commit 3cb3b23

Please sign in to comment.