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

atomic-router

v0.10.1

Published

Simple routing implementation that provides abstraction layer instead of inline URL's and does not break your architecture

Downloads

2,871

Readme

Atomic Router

Simple routing implementation that provides abstraction layer instead of inline URL's and does not break your architecture

  • Type-safe
  • No inline URL's
  • Atomic routes
  • Does not break architecture
  • Framework-agnostic
  • Isomorphic (pass your own history instance and it works everywhere)

Read the docs: atomic-router.github.io

❗️ Attention: At the moment atomic-router team collecting issues and feature requests to improve current design. Upgrade atomic-router version with caution. We are going to write migration guide when/if the release will contain breaking changes. Thank you for reporting issues 🧡

Get view-library bindings

Installation

$ npm install effector atomic-router

Initialization

Create your routes wherever you want:

// pages/home
import { createRoute } from 'atomic-router';
export const homeRoute = createRoute();

// pages/posts
import { createRoute } from 'atomic-router';
export const postsRoute = createRoute<{ postId: string }>();

And then create a router

// app/routing
import { createHistoryRouter } from 'atomic-router';
import { createBrowserHistory, createMemoryHistory } from 'history';
import { homeRoute } from '@/pages/home';
import { postsRoute } from '@/pages/posts';

const routes = [
  { path: '/', route: homeRoute },
  { path: '/posts', route: postsRoute },
];

const router = createHistoryRouter({
  routes: routes,
});

// Attach history
const history = isSsr ? createMemoryHistory() : createBrowserHistory();
router.setHistory(history);

Why atomic routes?

There are 3 purposes for using atomic routes:

  • To abstract the application from hard-coded paths
  • To provide you a declarative API for a comfortable work
  • To avoid extra responsibility in app features

Examples

  1. In your model, create effect and store which you'd like to trigger:
export const getPostFx = createEffect<{ postId: string }, Post>(
  ({ postId }) => {
    return api.get(`/posts/${postId}`);
  }
);

export const $post = restore(getPostFx.doneData, null);
  1. And just trigger it when postPage.$params change:
//route.ts
import { createRoute } from 'atomic-router';
import { getPostFx } from './model';

const postPage = createRoute<{ postId: string }>();

sample({
  source: postPage.$params,
  filter: postPage.$isOpened,
  target: getPostFx,
});

Imagine that we have a good architecture, where our code can be presented as a dependency tree.
So, we don't make neither circular imports, nor they go backwards.
For example, we have Card -> PostCard -> PostsList -> PostsPage flow, where PostsList doesn't know about PostsPage, PostCard doesn't know about PostsList etc.

But now we need our PostCard to open PostsPage route.
And usually, we add extra responisbility by letting it know what the route is

const PostCard = ({ id }) => {
  const post = usePost(id);

  return (
    <Card>
      <Card.Title>{post.title}</Card.Title>
      <Card.Description>{post.title}</Card.Description>
      {/* NOOOO! */}
      <Link to={postsPageRoute} params={{ postId: id }}>
        Read More
      </Link>
    </Card>
  );
};

With atomic-router, you can create a "personal" route for this card:

const readMoreRoute = createRoute<{ postId: id }>();

And then you can just give it the same path as your PostsPage has:

const routes = [
  { path: '/posts/:postId', route: readMoreRoute },
  { path: '/posts/:postId', route: postsPageRoute },
];

Both will work perfectly fine as they are completely independent

API Reference

// Params is an object-type describing query params for your route
const route = createRoute<Params>();

// Stores
route.$isOpened; // Store<boolean>
route.$params; // Store<{ [key]: string }>
route.$query; // Store<{ [key]: string }>

// Events (only watch 'em)
route.opened; // Event<{ params: RouteParams, query: RouteQuery }>
route.updated; // Event<{ params: RouteParams, query: RouteQuery }>
route.closed; // Event<{ params: RouteParams, query: RouteQuery }>

// Effects
route.open; // Effect<RouteParams>
route.navigate; // Effect<{ params: RouteParams, query: RouteQuery }>

// Note: Store, Event and Effect is imported from 'effector' package