@dudousxd/nestjs-inertia-codegen-extension (英语)
用于
@dudousxd/nestjs-codegen- 增加一个Inertiarouter导入和类型安全navigate()帮助生成的已键入客户端 .
输入的客户端代码源生活在独立的 @dudousxd/nestjs-codegen 软件包。 这个软件包是 只是Inertia扩展 登记 nestjsInertiaCodegen() 生成 api.ts 收益a navigate() 由 Inertia 支持的助手 router,因此您的突变和链接可以驱动 Inertia 访问,并使用完整的路由名称和参数自动完成.
安装
pnpm add -D @dudousxd/nestjs-inertia-codegen-extension
它是核心编码源的扩展,所以安装时要同时安装(React的Inertia适配器提供 router:
pnpm add -D @dudousxd/nestjs-codegen
pnpm add @inertiajs/react
| 同伴依赖关系 | 范围 |
|---|---|
@dudousxd/nestjs-codegen | >=0.1.0 |
@inertiajs/react | >=1.0.0 |
设置
在代码源中注册扩展 extensions: [...] 数组——通过巢穴模块或 defineConfig 用于CLI/CI运行.
import { NestjsCodegenModule } from '@dudousxd/nestjs-codegen/nest';
import { nestjsInertiaCodegen } from '@dudousxd/nestjs-inertia-codegen-extension';
NestjsCodegenModule.forRoot({
contracts: { glob: 'src/**/*.controller.ts' },
pages: { glob: 'inertia/pages/**/*.tsx' },
app: { moduleEntry: 'src/app.module.ts' },
codegen: { outDir: '.nestjs-inertia' },
extensions: [nestjsInertiaCodegen()],
});
相同的数组工作在 nestjs-codegen.config.ts (单位:千美元)defineConfig)——保留一出真相来源并导入两者.
上面写的
nestjsInertiaCodegen() 是一个 CodegenExtension 谁 apiHeader 钩子有助于生成 api.ts数字 :
- 导入 Inertia 的
router—— 说import { router } from '@inertiajs/react'; - (单位:千美元)
NavigateOptions类型(method, (中文(简体) ),data, (中文(简体) ),preserveState, (中文(简体) ),preserveScroll, (中文(简体) ),replace(中文(简体) ) - 类型安全
navigate()解析其 URL 的命名路由并调用router.visit()
import { navigate } from '~codegen/api';
// Route name autocompleted; params required when the route has :id
navigate('users.show', { params: { id: '42' } });
navigate('showDashboard.show');
// With Inertia visit options
navigate('users.list', { preserveState: true, replace: true });
Opt- in 文件 : shared 发射 shared.ts (单位:千美元)InertiaSharedProps, (中文(简体) ), scopes: { <scope>: { shared } } 各加一个 forFeature 范围是共享的道具InertiaScopeSharedProps, (中文(简体) ), ScopeSharedProps<S>),以及 pageExcludes: true 发射 page-excludes.ts。 。 。 .
没有延期, api.ts 是一个普通的打印客户端,并且 @inertiajs/react是因为 router 从未导入。 该扩展仅拥有 Inertia 表面 api.ts ——Inertia页面发现(pages.d.ts 编号 : components.json)和共享道具仍由核心处理 pages 编号 : app 相会.
怎样才合适
@dudousxd/nestjs-codegen 运行一个发现 ~ IR ~ 发射管线,并曝光 扩展合同在 @dudousxd/nestjs-codegen/extension整合不是配置旗,而是您注册的扩展旗 extensions: [...],每个形状的路由,释放额外的文件,或增强 api.ts本套件以单项执行合同 apiHeader 钩:
import type { CodegenExtension } from '@dudousxd/nestjs-codegen/extension';
export function nestjsInertiaCodegen(): CodegenExtension {
return {
name: 'nestjs-inertia',
apiHeader() {
return {
imports: ["import { router } from '@inertiajs/react';"],
statements: [/* NavigateOptions + navigate() */],
};
},
};
}
它与其它伴生扩展组成——例如. tanstackQuery() (单位:千美元)@dudousxd/nestjs-codegen-tanstack用于 queryOptions编号 :mutationOptions 财务报告和审定财务报表 nestjsFilterCodegen() (单位:千美元)@dudousxd/nestjs-filter-codegen)用于打入的过滤器.
链接
- 代码核心 :
@dudousxd/nestjs-codegen - NestJS的Inertia适配器:
@dudousxd/nestjs-inertia
许可证
麻省理工学院