All Projects → Tyrrrz → route-descriptor

Tyrrrz / route-descriptor

Licence: MIT license
Single source of truth for routing

Programming Languages

typescript
32286 projects

Labels

Projects that are alternatives of or similar to route-descriptor

vlm
Virtual loup de mer (aka Vlm) is an opensource sailing simulation
Stars: ✭ 25 (+47.06%)
Mutual labels:  routing
GTFS
.NET implementation of a General Transit Feed Specification (GTFS) feed parser.
Stars: ✭ 55 (+223.53%)
Mutual labels:  routing
journey
A conductor routing helper library
Stars: ✭ 35 (+105.88%)
Mutual labels:  routing
laroute
Generate Laravel route URLs from JavaScript.
Stars: ✭ 33 (+94.12%)
Mutual labels:  routing
app
Aplus Framework App Project
Stars: ✭ 338 (+1888.24%)
Mutual labels:  routing
routing-kit
🚍 High-performance trie-node router.
Stars: ✭ 95 (+458.82%)
Mutual labels:  routing
fastglue
Fastglue is an opinionated, bare bones wrapper that glues together fasthttp and fasthttprouter to act as a micro HTTP framework.
Stars: ✭ 71 (+317.65%)
Mutual labels:  routing
angular-ssr
Angular 14 Example SSR (Server side rendering)
Stars: ✭ 92 (+441.18%)
Mutual labels:  routing
svelte-hash-router
tienpv222.github.io/svelte-hash-router
Stars: ✭ 37 (+117.65%)
Mutual labels:  routing
es6-router
🌐 Simple client side router built in ES6
Stars: ✭ 16 (-5.88%)
Mutual labels:  routing
Router-deprecated
🛣 Simple Navigation for iOS - ⚠️ Deprecated
Stars: ✭ 458 (+2594.12%)
Mutual labels:  routing
framework
Aplus Full-Stack Framework
Stars: ✭ 172 (+911.76%)
Mutual labels:  routing
demo-ionic-tab-routing
An Ionic project which shows different kinds of route definition for a tab based layout.
Stars: ✭ 34 (+100%)
Mutual labels:  routing
BBB-routing
🅱🅱🅱-routing - a simulation of Network layer protocols with Byzantine Robustness
Stars: ✭ 15 (-11.76%)
Mutual labels:  routing
routex.js
🔼 Alternative library to manage dynamic routes in Next.js
Stars: ✭ 38 (+123.53%)
Mutual labels:  routing
routy
Routy is a lightweight high performance HTTP request router for Racket
Stars: ✭ 20 (+17.65%)
Mutual labels:  routing
organicmaps
🍃 Organic Maps is a free Android & iOS offline maps app for travelers, tourists, hikers, and cyclists. It uses crowd-sourced OpenStreetMap data and is developed with love by MapsWithMe (MapsMe) founders and our community. No ads, no tracking, no data collection, no crapware. Your donations and positive reviews motivate and inspire our small team!
Stars: ✭ 3,689 (+21600%)
Mutual labels:  routing
arcus.messaging
Messaging with Microsoft Azure in a breeze.
Stars: ✭ 20 (+17.65%)
Mutual labels:  routing
laravel-localized-routes
A convenient way to set up and use localized routes in a Laravel app.
Stars: ✭ 257 (+1411.76%)
Mutual labels:  routing
swagger routerl
Routing library that generate the routing table from swagger.yaml.
Stars: ✭ 14 (-17.65%)
Mutual labels:  routing

route-descriptor

Made in Ukraine Build Coverage Version Downloads Discord Donate Fuck Russia

🟢 Project status: active[?]

This package provides the means to statically represent routes, which helps establish a single source of truth for generating links inside an application.

💡 This library works best with TypeScript.

Install

  • 📦 npm: npm i route-descriptor

Usage

Routes are created by calling the route(...) function with the route's path and a type that encapsulates its parameters. This returns another function which can be further evaluated against a specific set of route parameters to resolve the matching URL.

Describing a static route

Static routes are routes that have no parameters and, as a result, always resolve to the same URL. To create a descriptor for a static route, call route(...) with just the route's path:

import { route } from 'route-descriptor';

const home = route('/home');

const href = home(); // '/home'

Describing a dynamic route

Dynamic routes can resolve to different URLs depending on the specified parameters. In order to create a descriptor for a dynamic route, call route(...) with a path template and a generic argument that defines the parameters it can accept:

import { route } from 'route-descriptor';

interface ProductParams {
  id: number;
}

const product = route<ProductParams>('/products/:id');

const href = product({ id: 3 }); // '/products/3'

To resolve the URL, the descriptor will try to match the parameter names with placeholders in the path template. If a parameter doesn't match with any of the placeholders, it will be added as a query parameter instead:

import { route } from 'route-descriptor';

interface ProductParams {
  id: number;
  showComments?: boolean;
}

const product = route<ProductParams>('/products/:id');

const href = product({
  id: 3,
  showComments: true
}); // '/products/3?showComments=true'

Retrieving the original path

Once descriptor is created, it's possible to retrieve its path template by accessing the path field:

import { route } from 'route-descriptor';

const profile = route<ProfileParams>('/profile/:id/:name?');

const path = profile.path; // '/profile/:id/:name?'

Combining with routing libraries

This package can be used in combination with practically any client-side routing library. For example, here is how to integrate it with React Router:

  • ./src/routes.ts:
// This module serves as a single source of truth for routing

import { route } from 'route-descriptor';

interface ProductParams {
  id: number;
  showComments?: boolean;
}

interface ProfileParams {
  id: number;
  name?: string;
}

export default {
  home: route('/home'),
  product: route<ProductParams>('/products/:id'),
  profile: route<ProfileParams>('/profile/:id/:name?')
};
  • ./src/App.tsx:
import { Route, Switch, BrowserRouter, Link } from 'react-router-dom';
import routes from './routes';

function Home() {
  // To resolve route link, pass the parameters that the route expects
  
  return (
    <div>
      <Link to={routes.home()}>Home</Link>
      <Link to={routes.profile({ id: 1, name: 'JohnDoe' })}>My Profile</Link>
      <Link to={routes.product({ id: 3, showComments: true })}>Random Product</Link>
    </div>
  );
}

function Product() {
  /* ... */
}

function Profile() {
  /* ... */
}

export default function App() {
  // We can use the `path` field to retrieve the original
  // path template and feed it to react-router.

  return (
    <BrowserRouter>
      <Switch>
        <Route path={routes.profile.path} component={Profile} />
        <Route path={routes.product.path} component={Product} />
        <Route path={routes.home.path} component={Home} />
      </Switch>
    </BrowserRouter>
  );
}

Static validation via TypeScript

This package is most useful when paired with TypeScript, as it provides static validation for parameters. For example, all of the following incorrect usages produce errors during compilation:

import { route } from 'route-descriptor';

const home = route('/home');

home({ id: 5 }); // <- error (static route can't accept parameters)
interface ProductParams {
  id: number;
  showComments?: boolean;
}

const product = route<ProductParams>('/products/:id');

product(); // <- error (dynamic route requires parameters)
product({ showComments: true }); // <- error (missing 'id')
product({ id: 3, name: 'apple' }); // <- error (unexpected 'name')
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].