Skip to content

Commit 14283d7

Browse files
authored
Merge pull request #64 from weissaufschwarz/chore/more-accessible-imports
make recent features of mitthooks more accessible to use
2 parents f682e68 + 5a07655 commit 14283d7

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
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

.changeset/red-chicken-strive.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+
made import parts of mitthooks more accessible to import

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.

packages/mitthooks/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"type": "module",
66
"exports": {
77
"./*.js": "./dist/*.js",
8-
"./*": "./dist/*.js"
8+
"./*": "./dist/*.js",
9+
".": "./dist/index.js"
910
},
1011
"publishConfig": {
1112
"registry": "https://registry.npmjs.org"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export * from "./addedToContext.js";
2+
export * from "./chain.js";
3+
export * from "./combinedPersistence.js";
4+
export * from "./extensionId.js";
5+
export * from "./instanceRemoved.js";
6+
export * from "./instanceUpdated.js";
7+
export * from "./interface.js";
8+
export * from "./logging.js";
9+
export * from "./secretRotated.js";
10+
export * from "./verification.js";

packages/mitthooks/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./factory/combined.js";
22
export * from "./factory/separate.js";
33
export * from "./webhook.js";
4+
export * from "./handler/index.js";
45
export * from "./bootstrapping/http-wrapper.js";

0 commit comments

Comments
 (0)