-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsaleor-webhook.ts
48 lines (40 loc) · 1.8 KB
/
saleor-webhook.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { APIGatewayProxyStructuredResultV2, Context } from "aws-lambda";
import { createDebug } from "@/debug";
import {
GenericSaleorWebhook,
GenericWebhookConfig,
} from "@/handlers/shared/generic-saleor-webhook";
import { WebhookContext } from "@/handlers/shared/saleor-webhook";
import { AsyncWebhookEventType, SyncWebhookEventType } from "@/types";
import { AwsLambdaAdapter, AWSLambdaHandler, AwsLambdaHandlerInput } from "../platform-adapter";
const debug = createDebug("SaleorWebhook");
export type WebhookConfig<Event = AsyncWebhookEventType | SyncWebhookEventType> =
GenericWebhookConfig<AwsLambdaHandlerInput, Event>;
/** Function type provided by consumer in `SaleorWebApiWebhook.createHandler` */
export type AwsLambdaWebhookHandler<TPayload = unknown> = (
event: AwsLambdaHandlerInput,
context: Context,
ctx: WebhookContext<TPayload>,
) => Promise<APIGatewayProxyStructuredResultV2> | APIGatewayProxyStructuredResultV2;
export abstract class SaleorWebApiWebhook<TPayload = unknown> extends GenericSaleorWebhook<
AwsLambdaHandlerInput,
TPayload
> {
/**
* Wraps provided function, to ensure incoming request comes from registered Saleor instance.
* Also provides additional `context` object containing typed payload and request properties.
*/
createHandler(handlerFn: AwsLambdaWebhookHandler<TPayload>): AWSLambdaHandler {
return async (event, context) => {
const adapter = new AwsLambdaAdapter(event, context);
const prepareRequestResult = await super.prepareRequest<AwsLambdaAdapter>(adapter);
if (prepareRequestResult.result === "sendResponse") {
return prepareRequestResult.response;
}
debug("Incoming request validated. Call handlerFn");
return handlerFn(event, context, {
...prepareRequestResult.context,
});
};
}
}