Giter Club home page Giter Club logo

form-auto-content's Introduction

form-auto-content

Build Status JavaScript Style Guide

Build a form payload without caring if it should be application/x-www-form-urlencoded or multipart/form-data. It works with Fastify and light-my-request too!

Install

npm install form-auto-content

Usage

This module will transform your JSON to a payload ready to submit to an HTTP server! The autosense feature will check if there is a stream or a buffer as input and it will act accordingly returning a multipart/form-data stream; otherwise it will create a x-www-form-urlencoded string.

The module return a JSON like this:

{
  payload: Stream, // the data Stream
  headers: {} // a JSON with the `content-type` field set
}

x-www-form-urlencoded

const formAutoContent = require('form-auto-content')

const myForm = formAutoContent({
  field1: 'value1',
  field2: ['value2', 'value2.2'] // array are supported too!!
})

myForm.payload // Stream of the string in application/x-www-form-urlencoded format
myForm.headers // JSON with the `content-type` field set

multipart/form-data

const formAutoContent = require('form-auto-content')

const myForm = formAutoContent({
  field1: 'value1',
  field2: ['value2', 'value2.2'], // array are supported too!!
  myFile: fs.createReadStream('the-file.xml'),
  multipleFiles: [fs.createReadStream('file1.xml'), fs.createReadStream('file2.xml')],
  wowBuffer: Buffer.from('a long string'),

  // the file options are supported too:
  myRenamedFile: {
    value: fs.createReadStream('./foo.md'),
    options: {
      filename: 'bar.md',
      contentType: 'text/markdown'
    }
  },
  // also in arrays!
  renamedArray: [
    {
      value: fs.createReadStream('./one.json'),
      options: { filename: 'foo.json' }
    },
    {
      value: fs.createReadStream('./two.json'),
      options: { filename: 'bar.json' }
    }
  ]
})

myForm.payload // Stream in multipart/form-data format
myForm.headers // JSON with the `content-type` field set to multipart/form-data

Options

To customize the output field names, add an extra option object with the payload and headers string!

const formAutoContent = require('form-auto-content')

const option = { payload: 'body', headers: 'head' }
const myCustomForm = formAutoContent({
  field1: 'value1',
  field2: ['value2', 'value2.2'] // array are supported too!!
}, option)

myForm.body // Stream of the string in application/x-www-form-urlencoded format
myForm.head // JSON with the `content-type` field set

Typescript

This module ships with a handwritten TypeScript declaration file for TS support. The declaration exports a single function.

import formAutoContent from 'form-auto-content';

When an options object is provided, the result types will be accurately inferred:

import formAutoContent from 'form-auto-content';

const option = {
  payload: 'body',
  headers: 'head',
  forceMultiPart: true,
} as const;

const myCustomForm = formAutoContent({
  field1: 'value1',
  field2: ['value2']
}, option);

myCustomForm.body // ok
myCustomForm.head // ok

myCustomForm.payload // Typescript error: property 'payload' does not exists in type...

License

Licensed under MIT.

form-auto-content's People

Contributors

andersonjoseph avatar dependabot-preview[bot] avatar dependabot[bot] avatar eomm avatar kidsysco avatar squivix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

form-auto-content's Issues

Typescript @types package

Is there a package that exports typescript types for this library?

Typescript support would be nice.

File from Buffers with filename

Form append supports options to allow specifying a filename. This possibility is not passed through. What do you think ?

E.g:

  const form = formAutoContent({
    file: {
      value: Buffer.from('just some content'),
      options: {
        filename: 'test'
      }
    }
  })

Where file.value would be used

Cannot add JWT Bearer token to a form

Form options not taking into consideration, options passed
Sample Code:

const option = {
        headers: {
          Authorization: `Bearer ${jwtToken}`,
        },
        payload: {
          'content-type': 'multipart/form-data',
        },
      }

const form = formAutoContent(
        {
          file: fs.createReadStream(`./test/fixtures/test.txt`),
        },
        option,
      )

Rest API call never will get JWT as it's not passed to request

Support optional file fileds with forced multipart content type

I am using this library for testing as recommended by light-my-request for fastify. I want to have optional field fields in a multipart/formdata request. It seems this library determines the content type automatically based on whether there are files in the payload. Is there a way to force a multipart content-type even if the payload has no files? I would set the type asmultipart/formdata manually but then I can't determine the boundary.

I also tried using node-formdata and Node 18's native FormData but neither seems compatible with the fake http injection library light-my-request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.