elbywan / wretch

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

Query params #169

Closed csulit closed 1 year ago

csulit commented 1 year ago

.get().query({name: "test"}) not working?? how to pass query params?

elbywan commented 1 year ago

@csulit It does work, you just need to import the right addon.

import wretch from "wretch";
import QueryStringAddon from "wretch/addons/queryString";

wretch("http://example.com")
  .addon(QueryStringAddon)
  .query({ name: "test" })

Please read the documentation: https://github.com/elbywan/wretch#querystring-

idafoh commented 8 months ago

@elbywan why adding query from instance not possible? I wanted to use from one created wretch instance everywhere in my app like this

// api.ts
import wretch from 'wretch';
import QueryStringAddon from 'wretch/addons/queryString'
import { store } from './store';

const token = store.getState().auth.token;

export const api = wretch()
  .addon(QueryStringAddon)
  .auth(`Bearer ${token}`)
  .catcher(401, () => {
    // clear token and redirect to login
    store.dispatch({ type: 'auth/logout' });
  })

// another_files.ts example usage
api.get('/users').query({ page: 1 }).json() // but this doesn't work
idafoh commented 8 months ago

Ok I found this way of working, but the way I showed rather intuitive for me

api.url('/users').query({ page: 1 }).get().json() // this is working