{"id":13765391,"url":"https://github.com/drewjbartlett/routegen","last_synced_at":"2025-07-31T17:32:43.520Z","repository":{"id":32908887,"uuid":"145631871","full_name":"drewjbartlett/routegen","owner":"drewjbartlett","description":"Define your API and SPA routes in one place. Use them anywhere. Only 1.3kb.","archived":false,"fork":false,"pushed_at":"2022-12-07T23:30:07.000Z","size":817,"stargazers_count":83,"open_issues_count":17,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-17T01:32:48.491Z","etag":null,"topics":["api","generator","map","router","routes","routes-api","spa"],"latest_commit_sha":null,"homepage":"","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/drewjbartlett.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":"2018-08-21T23:51:16.000Z","updated_at":"2023-02-15T04:24:22.000Z","dependencies_parsed_at":"2023-01-14T22:37:02.573Z","dependency_job_id":null,"html_url":"https://github.com/drewjbartlett/routegen","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/drewjbartlett%2Froutegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Froutegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Froutegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Froutegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drewjbartlett","download_url":"https://codeload.github.com/drewjbartlett/routegen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228275004,"owners_count":17895008,"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":["api","generator","map","router","routes","routes-api","spa"],"created_at":"2024-08-03T16:00:38.103Z","updated_at":"2024-12-05T10:14:34.908Z","avatar_url":"https://github.com/drewjbartlett.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Helper Components"],"sub_categories":[],"readme":"# routeGen\n\n[![npm](https://img.shields.io/npm/v/routegen.svg?style=flat-square)](https://www.npmjs.com/package/routegen)\n[![npm](https://img.shields.io/travis/drewjbartlett/routegen.svg?branch=master\u0026style=flat-square)](https://www.npmjs.com/package/routegen)\n[![npm](https://img.shields.io/npm/dt/routegen.svg?style=flat-square)](https://www.npmjs.com/package/routegen)\n\nDefine your API and SPA routes in one place. Use them anywhere.\n\n### The Problem\n\nDefining your routes as \"magic strings\" is messy never a good idea, especially as your app grows. You might have something like this: \n\n```js\n// foo.js\nhttp.get(`/user/${userId}`).then(...)\nhttp.post(`/some-service/create-something`).then(...)\n\n// and then again in bar.js\nhttp.get(`/user/${userId}`).then(...)\nhttp.post(`/some-service/create-something`).then(...)\n```\nAnd what if you decide to change your routes? You need to update them all over the place. What if you mistype a route in one place? It can break things and get messy. `routeGen` fixes all these issues. \n\n### The Solution / Basic Usage\n\nRather than have the \"magic strings\" all over the place, you can define them in one place and use them everywhere. Need to inerpolate a value into a url? No problem. You can even disable redefining routes after they're exported with the magic `lock()` method. Grouping certain routes with a prefix and namespace is a breeze with the `prefix()` method. `routeGen` is simple, useful and incredibly lightweight at only 1.6kb.\n\n```js\n// routes.js\nimport routeGen from 'routegen';\n\n// all routes will be prefixed with the baseUrl\nconst routes = routeGen({\n  baseUrl: 'http://myapi.test/api',\n});\n\nroutes.set('login', '/auth/login'); \nroutes.set('get_user_by_id', '/user/{id}');\n\nexport default routes;\n```\n\n```js\n// some-other-file.js\nimport http from 'utils/http';\nimport routes from './routes';\n\nhttp.post(routes.generate('login'), { data }); // POST =\u003e http://myapi.test/api/auth/login\nhttp.generate(routes.generate('get_user_by_id', { id: 1 })); // GET =\u003e http://myapi.test/api/user/1\n```\n\n### An example with axios\n\n```js\nimport axios from 'axios';\nimport routes from './routes';\n\naxios.post(routes.generate('login'), { data }); // POST =\u003e http://myapi.test/api/auth/login\naxios.generate(routes.generate('get_user_by_id', { id: 1 })); // GET =\u003e http://myapi.test/api/user/1\n```\n\n___\n\n### Installation \n\n```bash\nnpm i routegen --save\n```\n\n___\n\n### API\n\n#### routeGen({...})\n\nTo define sets of routes, simply call import `routegen` and call it as a function. The only parameter it accepts is an object with a `baseUrl`.\n\n```js\nimport routeGen from 'routegen';\n\nconst routes = routeGen({\n  baseUrl: 'http://myapi.test/api',\n});\n```\n\nYou may also define multiple sets of routes: \n\n```js\nimport routeGen from 'routegen';\n\nconst apiRoutes = routeGen({\n  baseUrl: 'http://myapi.test/api',\n});\n\nconst spaRoutes = routeGen({\n  baseUrl: 'http://mysite.test/dasbhboard',\n});\n\n```\n\n#### set(key, value)\n\nPretty straight forward. Set a new route.\n\n```js\nroutes.set('get_users', '/users');\nroutes.set('get_user_by_id', '/users/{id}');\n```\n\n#### generate(key, params = {})\n\nRetrieve a value from the routes. \n\n```js\nconst routes = routeGen();\n\nroutes.set('foo_bar', '/foo/bar');\n\nroutes.generate('foo_bar'); // =\u003e /foo/bar\n```\n\nSome routes require an interpolated value. For instance, getting a user by id. You can define a route that accepts params and retrieve it with `generate()`.\n\n```js\nconst routes = routeGen();\n\nroutes.set('get_user_by_id', '/user/{id}');\n\nroutes.generate('get_user_by_id', { id: 1 }); // GET =\u003e /user/1\n```\n\n#### lock()\n\nIf you'd like to define your routes in one place and disallow setting any new routes once exported, you may call the `lock()` method.\n\n```js\nconst routes = routeGen();\n\nroutes.set('foo_bar', '/foo/bar');\nroutes.set('foo_bar_baz', '/foo/bar/{id}');\n\nexport default routes.lock();\n```\n\nCalling `lock()` returns an api with access only to `generate()`, and `all()`. So, the above example could not be modified once imported.\n \n#### prefix({ path, name })\n \nYou may have times where you want to prefix routes with a namespace and/or a path. `prefix()` allows for just that.\n\n```js\nimport routeGen from 'routegen';\n\nconst routes = routeGen();\n\nroutes.prefix({ path: '/auth', name: 'auth' }, {\n  login: '/login',\n  logout: '/logout',\n  register: '/register',\n});\n\nroutes.generate('auth_login') // /auth/login\nroutes.generate('auth_logout') // /auth/logout\nroutes.generate('auth_register') // /auth/register\n```\n\n#### all()\n\nIf you need a way to retrieve all the routes at once, you may call `all()`. \n\n```js\nroutes.all().forEach(route =\u003e ...)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrewjbartlett%2Froutegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrewjbartlett%2Froutegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrewjbartlett%2Froutegen/lists"}