@saleor/app-sdk
Version:
SDK for building great Saleor Apps
161 lines (140 loc) • 5.51 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _chunkQD7O4CVVjs = require('../../chunk-QD7O4CVV.js');
require('../../chunk-I3JXSADO.js');
require('../../chunk-EJ6YJ4BD.js');
require('../../chunk-ODMQWUGY.js');
require('../../chunk-GZLV5NDP.js');
require('../../chunk-VEHOYPBT.js');
require('../../chunk-KM2345JL.js');
var _chunkDE4A7PETjs = require('../../chunk-DE4A7PET.js');
// 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 (0, _chunkQD7O4CVVjs.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 (0, _chunkQD7O4CVVjs.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 (0, _chunkQD7O4CVVjs.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 = _chunkDE4A7PETjs.createDebug.call(void 0, "SaleorWebhook");
var SaleorWebApiWebhook = class extends _chunkQD7O4CVVjs.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);
}
};
exports.AwsLambdaAdapter = AwsLambdaAdapter; exports.SaleorAsyncWebhook = SaleorAsyncWebhook; exports.SaleorSyncWebhook = SaleorSyncWebhook; exports.createAppRegisterHandler = createAppRegisterHandler; exports.createManifestHandler = createManifestHandler; exports.createProtectedHandler = createProtectedHandler;