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

@entando/router

v2.0.1

Published

router used with redux for entando frontend applications

Downloads

8

Readme

this package is currently deprecated and no longer supported

router

This is a router used with redux in the entando projects.

Installation instructions

run npm i @entando/router


To integrate the router:

1. Create the route configuration JSON object

{
  "mode": "browser", // history mode, can be 'hash' or 'browser'
  "pathPrefix": "my-web-context",
  "routes": [
    { "name": "homePage", "path": "/" },
    { "name": "about", "path": "/about" },
    { "name": "user", "path": "/user/:id" },
  ],
  "notFoundRoute": { "name": "notFound", "path": "/404" }
}
  • mode the history type. Can be 'hash' or 'browser' (default: 'browser')
  • pathPrefix optional value to be added to the beginning of every path in order to run app in sub-folder (e.g., '/appbuilder')
  • routes an array of objects representing the routes.
    • name the route unique name
    • path the route path pattern
  • notFoundRoute the route to redirect to, if no route is matched

The path can contain parameters, following the path-to-regexp syntax. Some example:

Mandatory parameters

/user/:id

  • /user/616{ route: 'user', params: { id: '616'} }
  • /user ⇒ no match
Optional parameters (?)

/pictures/:viewMode?

  • /pictures/list{ route: 'pictures', params: { viewMode: 'list'} }
  • /pictures{ route: 'pictures', params: { viewMode: ''} }
Custom regexp parameters

Use them to restrict a parameter to match a regular expression. E.g. to have the id parameter to be a sequence of numbers:

/user/:id(\\d+) (Note the double-escaped backslashes)

  • /user/12{ route: 'user', params: { id: '12'} }
  • /user/abc ⇒ no match
Unnamed parameters

Parameters with no name, the params key will be their position index. E.g.:

/coords/(\\d+)/(\\d+)

  • /coords/12/34{ route: 'coords', params: { '0': '12', '1': '34' } }
Repeated parameters

Use * for 0 or more repetitions

/users/:ids*

  • /users/john/jack/jim{ route: 'users', params: { 'ids': 'john/jack/jim' } }
  • /users{ route: 'users', params: { } }

Use + for 1 or more repetitions

/groupcall/:users+

  • /groupcall/john/jack/jim{ route: 'groupcall', params: { 'users': 'john/jack/jim' } }
  • /groupcall ⇒ no match

2. Add the router reducer to the app reducer

import { combineReducers } from 'redux';
import { routerReducer as router } from '@entando/router';

export default combineReducers({
  router, // the default key is 'router'
});

The resulting state will be:

{
  router: {
    location: {...},
    route: '...',
    params: {...},
    searchParams: {...},
  }
}

Note that the state key must be router. If for any reason you need to mount the router state to a different path, you must use the setRouterSelector function, passing a selector to the router state. E.g.

import { combineReducers } from 'redux';
import { routerReducer, setRouterSelector } from '@entando/router';

// needed to set up the custom router key(s)
setRouterSelector(state => state.one.two);

export default combineReducers({
  one: combineReducers({
    two: routerReducer,
  }),
});

The resulting state will be:

{
  one: {
    two: {
      location: {...},
      route: '...',
      params: {...},
      searchParams: {...},
    }
  }
}

3. Call the routerConfig passing the Redux store and the config objects

The routerConfig function needs the Redux store and the JSON routes config object. E.g.:

import { createStore } from 'redux';
import rootReducer from 'app/reducer';
import { routerConfig } from '@entando/router';

// this can be defined elsewhere and imported
const routerConfig = {
  routes: [
    { name: 'home', path: '/' },
  ],
  notFoundRoute: { name: 'notFound', path: '/not-found' },
};


const store = createStore(rootReducer);

routerConfig(store, routerConfig);

4. Create a routing Component

Just create a React Component taking the route via props, and connect it to the state:

import React from 'react';
import PropTypes from 'prop-types';

import { connect } from 'react-redux';
import { getRoute } from '@entando/router';

import showcase from 'react-showcase';
import LoginPage from 'ui/login/LoginPage';
import LoginForm from 'ui/login/LoginForm';

const MyRouter = ({ route }) => {
  switch (id) {
    case 'home':  return <MyHomePage />;
    case 'about': return <MyAboutPage />;
    default:      return <MyNotFoundPage />;
  }
};

MyRouter.propTypes = {
  route: PropTypes.string.isRequired,
};

const mapStateToProps = state => ({
  route: getRoute(state),
});

export default connect(mapStateToProps, null)(MyRouter);

5. Link / gotoRoute

Use Link component to create internal links. Usage:

// sample router config JSON:
const config = {
  routes: [
    { name: 'user', path: '/user/:id' }
  ],
};
// sample Component using Link
import { Link } from '@entando/router';

// ... into the component JSX ...

  // resulting URL: /user/72
  <Link
    route="user"
    params={{ id: '72' }}
  />

  // resulting URL: /user/72?view=profile
  <Link
    route="user"
    params={{ id: '72' }}
    searchParams={{ view: 'profile' }}
  />

To redirect to a route programmatically (e.g. in response to an authentication error), the gotoRoute function must be used. E.g.:

import { gotoRoute } from '@entando/router';

// ... other code ...

  // resulting URL: /user/72
  gotoRoute('user', { id: '72' });

  // resulting URL: /user/72?view=profile
  gotoRoute('user', { id: '72' }, { view: 'profile' });