@akemona-org/strapi
Version:
An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MongoDB, MySQL, MariaDB, PostgreSQL, SQLite
60 lines (51 loc) • 1.54 kB
JavaScript
;
/**
* Module dependencies
*/
const cors = require('@koa/cors');
const defaults = {
origin: '*',
maxAge: 31536000,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
headers: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
keepHeadersOnError: false,
};
module.exports = (strapi) => {
return {
/**
* Initialize the hook
*/
initialize() {
const { origin, expose, maxAge, credentials, methods, headers, keepHeadersOnError } =
Object.assign({}, defaults, strapi.config.get('middleware.settings.cors'));
strapi.app.use(
cors({
origin: async function (ctx) {
let originList;
if (typeof origin === 'function') {
originList = await origin(ctx);
} else {
originList = origin;
}
const whitelist = Array.isArray(originList) ? originList : originList.split(/\s*,\s*/);
const requestOrigin = ctx.accept.headers.origin;
if (whitelist.includes('*')) {
return '*';
}
if (requestOrigin && !whitelist.includes(requestOrigin)) {
return ctx.throw(`${requestOrigin} is not a valid origin`);
}
return requestOrigin;
},
exposeHeaders: expose,
maxAge,
credentials,
allowMethods: methods,
allowHeaders: headers,
keepHeadersOnError,
})
);
},
};
};