Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📚 Docs Feedback: Add informations about how to use Novu with NestJs #6552

Closed
1 task done
paulwer opened this issue Sep 23, 2024 · 9 comments · Fixed by #6654 or novuhq/docs#709
Closed
1 task done

📚 Docs Feedback: Add informations about how to use Novu with NestJs #6552

paulwer opened this issue Sep 23, 2024 · 9 comments · Fixed by #6654 or novuhq/docs#709
Assignees
Labels
Assigned & In-Progress Feature Created by Linear-GitHub Sync

Comments

@paulwer
Copy link
Contributor

paulwer commented Sep 23, 2024

Duplicates

  • I have searched the existing issues

Related page

https://docs.nestjs.com/middleware

Kind of issue

Missing information

Issue description

NestJs is like a superset of express, therefore a user can leverage the middleware implementation similar to express.

Afterwards you find some of our implemented code to use for your documentation:

the express middleware, using require, because we are using commonjs

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { serve } from '@novu/framework/dist/servers/express';
import workflows from './workflows';

@Injectable()
export class NovuMiddleware implements NestMiddleware {
    use(req: Request, res: Response, next: NextFunction) {
        // Using require instead of import syntax, because novu export is not commonjs
        require("@novu/framework/express").serve({
            workflows,
        } as Parameters<typeof serve>['0'])(req, res, next)
    }
}

Probably the right implementation

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { serve } from '@novu/framework/express';
import workflows from './workflows';

@Injectable()
export class NovuMiddleware implements NestMiddleware {
    use(req: Request, res: Response, next: NextFunction) {
        serve({
            workflows,
        })(req, res, next)
    }
}

app.module.ts

import { MiddlewareConsumer, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { NovuMiddleware } from './shared/novu/novu.middleware';

@Module({
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(NovuMiddleware)
      .forRoutes('/api/novu');
  }
}

Context 🔦

No response

Copy link

linear bot commented Sep 23, 2024

@jainpawan21
Copy link
Member

@paulwer

We are working on nestjs handler. it will be released soon.

@jainpawan21 jainpawan21 added Feature Created by Linear-GitHub Sync Assigned & In-Progress and removed triage labels Oct 7, 2024
@paulwer
Copy link
Contributor Author

paulwer commented Oct 7, 2024

@jainpawan21 very glad to hear this. we are currently considdering reusing the types of the handler to be used in our code structure.
Are there any plans to reuse types from the handler within the sdk to trigger an event in a typesafe way?

@rifont
Copy link
Contributor

rifont commented Oct 8, 2024

Hey @paulwer , thanks for raising the issue.

Yes, we absolutely plan to have type-safe event triggering, and we also want to establish an idiomatic NestJS approach for dependency injection usage inside Workflows. You can view the draft developer interface in this PR - #6654, we would love to hear your feedback.

We are providing 2 interfaces for NestJS usage of Novu Workflows:

  • NovuModule.register(...) - basic usage without a need for dependency injection inside the workflow(...) function
  • NovuModule.registerAsync(...) - advanced usage with native NestJS dependency injection inside the workflow(...) function

@rifont
Copy link
Contributor

rifont commented Oct 10, 2024

Hi @paulwer , the NestJS handler is released in @novu/framework@2.2.0 🎉

You can find the NestJS quickstart docs here.

@paulwer
Copy link
Contributor Author

paulwer commented Oct 10, 2024

very much aprecheated :)

thanks

@paulwer
Copy link
Contributor Author

paulwer commented Oct 14, 2024

@rifont we have the following error:
I guess this is not related to nest itself and was the reason, why i had implemented it like presented before (with the required syntax).
Would it be possible to provide the lib also in commonjs?

Cannot find module '@novu/framework/nest' or its corresponding type declarations.
There are types at 'C:/dev/repositories/almig/backends/default/node_modules/@novu/framework/dist/servers/nest.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

@Gbenga238
Copy link

Hi, I'm running into this error.

node_modules/@novu/framework/dist/handler-By7gnvmo.d.ts:4102:59 - error TS2536: Type '"output"' cannot be used to index type '{ chat: { discord: { output: { readonly type: "object"; readonly properties: {}; readonly required: readonly []; readonly additionalProperties: true; }; }; getstream: { output: { readonly type: "object"; readonly properties: {}; readonly required: readonly []; readonly additionalProperties: true; }; }; ... 7 more .....'.

4102 }) => Awaitable<WithPassthrough<FromSchemaUnvalidated<(typeof providerSchemas)[T_StepType][K]['output']>>>;
"
"
node_modules/@novu/framework/dist/handler-By7gnvmo.d.ts:4102:21 - error TS2589: Type instantiation is excessively deep and possibly infinite.

4102 }) => Awaitable<WithPassthrough<FromSchemaUnvalidated<(typeof providerSchemas)[T_StepType][K]['output']>>>;"

@rifont
Copy link
Contributor

rifont commented Nov 12, 2024

hey @Gbenga238 we released some Typescript performance improvements in @novu/framework@2.4.0 (ref: #6963).

Please try the new version and open a new issue if you continue to see Type instantiation is excessively deep and possibly infinite type errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Assigned & In-Progress Feature Created by Linear-GitHub Sync
Projects
None yet
4 participants