# @momsfriendlydevco/express-middleware-formatter

> ExpressJS middleware that provides alternative output formats from JSON data

Latest version **1.2.0** (published 2022-08-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @momsfriendlydevco/express-middleware-formatter
pnpm add @momsfriendlydevco/express-middleware-formatter
yarn add @momsfriendlydevco/express-middleware-formatter
bun add @momsfriendlydevco/express-middleware-formatter
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2022-08-29 |
| First published | 2018-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 21.2 KB |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Matt Carter |
| Maintainers | hash-bang, 1watt, eekthecat, melromero, mr-yellow |
| Keywords | express, expressjs, data, formatter, csv, xlsx |

## Links

- npm: https://www.npmjs.com/package/@momsfriendlydevco/express-middleware-formatter
- Repository: https://github.com/MomsFriendlyDevCo/express-middleware-formatter
- Issues: https://github.com/MomsFriendlyDevCo/express-middleware-formatter/issues
- npm.io page: https://npm.io/package/@momsfriendlydevco/express-middleware-formatter

## Dependencies (10)

- [temp](https://npm.io/package/temp.md) ^0.9.1
- [xlsx](https://npm.io/package/xlsx.md) ^0.15.6
- [buffed](https://npm.io/package/buffed.md) ^1.0.0
- [prince](https://npm.io/package/prince.md) ^1.8.0
- [cheerio](https://npm.io/package/cheerio.md) ^1.0.0-rc.3
- [fast-csv](https://npm.io/package/fast-csv.md) ^2.5.0
- [node-pdftk](https://npm.io/package/node-pdftk.md) ^2.1.2
- [flatten-obj](https://npm.io/package/flatten-obj.md) ^3.1.1
- [async-chainable](https://npm.io/package/async-chainable.md) ^2.6.1
- [@momsfriendlydevco/spreadsheet-templater](https://npm.io/package/@momsfriendlydevco/spreadsheet-templater.md) ^1.1.8

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 1.2.0 (latest) — 2022-08-29
- 1.1.16 — 2020-07-01
- 1.1.14 — 2020-03-22
- 1.1.12 — 2019-09-05
- 1.1.11 — 2019-09-04
- 1.1.10 — 2019-09-01
- 1.1.9 — 2019-09-01
- 1.1.8 — 2019-07-19
- 1.1.6 — 2019-07-19
- 1.1.5 — 2019-06-11
- 1.1.4 — 2019-06-11
- 1.1.3 — 2019-06-10
- 1.1.2 — 2019-05-15
- 1.1.1 — 2019-02-07
- 1.1.0 — 2019-02-05
- … 8 more at https://npm.io/package/@momsfriendlydevco/express-middleware-formatter/versions

## README

@MomsFriendlyDevCo/express-middleware-formatter
===============================================
Express middleware to provide alternate formatting of data when requested.


Simple example
--------------

```javascript
var emf = require('@momsfriendlydevco/express-middleware-formatter');

app.get('/api/data', emf(), function(req, res) {
	res.send(someData);
});

# If /api/data is requested - normal JSON will be sent
# If /api/data?format=csv - JSON data will be transformed into CSV
# If /api/data?format=xlsx - JSON data will be transformed into XLSX
# etc.

```

API
===


emf([options])
--------------
Function which returns formatting middleware.

Supported options:

| Option           | Type                   | Default                | Description                                                                                       |
|------------------|------------------------|------------------------|---------------------------------------------------------------------------------------------------|
| `filename`       | `string`               | Unset                  | If set this will be the filename used for any output format that requires a filename, overriding their own format specific filenames |
| `forceArray`     | `boolean`              | `false`                | Wrap the data in an array if the data provided is an object                                       |
| `format`         | `string` or `function` | See source             | Forced format to return as a string or if a function how to determine the format. By default this uses the `req.query.format` property if present |
| `key`            | `string`               | `null`                 | Extract tabular data from this key path (dotted or array notation is supported) instead of assuming the whole JSON response is the tabular data |
| `unpack`         | `function` or `array <function>` | Undefined    | A function or array of functions to run to mangle the data into something that can be processed, promise returns are supported. Called as `(data, req, res, settings)` |
| `csv`            | `object`               |                        | CSV specific options                                                                              |
| `csv.filename`   | `string`               | `"Exported Data.csv"`  | Default filename when exporting as CSV                                                            |
| `html`           | `object`               |                        | HTML specific options                                                                             |
| `html.download`  | `boolean`              | `false`                | If false, the HTML will be shown in the browser, if true the file will be forced as a download    |
| `html.filename`  | `string`               | `"Exported Data.html"` | Default filename when exporting as HTML                                                           |
| `html.footer`    | `string`               | (Simple HTML footer)   | Set the HTML suffix after outputting the body content for HTML                                    |
| `html.header`    | `string`               | (Simple HTML header)   | Set the HTML prefix before outputting the body content for HTML                                   |
| `html.passthru`  | `boolean`              | `false`                | Force the HTML output plugin to compute the data but not resolve the response - used internally by upstream formatters like `pdf` |
| `ods`            | `object`               |                        | ODS specific options                                                                              |
| `ods.filename`   | `string`               | `"Exported Data.ods"`  | Default filename when exporting as ODS                                                            |
| `pdf`            | `object`               |                        | PDF specific options                                                                              |
| `pdf.download`   | `boolean`              | `true`                 | If false, the PDF will be shown in the browser (if the browser supports it), if true the file will be forced as a download |
| `pdf.filename`   | `string`               | `"Exported Data.pdf"`  | Default filename when exporting as PDF                                                            |
| `xlsx`           | `object`               |                        | XLSX specific options                                                                             |
| `xlsx.checkArray`  | `boolean`            | `true`                 | Verify that the input is an array, disable this if picking your own fields when using `xlsx.template` |
| `xlsx.filename`  | `string`               | `"Exported Data.xlsx"` | Default filename when exporting as XLSX                                                           |
| `xlsx.sheetName` | `string`               | `"Exported Data"`      | Default sheet name in the exported file                                                           |
| `xlsx.template`  | `string` or `function` |                        | If specified use [@mfdc/spreadsheet-templater](https://github.com/MomsFriendlyDevCo/spreadsheet-templater) to format the XLSX output (see notes) |
| `xlsx.templateData` | `function`          | `(req, res, settings, content) => content` | How to mangle the content data before its passed to @mfdc/spreadsheet-templater |


**NOTES:**

* Since EMF has to extend the `res.send()` / `res.json()` Express functions, EMF must be installed as a middleware *before* calls to those functions occur. Thus: `app.get('/somwhere', emf(), (res, res) => ...)` is valid, `app.get('/somwhere', (res, res) => ..., emf())` is not.
* Extracting data from a specified `key` occurs before `unpack` is processed
* An array of items in `unpack` are evaluated in series, any combination of simple function return and promise return are supported
* If `xlsx.template` is specified or if it returns a path to a valid file it the input data (post `unpack`) will be passed to [@mfdc/spreadsheet-templater](https://github.com/MomsFriendlyDevCo/spreadsheet-templater) for formatting. This allows a template XLSX file which gets passed the input data
* If `xlsx.template` is a function it is called as `(req, res, emfSettings, content)`
* If `xlsx.template` is an async function or returns a promise it will be resolved asynchronously before continuing
* As with the above point @mfdc/spreadsheet-templater is an *optional* dependency and must be installed at the project level if `xlsx.template` is specified


emf.formats
-----------
An object of all the supported output formats. The key is the short format name (`csv`, `pdf` etc.) with the key as the export of the format module.


emf.flatten(object)
-------------------
Function used internally to take a nested object and return an flattened object in dotted notation.


emf.unflatten(object)
---------------------
Function used internally to expand dotted notation object keys into a nested object.

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