davestewart / axios-actions

Bundle endpoints as callable, reusable services
https://axios-actions.netlify.com
188 stars 15 forks source link

simple configuration causes weird doubletting og req params #14

Open klh opened 5 years ago

klh commented 5 years ago
const actions = {

  // url only
  users: 'users',

  // url and param only
  user: 'users?id=:id',

  // url and object
  create: {
    method: 'post',
    url: 'users',
    headers: { 'Content-Type': 'application/json' }
  },

  // url and param only
  update: {
    method: 'put',
    url: 'users/:id/',
    headers: { 'Content-Type': 'application/json' }
  },

  // url and param only
  delete: {
    method: 'delete',
    url: 'users/:id/',
    headers: { 'Content-Type': 'application/json' }
  },

  // method and url
  getMethodUrl: 'GET users/:id',

  // request object
  getObject: {
    method: 'get',
    url: 'users/:id/',
    headers: { 'X-Custom-Header': 'foobar' }
  }
};

Jest test:

  it(' users getObject', async () => {
    const response = await users.getObject({"id":2});
    expect(response.data)
      .toMatchSpecificSnapshot('./__snapshots__/endpoints_user_search.shot');
  });

results in a get request to the endpoint like this:

/v1/users/2/?id=2

where it in reality should be:

/v1/users/2/
klh commented 5 years ago

i realize it's sorta by design, but when using something like:

// method and url
  getMethodUrl: 'GET users/:id/:some/:thing',

readability increases when doing user.getMethodUrl({"id":2, "some": 0, "thing":0}) instead of user.getMethodUrl("2","0","0")

davestewart commented 5 years ago

Hey, I see what you're saying and I think it's something I've considered before.

You're basically saying, for a GET request (or any request) when a param has been used in the URL, exclude it from the payload?

There's another post about this kind of thing as well. I wonder if should enable some kind of configuration. Possibly globally and per actions set, i.e.:

// global config
ApiCore.configure({ ... })

const actions = {
  _config: { ... }, // local config
  foo: 'foo/:id'
}

Or maybe a syntax:

 foo: 'foo/:id!'

What do you think?

klh commented 5 years ago

I Think its a great idea, I can understand that there might be outliers and festure triggers on endpoints where you need both, but an endpoint toggle would be great.

Also checkout how apisauce does error normalization and api simplification - could be a good way to enhance a few things.

Also your plugins.upperCase example doesn’t really work as described (plugins cannot be extended as is)

On 3 Apr 2019, at 11.36, Dave Stewart notifications@github.com wrote:

Hey, I see what you're saying and I think it's something I've considered before.

You're basically saying, for a GET request (or any request) when a param has been used in the URL, exclude it from the payload?

There's another post about this kind of thing as well. I wonder if should enable some kind of configuration. Possibly globally and per actions set, i.e.:

ApiCore.configure({ ... })

const actions = { _config: { ... }, // rules on how these things could be handled foo: 'foo/:id'' } What do you think?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.