1.0.0 • Published 6 years ago

jason-form v1.0.0

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

jason-form

npm Build Status GitHub license

Utility for converting arbitrarily deep JavaScript objects to FromData, using Rails conventions for nested arrays and objects.

If you use Rails views to generate and submit forms already, you do not need this library.

If you use some other method for generating your front end data, or have it as a JavaScript of JSON object, then this library can be considered the glue code needed to plug in to a Rails backend.

Usage

import jasonForm from 'jason-form';

const input = {
 string: 'string',
 array: [1,2],
 object: {
  foo: 'foo',
  bar: 'bar'
  }
};

const body = jasonForm.formData(input) // [['string', 'string'], ['array[]', 1], ['array[]', 2], ['object[foo]', 'foo'] ['object[bar]', 'bar']]

fetch('http://my.rails.server', {
  method: 'POST',
  credentials: 'include',
  body,
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})

Why not just submit the JavaScript object as a JSON request?

In most cases, this will work just fine and should indeed be the preferred approach. The issue is when you need to transmit data that is not supported by the JSON format (such as file upload data), or the backend that you need to send data to only accepts form data. Then this library becomes helpful.

What if my JavaScript object isn't already in a rails-friendly format (_attributes suffixes, snake_cased keys, etc)?

Run it through rails_request first.

1.0.0

6 years ago

0.1.0

6 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago