• Stars
    star
    206
  • Rank 190,504 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Typesafe routing for your Remix apps.

remix-routes

remix-routes automatically generates typesafe helper functions for manipulating internal links in your Remix apps.

video.mp4

Installation

$ npm add remix-routes

Setup

Add remix-routes to your dev and build script in package.json.

With concurrently package:

{
  "scripts": {
    "build": "remix-routes && remix build",
    "dev": "concurrently \"remix-routes -w\" \"remix dev\""
  }
}

With npm-run-all package:

{
  "scripts": {
    "build": "run-s build:*",
    "build:routes": "remix-routes",
    "dev": "run-p dev:*",
    "dev:routes": "remix-routes -w",
  }
}

Usage

Basic usage

import type { ActionFunction } from 'remix';
import { redirect } from 'remix';
import { $path } from 'remix-routes'; // <-- Import magical $path helper from remix-routes.

export const action: ActionFunction = async ({ request }) => {
  let formData = await request.formData();
  const post = await createPost(formData);

  return redirect($path('/posts/:id', { id: post.id })); // <-- It's type safe.
};

Appending query string:

import { $path } from 'remix-routes';

$path('/posts/:id', { id: 6 }, { version: 18 }); // => /posts/6?version=18
$path('/posts', { limit: 10 }); // => /posts?limit=10
// You can pass any URLSearchParams init as param
$path('/posts/delete', [['id', 1], ['id', 2]]); // => /posts/delete?id=1&id=2

Typed query string:

Define type of query string by exporting a type named SearchParams in route file:

// app/routes/posts.tsx

export type SearchParams = {
  view: 'list' | 'grid',
  sort?: 'date' | 'views',
  page?: number,
}
import { $path } from 'remix-routes';

// The query string is type-safe.
$path('/posts', { view: 'list', sort: 'date', page: 1 });

You can combine this feature with zod and remix-params-helper to add runtime params checking:

import { z } from "zod";
import { getSearchParams } from "remix-params-helper";

const SearchParamsSchema = z.object({
  view: z.enum(["list", "grid"]),
  sort: z.enum(["price", "size"]).optional(),
  page: z.number().int().optional(),
})

export type SearchParams = z.infer<typeof SearchParamsSchema>;

export const loader = async (request) => {
  const result = getSearchParams(request, SearchParamsSchema)
  if (!result.success) {
    return json(result.errors, { status: 400 })
  }
  const { view, sort, page } = result.data;
}

Checking params:

import type { ActionFunction } from 'remix';
import { $params } from 'remix-routes'; // <-- Import $params helper.

export const action: ActionFunction = async ({ params }) => {
  const { id } = $params("/posts/:id/update", params) // <-- It's type safe, try renaming `id` param.

  // ...
}

Route type definitions

remix-routes also export all route type definitions for your convenience.

import type { Routes } from 'remix-routes';
import { useParams } from "remix";

export default function Component() {
  const { id } = useParams<Routes['/posts/:id']['params']>();
  ...
}

Command Line Options

  • -w: Watch for changes and automatically rebuild.
  • -o: Specify the output path for remix-routes.d.ts. Defaults to ./node_modules if arg is not given.

TypeScript Integration

A TypeScript plugin is available to help you navigate between route files.

Installation

$ npm add -D typescript-remix-routes-plugin

Setup

Add the plugin to your tsconfig.json:

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-remix-routes-plugin"
      }
    ]
  }
}

Select workspace version of TypeScript in VSCode:

Screenshot 2022-12-02 at 5 56 39 pm

License

MIT

More Repositories

1

jquery-jsonview

[UNMAINTAINED]View JSON in a more readable format
CoffeeScript
488
star
2

waque

[不再维护]同步本地 Markdown 文档到语雀
TypeScript
244
star
3

reactive.macro

Reduce the React boilerplate.
TypeScript
172
star
4

react-with-hooks

[OUTDATED]Ponyfill for the React Hooks API (Support RN)
JavaScript
152
star
5

redux-modal

Redux based modal
TypeScript
105
star
6

mini-store

A minimal state store for React component.
TypeScript
100
star
7

vs-codesandbox

Bring CodeSandbox into your favorite editor
TypeScript
81
star
8

camas

Minimal React authorization library
TypeScript
66
star
9

tmuxrc

My configuration for tmux.
Shell
65
star
10

webpack-less-theme-plugin

Inject less variables to your less file. Support HMR.
JavaScript
26
star
11

styled-antd

Proof of concept using styled-components with antd
JavaScript
25
star
12

react-context-io

Share state with ease.
TypeScript
22
star
13

hexdocset

Convert hex doc to Dash.app's docset format.
Elixir
21
star
14

babel-plugin-reactive

Make React Reactive Again!
TypeScript
18
star
15

rx-hackernews

HackerNews clone built with React/Redux/redux-observable.
JavaScript
17
star
16

dotbox

Backup your dotfiles to dropbox and restore them easily.
Ruby
10
star
17

antd.macro

A babel macro that helps you import antd's style.
TypeScript
9
star
18

antd-rtl

antd rtl demo
CSS
6
star
19

star

下载豆瓣红心歌曲
Ruby
6
star
20

toby

Ruby 版淘宝 API SDK
Ruby
6
star
21

redux-polymorphic

Reuse your reduers
JavaScript
5
star
22

umi-routes-drawer

TypeScript
5
star
23

yesmeck

5
star
24

HelloSketch

Swift
4
star
25

douban_fm_hotkey

Douban FM 全局快捷键
JavaScript
4
star
26

react-full-viewport

A React component that sets its children to full viewport.
TypeScript
4
star
27

antd-dash

JavaScript
3
star
28

neotable

JavaScript
3
star
29

formsy-react-inputs

A set of formsy-react inputs without any fancy.
JavaScript
3
star
30

git-phpcs-hooks

Shell
2
star
31

github-comment-webhook

Ruby
2
star
32

vim-tips.org

The source for vim-tips.org.
Ruby
2
star
33

is-test

Next.js template for CodeSandbox Projects
TypeScript
2
star
34

monkey-lang

A Monkey implementation in Rust for learning purpose.
Rust
2
star
35

bookmarklets

Chrome bookmarklet extension
JavaScript
2
star
36

babel-preset-antd

JavaScript
2
star
37

git-graph

Graph git commits.
CSS
1
star
38

tree-transfer

JavaScript
1
star
39

react-vue-antd-demo

JavaScript
1
star
40

babel-spread-undefined

JavaScript
1
star
41

huami.ex

A CLI version of flower password writing in Elixir.
Elixir
1
star
42

enzyme-set-props-bug

JavaScript
1
star
43

hello-codesandbox-projects

Remix template for CodeSandbox Projects
TypeScript
1
star
44

bug-24

JavaScript
1
star
45

antd-enzyme

[DEPRECATED]
JavaScript
1
star
46

hello-form-ios

Next.js template for CodeSandbox Projects
TypeScript
1
star
47

vimrc

My very personal vimrc
Vim Script
1
star
48

Sublime-IDE

My configuration for using Sublime Text as an IDE.
Python
1
star
49

huami.rb

A CLI version of flower password.
Ruby
1
star
50

hubot-tianqi

Hubot 天气查询
CoffeeScript
1
star
51

raycast-extensions

My Raycast extensions
TypeScript
1
star
52

hubot-xiexie

A hubot script that accepts your 谢谢
CoffeeScript
1
star
53

remax-suspense-demo

JavaScript
1
star
54

github-slideshow

A robot powered training repository 🤖
HTML
1
star
55

react-oom

JavaScript
1
star
56

babel-plugin-resolve-es6-module

JavaScript
1
star
57

tips.vim

Show vim tips everytime when you enter vim.
Vim Script
1
star