@breautek/storm
Version:
Object-Oriented REST API framework
81 lines (78 loc) • 2.94 kB
JavaScript
;
/*
Copyright 2017-2021 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CORSMiddleware = void 0;
const Middleware_1 = require("./Middleware");
const instance_1 = require("./instance");
/**
* CORSMiddleware is used to enable CORS on APIs.
* It will automatically add the necessary headers necessary to
* communicate with CORS enabled clients.
*/
class CORSMiddleware extends Middleware_1.Middleware {
/**
* @constructor
* @param allowedOrigin The allowed origin. By default it will use the request origin.
* @param allowedHeaders Array of allowed headers.
* @param allowedMethods Array of allowed HTTP methods.
*/
constructor(allowedOrigin, allowedHeaders, allowedMethods) {
super();
this.$allowedOrigin = (!allowedOrigin) ? this.getDefaultAllowedOrigin() : allowedOrigin;
this.$allowedHeaders = (!allowedHeaders) ? this.getDefaultAllowedHeaders() : allowedHeaders;
this.$allowedMethods = (!allowedMethods) ? this.getDefaultAllowedMethods() : allowedMethods;
}
/**
* Sets the allowed origin. By default,
*/
getDefaultAllowedOrigin() {
return null;
}
getDefaultAllowedHeaders() {
return [
'Accept',
(0, instance_1.getInstance)().getConfig().authentication_header,
'X-Requested-With',
'Content-Type',
'Access-Control-Allow-Origin'
];
}
getDefaultAllowedMethods() {
return [
'GET',
'POST',
'HEAD',
'OPTIONS',
'DELETE',
'PUT'
];
}
_execute(request, response) {
if (this.$allowedOrigin) {
response.setHeader('Access-Control-Allow-Origin', this.$allowedOrigin);
}
else {
response.setHeader('Access-Control-Allow-Origin', request.getHeader('Origin'));
}
response.setHeader('Access-Control-Allow-Headers', this.$allowedHeaders.join(', '));
response.setHeader('Access-Control-Allow-Methods', this.$allowedMethods.join(', '));
response.setHeader('Vary', 'Origin');
return Promise.resolve({
request: request,
response: response
});
}
}
exports.CORSMiddleware = CORSMiddleware;
//# sourceMappingURL=CORSMiddleware.js.map