# fison

> Post/Upload JSON and Files in a single request.

Latest version **0.0.15** (published 2021-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install fison
pnpm add fison
yarn add fison
bun add fison
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2021-01-30 |
| First published | 2020-12-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 16.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Vien Dinh |
| Maintainers | maxvien |
| Keywords | fison, post, upload, json, files, multipart, form-data |

## Links

- npm: https://www.npmjs.com/package/fison
- npm.io page: https://npm.io/package/fison

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^8.3.1
- [formidable](https://npm.io/package/formidable.md) ^1.2.2
- [universal-formdata](https://npm.io/package/universal-formdata.md) ^1.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.15 (latest) — 2021-01-30
- 0.0.14 — 2021-01-30
- 0.0.13 — 2021-01-30
- 0.0.12 — 2020-12-30
- 0.0.11 — 2020-12-28
- 0.0.10 — 2020-12-28
- 0.0.9 — 2020-12-28
- 0.0.8 — 2020-12-18
- 0.0.7 — 2020-12-11
- 0.0.6 — 2020-12-11
- 0.0.5 — 2020-12-10
- 0.0.4 — 2020-12-10
- 0.0.3 — 2020-12-10
- 0.0.2 — 2020-12-10
- 0.0.1 — 2020-12-10
- … 1 more at https://npm.io/package/fison/versions

## README

# Fison - Post/Upload `JSON` and `Files` in a Single Request.

## Installation

### For Browser

```
npm install --save fison
```

```ts
import fison from 'fison/browser';
```

```html
<script type="module">
  import fison from 'https://cdn.skypack.dev/fison@0/browser';
</script>
```

### For Node

```
npm install --save fison
```

```ts
const fison = require('fison/node'); // CommonJS

import fison from 'fison/node'; // TypeScript, Babel
```

## Usage

### Packing Data

```ts
function pack(data: any): FormData;
```

#### **Example**

```ts
import fison from 'fison/browser';

const data = {
  name: 'John Doe',
  image: new File([new Blob()], 'john-doe.png', { type: 'image/png' }),
  posts: [
    {
      title: 'Hello World!',
      image: new File([new Blob()], 'hello-world.png', {
        type: 'image/png',
      }),
    },
    {
      title: 'Hello Jane!',
      image: new File([new Blob()], 'hello-jane.png', {
        type: 'image/png',
      }),
    },
  ],
};

const formData = fison.pack(data);

fetch('/api/users', { method: 'POST', body: formData })
  .then((res) => res.json())
  .then((data) => {
    console.log(data);

    /*{
      name: 'John Doe',
      image: {
        size: 3747,
        path: '/var/folders/mh/y9lv30k50yq_l96wk2xczsfm0000gn/T/upload_23b47c98c13c6ac09929a93eb8fc6d50',
        name: 'john-doe.png',
        type: 'image/png',
        mtime: '2020-12-11T04:05:10.662Z',
      },
      posts: [
        {
          title: 'Hello World!',
          image: {
            size: 3747,
            path: '/var/folders/mh/y9lv30k50yq_l96wk2xczsfm0000gn/T/upload_e798c84d7975d69ce9a6f60ac3a6cf98',
            name: 'hello-world.png',
            type: 'image/png',
            mtime: '2020-12-11T04:05:10.663Z',
          },
        },
        {
          title: 'Hello Jane!',
          image: {
            size: 3747,
            path: '/var/folders/mh/y9lv30k50yq_l96wk2xczsfm0000gn/T/upload_a867ff9115a17161ef46cbe334292b3c',
            name: 'hello-jane.png',
            type: 'image/png',
            mtime: '2020-12-11T04:05:10.663Z',
          },
        },
      ],
    };*/
  });
```

### Unpacking Data

```ts
interface Options {
  hash?: boolean;
  encoding?: string;
  uploadDir?: string;
  maxFileSize?: number;
  maxJsonSize?: number;
  keepExtensions?: boolean;
  mapFiles?: (file: File) => unknown | Promise<unknown>;
}

function unpack<T>(request: unknown, options?: Options): Promise<T>;
```

#### **Example with default options**

```ts
const express = require('express');
const fison = require('fison/node');

const app = express();

app.post('/api/users', async (req, res, next) => {
  try {
    const data = await fison.unpack(req);

    console.log(data);

    /*{
      name: 'John Doe',
      image: {
        size: 3747,
        path: '/var/folders/mh/y9lv30k50yq_l96wk2xczsfm0000gn/T/upload_23b47c98c13c6ac09929a93eb8fc6d50',
        name: 'john-doe.png',
        type: 'image/png',
        mtime: '2020-12-11T04:05:10.662Z',
      },
      posts: [
        {
          title: 'Hello World!',
          image: {
            size: 3747,
            path: '/var/folders/mh/y9lv30k50yq_l96wk2xczsfm0000gn/T/upload_e798c84d7975d69ce9a6f60ac3a6cf98',
            name: 'hello-world.png',
            type: 'image/png',
            mtime: '2020-12-11T04:05:10.663Z',
          },
        },
        {
          title: 'Hello Jane!',
          image: {
            size: 3747,
            path: '/var/folders/mh/y9lv30k50yq_l96wk2xczsfm0000gn/T/upload_a867ff9115a17161ef46cbe334292b3c',
            name: 'hello-jane.png',
            type: 'image/png',
            mtime: '2020-12-11T04:05:10.663Z',
          },
        },
      ],
    };*/

    res.status(200).json(data);
  } catch (error) {
    next(error);
  }
});

app.listen(3000, () => {
  console.log('Server listening on http://localhost:3000 ...');
});
```

#### **Example with `mapFiles` option**

You can use the `mapFiles` function to upload your files to **Amazon S3**, **Azure Storage**, **Google Cloud Storage**, ... and return their URLs to the `JSON` object.

```ts
const express = require('express');
const fison = require('fison/node');

const app = express();

app.post('/api/users', async (req, res, next) => {
  try {
    const data = await fison.unpack(req, {
      mapFiles: async (file) => {
        // your upload code here

        return `https://fison.s3.amazonaws.com/${file.name}`;
      },
    });

    console.log(data);

    /*{
      name: 'John Doe',
      image: 'https://fison.s3.amazonaws.com/john-doe.png',
      posts: [
        {
          title: 'Hello World!',
          image: 'https://fison.s3.amazonaws.com/hello-world.png',
        },
        {
          title: 'Hello Jane!',
          image: 'https://fison.s3.amazonaws.com/hello-jane.png',
        },
      ],
    };*/

    res.status(200).json(data);
  } catch (error) {
    next(error);
  }
});

app.listen(3000, () => {
  console.log('Server listening on http://localhost:3000 ...');
});
```

---
_Source: https://npm.io/package/fison · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
