{"id":13527233,"url":"https://github.com/Prestaul/jsend","last_synced_at":"2025-04-01T09:31:18.552Z","repository":{"id":13324264,"uuid":"16011097","full_name":"Prestaul/jsend","owner":"Prestaul","description":"Node utilities and middleware to assist with sending and handling jsend responses","archived":false,"fork":false,"pushed_at":"2022-11-05T12:45:09.000Z","size":46,"stargazers_count":36,"open_issues_count":4,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T14:37:20.916Z","etag":null,"topics":[],"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/Prestaul.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":"2014-01-17T20:37:44.000Z","updated_at":"2025-03-22T08:56:04.000Z","dependencies_parsed_at":"2023-01-13T17:25:38.821Z","dependency_job_id":null,"html_url":"https://github.com/Prestaul/jsend","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fjsend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fjsend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fjsend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fjsend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Prestaul","download_url":"https://codeload.github.com/Prestaul/jsend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246616059,"owners_count":20806048,"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-08-01T06:01:43.776Z","updated_at":"2025-04-01T09:31:18.276Z","avatar_url":"https://github.com/Prestaul.png","language":"JavaScript","readme":"# jsend\n\nUtilities and middleware to assist with sending and handling [jsend](https://github.com/omniti-labs/jsend) responses.\n\n\n### Installation\n\n```bash\nnpm install -S jsend\n```\n\n\n\n## Response Creation\n\nResponses can be created using the `success`, `fail`, and `error` methods:\n\n```js\n// You can pass data or a jsend response to success\njsend.success({ foo: 'bar' }); // { status: 'success', data: { foo: 'bar' } }\njsend.success([ 'foo', 'bar' ]); // { status: 'success', data: [ 'foo', 'bar' ] }\njsend.success(1337); // { status: 'success', data: 1337 }\njsend.success(false); // { status: 'success', data: false }\n\n// You can pass data or a jsend response to fail\njsend.fail({ itsa: 'trap' }); // { status: 'fail', data: { itsa: 'trap' } }\njsend.fail(true); // { status: 'fail', data: true }\n\n// You can pass a message or an object with a message and optionally data and code\njsend.error('No soup for you!'); // { status: 'error', message: 'No soup for you!' }\njsend.error({\n\tcode: 123,\n    message: 'No soup for you!'\n}); // { status: 'error', code: 123, message: 'No soup for you!' }\njsend.error({\n\tcode: 123,\n    message: 'No soup for you!',\n    data: false\n}); // { status: 'error', code: 123, message: 'No soup for you!', data: false }\n```\n\n\n### Node-style Callbacks \n\nThe `fromArguments` method can be used to create jsend responses from node-style (i.e. `(err, data)`) callback arguments:\n\n```js\ngetData(id, function(err, data) {\n\tvar response = jsend.fromArguments(err, data);\n});\n```\n\n\n### Http Middleware\n\nThe jsend middleware has methods for easily sending \"succeess\", \"fail\" and \"error\" responses:\n\n```js\nexpressApp.use(jsend.middleware);\n\nexpressApp.get('/', function(req, res) {\n\tif(!req.params.someParam)\n\t\treturn res.jsend.fail({ validation:['someParam is required'] });\n\n\tloadData(req.params.someParam, function(err, data) {\n\t\tif(err) return res.jsend.error(err);\n\t\tres.jsend.success(data);\n\t});\n});\n```\n\nOr you can use `res.jsend` as a callback to respond automatically with a jsend wrapped response:\n\n```js\nexpressApp.use(jsend.middleware);\n\nexpressApp.get('/', function(req, res) {\n\tloadData(req.params.someParam, res.jsend);\n});\n```\n\nsame as:\n\n```js\nexpressApp.use(jsend.middleware);\n\nexpressApp.get('/', function(req, res) {\n\tloadData(req.params.someParam, function(err, data) {\n\t\tres.jsend(err, data);\n\t});\n});\n```\n\n\n### Responding with jsend sans-middleware\n\nIf you don't want to use the middleware you can simply create jsend responses and send them with `res.json`:\n\n```js\ngetData(id, function(err, data) {\n\tres.json(jsend.fromArguments(err, data));\n});\n```\n\n\n\n## Response Validation\n\nBy default `jsend.isValid` validates that all required properties exist.\n\n```js\nvar jsend = require('jsend');\n\n// Returns true\njsend.isValid({\n\tstatus: 'success',\n\tdata: { foo:'bar' }\n});\n\n// Returns false\njsend.isValid({\n\tstatus: 'success'\n});\n\n// Returns true\njsend.isValid({\n\tstatus: 'success',\n\tdata: { foo:'bar' },\n\tjunk: 'is ok'\n});\n```\n\nUsing the `strict` flag causes `jsend.isValid` to also validate that extraneous properties do not exist.\n\n```js\nvar jsend = require('jsend')({ strict:true });\n\n// Returns true\njsend.isValid({\n\tstatus: 'success',\n\tdata: { foo:'bar' }\n});\n\n// Returns false\njsend.isValid({\n\tstatus: 'success'\n});\n\n// Returns false\njsend.isValid({\n\tstatus: 'success',\n\tdata: { foo:'bar' },\n\tjunk: 'is ok'\n});\n```\n\n\n### Response Forwarding\n\nYou can forward a jsend response to a node style callback using the `forward` method.\n\n```js\njsend.forward(json, function(err, data) {\n\t// err will be set if status was 'error' or 'fail'\n\t// data will be set to the \"data\" property in all cases\n});\n```\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPrestaul%2Fjsend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPrestaul%2Fjsend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPrestaul%2Fjsend/lists"}