项目作者: drewjbartlett

项目描述 :
Define your API and SPA routes in one place. Use them anywhere. Only 1.3kb.
高级语言: JavaScript
项目地址: git://github.com/drewjbartlett/routegen.git
创建时间: 2018-08-21T23:51:16Z
项目社区:https://github.com/drewjbartlett/routegen

开源协议:

下载


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:

  1. // foo.js
  2. http.get(`/user/${userId}`).then(...)
  3. http.post(`/some-service/create-something`).then(...)
  4. // and then again in bar.js
  5. http.get(`/user/${userId}`).then(...)
  6. 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.

  1. // routes.js
  2. import routeGen from 'routegen';
  3. // all routes will be prefixed with the baseUrl
  4. const routes = routeGen({
  5. baseUrl: 'http://myapi.test/api',
  6. });
  7. routes.set('login', '/auth/login');
  8. routes.set('get_user_by_id', '/user/{id}');
  9. export default routes;
  1. // some-other-file.js
  2. import http from 'utils/http';
  3. import routes from './routes';
  4. http.post(routes.generate('login'), { data }); // POST => http://myapi.test/api/auth/login
  5. http.generate(routes.generate('get_user_by_id', { id: 1 })); // GET => http://myapi.test/api/user/1

An example with axios

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

Installation

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

  1. import routeGen from 'routegen';
  2. const routes = routeGen({
  3. baseUrl: 'http://myapi.test/api',
  4. });

You may also define multiple sets of routes:

  1. import routeGen from 'routegen';
  2. const apiRoutes = routeGen({
  3. baseUrl: 'http://myapi.test/api',
  4. });
  5. const spaRoutes = routeGen({
  6. baseUrl: 'http://mysite.test/dasbhboard',
  7. });

set(key, value)

Pretty straight forward. Set a new route.

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

generate(key, params = {})

Retrieve a value from the routes.

  1. const routes = routeGen();
  2. routes.set('foo_bar', '/foo/bar');
  3. 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().

  1. const routes = routeGen();
  2. routes.set('get_user_by_id', '/user/{id}');
  3. 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.

  1. const routes = routeGen();
  2. routes.set('foo_bar', '/foo/bar');
  3. routes.set('foo_bar_baz', '/foo/bar/{id}');
  4. 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.

  1. import routeGen from 'routegen';
  2. const routes = routeGen();
  3. routes.prefix({ path: '/auth', name: 'auth' }, {
  4. login: '/login',
  5. logout: '/logout',
  6. register: '/register',
  7. });
  8. routes.generate('auth_login') // /auth/login
  9. routes.generate('auth_logout') // /auth/logout
  10. routes.generate('auth_register') // /auth/register

all()

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

  1. routes.all().forEach(route => ...)