Using with openapi generated code in multiple nodes

Hello everyone,
I have a few nodes that all use the same API. Currently I build the needed URLs by hand. Since I don't want to do that for every function the API provides and because I want to have an easy way to see if changes in the API break my nodes I used openapi generator to generate javascript code.
The issue now is, that I have no idea how to use the generated classes in my nodes. I could of course just copy the complete class into every node where I want to use it, but that would defeat the purpose. Is there a way to use the generated classes in all my nodes so I don't have to copy and paste the generated code? Ideally I want just a folder with all the generated classes from the API.

I'm thankful for any reply!
Flatric

I think there are a couple of options. But for your use-case, the best approach may be to create a separate npm package just containing the code for the APIs. Then you can make that a dependency of all of your node packages.

I'm doing that in uibuilder now as I have a standardised event handler that is used across a number of nodes.

Thanks for your reply! That would mean I have to put all the classes into one .js file?

No, you can have as many files as you like in a dependency. You have a single package which is defined by a package.json file then any folders/files you want to have. You can then publish that to the npm registry & include that as a dependency in your node package.

Have a look at my https://github.com/TotallyInformation/ti-common-event-handler repo as an example.

Oh I see what you mean. Looks like I need to find an alternative to the openapi generator since it uses imports and my current knowledge is, is that you can't use imports

It is possible to use them. Trouble is that Node-RED uses CJS rather than ESM modules. Node.js allows you now to use both but it is more complex. You can use a dynamic import to get an ESM module into a CJS node.js app.

Sadly, dynamic imports are async promise based though so it makes your node's js file more complex because you need to ensure that the promise has resolved successfully before going on to define and register the node.

Is this really how it's supposed to be done? Or am I trying to do something that shouldn't be done with node-red? All this is somehow way more complex than I imagined. I thought I could just put the folder with the ~100 .js files, each containing a class, into the folder of my module and just use the classes in the nodes I need.

Ah, well, some confusion here about the word "nodes". You didn't say that all of them were in a single package. If they are all in a single package then yes, you can simply include them all. Check out node-red-contrib-uibuilder and you will see that there is a nodes/libs folder containing a bunch of singleton classes, each in their own file. Those are used in any of the nodes in that package where needed.

If you want to use the same API's across different packages, then you need to put the common files into a separate package that you can reference as a dependency.

Either way, you will still have the issue of the differences between CJS and ESM though.