routes gen
1.0.0
routes-gen是一种框架不可知的CLI工具,用于解析和生成类型安全的助手,用于安全路线使用。将其视为Prisma,但对于路线。
首先,您必须安装路由生成器本身:
yarn add routes-gen
发电机与“驱动程序”一起工作,这是用于不同框架的路由解析器。
| 司机 | 安装 |
|---|---|
| 混音 | yarn add @routes-gen/remix |
| 实心 | yarn add @routes-gen/solid-start |
您可以简单地运行:
yarn routes-gen -d @routes-gen/remix
它将根据您提供的驱动程序来解析和导出路线。
例如, @routes-gen/remix驱动程序默认情况下将导出路由到/app/routes.d.ts 。
请注意,您可以通过
--output或-o标志更改输出路径。
现在,您可以在任何地方导入生成的route帮助者并享受打字:
import { route } from "routes-gen" ;
// Compiles to /products
route ( "/products" ) ;
// Compiles to /products/1337
route ( "/products/:productId" , {
productId : "1337" ,
} ) ; 您可以使用RouteParams类型将打字添加到动态路由参数/段。例子:
import { RouteParams } from "routes-gen" ;
const { productId } = params as RouteParams [ "/products/:productId" ] ;混音示例:
import { LoaderFunction , useParams } from "remix" ;
import { RouteParams } from "routes-gen" ;
export const loader : LoaderFunction = async ( { params } ) => {
const { productId } = params as RouteParams [ "/products/:productId" ] ;
} ;
export default function Product ( ) {
const { productId } = useParams < RouteParams [ "/products/:productId" ] > ( ) ;
return < div / >;
} | 选项 | 别名 | 描述 |
|---|---|---|
| - 帮助 | 打印帮助消息并退出 | |
| - 版本 | -v | 打印CLI版本并退出 |
| - 输出 | -o | 路线导出的路径 |
| - 司机 | -d | 处理路线解析的驾驶员 |
| - 手表 | -w | 注意更改 |
| - POST-EXPORT | 路由导出后执行命令 |
如果没有您首选的框架驱动程序,则可以编写自己的框架。例如,在项目中创建一个简单的driver.js程序。
module . exports = {
// Where to export the typings if the "output" flag was not provided.
defaultOutputPath : "src/routes.d.ts" ,
// The routes parser. Must export and array of routes matching the { path: string } interface.
routes : async ( ) => {
return [
{
path : "/products" ,
} ,
{
path : "/products/:productId" , // Note that the dynamic segments must match the :myVar pattern.
} ,
] ;
} ,
// The paths to be watched for changes. Must return and array of relative paths.
watchPaths : async ( ) => {
return [ "/my-routes/**/*.{ts,tsx,js,jsx}" ] ;
} ,
}现在您可以轻松使用它:
routes-gen -d driver.js
您也可以将其发布到NPM,安装并将其用作包装:
routes-gen -d my-driver-package
请考虑将您的驾驶员提交到此存储库中。