node.js - 将 nodejs DI 容器 awilix 与类型安全集成
问题描述
我正在考虑将 DI 容器集成到我现有的 nodejs 项目之一中。我已经集成了 awilix,一切都按预期工作。
但是,我习惯于打字并在许多地方使用类型安全。这是我无法使用 awilix 注册依赖项的一件事。
例如,我编写高阶函数之类的用例
function createReport(specs){
const {reportRepostiory} = specs;
return async (param1: string, param2: string){
//...
reportRepostiory.create({//some payload})
}
}
调用函数调用如下
const reportService : any = container.resolve("createReport");
const result = await reportService("1", "2")
上面的代码适用于正确的容器配置。但是,解析的函数对象没有类型推断。有没有办法获取类型?
解决方案
这不是我自己的原始答案,但我遇到了这个可能对您有帮助的代码沙箱:https ://codesandbox.io/s/qykt1?file=/src/index.ts 。
如果链接失效,这里是从上面的链接中提取的片段。
所有功劳归于作者 derekrjones ( https://codesandbox.io/u/derekrjones )。
import {
AwilixContainer,
asFunction,
asValue,
asClass,
InjectionMode,
createContainer,
Resolver,
ResolveOptions,
ContainerOptions
} from "awilix";
/**
* Container definition base.
*/
interface ContainerDefinition {
[key: string]: Resolver<unknown>;
}
/**
* Extracts the type that will be resolved from a resolver.
*/
type ExtractResolverType<T> = T extends Resolver<infer X> ? X : null;
/**
* Strongly-typed container.
*/
interface TypedAwilixContainer<T extends ContainerDefinition>
extends Pick<AwilixContainer, Exclude<keyof AwilixContainer, "resolve">> {
/**
* Resolves the registration with the given name.
*
* @param {string} name
* The name of the registration to resolve.
*
* @return {*}
* Whatever was resolved.
*/
resolve<K extends keyof T>(
key: K,
resolveOptions?: ResolveOptions
): ExtractResolverType<T[K]>;
}
/**
* Wraps `createContainer` and calls `register` on it.
*/
function createTypedContainer<T extends ContainerDefinition>(
registrations: T,
opts?: ContainerOptions
): TypedAwilixContainer<T> {
return createContainer(opts).register(registrations) as any;
}
推荐阅读
- c - 无法读取 gzip 压缩文件
- python - 如何在数据框中添加日期值列,在 python 中重复名称和季节?
- json - 如何在python中解码通过套接字服务器接收的数据?
- azure-devops - 在发布到生产之前将更改请求表/批准表集成到 Azure DevOps
- amazon-web-services - 即使使用正确的标签键,我的 AWS SCP 会阻止创建任何 EC2 实例是什么?
- html - Selenium/Python:使用 Selenium 后加载程序不会消失,即使我使用 EC.invisibility 命令
- kubernetes - 在 k8s 中将文件引用为 configmap
- python - 如何轻松替换代码?从 VBA 到 Python
- php - Laravel 中与 first 的分页关系
- python - Python:删除嵌套字典中的键会出错