yandex-cloud-functions-router
Version:
Node router for Yandex Cloud Functions
31 lines (30 loc) • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleCorsPreflight = void 0;
const getHeaderValue_1 = require("../getHeaderValue");
const isOriginValid_1 = require("./isOriginValid");
const handleCorsPreflight = (event, options) => {
var _a, _b, _c;
if (event && options && options.enable) {
const method = (_a = event.httpMethod) === null || _a === void 0 ? void 0 : _a.toString().trim().toUpperCase();
const currentOrigin = getHeaderValue_1.getHeaderValue(event, 'origin');
if (method === 'OPTIONS') {
if (currentOrigin && isOriginValid_1.isOriginValid(options, currentOrigin)) {
const accessControlAllowMethods = (_b = options.allowedMethods) === null || _b === void 0 ? void 0 : _b.map((m) => m.trim().toUpperCase()).join(', ');
const accessControlAllowHeaders = (_c = options.allowedHeaders) === null || _c === void 0 ? void 0 : _c.map((m) => m.trim()).join(', ');
const isCookieSet = Boolean(getHeaderValue_1.getHeaderValue(event, 'cookie'));
return {
statusCode: 204,
headers: Object.assign(Object.assign(Object.assign({ 'Access-Control-Allow-Origin': currentOrigin }, (accessControlAllowMethods ? { 'Access-Control-Allow-Methods': accessControlAllowMethods } : {})), (accessControlAllowHeaders ? { 'Access-Control-Allow-Headers': accessControlAllowHeaders } : {})), (options.allowCredentials === true && isCookieSet ? { 'Access-Control-Allow-Credentials': 'true' } : {}))
};
}
else {
return {
statusCode: 204
};
}
}
}
return undefined;
};
exports.handleCorsPreflight = handleCorsPreflight;