Skip to content

Commit

Permalink
added rawBody support for express
Browse files Browse the repository at this point in the history
  • Loading branch information
jleeson committed Aug 15, 2024
1 parent b009b33 commit b17f036
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/firefly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ export class TaskService {
Firefly is platform agnostic. The controller decorators provide metadata to the class which is handled by the application interface,
out of the box, Firefly provides a platform binding for express, enabling integration with the entire express ecosystem.

Firefly also extends the Express Request object by adding a `rawBody` property to it, which is useful for webhook signature validation. When using typescript you can access the request typing for this by importing `RawBodyRequest` from `@outwalk/firefly/express`.

**Example:**
```js
import { Application } from "@outwalk/firefly";
Expand Down
10 changes: 8 additions & 2 deletions packages/firefly/src/modules/platform/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ export class ExpressPlatform extends Platform {

this.app = express();

/* this function adds a rawBody property to the request object */
const rawBodyBuffer = (req, res, buffer, encoding) => {
if (!buffer || !buffer.length) return;
req.rawBody = buffer.toString(encoding || "utf8");
};

this.app.disable("x-powered-by");
this.app.use(express.urlencoded({ extended: true }));
this.app.use(express.json());
this.app.use(express.urlencoded({ verify: rawBodyBuffer, extended: true }));
this.app.use(express.json({ verify: rawBodyBuffer }));
this.app.use(cookieParser());
}

Expand Down
5 changes: 5 additions & 0 deletions packages/firefly/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ declare module "@outwalk/firefly/errors" {

declare module "@outwalk/firefly/express" {
import type { Platform, Route } from "@outwalk/firefly";
import type { Request } from "express";

export class ExpressPlatform extends Platform {

Expand All @@ -102,6 +103,10 @@ declare module "@outwalk/firefly/express" {

use(...middleware: any[]): void;
}

export interface RawBodyRequest extends Request {
rawBody?: string;
}
}

declare module "@outwalk/firefly/mongoose" {
Expand Down

0 comments on commit b17f036

Please sign in to comment.