# retil-nav-scheme

> Consistent URLs made easy

Latest version **0.28.4** (published 2022-05-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install retil-nav-scheme
pnpm add retil-nav-scheme
yarn add retil-nav-scheme
bun add retil-nav-scheme
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.28.4 |
| Published | 2022-05-17 |
| First published | 2021-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 100.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | James K Nelson |
| Maintainers | jamesknelson |

## Links

- npm: https://www.npmjs.com/package/retil-nav-scheme
- npm.io page: https://npm.io/package/retil-nav-scheme

## Dependencies (2)

- [retil-nav](https://npm.io/package/retil-nav.md) ^0.28.4
- [retil-support](https://npm.io/package/retil-support.md) ^0.28.4

## Recent versions

- 0.28.4 (latest) — 2022-05-17
- 0.28.3 — 2022-05-17
- 0.28.2 — 2022-02-14
- 0.28.1 — 2022-02-14
- 0.28.0 — 2022-02-06
- 0.27.0 — 2022-02-04
- 0.26.2 — 2022-01-28
- 0.26.0 — 2021-12-18

## README

# retil-nav-scheme

Link and route your app with a well-typed, refactorable URL scheme.

## Usage

### `createScheme(urlFunction)`

Define your URL scheme in separate files adjacent to your routes. This allows URLs to be imported and referenced without needing to load the full route content.

### `nestScheme(urlFunction, scheme): nestedNavScheme`

Nests a scheme underneath a url, possibly including paremeters. Returns a nested scheme object, which can be passed to a key in a `scheme()` call.

### `patternFor(schemeNode)`

Returns a routing pattern for a specific scheme entry. The pattern is compatible with retil-nav, and all other [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) based routing libraries.

## Example

```ts
/**
 * appURLs.ts
 */

import { createScheme, nestScheme } from 'retil-nav-scheme'
import { encode as encodeBase58UUID } from 'uuid-base58'

import { PostParams, PostQuery } from './post/postURLs'
import profileURLs, { ProfileParams } from './profile/profileURLs'

export default createScheme({
  top: () =>  '/',

  post: ({
    userHandle,
    postId,
    ...query
  }: PostParams & PostQuery) => ({
    query: { ...query },
    pathname: `/@${userHandle}/${encodeBase58UUID(postId)}`,
  }),

  login: () => `/login`,
  logout: () => `/logout`,

  profile: nestScheme(
    (profileParams: ProfileParams) => `/@${profileParams.userHandle}`,
    profileURLs,
  ),
})
```

```ts
/**
 * appLoader.ts
 */

import { loadLazy } from 'retil-mount'
import { loadMatch } from 'retil-nav'
import { patternFor } from 'retil-nav-scheme'

import urls from './appURLs'

export default loadMatch<AppEnv>({
  [patternFor(urls.top)]: loadLazy(() => import('./front/frontLoader')),
  [patternFor(urls.post)]: loadLazy(() => import('./post/postLoader')),
  [patternFor(urls.login)]: loadLazy(() => import('./login/loginLoader')),
  [patternFor(urls.logout)]: loadLazy(() => import('./logout/logoutLoader')),
  [patternFor(urls.profile)]: loadLazy(() => import('./profile/profileLoader')),
})
```

```ts
/**
 * profileURLs.ts
 */

import { createScheme } from 'retil-nav-scheme'

export type ProfileParams = {
  userHandle: string
}

export default createScheme({
  top: () => `/`,
  replies: () => `/replies`,
})
```

```ts
/**
 * profileLoader.ts
 */

import { loadMatch } from 'retil-nav'
import { patternFor } from 'retil-nav-scheme'

import urls from './profileURLs'

export default loadMatch<AppEnv>({
  [patternFor(urls.top)]: loadLazy(() => import('./profileLoader')),
  [patternFor(urls.replies)]: loadLazy(() => import('./profileRepliesLoader')),
})
```

---
_Source: https://npm.io/package/retil-nav-scheme · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
