Modifying the NestJS Decorator string parameter upon library import: A step-by-step guide

I am working with a NestJs monorepo that contains several Apps (microservices) and Libs. One common Service class is used across all apps, so I decided to extract it into a separate lib.

Initially, I thought this was a good idea. However, I soon realized that I have a decorator on a method within this class that requires a unique const string for each individual app.

It seems challenging to accomplish this. Is there a solution available?

Essentially, I need a way to "inject" a const string from the loading app into the lib so that it can be used by the decorator.

Specifically, the decorator in question is the @RabbitSubscribe() decorator from golevelup/nestjs-rabbitmq. The queue name (a const string parameter for the decorator) needs to vary for each module.

More broadly, if I have this class in a lib:

@Injectable()
export class MyService {
  @ADecorator({
    param: 'this is the string',
  })
  myFunction() { ... }
}

How can I import it into different Apps with a different string parameter for each app?

Answer №1

Full disclosure: I am the creator of @golevelup/nestjs-rabbitmq.

The official configuration module for NestJS can only be initialized after the DI system for NestJS is up and running. As a result, it is not feasible to dynamically configure a decorator using this module.

To address this limitation (as well as enhance type safety), I developed a different configuration package that leverages ENV and .env files to facilitate the configuration of your application(s). With this package, you can set up your app configuration prior to NestJS DI and the importation of any files or Decorators, allowing you to write code like:

@Injectable()
export class MyService {
  @ADecorator({
    param: config.queueName,
  })
  myFunction() { ... }
}

With config.queueName potentially being retrieved differently for each instance of the app based on an ENV value or fetched from a .env or .json file

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for sending an image to Cloudinary using the fetch API

Struggling to figure out how to successfully upload a file to Cloudinary using fetch on my front-end. After consulting the documentation and various StackOverflow threads, I'm still facing a frustrating 400 error: export async function uploadImageToCl ...

Exploring the visitor design pattern with numerical enumerated types

I am exploring the use of the visitor pattern to ensure comprehensive handling when adding a new enum value. Here is an example of an enum: export enum ActionItemTypeEnum { AccountManager = 0, Affiliate = 4, } Currently, I have implemented the fol ...

Encountering the error message "Received interpolation ({{}}) when an expression was expected" is a common issue when trying to interpolate tag attribute values within an ngFor loop

I've been working with a Devextreme's Datatable and my goal is to dynamically configure the table using a columns configuration array. The plan is to loop through this array using an ngFor and set column tags properties dynamically. Now, the bi ...

Leveraging an intersection type that encompasses a portion of the union

Question: I am currently facing an issue with my function prop that accepts a parameter of type TypeA | TypeB. The problem arises when I try to pass in a function that takes a parameter of type Type C & Type D, where the intersection should include al ...

The function res.status is not defined

Currently, I am in the process of integrating my upcoming app with Google Sheets. I have relocated the function that manages the post request to "app/api/sheets" as per the recommended documentation. import type { NextApiRequest, NextApiResponse } from &ap ...

Tips on adding a select tag option value to an Angular 4 web service

I am currently working on a web service that allows me to post data in order to create profiles. I have encountered an issue with a drop-down list in the form where the selected option value does not get posted. This results in an 'undefined' err ...

Can you explain the significance of the `?` symbol before an object attribute? And why is my TypeScript file not recognizing it?

I have always utilized this particular behavior in Angular using the *ngIf directive when dealing with an object that could potentially be undefined or not the required object. <div *ngIf="object?.foo"> ... </div> Although I know that this ...

How can I receive the response from a GET request using React Query? I am currently only able to get the response

I have created a search component where I input a name in the request, receive a response, and display it immediately. However, after the first input submit, I get undefined in response. Only after the second submit do I get the desired response. The tec ...

Creating object interfaces in TypeScript dynamically based on function parameters

I'm currently working on a small project that involves creating lists of products in a certain format. For example: let products = createList( new Product('product1'), new Product('product2') ) When it comes to accessing a s ...

Error encountered in typescript when trying to implement the Material UI theme.palette.type

Just starting out with Material UI and TypeScript, any tips for a newcomer like me? P.S. I'm sorry if my question formatting isn't quite right, this is my first time on Stack Overflow. https://i.stack.imgur.com/CIOEl.jpg https://i.stack.imgur.co ...

Develop a series of sequential tests for the playwright to execute

Can someone assist me with my code? I am attempting to write a test in Playwright that navigates to the forgot password page, creates a new password, and then tries to log in using that new password. However, I am encountering an issue with retrieving the ...

Are you facing difficulties while trying to utilize useContext in your React application?

I have a basic React application where I need to implement useContext. (by the way, I am using Vite + React) Below is the code for my Context.jsx file: import React, {useContext} from 'react'; const emailContext = React.createContext(); expor ...

Nest faces difficulty resolving the dependencies required by the TMPController

I've tried everything to fix this error, but nothing seems to be working. tmp.module.ts import { Module } from "@nestjs/common"; import { TMPController } from "./tmp.controller"; import { TMPService } from "./tmp.service"; @Module({ controllers: ...

What causes Angular2 to detect both reference changes and primitive changes even when the OnPush flag is enabled?

Take a look at this code snippet import {Component, OnInit, Input, OnChanges, DoCheck, ChangeDetectionStrategy} from 'angular2/core' @Component({ selector: 'child1', template: ` <div>reference change for entire object: { ...

"encountered net::ERR_NAME_NOT_RESOLVED error when trying to upload image to s3 storage

I am currently developing an application using Angular. I have been attempting to upload a picture to my S3 bucket, but each time I try, I encounter this error in the console. https://i.stack.imgur.com/qn3AD.png Below is the code snippet from my upload.s ...

Angular2 - leveraging root-relative imports

Having trouble with imports in angular2/typescript? Want to use paths relative to the project root like 'app/components/calendar', but currently stuck using something like this: //app/views/order/order-view.ts import {Calendar} from '../../ ...

Tips for styling the Button component in the shadcn/ui library for maximum impact

I'm currently working on a project using the shadcn/ui library. How can I properly customize it to meet my specific needs? For example, let's say I require an extra large red rounded Button for a call-to-action button in my project. What would be ...

Issue with displaying decimal places in Nivo HeatMap

While utilizing Nivo HeatMap, I have observed that the y value always requires a number. Even if I attempt to include decimal places (.00), it will still trim the trailing zeros and display the value without them. The expected format of the data is as foll ...

Unable to locate the name 'Cheerio' in the @types/enzyme/index.d.t file

When I try to run my Node application, I encounter the following error: C:/Me/MyApp/node_modules/@types/enzyme/index.d.ts (351,15): Cannot find name 'Cheerio'. I found a suggestion in a forum that recommends using cheerio instead of Cheerio. H ...

Invoke the router function dynamically

I am looking for a way to simplify route registration without manually writing out app.get('/', function (req, res, next) { }); each time. I want to automate this process by passing in a router object like the one below... { path: '&ap ...