# api-route-name

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

Latest version **3.0.0** (published 2019-11-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install api-route-name
pnpm add api-route-name
yarn add api-route-name
bun add api-route-name
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2019-11-15 |
| First published | 2019-07-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | mohammed almassri |
| Maintainers | mohammed-almassri |
| Keywords | routing, named routes, api |

## Links

- npm: https://www.npmjs.com/package/api-route-name
- Repository: https://github.com/mohammed-almassri/api-route-name
- Homepage: https://github.com/mohammed-almassri/api-route-name#readme
- Issues: https://github.com/mohammed-almassri/api-route-name/issues
- npm.io page: https://npm.io/package/api-route-name

## Dependencies (2)

- [qs](https://npm.io/package/qs.md) ^6.9.0
- [url-pattern](https://npm.io/package/url-pattern.md) ^1.0.3

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 3.0.0 (latest) — 2019-11-15
- 2.1.0 — 2019-11-01
- 2.0.3 — 2019-10-21
- 2.0.2 — 2019-07-13
- 2.0.1 — 2019-07-09
- 2.0.0 — 2019-07-09
- 1.1.2 — 2019-07-07
- 1.1.1 — 2019-07-07
- 1.1.0 — 2019-07-02
- 1.0.0 — 2019-07-01

## 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:

```javascript
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;`
<br>
or
<br>
`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

```javascript
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

```javascript
[{
    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](https://www.npmjs.com/package/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:

```javascript
{
...
userPosts:"users/:id/posts"
}
```

then to retrieve the posts for user with id 21 you pass an object with attributes matching your arguments.

```javascript
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.

```javascript
//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

```javascript
//other file
import route from './api-routes';
route.get('posts.create'),
```

---
_Source: https://npm.io/package/api-route-name · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
