UNPKG

nest-koa-adapter

Version:
68 lines 2.75 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KoaCorsOptions = void 0; class KoaCorsOptions { static validateNestOptions(options = {}) { if (typeof options.origin === 'function') { throw new Error(`KoaNestAdapter doesn't support CustomOrigin type as origin option.`); } } constructor(nestOptions) { this.allowMethods = nestOptions.methods; this.allowHeaders = nestOptions.allowedHeaders; this.exposeHeaders = nestOptions.exposedHeaders; this.credentials = nestOptions.credentials; this.maxAge = nestOptions.maxAge; this.origin = this.createOriginResolver(nestOptions); } createOriginResolver(nestOptions) { const { origin } = nestOptions; if (!origin) { return; } return (ctx) => { const requestOrigin = ctx.get('Origin'); if (origin === true) { return requestOrigin; } const origins = Array.isArray(origin) ? origin : [origin]; const matches = origins.some((o) => { if (typeof o === 'string') { return o === requestOrigin; } if (o instanceof RegExp) { return o.test(requestOrigin); } return false; }); if (!matches) { return ''; } return requestOrigin; }; } handleOptionsSuccessStatus(koa, nestOptions = {}) { const { optionsSuccessStatus } = nestOptions; if (!optionsSuccessStatus) { return; } koa.use((ctx, next) => __awaiter(this, void 0, void 0, function* () { yield next(); if (ctx.method === 'OPTIONS' && ctx.get('Access-Control-Request-Method')) { ctx.status = optionsSuccessStatus; } })); } } exports.KoaCorsOptions = KoaCorsOptions; //# sourceMappingURL=KoaCorsOptions.js.map