-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathnovu-bridge-client.ts
More file actions
58 lines (51 loc) · 2.45 KB
/
novu-bridge-client.ts
File metadata and controls
58 lines (51 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Inject } from '@nestjs/common';
import { PostActionEnum, type Workflow } from '@novu/framework/internal';
import { Client, NovuHandler, NovuRequestHandler } from '@novu/framework/nest';
import { EnvironmentTypeEnum } from '@novu/shared';
import type { Request, Response } from 'express';
import { ConstructFrameworkWorkflow, ConstructFrameworkWorkflowCommand } from './usecases/construct-framework-workflow';
/*
* A custom framework name is specified for the Novu-managed Bridge endpoint
* to provide a clear distinction between Novu-managed and self-managed Bridge endpoints.
*/
const frameworkName = 'novu-nest';
/**
* This class overrides the default NestJS Novu Bridge Client to allow for dynamic construction of
* workflows to serve on the Novu Bridge.
*/
export class NovuBridgeClient {
constructor(
@Inject(NovuHandler) private novuHandler: NovuHandler,
private constructFrameworkWorkflow: ConstructFrameworkWorkflow
) {}
public async handleRequest(req: Request, res: Response) {
const workflows: Workflow[] = [];
/*
* Only construct a workflow when dealing with a POST request to the Novu-managed Bridge endpoint.
* Non-POST requests don't have a `workflowId` query parameter, so we can't construct a workflow.
* Those non-POST requests are handled for the purpose of returning a successful health-check.
*/
if (Object.values(PostActionEnum).includes(req.query.action as PostActionEnum)) {
const programmaticallyConstructedWorkflow = await this.constructFrameworkWorkflow.execute(
ConstructFrameworkWorkflowCommand.create({
environmentId: req.params.environmentId,
workflowId: req.query.workflowId as string,
layoutId: req.query.layoutId as string,
controlValues: req.body.controls,
action: req.query.action as PostActionEnum,
skipLayoutRendering: req.query.skipLayoutRendering === 'true',
jobId: req.query.jobId ? (req.query.jobId as string) : undefined,
environmentType: req.query.environmentType as EnvironmentTypeEnum | undefined,
})
);
workflows.push(programmaticallyConstructedWorkflow);
}
const novuRequestHandler = new NovuRequestHandler({
frameworkName,
workflows,
client: new Client({ secretKey: 'INTERNAL_KEY', strictAuthentication: false, verbose: false }),
handler: this.novuHandler.handler,
});
await novuRequestHandler.createHandler()(req as any, res as any);
}
}