Skip to content

Commit 5a07655

Browse files
committed
document how to use the HttpWebhookHandler
1 parent 63c4e5d commit 5a07655

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

.changeset/little-webs-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@weissaufschwarz/mitthooks": patch
3+
---
4+
5+
documented how to use the HttpWebhookHandler

packages/mitthooks/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,68 @@ function createCustomSeparateWebhookHandlerWithHandlerForAdded(
217217
}
218218
```
219219

220+
## Generic HTTP Wrapper
221+
222+
The `HttpWebhookHandler` is a generic wrapper that converts HTTP requests into webhook content and delegates them to a webhook handler.
223+
It extracts the necessary signature headers from the request, reads the request body, and converts errors into appropriate HTTP responses.
224+
225+
This wrapper is useful when you want to handle webhooks directly from HTTP requests without framework-specific code.
226+
227+
### Basic Usage
228+
229+
```typescript
230+
import { HttpWebhookHandler } from "@weissaufschwarz/mitthooks/bootstrapping/http-wrapper.js";
231+
import { CombinedWebhookHandlerFactory } from "@weissaufschwarz/mitthooks/factory/combined.js";
232+
import type { ExtensionStorage } from "@weissaufschwarz/mitthooks/storage/extensionStorage.js";
233+
234+
function createHttpWebhookHandler(
235+
extensionStorage: ExtensionStorage,
236+
extensionId: string,
237+
): HttpWebhookHandler {
238+
const handleWebhook = new CombinedWebhookHandlerFactory(
239+
extensionStorage,
240+
extensionId,
241+
).build();
242+
243+
return new HttpWebhookHandler(handleWebhook);
244+
}
245+
246+
// Usage with a standard Request object
247+
const handler = createHttpWebhookHandler(storage, "your-extension-id");
248+
const response = await handler.handleWebhook(request);
249+
```
250+
251+
### How It Works
252+
253+
The `HttpWebhookHandler` performs the following steps:
254+
255+
1. **Extract Headers**: Reads the signature-related headers from the incoming request:
256+
- `X-Marketplace-Signature-Serial`
257+
- `X-Marketplace-Signature-Algorithm`
258+
- `X-Marketplace-Signature`
259+
260+
2. **Read Body**: Reads the raw request body as text
261+
262+
3. **Delegate to Handler**: Passes the webhook content to the configured handler chain
263+
264+
4. **Error Handling**: Converts handler errors into appropriate HTTP responses:
265+
- `400 Bad Request`: Missing or invalid request data
266+
- `500 Internal Server Error`: Server-side processing errors
267+
- `200 OK`: Successfully processed webhook
268+
269+
5. The wrapper automatically handles mitthooks specific error types
270+
271+
### Framework Integration
272+
273+
The `HttpWebhookHandler` works with the standard Web API `Request` and `Response` objects, making it compatible with:
274+
- Node.js HTTP servers
275+
- Cloudflare Workers
276+
- Deno
277+
- Bun
278+
- Any framework supporting standard Request/Response objects like tanstack Start
279+
280+
For framework-specific integrations (like Next.js), see the dedicated packages like `mitthooks-nextjs`.
281+
220282
## ExtensionStorage
221283

222284
The `ExtensionStorage` is an interface that you have to implement to store the extension instances.

0 commit comments

Comments
 (0)