rails / request.js

MIT License
400 stars 30 forks source link

shorthand methods : get, post, put, delete #9

Closed adrienpoly closed 3 years ago

adrienpoly commented 3 years ago

This package looks promising as a replacement for the good old Rails.ajax

I would suggest to provide, in addition to the Class base request constructor some shorthand version for the 4 main verbs

so that we could write the example like that

import { post } from '@rails/request.js'

....

async myMethod () {
  const response = await post('localhost:3000/my_endpoint', { body: { name: 'Request.JS' }}))
  if (response.ok) {
    const body = await response.text
    // Do whatever do you want with the response body
    // You also are able to call `response.html` or `response.json`, be aware that if you call `response.json` and the response contentType isn't `application/json` there will be raised an error.
  }
}

For me, this would feel more idiomatic

implementation could be something like

async post = (url, options) {
  const request = new Request('post', url, options)
  return request.perform()
}

I can submit a PR if this is of interest

marcelolx commented 3 years ago

@adrienpoly The idea looks good to me, feel free to send a PR.

excid3 commented 3 years ago

This would be handy. 👍