How to use declare functions in NestJS - nestjs

I was using this repo with Node.js:
https://www.npmjs.com/package/pg-essential
But now, with NestJS I'm having problems to make the import properly.
The original lines are:
var pg = require('pg');
require('pg-essential').patch(pg);
And using NestJS I can use: import * as pg from 'pg';
for the pg require, but how can import the second require?

You could do
import { patch } from 'pg-essential';
patch(pg);
Importing a named exporting and calling the function, instead of doing a chained require function.

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.

How to call constructor function when using ESM import syntax in nodejs?

in NodeJS you can 'import' an external file like so:
var myModule = require('./module.js');
ESM modules allow you to do the same in nodejs as you do in the backend...
import myModule from './module.js';
However, with the require syntax, I can call a constructor function (forgive if my term is wrong) like so:
var myModule = require('./module.js')(myObject);
... how would one do this with ESM syntax?
I really don't think this is possible, I think you just have to use
import MyModule from './module';
const myModule = MyModule();
For this reason I use PascalCase for modules, even though that's not the normal convention.

Supporting node and es-module style imports for typescript package

I have a project that I am converting from JS (transpiled via babel) to Typescript. The module consists of a single function and uses an module.exports = myFunction to set it as the default.
The issue that I've run into is that I'd like to preserve require format for node users:
const myModule = require('my-module')
still allow es module aware users (typescript/babel) to import:
import myModule from 'my-module'
I've seen the typescript-library-starter which has a separate build process using rollup to emit UMDified version of the module that translates the module.exports.default = myModule to a node style module.exports = myModule but I'd prefer not to have to include an entirely separate build system for server side code (this is server only, so there's no great need for the tree-shaking/compression that rollup provides).
Currently, I can do something like the following
export = myFunction
which will support the const myModule = require('my-module') use case, but means that TS/es module users will need to do something like:
import * as myModule from 'my-module'
or drop back to a simple const myModule = require('my-module') (which removes the benefits of typescript).
Is there a pattern to distribute my module that works for both node style requires and es-module style imports?
I didn't get a great answer to this so I ended up using export = and import = require
// index.ts
function myModule () {}
export = myModule
Node users can use a regular require and typescript users can use a mix of import and require to get type safety:
// consumer.ts
import myModule = require('my-module')
Which is a pattern I gleaned from theTypescript docs:
When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
You can see this "live" in my protractor-flake module.

How to import node module in Angular?

I'm developing an Angular app.
For some reason I want to use querystring.escape().
I wrote import { escape } from 'querystring', but I got querystring.escape is not a function error.
How can I import a node module in typescript?
You are destructuring querystring trying to get the escape property, but then you want to call querystring.escape() which you haven't imported.
What you want to do is this:
import * as querystring from 'querystring';
or
import querystring from 'querystring'

Resources