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

@operational/router

v0.0.3

Published

Router utility to deal with custom base path

Downloads

12

Readme

Operational router

npm (tag)

The goal of this tiny library is to deal with custom basePath in a sub-application.

In the context of Contiamo, every application can be run in standalone mode or be embeded inside a global contiamo product. In the second case, some url params are managed in an higher level, this is exactly what we try to solve with this library. This is our solution to compose "easily" multiple application in one.

Note: If you have a better solution, please tell us :wink: We are always happy to learn something.

Getting started

  1. npm i @operational/router react-router react-router-dom
  2. Follow the Archictecture setup

Architecture setup

Let's do an example to illustrate the setup of this library:

<Router>
  <Route path="/:tenant/:project/pantheon" render={/* ... */} />
  <Route path="/:tenant/:project/hub" render={/* ... */} />
  <Route
    path="/:tenant/:project/labs"
    render={({ history: { push, replace } }) => (
      <OperationalRouterProvider
        basePath="/:tenant/:project/labs/"
        pushState={push}
        replaceState={replace}>
        {({ resolvePath, basePath, pushState }) => {
          /**
           *  Labs application router, relative to the `basePath`
           * - I can use `basePath` to define my `Route` and retrieve `tenant` and `project` from the parent
           * - I can use `<Link to={resolvePath("project")} />` to navigate inside the labs context
           * - I can use `pushState("project")` to navigate programatically
           */
        }}
      </OperationalRouterProvider>
    )}
  />

<Router>

Finally, this lib exports OperationalRouter and withOperationalRouter to have access to the same resolvePath, basePath and pushState anywhere inside my application.

Render function style:

const MyPage = () => (
  <Page title="my page">
    <OperationalRouter>
      {({ pushState }) => <button onClick={() => pushState("/")}>Go to labs home</button>}
    </OperationalRouter>
  </Page>
);

export default Page;

HOC style:

const MyPage = ({ pushState }) => (
  <Page title="my page">{<button onClick={() => pushState("/")}>Go to labs home</button>}</Page>
);

export default withOperationalRouter(MyPage);

How to use this lib to compose applications

In contiamo, every applications expose a component with this shape:

export interface MyApplicationProps extends OperationalRouterProps {
  /* custom props if needed to configure the app */
}

export const MyApplication: React.SFC<MyApplicationProps> = props => (
  <OperationalRouterProvider {...props}>
    {({ basePath }) => {
      if (!basePath.includes(":tenantId")) {
        throw new Error("This application need to have ':tenantId' in the basePath")
      }

     return <Switch>
        <Route path={basePath} render={<Home />} />
        <Route path={`${basePath}/projects`} render={<Projects />} />
        <Route path={`${basePath}/users`} render={<Users />} />
      </Switch>
    }
  </OperationalRouterProvider>
);

Contributions

Issues, Pull Requests and extensions are welcome. No question is a silly question. Head on over to issues to see where you could get involved, or to open one that will help us further improve this project.