Coder Social home page Coder Social logo

app-router's Introduction

App Route

App route core package for js runtime, e.g Node Bun.

Install

npm i @app-route/core

Usage

  • build build the directory tree
  • match match url return directory node and params

example:

import { build, match } from "@app-route/core";

const root = await build("./app", {
  resolve: async (node) => {
    // return extra data to `node.resolved`
    // example:
    return {
      hello: "world",
    };
  },
});

const matched = match("/hello", root);

if (!matched) return;

const [node, params] = matched;

// node.resolved.hello === "world"
// params: {}

Dynamic Routes

The App Router file system routing has three types of dynamic route segments

  • [slug]
  • [...slug]
  • [[...slug]]
app
├── roles
│   └── [id]
│       └── [name]
├── posts
│   └── [[...ids]]
└── users
    └── [...ids]

Corresponding URLs, routes, and parameter matching table

URL Route Params
/roles/a/b roles/[id]/[name] { "id": "a", "name": "b" }
/roles/a roles/[id] { "id": "a" }
/users/a/b users/[...ids] { "ids": ["a", "b"] }
/users/a users/[...ids] { "ids": ["a"] }
/users users {}
/posts/a/b posts/[[...ids]] { "ids": ["a", "b"] }
/posts/a posts/[[...ids]] { "ids": ["a"] }
/posts posts/[[...ids]] {"ids": []}

Get Params in the interface

Node Struct

interface BasicTreeNode<R = unknown> {
  name: string;
  route: string | undefined;
  path: string;
  parent?: BasicTreeNode<R>;
  children: BasicTreeNode<R>[];
  resolved?: R;
}

LICENSE

MIT @anuoua

app-router's People

Contributors

anuoua avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.