简体   繁体   English

可扩展和可修改节点(npm)模块的最佳做法

[英]Best practices for extendable and modifiable node (npm) modules

I need advice from the more experienced javascript(typescript) / nodejs developers. 我需要经验丰富的javascript(typescript)/ nodejs开发人员的建议。 For a few months a I'm trying to find best practice for making extendable and modifiable node (npm) modules. 几个月以来,我一直在寻找制作可扩展和可修改节点(npm)模块的最佳实践。

For better understanding: In most of PHP frameworks (like as Symfony, Laravel, Nette) we have DI container which can be used for changing or adding own implementation for services coming from packages. 为了更好地理解:在大多数PHP框架(例如Symfony,Laravel,Nette)中,我们都有DI容器,该容器可用于更改或添加针对来自包的服务的自己的实现。 For example. 例如。 I have a cart package which implements service for calculating cart price and apply taxes. 我有一个购物车包,用于执行计算购物车价格和征税的服务。 When i need to change taxes calculation I can change implementation over DI container like as 当我需要更改税收计算时,可以像这样更改DI容器的实现

services:
      myTaxCalculator:
    class: MyTaxCalculator
      Package\Taxes\CalculatorInterface: ‘@myTaxCalculator’

and now when package work with Package\\Taxes\\CalculatorInterface use my own calculator instead of default implementation. 现在,当程序包与Package \\ Taxes \\ CalculatorInterface一起使用时,请使用我自己的计算器而不是默认实现。

And I looking for something like this in javascript(typescript)/nodejs. 我在javascript(typescript)/ nodejs中寻找类似的东西。 If I build any package and in package I need function for calculate taxes use this const taxCalculator = require('...') but now I can't change implementation of this function. 如果我构建任何程序包,并且在程序包中,我需要用于计算税金的函数,请使用此const taxCalculator = require('...'),但现在我无法更改此函数的实现。

Of course I can make package configurable. 当然,我可以使程序包可配置。 Add some mechanism for set a custom function for specific cases but I think that I need this logic for all classes / function which is used in application to never have to call require('something'). 添加一些机制来设置针对特定情况的自定义函数,但是我认为我需要用于应用程序中的所有类/函数的逻辑,而不必调用require('something')。

The point is build basic and standard package with default logic which can be in concrete application modify to solve customer problems without writing a new package with 90% same code. 关键是构建具有默认逻辑的基本和标准软件包,可以在具体的应用程序中对其进行修改以解决客户问题,而无需编写具有90%相同代码的新软件包。 I know that exists some IoC/DI implementation for javascript(typescript) / nodejs like as InversifyJS but I'm not sure when is a best way for javascript(typescript) / nodejs applications. 我知道对于javascript(typescript)/ nodejs存在一些IoC / DI实现,例如InversifyJS,但我不确定何时是javascript(typescript)/ nodejs应用程序的最佳方法。 Do you have any experiences with this? 您对此有任何经验吗? How do you solve these problems? 您如何解决这些问题?

Thanks for any response! 感谢您的任何回复!

I wouldn't say I'm an expert or "best practices guru", but I think three scenarios are pretty common. 我不会说我是专家或“最佳实践专家”,但我认为三种情况很常见。 I won't dig into Inversify because you're already aware of it. 我不会深入研究Inverseify,因为您已经意识到了这一点。

  1. Take an object with config at the entry point class/function. 在入口点类/函数处使用config作为对象。 Default to your implementation. 默认为您的实现。
interface TaxCalculator { /* tax related stuff */ }

interface CalculateCartPriceArgs {
  taxCalculator: TaxCalculator,
  // probably lots of other stuff
}

export function calculateCartPrice({
  taxCalculator = defaultTaxCalculator
}: CalculateCartPriceArgs) {
  // implementation
}
  1. Plugins / middleware. 插件/中间件。
  2. Expose internals to allow the user to build his own version of your thing. 公开内部信息,以允许用户构建自己的事物版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM