All Projects β†’ drewjbartlett β†’ Routegen

drewjbartlett / Routegen

Define your API and SPA routes in one place. Use them anywhere. Only 1.3kb.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Routegen

Universal Router
A simple middleware-style router for isomorphic JavaScript web apps
Stars: ✭ 1,598 (+1758.14%)
Mutual labels:  router, routes, spa
Hoppscotch
πŸ‘½ Open source API development ecosystem https://hoppscotch.io
Stars: ✭ 34,569 (+40096.51%)
Mutual labels:  api, spa
Shgf
Simple HTTP golang framework
Stars: ✭ 13 (-84.88%)
Mutual labels:  api, router
Vanilla Ui Router
Simple vanilla JavaScript router
Stars: ✭ 42 (-51.16%)
Mutual labels:  router, spa
Wobike
Documentation of Bike Sharing APIs πŸš΄πŸ›΄πŸ›΅
Stars: ✭ 705 (+719.77%)
Mutual labels:  api, map
Forge Server Utils
Tools for accessing Autodesk Forge APIs from modern Node.js apps.
Stars: ✭ 23 (-73.26%)
Mutual labels:  api, generator
Copper
Copper is a set of Go packages that help you build backend APIs quickly and with less boilerplate.
Stars: ✭ 35 (-59.3%)
Mutual labels:  api, router
Full Stack
Full stack, modern web application generator. Using Flask, PostgreSQL DB, Docker, Swagger, automatic HTTPS and more.
Stars: ✭ 451 (+424.42%)
Mutual labels:  api, generator
Pgo
Go library for PHP community with convenient functions
Stars: ✭ 51 (-40.7%)
Mutual labels:  api, map
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+12265.12%)
Mutual labels:  api, generator
Mailman
Mailman is a GUI to help you manage your email accounts stored in a MySQL/MariaDB database.
Stars: ✭ 62 (-27.91%)
Mutual labels:  api, spa
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (+713.95%)
Mutual labels:  api, router
Beauty Vuejs Boilerplate
❀️ Real world base Vue.js app. Access/refresh tokens auth, api services, http client, vuex modules
Stars: ✭ 583 (+577.91%)
Mutual labels:  api, spa
Jsoo router
A small router to write easily single-page-app in Js_of_ocaml
Stars: ✭ 24 (-72.09%)
Mutual labels:  router, spa
Gearbox
Gearbox βš™οΈ is a web framework written in Go with a focus on high performance
Stars: ✭ 455 (+429.07%)
Mutual labels:  api, router
Altair
Lightweight and Robust API Gateway written in Go
Stars: ✭ 34 (-60.47%)
Mutual labels:  api, router
Flowa
πŸ”₯Service level control flow for Node.js
Stars: ✭ 66 (-23.26%)
Mutual labels:  api, router
Diet
A tiny, fast and modular node.js web framework. Good for making fast & scalable apps and apis.
Stars: ✭ 394 (+358.14%)
Mutual labels:  api, router
Verb
HEADS UP! Verb is going though a major transition, we've completely refactored everything from the ground up. If you're interested, please see the dev branch.
Stars: ✭ 442 (+413.95%)
Mutual labels:  api, generator
Webvr Webpack Boilerplate
A webvr multi-scenes Single-page application for three.js, webpack
Stars: ✭ 47 (-45.35%)
Mutual labels:  router, spa

routeGen

npm npm npm

Define your API and SPA routes in one place. Use them anywhere.

The Problem

Defining your routes as "magic strings" is messy never a good idea, especially as your app grows. You might have something like this:

// foo.js
http.get(`/user/${userId}`).then(...)
http.post(`/some-service/create-something`).then(...)

// and then again in bar.js
http.get(`/user/${userId}`).then(...)
http.post(`/some-service/create-something`).then(...)

And 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.

The Solution / Basic Usage

Rather 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.

// routes.js
import routeGen from 'routegen';

// all routes will be prefixed with the baseUrl
const routes = routeGen({
  baseUrl: 'http://myapi.test/api',
});

routes.set('login', '/auth/login'); 
routes.set('get_user_by_id', '/user/{id}');

export default routes;
// some-other-file.js
import http from 'utils/http';
import routes from './routes';

http.post(routes.generate('login'), { data }); // POST => http://myapi.test/api/auth/login
http.generate(routes.generate('get_user_by_id', { id: 1 })); // GET => http://myapi.test/api/user/1

An example with axios

import axios from 'axios';
import routes from './routes';

axios.post(routes.generate('login'), { data }); // POST => http://myapi.test/api/auth/login
axios.generate(routes.generate('get_user_by_id', { id: 1 })); // GET => http://myapi.test/api/user/1

Installation

npm i routegen --save

API

routeGen({...})

To 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.

import routeGen from 'routegen';

const routes = routeGen({
  baseUrl: 'http://myapi.test/api',
});

You may also define multiple sets of routes:

import routeGen from 'routegen';

const apiRoutes = routeGen({
  baseUrl: 'http://myapi.test/api',
});

const spaRoutes = routeGen({
  baseUrl: 'http://mysite.test/dasbhboard',
});

set(key, value)

Pretty straight forward. Set a new route.

routes.set('get_users', '/users');
routes.set('get_user_by_id', '/users/{id}');

generate(key, params = {})

Retrieve a value from the routes.

const routes = routeGen();

routes.set('foo_bar', '/foo/bar');

routes.generate('foo_bar'); // => /foo/bar

Some 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().

const routes = routeGen();

routes.set('get_user_by_id', '/user/{id}');

routes.generate('get_user_by_id', { id: 1 }); // GET => /user/1

lock()

If you'd like to define your routes in one place and disallow setting any new routes once exported, you may call the lock() method.

const routes = routeGen();

routes.set('foo_bar', '/foo/bar');
routes.set('foo_bar_baz', '/foo/bar/{id}');

export default routes.lock();

Calling lock() returns an api with access only to generate(), and all(). So, the above example could not be modified once imported.

prefix({ path, name })

You may have times where you want to prefix routes with a namespace and/or a path. prefix() allows for just that.

import routeGen from 'routegen';

const routes = routeGen();

routes.prefix({ path: '/auth', name: 'auth' }, {
  login: '/login',
  logout: '/logout',
  register: '/register',
});

routes.generate('auth_login') // /auth/login
routes.generate('auth_logout') // /auth/logout
routes.generate('auth_register') // /auth/register

all()

If you need a way to retrieve all the routes at once, you may call all().

routes.all().forEach(route => ...)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].