Beraliv / atomic-router

Platform-agnostic router that does not break your architecture

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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)

Installation

$ npm install effector atomic-router

Initialization

Create your routes wherever you want:

// pages/home
export const homeRoute = createRoute()

// pages/posts
export const postsRoute = createRoute<{ postId: string }>()

And then create a router

// app/routing
import { homeRoute } from '@/pages/home'
import { postsRoute } from '@/pages/home'

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

Fetch post on page open
  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 { getPostFx } from './model'

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

guard({
  source: postPage.$params,
  filter: postPage.$isOpened,
  target: getPostFx
})
Avoid breaking architecture

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

// 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.left       // Event<{ params: RouteParams, query: RouteQuery }>

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

About

Platform-agnostic router that does not break your architecture

License:MIT License


Languages

Language:TypeScript 100.0%