T3 - Importing router from `../trpc` - next

I am using t3-oss https://github.com/t3-oss.
In latest version of t3, when I want to create a new router and want to import {router} from trpc, I have error:
import { router, publicProcedure, protectedProcedure } from "../trpc";
Module '"../trpc"' has no exported member 'router'.ts(2305)
I checked the trpc documentation, and {router} should include the `trpc.ts' file. -> https://trpc.io/docs/router
What should I do? When is router in T3?
This is my trpc.ts
https://pastebin.com/g3Y0iP14

In the template generated by Create T3 App, all of the tRPC related stuff is exported from ./src/server/api/trpc.ts. You can look in ./src/server/api/routers/example.ts for an example of all the imports you'll need.
The specific thing you're looking for is called createTRPCRouter.

Related

How to import new dynamic module in service?

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{}

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.

Is it possible to import a function in a modules subfolder

I'm trying to access the decode() method in the jsQR module.
I found an example that called decode() directly but that was from an HTML file, not nodejs.
In visual code I see this...
I know that the default export is defined in index.d.ts but is there anyway of importing the other classes/functions on the rest of the dist folder?
I've tried to import using require("jsqr/decoder") and require("jsqr/decoder/decode") to no avail.
EDIT
To be clear, I don't want jsQR, the default export. That deals with images. I'm trying to explicitly call the decode() method in the pic which accepts a BitMatrix
There are no limitations in what you can require from node_modules. So, with your case it should look like:
TypeScript\ESM Modules:
import { decode } from 'jsqr/dist/decoder/decoder';
CommonJS:
const { decode } = require('jsqr/dist/decoder/decoder');
UPD: If you take a look into dist folder for jsqr package, you can find that there are only d.ts files. Which means that you can not import it from there.
But, you can find an actual export of the module here:
Which means that you should able to import it from jsqr:
const { decode } = require('jsqr');

How to set up references and import external modules as top-level objects in Typescript in Node.js

So this is a Typescript file structure and naming question. I'm just starting to learn about Typescript classes and modules and I have a few questions on how to structure model logic across multiple files.
Say I have a small rest api written in node.js with User and Photo models, and it's a simple api that lets users register and upload a photo.
Assuming this file structure:
-app
-models
-user.js
-photo.js
-controller.js
In my controller is it better to use a reference path declaration or external modules when referencing models? Does this overlap with just using a standard node.js require statement?
If I use an import statement to load the models as an external module, can I change the below example to move the export to the top-level so that I can just new up a User() rather than models.User().
for example, the models, and anotherModel seems a bit redundant - and I don't want to combine all the models into one file.
controller.js
import models = require('./models/user');
import anotherModel = require('./models/photo');
var newUser = new models.User();
var newMoment = new anotherModel.Moment();
user.js
export class User {
name:string
}
photo.js
export class Photo {
url:string
}
You can create a model / index.ts and re-export all the models from that file for easier consumption. This is conventional node.js paradigm.

Loading typescript external module without importing?

I am having a headache at the moment because I am writing a nodejs typescript app which is basically one big internal module (spread over lots of files and outputted as one).
Now the problem I have is that express.d.ts (found on definitely typed) is written so it can only be loaded as an external module: import express = require("express"); however then that means that I have to compile my application as a single file as the moment you put the import keyword in it treats it like your module is now external, which I do not want.
So is there any way for me to change this code:
/// <reference path="../../../typescript-descriptors/express/express.d.ts" />
import express = require('express');
var app = express();
app.get('/', (req: express.Request, res: express.Response) => {
res.render('index');
});
so it does not require the import and I can just do var express = require("express"); and still get the type safety?
As I NEED to be able to split my TS logic over multiple files and using the import method does not allow this.
Updated based on the comment...
Express itself is what TypeScript calls an external module. It is not possible to import an external module into an internal module - so you can't actually achieve what you want directly here.
However, if the real underlying problem is...
As I NEED to be able to split my TS logic over multiple files and using the import method does not allow this
What is stopping you from simply switching to external modules, which allows you to split your logic into many files and import them as needed? Rather than trying to combine the output into a single file - lean on the simple module loading that Node gives you for free and you're back in the game!

Resources