Pass strapi in factory args

This commit is contained in:
Alexandre Bodin 2021-09-28 12:05:14 +02:00
parent f8695e615e
commit d953d3970e
15 changed files with 58 additions and 24 deletions

View File

@ -2,4 +2,7 @@
const compress = require('koa-compress');
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (options = {}) => compress(options);

View File

@ -11,6 +11,9 @@ const defaults = {
keepHeadersOnError: false,
};
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = options => {
const {
origin,

View File

@ -1,10 +1,5 @@
'use strict';
/**
* Boom hook
*/
// Public node modules.
const _ = require('lodash');
const Boom = require('@hapi/boom');
const delegate = require('delegates');
@ -58,7 +53,11 @@ const formatBoomPayload = boomError => {
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');
boomMethods.forEach(method => {
@ -102,9 +101,11 @@ const createResponseUtils = () => {
.method('deleted');
};
// TODO: inject strapi
module.exports = () => {
createResponseUtils();
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (_, { strapi }) => {
createResponseUtils(strapi);
strapi.errors = Boom;
return async (ctx, next) => {

View File

@ -9,8 +9,10 @@ const defaults = {
maxAge: 86400000,
};
// TODO: inject strapi
module.exports = options => {
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (options, { strapi }) => {
const { maxAge, path: faviconPath } = defaultsDeep(defaults, options);
return favicon(resolve(strapi.dirs.root, faviconPath), { maxAge });

View File

@ -0,0 +1,4 @@
import { Strapi } from '../';
import { Middleware } from 'koa';
export type MiddlewareFactory = (options: any, ctx: { strapi: Strapi }) => Middleware;

View File

@ -11,6 +11,7 @@ const request = require('./request');
const responseTime = require('./response-time');
const responses = require('./responses');
const security = require('./security');
// TODO: add back ?
// session: require('./session'),
const publicStatic = require('./public');

View File

@ -2,4 +2,7 @@
const ip = require('koa-ip');
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = options => ip(options);

View File

@ -13,8 +13,10 @@ const codeToColor = code => {
: code;
};
// TODO: inject strapi
module.exports = () => {
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (_, { strapi }) => {
return async (ctx, next) => {
const start = Date.now();
await next();

View File

@ -4,6 +4,9 @@ const defaults = {
poweredBy: 'Strapi <strapi.io>',
};
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = options => {
const { poweredBy } = Object.assign({}, defaults, options);

View File

@ -15,8 +15,10 @@ const defaults = {
defaultIndex: true,
};
// TODO: inject strap
module.exports = options => {
/**
* @type {import('../').MiddlewareFactory}
*/
module.exports = (options, { strapi }) => {
const { defaultIndex, maxAge, path: publicPath } = defaultsDeep(defaults, options);
const staticDir = path.resolve(strapi.dirs.root, publicPath || strapi.config.paths.static);

View File

@ -40,14 +40,15 @@ const addQsParser = (app, settings) => {
return app;
};
// TODO: inject strapi
module.exports = options => {
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (options, { strapi }) => {
const { queryStringParser, ...bodyOptions } = defaultsDeep(defaults, options);
addQsParser(strapi.server.app, queryStringParser);
return async (ctx, next) => {
// disable for graphql
// TODO: find a better way later
if (ctx.url === '/graphql') {
return next();

View File

@ -1,5 +1,8 @@
'use strict';
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = () => {
return async (ctx, next) => {
const start = Date.now();

View File

@ -2,6 +2,9 @@
const { prop, isFunction } = require('lodash/fp');
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (options = {}) => {
return async (ctx, next) => {
await next();

View File

@ -19,4 +19,7 @@ const defaults = {
},
};
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = options => helmet(defaultsDeep(defaults, options));

View File

@ -3,9 +3,9 @@
const { yup } = require('@strapi/utils');
/**
* @type {import('../../').Strapi} Strapi
* @type {Array<string|{name?: string, resolve?: string, config: any}>} MiddlewaresConfig
* @type {Array<{name: string, hanlder: Function}>} Middlewares
* @typedef {import('../../').Strapi} Strapi
* @typedef {Array<string|{name?: string, resolve?: string, config: any}>} MiddlewaresConfig
* @typedef {Array<{name: string, hanlder: Function}>} Middlewares
*/
const defaultConfig = [
@ -120,7 +120,7 @@ const initMiddlewares = async (config, strapi) => {
middlewares.push({
name: item,
handler: await middlewareFactory(),
handler: await middlewareFactory(null, { strapi }),
});
continue;
@ -133,7 +133,7 @@ const initMiddlewares = async (config, strapi) => {
const middlewareFactory = strapi.middleware(name);
middlewares.push({
name,
handler: await middlewareFactory(config),
handler: await middlewareFactory(config, { strapi }),
});
continue;
@ -142,7 +142,7 @@ const initMiddlewares = async (config, strapi) => {
if (resolve) {
middlewares.push({
name: resolve,
handler: await require(resolve)(config),
handler: await require(resolve)(config, { strapi }),
});
continue;