npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-route-name

v3.0.0

Published

give names to your api endpoints and easily get their url.

Downloads

4

Readme

Api Route Name

give names to your api endpoints and easily get their url.

Installation

npm install api-route-name

Benefits

  • not having to remember the full path to your api endpoints
  • easily modify the url/arguments without changing a lot of code
  • readability/data independence: your routes will be defined in their own file not in ajax calls

Usage

declare an array of routes as so:

const routes = [
  {
    prefix: '/api/v1',
    routes: [
      {
        name: 'auth',
        prefix: 'auth',
        resources: { signup: 'signup', login: 'login', user: 'user/:id' },
      },
      {
        name: 'users',
        prefix: 'users',
        resources: {
          get: ':id',
        },
      },
    ],
  },
];

import the class:

const ApiRouteName = require('api-route-name').default; or import ApiRouteName from 'api-route-name

initialize an ApiRouteName object with the route array you declared.

const route = new ApiRouteName(routes)

get your urls with the get method

const signup = route.get('auth.signup');
//signup = "/api/v1/signup"
const user = route.get('user', { id: 10 });
//user =  "/api/v1/user/10"

Urls and Names

the prefix attribute specifies a prefix for all the routes in the routes array and it's children.

any object with a prefix should have a name attribute but it's not required.

every routes can have a nested routes, in that case it should have a prefix and a name.

to specify the bottom-level routes you need to declare a resources object inside one of the objects in a routes array

a route in a route object like

[{
    prefix: "prefix1",
    name: "name1"
    routes: [{
        prefix: "prefix2",
        name: "name2"
        routes: [{
            prefix: "prefix3",
            name: "name3"
            resources: {
                routeName: "routeURL"
            }
        }]
    }]
}]

will have the name name1.name2.name3.routeName and a path of prefix1/prefix2/prefix3/routeURL

the name of the resource is taken from the attribute name.

Arguments

api-route-name uses the url-pattern library for it's pattern matching so consider reading it's docs.

you can add arguments to your urls by adding a colon ":" before the argument name. for example you want to get the posts for a specific user you would do the following:

{
...
userPosts:"users/:id/posts"
}

then to retrieve the posts for user with id 21 you pass an object with attributes matching your arguments.

const posts = route.get('userPost', { id: 21 });
//posts =  "{prefix you specified}/user/21/posts"

you can add multiple arguments blogsByDate : "users/:name/blogposts/:date"

and get the url with routeNames(routes,'blogsByDate',{name:"John Doe",date:"2019-1-2"})

Recommended Usage

I recommend putting your routes in a separate module, creating a function that returns the value from calling routeNames with your routes object and exporting that function as default.

//filename: api-routes.js
import ApiRouteName from 'api-route-name';

const routes = [
  {
    //specify your routes here
  },
];

export default new ApiRouteName(routes);

then use it in anywhere in your app

//other file
import route from './api-routes';
route.get('posts.create'),