Skip to main content

@rtorcato/api-common v1.0.0

Interfaces​

WebhookOptions​

Defined in: express/index.ts:5

Extends​

  • SignatureOptions

Properties​

secret​

secret: string

Defined in: express/index.ts:7

Shared secret used to compute the expected HMAC.

optional header?: string

Defined in: express/index.ts:9

Request header carrying the signature. Default: 'x-signature'.

algorithm?​

optional algorithm?: string

Defined in: index.ts:5

HMAC hash algorithm. Default: 'sha256'.

Inherited from​

SignatureOptions.algorithm

prefix?​

optional prefix?: string

Defined in: index.ts:10

Prefix carried on the signature header value (included in both the computed and the compared value), e.g. GitHub's 'sha256='. Default: ''.

Inherited from​

SignatureOptions.prefix

Functions​

webhookMiddleware()​

webhookMiddleware(options): RequestHandler

Defined in: express/index.ts:33

Express webhook middleware: captures the raw request body, verifies its HMAC signature, then parses the JSON onto req.body for downstream handlers.

Mount it as the route's middleware, before any express.json(), which would otherwise consume the raw body needed to verify the signature:

app.post('/webhooks/github', webhookMiddleware({
secret: env.WEBHOOK_SECRET,
header: 'x-hub-signature-256',
prefix: 'sha256=',
}), (req, res) => {
// req.body is the parsed, verified payload
res.sendStatus(204)
})

Responds 401 on a missing/invalid signature and 400 on an unparseable JSON body, using the standard @rtorcato/api-common/errors envelope.

Parameters​

options​

WebhookOptions

Returns​

RequestHandler