Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to send raw request? #414

Closed
gadelkareem opened this issue May 19, 2020 · 11 comments
Closed

how to send raw request? #414

gadelkareem opened this issue May 19, 2020 · 11 comments

Comments

@gadelkareem
Copy link

Is there a way to send a raw request? for ex I used to do something like this with https://github.com/twg/devour:

api.runMiddleware({
      url: api.apiUrl + '/users/' + id + '/password',
      method: 'PATCH',
      data,
      model: 'update-password-request'
})
@wopian
Copy link
Owner

wopian commented May 19, 2020

As in without JSON:API (de)serialisation - just regular JSON (etc.) requests?

@gadelkareem
Copy link
Author

No it is a jsonapi request but with a custom URL

@wopian
Copy link
Owner

wopian commented May 19, 2020

There's nothing explicitly built-in to do that (middleware support would be a good feature request to make in a new issue).

However the Axios instance is exposed so you can construct a custom PATCH request yourself using the methods from kitsu-core.

E.g:

import Kitsu from 'kitsu'
import { serialise, deserialise } from 'kitsu-core'

const api = new Kitsu({ baseURL: 'https://example.com' })

const updatePassword = async (body) => {
  const { data } = await api.axios.patch(
	`users/${body.id}/password`, // Sends request to https://example.com/users/1/password
  	serialise('myModelType', body)
  )
  return deserialise(data)
}

updatePassword({ id: '1', password: 'myPassword' })

@wopian
Copy link
Owner

wopian commented May 19, 2020

Although, deprecating the automatic extraction of type and id from the model parameter (first param of api.patch) would make it a plain URL. With the id and type attributes defined in the body parameter (second). Fully supporting this issue and arguably simplifying the library code.

Another weird choice I made years ago and yet another major breaking change if I go ahead with this in the future.

@wopian
Copy link
Owner

wopian commented May 19, 2020

Moved this to #415 to discuss this change further for a future 10.x release. With changes proposed in #415 the code in OP would be the following with kitsu:

api.patch(`users/${id}/password`, data)

If #414 (comment) solves your issue in 9.x then you can close this issue 👍

@gadelkareem
Copy link
Author

gadelkareem commented May 20, 2020

Thanks a lot for the quick answer. I think sending an object would simplify the call so I wonder if something like this would work?

import Kitsu from 'kitsu'
import { serialise, deserialise } from 'kitsu-core'

const api = new Kitsu({ baseURL: 'https://example.com' })

api.rawRequest = async (payload) => {
  const { data } = await api.axios.request({
    method: payload.method,
    url: payload.url,
    data: serialise(payload.model, payload.body)
  })
  return deserialise(data)
}

@wopian
Copy link
Owner

wopian commented May 21, 2020

Added in e8aacc5

Documentation will be at https://github.com/wopian/kitsu/tree/master/packages/kitsu#request once it's released.

Until then, example usage is available in the JSDoc comments here: https://github.com/wopian/kitsu/blob/master/packages/kitsu/src/index.js#L366-L423

E.g

api.request({
  method: 'PATCH',
  url: 'anime',
  type: 'anime',
  body: { id: '1', subtype: 'tv' }
})

@wopian
Copy link
Owner

wopian commented May 21, 2020

Released in 9.1.0

@gadelkareem
Copy link
Author

@wopian I see the tests are passing but I still cannot call it from my app. I also do not see the declaration in the index.d.ts

@gadelkareem
Copy link
Author

gadelkareem commented May 21, 2020

never mind it is working now v9.1.1

@wopian
Copy link
Owner

wopian commented May 21, 2020

I also do not see the declaration in the index.d.ts

Sorry, I forgot to update the TypeScript definitions for the past few releases.

From 9.1.2 onwards they're now updated during release (thanks to TS 3.7 supporting JSDoc) and I've improved the documentation to give TS users much better type information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants