Skip to content

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

Closed
@adrienpoly

Description

@adrienpoly

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions