feat(events-backend,events-node): add a parser option to events by content type

Signed-off-by: Rogerio Angeliski <angeliski@hotmail.com>
This commit is contained in:
Rogerio Angeliski
2025-02-12 22:25:37 -03:00
parent 9c3eb928ef
commit b95aa77ce2
18 changed files with 393 additions and 47 deletions
+42
View File
@@ -140,6 +140,48 @@ export const eventsModuleYourFeature = createBackendModule({
});
```
### Request Body Parse
We need to parse the request body before we can validate it. We have some default parsers but you can provide your own when necessary.
```ts
import { eventsExtensionPoint } from '@backstage/plugin-events-node/alpha';
// [...]
export const eventsModuleYourFeature = createBackendModule({
pluginId: 'events',
moduleId: 'your-feature',
register(env) {
// [...]
env.registerInit({
deps: {
// [...]
events: eventsExtensionPoint,
// [...]
},
async init({ /* ... */ events /*, ... */ }) {
// [...]
events.addHttpPostBodyParser({
contentType: 'application/x-www-form-urlencoded',
parser: async (req, _topic) => {
return {
bodyParsed: req.body.toString('utf-8'),
bodyBuffer: req.body,
encoding: 'utf-8',
};
},
});
},
});
},
});
```
We have the following default parsers:
- `application/json`
#### Legacy Backend System
```ts