adaltas / node-plug-and-play

Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.
MIT License
55 stars 2 forks source link

typescript definitions #1

Open g5becks opened 4 years ago

g5becks commented 4 years ago

Hi, cool library.

Are there any plans to add typescript definitions?

Thanks.

wdavidw commented 4 years ago

Like in most in my libraries, I welcome contributions in this regard and then try to maintain those but I am not a TypeScript user, sometimes I need help to express those types.

saugatdai commented 3 years ago

Someone, please provide typescript definitions for this package, in my opinion, this would be a very useful plugin for some startups who want their system be extensible.

wdavidw commented 3 years ago

I would appreciate some help in this matter. I am not a TypeScript user nor an expert.

LucasDemea commented 1 year ago

Hi, I can help with writing the types. Would you rather have the library rewritten in typescript, or only added types definitions ?

wdavidw commented 1 year ago

I have been hesitating, as a matter of fact. If you wish to go full TS, why not.

LucasDemea commented 1 year ago

I'm working on the TS rewrite and this line bugs me. https://github.com/adaltas/node-plug-and-play/blob/89a80c6e8ebcf082769ec8844f6bb388d1405794/lib/index.js#L122 Could you please explain ? Isn't plugin.hooks[name] supposed to refer to a single hook ? How come we could map this ?

LucasDemea commented 1 year ago

Not so sure about this[key] in this line : https://github.com/adaltas/node-plug-and-play/blob/89a80c6e8ebcf082769ec8844f6bb388d1405794/lib/error.js#L25 Is there any reason for not simply doing key = ... ?

I'd need some help to make the tests work as I'm not familiar at all with mocha and currently it won't run:

Error: Use CoffeeScript.register() or require the coffeescript/register module to require .coffee files.
wdavidw commented 1 year ago

return plugin.hooks[name].map(function(hook){

It is always an array. the property plugin.hooks[name] is normalized at line 81 with the normalize_hook function.

wdavidw commented 1 year ago

Not so sure about this[key] in this line :

This refers to the error object instance. In here, we add any property of the contexts arguments to the error instance. For example, const err = new Error('CODE', 'some message', { prop: 'value' }) leads to err.prop === 'value'. Note, a commited a change to the error JS file to clean up the variable (a leftover from the coffee to js migration).

wdavidw commented 1 year ago

I'd need some help to make the tests work as I'm not familiar at all with mocha

The error is related to mocha not calling the Node.js --loader argument to load .coffee file, i'll have a look, I have done it in the past with the csv parser.

Note, I fixed the sample test, you'll have to rebase your branch.

wdavidw commented 1 year ago
Error: Use CoffeeScript.register() or require the coffeescript/register module to require .coffee files.

Replace all occurrences of ../lib/index.js with ../dist/esm/index.js in the tests and they will run. I just tested after a rebase on master. However, I am not against rewriting the test in TS but I can do this, it will train me with TS, I have too little experience with it.

LucasDemea commented 1 year ago

Thank you, I'll have a look at all this, this afternoon.

LucasDemea commented 1 year ago

Do you mind if we replace the array_flatten method with lodash.flatten, as it is already typed ?

wdavidw commented 1 year ago

I am reluctant to introduce dependencies, but there is arr.flat(Infinity) now which shall do the job.

LucasDemea commented 1 year ago

Didn't know this one, thanks

LucasDemea commented 1 year ago

One these lines https://github.com/adaltas/node-plug-and-play/blob/1f4b16b308a5e9e0706ce55fd4939d55dfc082f8/lib/index.js#L141-L143 We use the plugin property that is added to hooks a few lines before.

2 questions:

The Hook interface looks like this atm:

export interface Hook {
  /**
   * List of plugin names with hooks of the same name are to be executed after, a string is coerced to an array.
   */
  after?: string[];
  /**
   * List of plugin names with hooks of the same name are to be executed before, a string is coerced to an array.
   */
  before?: string[];
  /**
   * Name to indentify the hook.
   */
  name: string;
  /**
   * The hook handler to be executed
   */
  handler: HookHandler;

  // require?: string[];
  // plugin?: string;
}
wdavidw commented 1 year ago

I am trying to refresh my mind but I am pretty sure you are right, require and plugin look like transitional properties.

hook.plugin is the associated plugin name, and hook.require is used to get the list of required plugins defined in its associated plugin

I just push a commit on master to correct line 142.

LucasDemea commented 1 year ago

Thanks. This part is a pain to adapt to typescript, as it goes against TS strong typing practice. In the TS paradigm, we should not dynamically add properties to the Hook interface. I found a workaround, but it's not the cleanest way. This will be a point of improvement in the future

LucasDemea commented 1 year ago

Is the call_sync function still in use ? You don't speak about it in the docs nor in your blog post.

wdavidw commented 1 year ago

we should not dynamically add properties to the Hook interface

Then you can always declare those properties. If I remember correctly, the idea is that user-provided hooks don't need a name and required plugins but I am not even sure anymore.

wdavidw commented 1 year ago

Is the call_sync function still in use

Yes, I use it in some libraries