{"id":22428403,"url":"https://github.com/momsfriendlydevco/express-middleware-formatter","last_synced_at":"2025-03-27T06:43:44.539Z","repository":{"id":57127890,"uuid":"120711915","full_name":"MomsFriendlyDevCo/express-middleware-formatter","owner":"MomsFriendlyDevCo","description":"ExpressJS middleware that provides alternative output formats from JSON data","archived":false,"fork":false,"pushed_at":"2022-08-29T09:44:50.000Z","size":130,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-25T12:50:54.617Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MomsFriendlyDevCo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-08T04:43:26.000Z","updated_at":"2022-08-29T09:44:54.000Z","dependencies_parsed_at":"2022-08-31T17:21:09.064Z","dependency_job_id":null,"html_url":"https://github.com/MomsFriendlyDevCo/express-middleware-formatter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MomsFriendlyDevCo%2Fexpress-middleware-formatter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MomsFriendlyDevCo%2Fexpress-middleware-formatter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MomsFriendlyDevCo%2Fexpress-middleware-formatter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MomsFriendlyDevCo%2Fexpress-middleware-formatter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MomsFriendlyDevCo","download_url":"https://codeload.github.com/MomsFriendlyDevCo/express-middleware-formatter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245798538,"owners_count":20673901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T20:14:42.736Z","updated_at":"2025-03-27T06:43:44.521Z","avatar_url":"https://github.com/MomsFriendlyDevCo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"@MomsFriendlyDevCo/express-middleware-formatter\n===============================================\nExpress middleware to provide alternate formatting of data when requested.\n\n\nSimple example\n--------------\n\n```javascript\nvar emf = require('@momsfriendlydevco/express-middleware-formatter');\n\napp.get('/api/data', emf(), function(req, res) {\n\tres.send(someData);\n});\n\n# If /api/data is requested - normal JSON will be sent\n# If /api/data?format=csv - JSON data will be transformed into CSV\n# If /api/data?format=xlsx - JSON data will be transformed into XLSX\n# etc.\n\n```\n\nAPI\n===\n\n\nemf([options])\n--------------\nFunction which returns formatting middleware.\n\nSupported options:\n\n| Option           | Type                   | Default                | Description                                                                                       |\n|------------------|------------------------|------------------------|---------------------------------------------------------------------------------------------------|\n| `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 |\n| `forceArray`     | `boolean`              | `false`                | Wrap the data in an array if the data provided is an object                                       |\n| `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 |\n| `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 |\n| `unpack`         | `function` or `array \u003cfunction\u003e` | 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)` |\n| `csv`            | `object`               |                        | CSV specific options                                                                              |\n| `csv.filename`   | `string`               | `\"Exported Data.csv\"`  | Default filename when exporting as CSV                                                            |\n| `html`           | `object`               |                        | HTML specific options                                                                             |\n| `html.download`  | `boolean`              | `false`                | If false, the HTML will be shown in the browser, if true the file will be forced as a download    |\n| `html.filename`  | `string`               | `\"Exported Data.html\"` | Default filename when exporting as HTML                                                           |\n| `html.footer`    | `string`               | (Simple HTML footer)   | Set the HTML suffix after outputting the body content for HTML                                    |\n| `html.header`    | `string`               | (Simple HTML header)   | Set the HTML prefix before outputting the body content for HTML                                   |\n| `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` |\n| `ods`            | `object`               |                        | ODS specific options                                                                              |\n| `ods.filename`   | `string`               | `\"Exported Data.ods\"`  | Default filename when exporting as ODS                                                            |\n| `pdf`            | `object`               |                        | PDF specific options                                                                              |\n| `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 |\n| `pdf.filename`   | `string`               | `\"Exported Data.pdf\"`  | Default filename when exporting as PDF                                                            |\n| `xlsx`           | `object`               |                        | XLSX specific options                                                                             |\n| `xlsx.checkArray`  | `boolean`            | `true`                 | Verify that the input is an array, disable this if picking your own fields when using `xlsx.template` |\n| `xlsx.filename`  | `string`               | `\"Exported Data.xlsx\"` | Default filename when exporting as XLSX                                                           |\n| `xlsx.sheetName` | `string`               | `\"Exported Data\"`      | Default sheet name in the exported file                                                           |\n| `xlsx.template`  | `string` or `function` |                        | If specified use [@mfdc/spreadsheet-templater](https://github.com/MomsFriendlyDevCo/spreadsheet-templater) to format the XLSX output (see notes) |\n| `xlsx.templateData` | `function`          | `(req, res, settings, content) =\u003e content` | How to mangle the content data before its passed to @mfdc/spreadsheet-templater |\n\n\n**NOTES:**\n\n* 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) =\u003e ...)` is valid, `app.get('/somwhere', (res, res) =\u003e ..., emf())` is not.\n* Extracting data from a specified `key` occurs before `unpack` is processed\n* An array of items in `unpack` are evaluated in series, any combination of simple function return and promise return are supported\n* 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\n* If `xlsx.template` is a function it is called as `(req, res, emfSettings, content)`\n* If `xlsx.template` is an async function or returns a promise it will be resolved asynchronously before continuing\n* 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\n\n\nemf.formats\n-----------\nAn 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.\n\n\nemf.flatten(object)\n-------------------\nFunction used internally to take a nested object and return an flattened object in dotted notation.\n\n\nemf.unflatten(object)\n---------------------\nFunction used internally to expand dotted notation object keys into a nested object.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmomsfriendlydevco%2Fexpress-middleware-formatter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmomsfriendlydevco%2Fexpress-middleware-formatter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmomsfriendlydevco%2Fexpress-middleware-formatter/lists"}