{"id":15025044,"url":"https://github.com/hikerfeed/restful-resource","last_synced_at":"2025-04-12T12:51:40.842Z","repository":{"id":36301476,"uuid":"223047351","full_name":"hikerfeed/restful-resource","owner":"hikerfeed","description":"The JavaScript URL builder for creating consistent and RESTful resource requests, so you don't have to. Only 1.2kb.","archived":false,"fork":false,"pushed_at":"2023-01-05T01:21:13.000Z","size":1399,"stargazers_count":34,"open_issues_count":14,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-03-26T07:36:26.454Z","etag":null,"topics":["http","javascript","laravel","rest-api","restful-api","typescript","url-builder","url-generator"],"latest_commit_sha":null,"homepage":"https://hikerfeed.com","language":"TypeScript","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/hikerfeed.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}},"created_at":"2019-11-20T23:32:58.000Z","updated_at":"2024-12-29T12:29:32.000Z","dependencies_parsed_at":"2023-01-17T00:15:51.484Z","dependency_job_id":null,"html_url":"https://github.com/hikerfeed/restful-resource","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/hikerfeed%2Frestful-resource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hikerfeed%2Frestful-resource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hikerfeed%2Frestful-resource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hikerfeed%2Frestful-resource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hikerfeed","download_url":"https://codeload.github.com/hikerfeed/restful-resource/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248571427,"owners_count":21126517,"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":["http","javascript","laravel","rest-api","restful-api","typescript","url-builder","url-generator"],"created_at":"2024-09-24T20:01:26.962Z","updated_at":"2025-04-12T12:51:40.823Z","avatar_url":"https://github.com/hikerfeed.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg width=\"300\" src=\"https://hikerfeed.com/_nuxt/img/f3d1507.svg\"\u003e\u003c/p\u003e\n\n---\n\n# RESTful Resource\n\n\u003e The JavaScript URL builder for creating consistent and RESTful resource requests, so you don't have to.\n\n\u003cimg src=\"https://github.com/hikerfeed/restful-resource/workflows/Node CI/badge.svg\" /\u003e\n\n---\n\n### Installation\n\n```ts\nnpm i @hikerfeed/restful-resource --save\n```\n\n---\n\n### Usage\n\nRESTful Resource is a JavaScript URL builder for creating consistent and RESTful resource requests, so you don't have to. RESTful resource does _not_ make HTTP requests. Instead, it generates the proper routes that would match the controller name. For example, take this Laravel route which maps to a controller action:\n\n\n```php\nRoute::get('/users', 'UsersController@index');\n```\n\nYou can utilize RESTful resource like:\n\n```ts\nimport { createRestfulResource } from '@hikerfeed/restful-resource';\nimport http from 'my-http';\n\nconst UserResource = createRestfulResource('users');\n\nhttp.get(UserResource.index()); // =\u003e '/users'\n```\n\nCalling `UserResource.index()` will return the appropriate route for REST conventions, in this case for the `index` action. Each resource method accepts a `String`, `Number`, or `Array`. You may call any of the standard REST methods as such:\n\n```ts\nconst UserResource = createRestfulResource('users');\n\nhttp.get(UserResource.index()); // =\u003e '/users'\nhttp.post(UserResource.create()); // =\u003e '/users/create'\nhttp.post(UserResource.store()); // =\u003e '/users'\nhttp.get(UserResource.show(3)); // =\u003e '/users/3'\nhttp.get(UserResource.edit(4)); // =\u003e '/users/4/edit'\nhttp.patch(UserResource.update(5)); // =\u003e '/users/5'\nhttp.delete(UserResource.destroy('5')); // =\u003e '/users/5'\n```\n\nThis works nicely with a framework like Laravel which allows you to define a controller as a resource:\n\n```php\nRoute::resource('users', 'UsersController');\n```\n\n---\n\n#### Nested Routes\n\nIn plenty of cases you may have nested routes such as `/users/2/photos`. In this case, you can simply add a `.` between names. If you want to pass an id to the parent and child resource you may pass an `Array` of numbers.\n\n```ts\nconst UserPhotosResource = createRestfulResource('users.photos');\n\nhttp.get(UserPhotosResource.index(2)); // =\u003e '/users/2/photos'\nhttp.get(UserPhotosResource.update([2, 33])); // =\u003e '/users/2/photos/33'\n```\n\n---\n\n#### Excluding Routes\n\nLet's say you have a controller on your backend that excludes the actions such as `create`, `edit`. In Laravel, it may look like this:\n\n```php\nRoute::resource('hikes', 'HikesController')-\u003eexcept(['create', 'edit']);\n```\n\nThis would generate the following routes: \n\n- `GET   /hikes   HikesController@index`\n- `POST  /hikes   HikesController@store`\n- `GET   /hikes   HikesController@show`\n- `POST  /hikes   HikesController@update`\n- `POST  /hikes   HikesController@destroy`\n\n\nTo ensure you're not calling routes that don't exist on your API, you can pass the `except` option like so:\n\n```ts\n// typescript\nimport { createRestfulResource, RestfulResource } from '@hikerfeed/restful-resource';\n\nconst HikesResource = createRestfulResource('hikes', {\n  except: [RestfulResource.Routes.Create, RestfulResource.Routes.Edit],\n});\n\nHikesResource.index(); // /hikes\nHikesResource.create() // throws an Error\n```\n\n```js\n// javascript\nimport { createRestfulResource } from '@hikerfeed/restful-resource';\n\nconst HikesResource = createRestfulResource('hikes', {\n  except: ['create', 'edit'],\n});\n\nHikesResource.index(); // /hikes\nHikesResource.create() // throws an Error\n```\n\n---\n\n#### Only Including Certain Routes\n\nOn the contrary, you may want to _only_ include certain routes. In Laravel this may look like: \n\n```php\nRoute::resource('hikes', 'HikesController')-\u003eonly(['index']);\n```\n\nYou may pass an `only` option like so:\n\n```ts\n// typescript\nimport { createRestfulResource, RestfulResource } from '@hikerfeed/restful-resource';\n\nconst HikesResource = createRestfulResource('hikes', {\n  only: [RestfulResource.Routes.Index],\n});\n\nHikesResource.index(); // /hikes\nHikesResource.edit() // throws an Error\n```\n\n```js\n// javascript\nimport { createRestfulResource } from '@hikerfeed/restful-resource';\n\nconst HikesResource = createRestfulResource('hikes', {\n  only: ['index'],\n});\n\nHikesResource.index(); // /hikes\nHikesResource.edit() // throws an Error\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhikerfeed%2Frestful-resource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhikerfeed%2Frestful-resource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhikerfeed%2Frestful-resource/lists"}