Skip to content

Commit

Permalink
📝 More details about the response types in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Dec 8, 2018
1 parent 03ca27e commit 270fe16
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,52 +776,63 @@ wretch("/resource")
**Required**
All these methods accept an optional callback, and will return a Promise resolved with either the return value of the provided callback or the expected type.
```js
// Without a callback
wretch("...").get().json().then(json => /* json is the parsed json of the response body */)
// Without a callback using await
const json = await wretch("...").get().json()
//With a callback the value returned is passsed to the Promise
wretch("...").get().json(() => "Hello world!").then(console.log) // Hello world!
```
*If an error is caught by catchers, the response type handler will not be called.*
| [res](#rescb-response--response--any) | [json](#jsoncb-json--object--any) | [blob](#blobcb-blob--blob--any) | [formData](#formdatacb-fd--formdata--any) | [arrayBuffer](#arraybuffercb-ab--arraybuffer--any) | [text](#textcb-text--string--any) |
|-----|-----|-----|-----|-----|-----|
#### res(cb: (response : Response) => any)
#### res(cb?: (response : Response) => T) : Promise<Response | T>
Raw Response handler.
```js
wretch("...").get().res(response => console.log(response.url))
```
#### json(cb: (json : Object) => any)
#### json(cb?: (json : Object) => T) : Promise<Object | T>
Json handler.
```js
wretch("...").get().json(json => console.log(Object.keys(json)))
```
#### blob(cb: (blob : Blob) => any)
#### blob(cb?: (blob : Blob) => T) : Promise<Blob | T>
Blob handler.
```js
wretch("...").get().blob(blob => /* ... */)
```
#### formData(cb: (fd : FormData) => any)
#### formData(cb: (fd : FormData) => T) : Promise<FormData | T>
FormData handler.
```js
wretch("...").get().formData(formData => /* ... */)
```
#### arrayBuffer(cb: (ab : ArrayBuffer) => any)
#### arrayBuffer(cb: (ab : ArrayBuffer) => T) : Promise<ArrayBuffer | T>
ArrayBuffer handler.
```js
wretch("...").get().arrayBuffer(arrayBuffer => /* ... */)
```
#### text(cb: (text : string) => any)
#### text(cb: (text : string) => T) : Promise<string | T>
Text handler.
Expand Down

0 comments on commit 270fe16

Please sign in to comment.