{"id":23510819,"url":"https://github.com/danielkov/restponse","last_synced_at":"2025-05-13T17:17:18.670Z","repository":{"id":98242339,"uuid":"96525719","full_name":"danielkov/restponse","owner":"danielkov","description":"Easy RESTful JSON response generator for clean API development.","archived":false,"fork":false,"pushed_at":"2017-07-24T10:10:39.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T11:52:33.596Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-07T10:00:12.000Z","updated_at":"2017-07-07T15:11:45.000Z","dependencies_parsed_at":"2023-05-18T22:00:13.999Z","dependency_job_id":null,"html_url":"https://github.com/danielkov/restponse","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/danielkov%2Frestponse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Frestponse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Frestponse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielkov%2Frestponse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielkov","download_url":"https://codeload.github.com/danielkov/restponse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990503,"owners_count":21995776,"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-25T12:12:26.874Z","updated_at":"2025-05-13T17:17:18.651Z","avatar_url":"https://github.com/danielkov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RESTponse\nA simple library that makes it extremely simple to streamline API responses.\n___\n\nInstall the package with\n\n```sh\nnpm i -s restponse\n```\n\nor use it with Yarn\n\n```sh\nyarn add restponse\n```\n\nThis package has 0 dependencies (excluding devDependencies used for testing and coverage), and leverages the built in Node JS `http` module to provide status codes. Refer to said module to see what status codes you can include in your app.\n\n### Usage example (with Express):\n\n```js\nconst Restponse = require('restponse')\nconst restponse = new Restponse()\n\napp.get('/', (req, res) =\u003e {\n  res.send(restponse[200])\n})\n```\n\nThe endpoint will respond with the following:\n\n```json\n{\n  \"status\": \"200\",\n  \"reason\": \"OK\"\n}\n```\n\nExtending with more options:\n\nTo add a new key and value pair to the generated JSON, you can just add it to the config object, which is a parameter of the constructor.\n\nIn this example we'll add version and name information:\n\n```js\nconst restponse = Restponse({\n  version: '1.2',\n  name: 'MyAPI'\n})\n```\n\nThe response to the previous request will now be:\n\n```json\n{\n  \"status\": \"200\",\n  \"reason\": \"OK\",\n  \"name\": \"MyAPI\",\n  \"version\": \"1.2\"\n}\n```\n\nThe type of the value for these can also be a number or a string, which will behave the same way, however if you provide a function, it will take the options object as the parameter and the returned value will be passed into the returned object.\n\n\n```js\nconst restponse = new Restponse({\n  url: function ({status, version}) {\n    return `https://mywebsite.com/api/${version}/status/${status}`\n  },\n  version: '1.2'\n})\n```\n\nThe response to this request will now be:\n\n```json\n{\n  \"status\": \"200\",\n  \"reason\": \"OK\",\n  \"version\": \"1.2\",\n  \"url\": \"https://mywebsite.com/api/1.2/status/200\"\n}\n```\n\nThis feature is particularly useful if you want developers to have quick access to more information about your responses.\n\nFinally if you want to add fields that only apply to specific statuses, you can do so by giving an object that has the specific key for the request.\n\nIt is advised to provide at least an empty string to all the status codes.\n\n```js\nconst restponse = new Restponse({\n  message: {\n    200: 'This means everything is fine.'\n  }\n})\n```\n\nResponse:\n\n```json\n{\n  \"status\": \"200\",\n  \"reason\": \"OK\",\n  \"message\": \"This means everything is fine.\"\n}\n```\n\nIf you want to add objects (like arrays) to your response, you can do so after intialization. To add payload to the response, you can just add it as a property:\n\n```js\napp.get('/', (req, res) =\u003e {\n  let response = Object.assign({}, restponse[200])\n  response.payload = 'Welcome to index.'\n  res.send(response)\n})\n```\n\n## Contributing\n\nFeel free to add PRs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Frestponse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielkov%2Frestponse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielkov%2Frestponse/lists"}