How to import new dynamic module in service? - nestjs

I have dynamic module with params. I use params in his service.
How to import this module in service another module.
If I use this, I must add all parameters and another services, which I use there.
const serviceDynamicModule = new ServiceDynamicModule(param, service, ...);
I have found ModuleRef, but it doesn't contain this functionality.

You can import your module in another module by listing module you want to import to import section of another module's #module decorator.
Here is a explanation on Modules and dynamic module please check that out for more detail explanation. Nest_docs#dynamicModule
eg:-
lets say you want to import your dynamic module here.
import {Module} from '#nestjs/common';
#Module(
import:[yourDynamicModule.forRoot(neededParams)]
)
export calss MyModule{}

Related

How to import interface from package

I'd like to import the Schema$ListMessagesResponse interface so I can validate the response that's returned.
I tried import { google, Schema$ListMessagesResponse } from 'googleapis'; but that doesn't work. I'm not familiar with importing this object from the package. It's documented here and here - How do I properly reference it?
https://googleapis.dev/nodejs/googleapis/latest/gmail/interfaces/Schema$Message.html
Node v12
TypeScript 4.3.5
You need to respect the given namespace gmail_v1 exporting the interface
try:
import {gmail_v1} from 'googleapis';
const messages: gmail_v1.Schema$ListMessagesResponse = //...

How to import module based dependencies for firebase cloud functions?

I want to organize my firebase cloud functions in specific files,
and currently, I have these 3:
index.ts
crypto.ts
webscrape.ts
Inside of these files, I have functions that use specific dependencies that are needed nowhere else.
For example, in crypto.ts I need the crypto-js package to encrypt some user data and store it into the database.
So I am importing it like so:
import * as CryptoJS from "crypto-js";
as advised in https://firebase.google.com/docs/functions/handle-dependencies#typescript
On the other hand, when I try to import puppeteer into webscrape.ts like this:
import * as puppeteer from"puppeteer-extra";
then calling puppeteer.launch(); gives me an error :
Property 'launch' does not exist on type 'typeof import("c:/Users/username/Desktop/project/firebasee/functions/node_modules/puppeteer-extra/dist/index")'
and it only works when I do const puppeteer = require("puppeteer-extra");
What's the difference here?
My goal is to keep the dependencies of each functions and file/module as small as possible because I assume that this will also keep the size of each function container small (Is that even true?)
I didn't want to import everything to index.ts even when I trigger a function, that doesn't use this dependency at all.
So what is the correct way of handling these dependencies?
Thanks!
The following import will get the default export from that package.
import puppeteer from "puppeteer-extra"
I looked for the default export in the Github repository and found that.
const defaultExport: PuppeteerExtra = (() => {
return new PuppeteerExtra(...requireVanillaPuppeteer())
})()
export default defaultExport
They have mentioned both ES6 import and require methods here.
// javascript import
const puppeteer = require('puppeteer-extra')
// typescript/es6 module import
import puppeteer from 'puppeteer-extra'
You can read more about import on MDN.

import * best practice in node and react

Should I be importing my node modules using *
example
import * from 'express';
import * from './../../myCode';
Is it correct that by using the * that all exports will be imported which will make me bring functionality which will increase the file size.
import * as myCode from './../../myCode';
This inserts myCode into the current scope, containing all the exports from the module in the file located in ./../../myCode.
import React, { Component } from 'react';
class myComponent extends Component { ... }
By Using above syntax your bundler ( e.g : webpack) will still bundle the ENTIRE dependency but since the Component module is imported in such a way using { } into the namespace, we can just reference it with Componentinstead of React.Component.
For more information you can read mozilla ES6 module docs.
The answer is, it depends: there is no best practice for import/export since it's very up to you and your use-case. But normally I will just import what I need, not everything. And yes, if you do import * the file size of bundle.js can be big.

Is there any equivalent to import * from 'module' in nodejs?

I know there is import {thing} from 'module' or const {thing} = require('module'), and I can also just do const thing = require('thing') and use thing.method(), but can I, through any tricks available, import all the exports in a module (the way python does import * from 'lib') so that I can just call method() without specifying it in the import?
It's possible to import all exports from a module with the syntax
import * as Utils from 'utils';
This will create an object called Utils which will have all the exports from the utils module as its properties.
In JS, a scoped variable has to be explicitly declared in that scope. Any reference to a variable that has not been explicitly declared is assumed to be a property of the global namespace. That's why a syntax like import * from 'module' can't work as it does in Python. Since the individual imports are not explicitly declared, the only alternative would be to add them as members to the global namespace (in order to be able to access them without a namespace prefix) and that would defeat the purpose of having modules.
import * as methods from 'module' will create an object named methods that contains all exports from the module. There's no way to import and attach directly to the global namespace. --Very right!

Develop/Organize a node module in TypeScript

I want to develop a node module in TypeScript, but I'm having some problems with all the possible options to require, import, etc.
What I'm doing right now is having every class and interface in it's own file. So I would need to require all the dependencies which is kind of stupid, because I'm typing the class name twice, like this:
import Target = require('./Target');
export interface IFace {
getTarget(): Target.Target
}
I could write import t = require('./Target'); instead but then I need to write t.Target which I think is also pretty ugly.
And also I can't give it a module name (like FaceApp), because when I need to import two files, there's a naming conflict.
Obviously that would not be needed if everything would live in one file, but this is far from optimal I think.
So how do you guys organize your node module in TypeScript? I'd be happy to hear your suggestions.
You can avoid the name duplication by using the export = syntax. i.e. Do:
class Target{}
export = Target;
instead of export class Target.
Also grunt-ts transformers can help you with the import statement explosion : https://github.com/grunt-ts/grunt-ts/issues/85#issue-29515541
The way recommended by TypeScript is to do
export default class Target {}
and then you can do a true typescript import with
import Target from './Target'
alternatively, you can rename it
import NewName from './Target'
Also note that you can export multiple things from a file if they are related
export class SomeClass {}
export class OtherClass {}
And that on import, you can change the names
import { SomeClass as MySomeClass, OtherClass as MyOtherClass } from './Target'

Resources