Skip to content

Commit

Permalink
🏭 Add baseUrl
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
elbywan committed Oct 3, 2017
1 parent ae0c9c0 commit a0107c9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ Create a new Wretcher object with an url and [vanilla fetch options](https://dev
Set default fetch options which will be used for every subsequent requests.

```js
// Interestingly enough, default options are mixed in :

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

// Interestingly, default options are mixed in :
// The fetch request is sent with both headers.
wretch("...", { headers:"X-Custom": "Header" }}).get()
// The request was sent with both headers.
```

#### mixdefaults(opts: Object)
Expand All @@ -225,7 +226,6 @@ Mix in (instead of simply overwriting) the current default options with the new

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

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

/* The new options are :
Expand Down Expand Up @@ -279,6 +279,22 @@ Set the url.
wretch({ credentials: "same-origin" }).url("...").get()
```
#### baseUrl(baseurl: string)
Returns a wretch factory (same signature as the [wretch](#wretchurl---opts--) method) which when called creates a new Wretcher object with the base url as an url prefix.
```js
// Subsequent requests made using the 'blogs' object will be prefixed with "http://mywebsite.org/api/blogs"
const blogs = wretch().baseUrl("http://mywebsite.org/api/blogs")

// Perfect for CRUD apis
const id = await blogs("").json({ name: "my blog" }).post().json(_ => _.id)
const blog = await blogs(`/${id}`).get().json()
console.log(blog.name)
await blogs(`/${id}`).delete()
// ... //
```
#### query(qp: Object)
Converts a javascript object to query parameters, then appends this query string to the current url.
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle/wretch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a0107c9

Please sign in to comment.