mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 16:22:10 +00:00
Pass strapi in factory args
This commit is contained in:
parent
f8695e615e
commit
d953d3970e
@ -2,4 +2,7 @@
|
|||||||
|
|
||||||
const compress = require('koa-compress');
|
const compress = require('koa-compress');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = (options = {}) => compress(options);
|
module.exports = (options = {}) => compress(options);
|
||||||
|
|||||||
@ -11,6 +11,9 @@ const defaults = {
|
|||||||
keepHeadersOnError: false,
|
keepHeadersOnError: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = options => {
|
module.exports = options => {
|
||||||
const {
|
const {
|
||||||
origin,
|
origin,
|
||||||
|
|||||||
@ -1,10 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/**
|
|
||||||
* Boom hook
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Public node modules.
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const Boom = require('@hapi/boom');
|
const Boom = require('@hapi/boom');
|
||||||
const delegate = require('delegates');
|
const delegate = require('delegates');
|
||||||
@ -58,7 +53,11 @@ const formatBoomPayload = boomError => {
|
|||||||
return { status: output.statusCode, body: output.payload };
|
return { status: output.statusCode, body: output.payload };
|
||||||
};
|
};
|
||||||
|
|
||||||
const createResponseUtils = () => {
|
/**
|
||||||
|
* Create short responses ctx.(send|created|deleted)
|
||||||
|
* @param {Strapi} strapi
|
||||||
|
*/
|
||||||
|
const createResponseUtils = strapi => {
|
||||||
const delegator = delegate(strapi.server.app.context, 'response');
|
const delegator = delegate(strapi.server.app.context, 'response');
|
||||||
|
|
||||||
boomMethods.forEach(method => {
|
boomMethods.forEach(method => {
|
||||||
@ -102,9 +101,11 @@ const createResponseUtils = () => {
|
|||||||
.method('deleted');
|
.method('deleted');
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: inject strapi
|
/**
|
||||||
module.exports = () => {
|
* @type {import('./').MiddlewareFactory}
|
||||||
createResponseUtils();
|
*/
|
||||||
|
module.exports = (_, { strapi }) => {
|
||||||
|
createResponseUtils(strapi);
|
||||||
strapi.errors = Boom;
|
strapi.errors = Boom;
|
||||||
|
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
|
|||||||
@ -9,8 +9,10 @@ const defaults = {
|
|||||||
maxAge: 86400000,
|
maxAge: 86400000,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: inject strapi
|
/**
|
||||||
module.exports = options => {
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
|
module.exports = (options, { strapi }) => {
|
||||||
const { maxAge, path: faviconPath } = defaultsDeep(defaults, options);
|
const { maxAge, path: faviconPath } = defaultsDeep(defaults, options);
|
||||||
|
|
||||||
return favicon(resolve(strapi.dirs.root, faviconPath), { maxAge });
|
return favicon(resolve(strapi.dirs.root, faviconPath), { maxAge });
|
||||||
|
|||||||
4
packages/core/strapi/lib/middlewares/index.d.ts
vendored
Normal file
4
packages/core/strapi/lib/middlewares/index.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import { Strapi } from '../';
|
||||||
|
import { Middleware } from 'koa';
|
||||||
|
|
||||||
|
export type MiddlewareFactory = (options: any, ctx: { strapi: Strapi }) => Middleware;
|
||||||
@ -11,6 +11,7 @@ const request = require('./request');
|
|||||||
const responseTime = require('./response-time');
|
const responseTime = require('./response-time');
|
||||||
const responses = require('./responses');
|
const responses = require('./responses');
|
||||||
const security = require('./security');
|
const security = require('./security');
|
||||||
|
// TODO: add back ?
|
||||||
// session: require('./session'),
|
// session: require('./session'),
|
||||||
const publicStatic = require('./public');
|
const publicStatic = require('./public');
|
||||||
|
|
||||||
|
|||||||
@ -2,4 +2,7 @@
|
|||||||
|
|
||||||
const ip = require('koa-ip');
|
const ip = require('koa-ip');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = options => ip(options);
|
module.exports = options => ip(options);
|
||||||
|
|||||||
@ -13,8 +13,10 @@ const codeToColor = code => {
|
|||||||
: code;
|
: code;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: inject strapi
|
/**
|
||||||
module.exports = () => {
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
|
module.exports = (_, { strapi }) => {
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
await next();
|
await next();
|
||||||
|
|||||||
@ -4,6 +4,9 @@ const defaults = {
|
|||||||
poweredBy: 'Strapi <strapi.io>',
|
poweredBy: 'Strapi <strapi.io>',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = options => {
|
module.exports = options => {
|
||||||
const { poweredBy } = Object.assign({}, defaults, options);
|
const { poweredBy } = Object.assign({}, defaults, options);
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,10 @@ const defaults = {
|
|||||||
defaultIndex: true,
|
defaultIndex: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: inject strap
|
/**
|
||||||
module.exports = options => {
|
* @type {import('../').MiddlewareFactory}
|
||||||
|
*/
|
||||||
|
module.exports = (options, { strapi }) => {
|
||||||
const { defaultIndex, maxAge, path: publicPath } = defaultsDeep(defaults, options);
|
const { defaultIndex, maxAge, path: publicPath } = defaultsDeep(defaults, options);
|
||||||
|
|
||||||
const staticDir = path.resolve(strapi.dirs.root, publicPath || strapi.config.paths.static);
|
const staticDir = path.resolve(strapi.dirs.root, publicPath || strapi.config.paths.static);
|
||||||
|
|||||||
@ -40,14 +40,15 @@ const addQsParser = (app, settings) => {
|
|||||||
return app;
|
return app;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: inject strapi
|
/**
|
||||||
module.exports = options => {
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
|
module.exports = (options, { strapi }) => {
|
||||||
const { queryStringParser, ...bodyOptions } = defaultsDeep(defaults, options);
|
const { queryStringParser, ...bodyOptions } = defaultsDeep(defaults, options);
|
||||||
|
|
||||||
addQsParser(strapi.server.app, queryStringParser);
|
addQsParser(strapi.server.app, queryStringParser);
|
||||||
|
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
// disable for graphql
|
|
||||||
// TODO: find a better way later
|
// TODO: find a better way later
|
||||||
if (ctx.url === '/graphql') {
|
if (ctx.url === '/graphql') {
|
||||||
return next();
|
return next();
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
const { prop, isFunction } = require('lodash/fp');
|
const { prop, isFunction } = require('lodash/fp');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = (options = {}) => {
|
module.exports = (options = {}) => {
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
await next();
|
await next();
|
||||||
|
|||||||
@ -19,4 +19,7 @@ const defaults = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {import('./').MiddlewareFactory}
|
||||||
|
*/
|
||||||
module.exports = options => helmet(defaultsDeep(defaults, options));
|
module.exports = options => helmet(defaultsDeep(defaults, options));
|
||||||
|
|||||||
@ -3,9 +3,9 @@
|
|||||||
const { yup } = require('@strapi/utils');
|
const { yup } = require('@strapi/utils');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {import('../../').Strapi} Strapi
|
* @typedef {import('../../').Strapi} Strapi
|
||||||
* @type {Array<string|{name?: string, resolve?: string, config: any}>} MiddlewaresConfig
|
* @typedef {Array<string|{name?: string, resolve?: string, config: any}>} MiddlewaresConfig
|
||||||
* @type {Array<{name: string, hanlder: Function}>} Middlewares
|
* @typedef {Array<{name: string, hanlder: Function}>} Middlewares
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const defaultConfig = [
|
const defaultConfig = [
|
||||||
@ -120,7 +120,7 @@ const initMiddlewares = async (config, strapi) => {
|
|||||||
|
|
||||||
middlewares.push({
|
middlewares.push({
|
||||||
name: item,
|
name: item,
|
||||||
handler: await middlewareFactory(),
|
handler: await middlewareFactory(null, { strapi }),
|
||||||
});
|
});
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@ -133,7 +133,7 @@ const initMiddlewares = async (config, strapi) => {
|
|||||||
const middlewareFactory = strapi.middleware(name);
|
const middlewareFactory = strapi.middleware(name);
|
||||||
middlewares.push({
|
middlewares.push({
|
||||||
name,
|
name,
|
||||||
handler: await middlewareFactory(config),
|
handler: await middlewareFactory(config, { strapi }),
|
||||||
});
|
});
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@ -142,7 +142,7 @@ const initMiddlewares = async (config, strapi) => {
|
|||||||
if (resolve) {
|
if (resolve) {
|
||||||
middlewares.push({
|
middlewares.push({
|
||||||
name: resolve,
|
name: resolve,
|
||||||
handler: await require(resolve)(config),
|
handler: await require(resolve)(config, { strapi }),
|
||||||
});
|
});
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user