Giter Site home page Giter Site logo

routtl's Introduction

Routtl (pronounced rout·le)

Lightweight routing primitives.

WIP, don't use in production. Expect occasional breaking changes until we hit stable in 1.0.0.

Usage

import { route, string } from 'routtl';

const helloRoute = route`/hello/${['world', string]}`;

const url = helloRoute.encode({ world: 'world' });
//     ^ '/hello/world'

const data = helloRoute.decode('/hello/world');
//     ^ { world: 'world' }

Overview

Routtl is a small set of primitives for defining routes and encoding/decoding data to/from that route. It is small (less than 500kb of JS), has no dependencies, and provides type-safe decoding. It uses JavaScript's tagged template literals to provide a declarative, composable API for defining routes.

Decoders

Default decoders are provided for primitive JS types (e.g. string, number, boolean, date). A simple Decoder interface is provided to extend or build your own decoding. Additionally, a decoder factory is provided for arrays of arbitrary types.

Templating HTML

When templating HTML, you have use route.encode() to generate a href for an <a>. Here is an example using lit-html as a template engine:

import { route, string } from 'routtl';
import { render, html } from 'lit-html';

const helloRoute = route`/hello/${['world', string]}`;

const url = helloRoute.encode({ world: 'world' });

render(document.body, html`<a href="${url}">Link</a>`);

Composing

RouteParsers can be composed together as shown here:

import { route, string } from 'routtl';

const helloRoute = route`/hello`;

const worldRoute = route`${helloRoute}/world/${['id', string]}`;

const url = helloRoute.encode({ id: 'foo' });
//     ^ '/hello/world/foo'

Roadmap

  • Built-in decoders
  • Decoder type safely
  • Nest Route Parsers
  • Decode query parameters
  • Ignore case and other options

Contributing

See the guide.

Inspiration

Lots of inspiration for this project! @ncthbrt was crucial to the early prototypes. vue-router, navaid, typesafe-routes have been influential in the design of these primitives.

There is allow an API called URLPattern that has recently emerged from the WHATWG standards body that addresses some of the problems routtle is solving. At the time of this writing most browsers besides Firefox and Safari support it and a polyfill is available.

routtl's People

Contributors

chrisshank avatar

Stargazers

Dylan Frankland avatar Duc-Thien Bui avatar Aaron avatar Jesse Pence avatar Michael J. Roberts avatar Travis Arnold avatar Will Mruzek avatar Stéphane Le Dorze avatar omar avatar Winston Fassett avatar  avatar Toni Villena avatar Joseph Fitz Hughes avatar Stijn Van Hulle avatar Andri Óskarsson avatar Federico Biccheddu avatar Oscar Luis Jiménez avatar Steven Scaffidi avatar Alexandre Stahmer avatar Óscar Garcia avatar Darius avatar Dmytro Tsysar avatar Andrew Chou avatar David Khourshid avatar Egor A. Trubnikov-Panov avatar Tobi Obeck avatar  avatar Patrick Smith avatar  avatar Johannes avatar Aldo Velasco avatar  avatar Bahlul Hasanli avatar Carlos Camilo avatar adamcl avatar Christian Todd avatar Justin Philpott avatar methimpact avatar Sung Jeon avatar Lan Qingyong avatar Hyeseong Kim avatar Natalie Cuthbert avatar Patrick Vale avatar Maxime Alardo avatar DK avatar Vis avatar Ian Jones avatar  avatar Willy Brauner avatar Hazmee Evolutioan.ai avatar Marco Link avatar Robert Penner avatar Brandon Orther avatar Santi avatar

Watchers

Johannes avatar James Cloos avatar Justin Philpott avatar Vis avatar  avatar

Forkers

gifhuppp

routtl's Issues

suggestion - how to sync context/store and update router

Love to see an example of how to update routing based on state(context) and then update state based on route

e.g
?modal=open&items=10

There are so many instances in front end where this needs to happen. Any routing requests obviously need to be validated against.

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.