节点打字稿库环境具体配置
Node typescript library environment specific configuration
我是节点和打字稿的新手。我正在开发一个节点库,它可以连接到另一个 API 来获取和 post 数据。 a/any UI 应用程序使用此库来发送和接收来自 API 服务的数据。现在我的问题是,如何在库中维护特定于环境的配置?例如:
消费者来电GET /user
消费者端的用户端调用库中的方法get data
但是如果消费者在测试环境中调用用户端点我希望库点击以下 API Url
测试http://api.test.userinformation.company.com/user
测试版 http://api.beta.userinformation.company.com/user
据我所知,该库只是一个参考,并且 运行 在消费者应用程序中。图书馆肯定可以从消费者那里获得环境,但我不希望消费者必须指定需要命中的完整 URL,因为这将是图书馆的责任。
注意:URL 不是唯一的问题,我可以通过库中的环境切换来解决这个问题,我有一些基于环境的客户端机密,我既不能存储在代码中也不能签入源代码管理.
附加信息
(根据jfriend00在评论中的要求)
我的库有一个 LibExecutionEngine class 和一个方法,它是库的入口点:
export class LibExecutionEngine implements ExecutionEngine {
constructor(private environment: Environments, private trailLoader:
TrailLoader) {}
async GetUserInfo(
userId: string,
userGroupVersion: string
): Promise<UserInfo> {
return this.userLoader.loadUserInfo(userId, userGroupVersion)
}
}
export interface ExecutionEngine {
GetUserInfo(userId: string, userGroupVersion: string): Promise<UserInfo>
}
消费者通过创建 LibraryExecution 实例然后调用 getuserinfo 来开始使用库。如您所见,class 的构造函数接受一个环境。在库中拥有环境后,我需要以某种方式从构造函数中加载键 API Url
、APIClientId
和 APIClientSecret
的值。我知道有两种方法可以做到这一点:
选项 1
我可以做类似 this._configLoader.SetConfigVariables(environment)
的事情,其中 configLoader.ts
是一个 class,它从文件 ({environment}.json
) 加载特定的配置值,但这意味着我要维护上面提到的 URL
变量和相应的 clientid
、clientsecret
能够在 json 文件中命中 URL,我不应该签入到源代码管理。
选项 2
我可以使用 dotenv
npm 包,并创建一个我定义三个键的 .env 文件,然后将值存储在部署配置中,这非常适合独立部署的应用程序,但是这是一个库,在任何环境中都不会 运行 单独存在。
选项 3
接受一个来自消费者的配置对象,即库的消费者根据环境提供URL、clientId、clientSecret供库访问,但为什么要维护责任图书馆的必要变量应该放在消费者身上吗?
请就如何最好地实现这一点提出建议。
So, I think I got some clarity. Lets call my Library L
, and consuming app C1
and the API that the library makes a call out to get user info as A
. All are internal applications in our org and have a OAuth setup to be able to communicate, our infosec team provides those clientids and secrets to individual applications, so I think my clarity here is: C1
would request their own clientid
and clientsecret
to hit A
's URL
, C1
would then pass in the three config values to the library, which the library uses to communicate with A
. Same applies for some C2
in the future.
Which would mean that L
somehow needs to accept a full configuration object with all required config values from its consumers C1
, C2
etc.
是的,这听起来是正确的方法。该库只是一些代码,按照它所说的去做。在这种情况下,客户端必须从 infosec 团队获取 clientid 和 clientsecret 并维护它们并保证它们的安全,并且客户端还具有随附的 URL。因此,客户端将所有这些传递到您的库中,理想情况下每个实例只传递一次,然后您在该实例的持续时间内将其保存在实例数据中
我是节点和打字稿的新手。我正在开发一个节点库,它可以连接到另一个 API 来获取和 post 数据。 a/any UI 应用程序使用此库来发送和接收来自 API 服务的数据。现在我的问题是,如何在库中维护特定于环境的配置?例如:
消费者来电GET /user
消费者端的用户端调用库中的方法get data
但是如果消费者在测试环境中调用用户端点我希望库点击以下 API Url
测试http://api.test.userinformation.company.com/user
测试版 http://api.beta.userinformation.company.com/user
据我所知,该库只是一个参考,并且 运行 在消费者应用程序中。图书馆肯定可以从消费者那里获得环境,但我不希望消费者必须指定需要命中的完整 URL,因为这将是图书馆的责任。
注意:URL 不是唯一的问题,我可以通过库中的环境切换来解决这个问题,我有一些基于环境的客户端机密,我既不能存储在代码中也不能签入源代码管理.
附加信息
(根据jfriend00在评论中的要求)
我的库有一个 LibExecutionEngine class 和一个方法,它是库的入口点:
export class LibExecutionEngine implements ExecutionEngine {
constructor(private environment: Environments, private trailLoader:
TrailLoader) {}
async GetUserInfo(
userId: string,
userGroupVersion: string
): Promise<UserInfo> {
return this.userLoader.loadUserInfo(userId, userGroupVersion)
}
}
export interface ExecutionEngine {
GetUserInfo(userId: string, userGroupVersion: string): Promise<UserInfo>
}
消费者通过创建 LibraryExecution 实例然后调用 getuserinfo 来开始使用库。如您所见,class 的构造函数接受一个环境。在库中拥有环境后,我需要以某种方式从构造函数中加载键 API Url
、APIClientId
和 APIClientSecret
的值。我知道有两种方法可以做到这一点:
选项 1
我可以做类似 this._configLoader.SetConfigVariables(environment)
的事情,其中 configLoader.ts
是一个 class,它从文件 ({environment}.json
) 加载特定的配置值,但这意味着我要维护上面提到的 URL
变量和相应的 clientid
、clientsecret
能够在 json 文件中命中 URL,我不应该签入到源代码管理。
选项 2
我可以使用 dotenv
npm 包,并创建一个我定义三个键的 .env 文件,然后将值存储在部署配置中,这非常适合独立部署的应用程序,但是这是一个库,在任何环境中都不会 运行 单独存在。
选项 3
接受一个来自消费者的配置对象,即库的消费者根据环境提供URL、clientId、clientSecret供库访问,但为什么要维护责任图书馆的必要变量应该放在消费者身上吗?
请就如何最好地实现这一点提出建议。
So, I think I got some clarity. Lets call my Library
L
, and consuming appC1
and the API that the library makes a call out to get user info asA
. All are internal applications in our org and have a OAuth setup to be able to communicate, our infosec team provides those clientids and secrets to individual applications, so I think my clarity here is:C1
would request their ownclientid
andclientsecret
to hitA
'sURL
,C1
would then pass in the three config values to the library, which the library uses to communicate withA
. Same applies for someC2
in the future.Which would mean that
L
somehow needs to accept a full configuration object with all required config values from its consumersC1
,C2
etc.
是的,这听起来是正确的方法。该库只是一些代码,按照它所说的去做。在这种情况下,客户端必须从 infosec 团队获取 clientid 和 clientsecret 并维护它们并保证它们的安全,并且客户端还具有随附的 URL。因此,客户端将所有这些传递到您的库中,理想情况下每个实例只传递一次,然后您在该实例的持续时间内将其保存在实例数据中