{"id":18368774,"url":"https://github.com/greena13/rails-request","last_synced_at":"2025-04-06T17:31:52.534Z","repository":{"id":65371858,"uuid":"81919764","full_name":"greena13/rails-request","owner":"greena13","description":"Convert JSON and JavaScript objects to Rails-friendly formats","archived":false,"fork":false,"pushed_at":"2018-02-12T07:13:53.000Z","size":47,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T08:35:48.856Z","etag":null,"topics":["converter","javascript","rails","rest-api","update"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/greena13.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":"2017-02-14T07:51:34.000Z","updated_at":"2024-01-09T10:29:08.000Z","dependencies_parsed_at":"2023-01-20T01:32:10.781Z","dependency_job_id":null,"html_url":"https://github.com/greena13/rails-request","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greena13%2Frails-request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greena13%2Frails-request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greena13%2Frails-request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greena13%2Frails-request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greena13","download_url":"https://codeload.github.com/greena13/rails-request/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247522428,"owners_count":20952546,"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":["converter","javascript","rails","rest-api","update"],"created_at":"2024-11-05T23:27:24.647Z","updated_at":"2025-04-06T17:31:52.158Z","avatar_url":"https://github.com/greena13.png","language":"JavaScript","readme":"# rails-request\n\n[![npm](https://img.shields.io/npm/dm/rails-request.svg)]()\n[![Build Status](https://travis-ci.org/greena13/rails-request.svg)](https://travis-ci.org/greena13/rails-request)\n[![GitHub license](https://img.shields.io/github/license/greena13/rails-request.svg)](https://github.com/greena13/rails-request/blob/master/LICENSE)\n\nUtility for producing a Rails-compatible object from a JavaScript or JSON one; perfect for create (POST) or update (PUT) endpoints.\n\n## Usage\n\n```javascript\nimport railsRequest from 'rails-request';\n\nconst railsObject = railsRequest(obj);\n```\n\n## Creation objects\n\nBy default, calling `rails-request` with a JavaScript object will recursively convert your object to one that uses Rails-friendly attribute names and conventions. In particular:\n \n* Attribute names will be snake cased\n* A `_attributes` suffix will be added to all nested objects if not already present\n* Arrays of nested objects will be converted to hashes with index keys\n \n```javascript\nconst jsObject = {\n  userName: 'user123',\n  address: {\n    line1: '1 Street',\n    line2: 'City, Country'\n  },\n  achievementIds: [3,5],\n  photos: [\n    { id: 23, url: 'http://url.com/123' },    \n    { id: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst railsObject = railsRequest(jsObject);\n\nrailsObject === {\n  user_name: 'user123',\n  address_attributes: {\n    line1: '1 Street',\n    line2: 'City, Country'  \n  },\n  achievement_ids: [3,5],\n  photos_attributes: {\n    0: { id: 23, url: 'http://url.com/123' },\n    1: { id: 25, url: 'http://url.com/123' }\n  }\n}\n```\n\n### Customising creation objects\n\nYou can start to deviate from Rail's conventions if you need to do so by passing options as a second argument.\n\n\n#### Setting nested attribute suffix\n \n```javascript\nconst jsObject = {\n  userName: 'user123',\n  address: {\n    line1: '1 Street',\n    line2: 'City, Country'\n  },\n  achievementIds: [3,5],\n  photos: [\n    { id: 23, url: 'http://url.com/123' },    \n    { id: 25, url: 'http://url.com/123' }    \n  ]\n};\n\nconst railsObject = railsRequest(jsObject, { nestedAttributesSuffix: false });\n\nrailsObject === {\n  user_name: 'user123',\n  address: {\n    line1: '1 Street',\n    line2: 'City, Country'  \n  },\n  achievement_ids: [3,5],\n  photos: {\n    0: { id: 23, url: 'http://url.com/123' },\n    1: { id: 25, url: 'http://url.com/123' }\n  }\n}\n\nconst railsObject = railsRequest(jsObject, { nestedAttributesSuffix: '_fields' });\n\nrailsObject === {\n  user_name: 'user123',\n  address_fields: {\n    line1: '1 Street',\n    line2: 'City, Country'  \n  },\n  achievement_ids: [3,5],\n  photos_fields: {\n    0: { id: 23, url: 'http://url.com/123' },\n    1: { id: 25, url: 'http://url.com/123' }\n  }\n}\n```\n\n#### Changing attribute name format\n\nCurrently only `camelCase` and `snakeCase` (default) are supported.\n\n```javascript\nconst jsObject = {\n  userName: 'user123',\n  address: {\n    line1: '1 Street',\n    line2: 'City, Country'\n  },\n  achievementIds: [3,5],\n  photos: [\n    { id: 23, url: 'http://url.com/123' },    \n    { id: 25, url: 'http://url.com/123' }    \n  ]\n};\n\nconst railsObject = railsRequest(jsObject, { attributeFormat: 'camelCase' });\n\nrailsObject === {\n  userName: 'user123',\n  addressAttributes: {\n    line1: '1 Street',\n    line2: 'City, Country'  \n  },\n  achievementIds: [3,5],\n  photosAttributes: {\n    0: { id: 23, url: 'http://url.com/123' },\n    1: { id: 25, url: 'http://url.com/123' }\n  }\n}\n```\n\n## Update Objects\n\n`rails-request` provides a `diff` option for generating update objects, which:\n\n* Returns only the changes between two objects to reduce request payload\n* Always includes identifier fields when needed\n* Correctly sets destroy objects when nested objects have been removed from arrays\n\n```javascript\nconst jsObject = {\n  userName: 'user123',\n  address: {\n    id: 3,\n    line1: '1 Street',\n    line2: 'City, Country'\n  },\n  achievementIds: [3,5],\n  photos: [\n    { id: 23, url: 'http://url.com/123' },    \n    { id: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst newJsObject = {\n  userName: 'user4',\n  address: {\n    id: 3,\n    line1: '2 Street',\n    line2: 'City, Country'\n  },\n  achievementIds: [3,5,7],\n  photos: [\n    { id: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst railsObject = railsRequest(newJsObject, { diff: jsObject });\n\nrailsObject === {\n  user_name: 'user4', \n  address_attributes: {\n    id: 3,\n    line1: '2 Street'\n  },\n  achievement_ids: [3,5,7],\n  photos_attributes: [\n    { id: 23, _destroy: 1 }    \n  ]\n}\n```\n\n### Customising update objects\n\nLike creation objects, you can customise update objects if you need to. In addition to those listed below, you can also use any of the options mentioned above for creation objects.\n\n#### Changing identifier fields\n\nBy default, `rails-request` will use `id` as the only identifier field. You can add extra fields, or use alternative fields if you wish:\n\n```javascript\nconst jsObject = {\n  photos: [\n    { externalId: 23, url: 'http://url.com/123' },    \n    { externalId: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst newJsObject = {\n  photos: [\n    { externalId: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst railsObject = railsRequest(newJsObject, { diff: jsObject, identifiers: [ 'externalId'] });\n\nrailsObject === {\n  photos_attributes: [\n    { external_id: 23, _destroy: 1 }    \n  ]\n}\n```\n\n#### Customising destroy objects\n\n```javascript\nconst jsObject = {\n  photos: [\n    { id: 23, url: 'http://url.com/123' },    \n    { id: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst newJsObject = {\n  photos: [\n    { id: 25, url: 'http://url.com/123' }    \n  ]\n}; \n\nconst railsObject = railsRequest(newJsObject, { diff: jsObject, destroyAttributeValue: true });\n\nrailsObject === {\n  photos_attributes: [\n    { id: 23, _destroy: true }    \n  ]\n};\n\nconst railsObject = railsRequest(newJsObject, { diff: jsObject, destroyAttributeName: 'delete' });\n\nrailsObject === {\n  photos_attributes: [\n    { id: 23, delete: 1 }    \n  ]\n};\n```\n\n## Running the test suite\n\n```bash\nnpm run tests\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreena13%2Frails-request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreena13%2Frails-request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreena13%2Frails-request/lists"}