UNPKG

@saleor/app-sdk

Version:
161 lines (153 loc) 5.23 kB
import { GenericSaleorWebhook, ManifestActionHandler, ProtectedActionValidator, RegisterActionHandler } from "../../chunk-6WJWQ3UC.mjs"; import "../../chunk-WR25JCBM.mjs"; import "../../chunk-NHGUNOYT.mjs"; import "../../chunk-FTAQRPFZ.mjs"; import "../../chunk-DMVVCVUO.mjs"; import "../../chunk-SAZABKLB.mjs"; import "../../chunk-UCTHLSSD.mjs"; import { createDebug } from "../../chunk-CPDLIPGD.mjs"; // src/handlers/platforms/aws-lambda/platform-adapter.ts var AwsLambdaAdapter = class { constructor(event, context) { this.event = event; this.context = context; // This stage name is used when no stage name is provided in AWS CDK // this means that stage name is not appended to the lambda URL this.DEFAULT_STAGE_NAME = "$default"; this.request = event; } getHeader(requestedName) { const name = requestedName.toLocaleLowerCase(); return this.request.headers[name] || null; } async getBody() { if (!this.request.body) { return null; } return JSON.parse(this.request.body); } async getRawBody() { if (!this.request.body) { return null; } return this.request.body; } getBaseUrl() { const xForwardedProto = this.getHeader("x-forwarded-proto") || "https"; const host = this.getHeader("host"); const xForwardedProtos = Array.isArray(xForwardedProto) ? xForwardedProto.join(",") : xForwardedProto; const protocols = xForwardedProtos.split(","); const protocol = protocols.find((el) => el === "https") || protocols.find((el) => el === "http") || protocols[0]; const { stage } = this.event.requestContext; if (stage && stage !== this.DEFAULT_STAGE_NAME) { return `${protocol}://${host}/${stage}`; } return `${protocol}://${host}`; } get method() { return this.event.requestContext.http.method; } async send(result) { const body = result.bodyType === "json" ? JSON.stringify(result.body) : result.body; return { statusCode: result.status, headers: { "Content-Type": result.bodyType === "json" ? "application/json" : "text/plain" }, body }; } }; // src/handlers/platforms/aws-lambda/create-app-register-handler.ts var createAppRegisterHandler = (config) => async (event, context) => { const adapter = new AwsLambdaAdapter(event, context); const useCase = new RegisterActionHandler(adapter); const result = await useCase.handleAction(config); return adapter.send(result); }; // src/handlers/platforms/aws-lambda/create-manifest-handler.ts var createManifestHandler = (config) => async (event, context) => { const adapter = new AwsLambdaAdapter(event, context); const actionHandler = new ManifestActionHandler(adapter); const result = await actionHandler.handleAction(config); return adapter.send(result); }; // src/handlers/platforms/aws-lambda/create-protected-handler.ts var createProtectedHandler = (handlerFn, apl, requiredPermissions) => async (event, context) => { const adapter = new AwsLambdaAdapter(event, context); const actionValidator = new ProtectedActionValidator(adapter); const validationResult = await actionValidator.validateRequest({ apl, requiredPermissions }); if (validationResult.result === "failure") { return adapter.send(validationResult.value); } const saleorContext = validationResult.value; try { return await handlerFn(event, context, saleorContext); } catch (err) { return { statusCode: 500, body: "Unexpected Server Error" }; } }; // src/handlers/platforms/aws-lambda/saleor-webhooks/saleor-webhook.ts var debug = createDebug("SaleorWebhook"); var SaleorWebApiWebhook = class extends GenericSaleorWebhook { /** * 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) { return async (event, context) => { const adapter = new AwsLambdaAdapter(event, context); const prepareRequestResult = await super.prepareRequest(adapter); if (prepareRequestResult.result === "sendResponse") { return prepareRequestResult.response; } debug("Incoming request validated. Call handlerFn"); return handlerFn(event, context, { ...prepareRequestResult.context }); }; } }; // src/handlers/platforms/aws-lambda/saleor-webhooks/saleor-async-webhook.ts var SaleorAsyncWebhook = class extends SaleorWebApiWebhook { constructor(configuration) { super(configuration); this.eventType = "async"; this.event = configuration.event; } createHandler(handlerFn) { return super.createHandler(handlerFn); } }; // src/handlers/platforms/aws-lambda/saleor-webhooks/saleor-sync-webhook.ts var SaleorSyncWebhook = class extends SaleorWebApiWebhook { constructor(configuration) { super(configuration); this.eventType = "sync"; this.event = configuration.event; } createHandler(handlerFn) { return super.createHandler(handlerFn); } }; export { AwsLambdaAdapter, SaleorAsyncWebhook, SaleorSyncWebhook, createAppRegisterHandler, createManifestHandler, createProtectedHandler };