{"id":17381731,"url":"https://github.com/Salesflare/hapi-csv","last_synced_at":"2025-02-27T10:30:26.200Z","repository":{"id":9692742,"uuid":"62885539","full_name":"Salesflare/hapi-csv","owner":"Salesflare","description":"Convert response to CSV based on the response schema","archived":false,"fork":false,"pushed_at":"2024-01-15T04:10:20.000Z","size":596,"stargazers_count":6,"open_issues_count":9,"forks_count":6,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-10-02T20:47:04.778Z","etag":null,"topics":["csv","hapi","hapi-csv","hapijs","joi"],"latest_commit_sha":null,"homepage":"","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/Salesflare.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-08T11:59:34.000Z","updated_at":"2024-09-17T19:17:31.000Z","dependencies_parsed_at":"2023-01-13T15:30:58.435Z","dependency_job_id":null,"html_url":"https://github.com/Salesflare/hapi-csv","commit_stats":null,"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salesflare%2Fhapi-csv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salesflare%2Fhapi-csv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salesflare%2Fhapi-csv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Salesflare%2Fhapi-csv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Salesflare","download_url":"https://codeload.github.com/Salesflare/hapi-csv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219842816,"owners_count":16556562,"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":["csv","hapi","hapi-csv","hapijs","joi"],"created_at":"2024-10-16T07:01:36.109Z","updated_at":"2024-10-16T07:04:43.306Z","avatar_url":"https://github.com/Salesflare.png","language":"JavaScript","readme":"# hapi-csv [![Build Status](https://travis-ci.org/Salesflare/hapi-csv.svg?branch=master)](https://travis-ci.org/Salesflare/hapi-csv)\n\n## What\n\nConverts the response to csv based on the Joi response schema when the Accept header includes `text/csv` or `application/csv` or the requested route ends with `.csv`.\nConverts the response to xlsx (Excel) with the `enableExcel` option when the Accept header includes `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` or the requested route ends with `.xlsx`.\n\n## How\n\n`npm install --save hapi-csv`\n\nRegister the hapi-csv plugin on the server\n\n```javascript\nawait server.register({\n    plugin: require('hapi-csv'),\n    options: {\n        maximumElementsInArray: 5,\n        separator: ',',\n        resultKey: 'items'\n    }\n});\n```\n\nWhen you have a route on which a response schema is defined, like in the example below, the plugin will convert the response to csv when the Accept header includes `text/csv` or `application/csv` or the requested route ends with `.csv`\n\n```javascript\n\nconst routes = [{\n    method: 'GET',\n    path: '/users',\n    handler: Users.getAll,\n    config: {\n        response: {\n            schema: Joi.object().keys({\n                first_name: Joi.string(),\n                last_name: Joi.string(),\n                age: Joi.number()\n            })\n        }\n    }\n}]\n```\n\nEither do `GET /users` with header `Accept: text/csv` or `Accept: application/csv`.\nOr do `GET /users.csv`.\nThe header approach is preferred.\nWhen the request path ends in `.csv` the `.csv` part will be stripped and the accept header will be set to `text/csv`.\n\nCurrently the `Content-Disposition` header is set to `attachment;` by default since this plugin is intended for exporting purposes, if this hinders you just let us know.\n\n### Paginated responses\n\nTo handle typical pagination responses pass the `resultKey` option. The value is the top level key you want to convert to csv.\n\n```json\n// paginated response\n{\n    \"page\": 1,\n    \"items\": [\n        { \"name\": \"Anton\", \"age\": 22 },\n        { \"name\": \"Lisa\", \"age\": 25 }\n    ]\n}\n```\n\n```javascript\nawait server.register({\n    plugin: require('hapi-csv'),\n    options: {\n        resultKey: 'items' // We only want the `items` in csv\n    }\n});\n```\n\n### Dynamic schemas\n\nhapi-csv supports dynamic response schemas as well.\nImagine one of your property's schema is dynamic but you still want to export the value of it to csv.\nYou can tell hapi-csv to translate a given key on the fly when it is converting the response to csv (`onPreResponse`).\nOn the route config set the plugin config to an object like\n\n```javascript\n{\n    'keyPath': async (request) =\u003e {\n\n        return JoiSchema;\n    }\n}\n```\n\nThe key is the path of the property you want to resolve dynamically.\nE.g.\n\n```javascript\nJoi.object().keys({\n    a: Joi.object(),\n    b: Joi.object().keys({\n        c: Joi.object()\n    })\n})\n```\n\nIf you want to convert `a` the key would be `a`.\nFor `c` it would be `b.c`.\n\nFull example:\n\n```javascript\nserver.route([{\n    ...,\n    config: {\n        ...,\n        response: {\n            schema: Joi.object().keys({\n                first_name: Joi.string(),\n                last_name: Joi.string(),\n                age: Joi.number(),\n                custom: Joi.object(),\n                deepCustom: Joi.object().keys({\n                    deepestCustom: Joi.object()\n                })\n            })\n        },\n        plugins: {\n            'hapi-csv': {\n                'custom': (request) =\u003e {\n\n                    const schema = Joi.object().keys({\n                        id: Joi.number(),\n                        name: Joi.string()\n                    });\n\n                    return schema;\n                },\n                'deepCustom.deepestCustom': (request) =\u003e {\n\n                    throw new Error('nope');\n                }\n            }\n        }\n     }\n ])\n```\n\n### Excel\n\nYou can also enable Excel conversion.\nIt will convert the response to xlsx (Excel) when the Accept header includes `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` or the requested route ends with `.xlsx`.\n\n```javascript\nawait server.register({\n    plugin: require('hapi-csv'),\n    options: {\n        enableExcel: true,\n        excelWriteOptions: { /* compression: false */ }\n    }\n});\n```\n\n`excelWriteOptions` takes anything for [https://github.com/SheetJS/js-xlsx#writing-options](https://github.com/SheetJS/js-xlsx#writing-options)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSalesflare%2Fhapi-csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSalesflare%2Fhapi-csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSalesflare%2Fhapi-csv/lists"}