elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

Ability to set baseUrl #2

Closed chimon2000 closed 7 years ago

chimon2000 commented 7 years ago

It would be nice to be able to set a baseUrl in the defaults, similar to how you can in axios:

var instance = axios.create({
  baseURL: 'https://api.example.com'
});

Awesome work, this library is just what I have been looking for!

elbywan commented 7 years ago

Awesome work, this library is just what I have been looking for!

Thanks !

It would be nice to be able to set a baseUrl in the defaults, similar to how you can in axios:

This is just the feedback I need, I will implement it this morning 😄

elbywan commented 7 years ago

Just implemented the functionality but in a slightly different way as I prefer immutability over global options ;).

(updated the docs)

// 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().res()
// ... //

Hope it suits your needs !

chimon2000 commented 7 years ago

That is awesome, thanks for the quick turnaround