@middy/core
Advanced tools
Comparing version 1.0.0-alpha.22 to 1.0.0-alpha.23
const middy = require('../') | ||
describe('🛵 Middy test suite', () => { | ||
test('Middleware attached with "use" must be an object', () => { | ||
test('Middleware attached with "use" must be an object or array', () => { | ||
const handler = middy(jest.fn()) | ||
expect(() => { handler.use(() => {}) }).toThrow('Middleware must be an object') | ||
expect(() => { handler.use(() => {}) }).toThrow('Middy.use() accepts an object or an array of objects') | ||
}) | ||
@@ -14,3 +14,3 @@ | ||
test('"use" can add before middlewares', () => { | ||
test('"use" can add single before middleware', () => { | ||
const before = jest.fn() | ||
@@ -23,3 +23,3 @@ const middleware = () => ({ before }) | ||
test('"use" can add after middlewares', () => { | ||
test('"use" can add single after middleware', () => { | ||
const after = jest.fn() | ||
@@ -32,3 +32,3 @@ const middleware = () => ({ after }) | ||
test('"use" can add error middlewares', () => { | ||
test('"use" can add single error middleware', () => { | ||
const onError = jest.fn() | ||
@@ -41,3 +41,3 @@ const middleware = () => ({ onError }) | ||
test('"use" can add all types of middlewares', () => { | ||
test('"use" can add single object with all types of middlewares', () => { | ||
const before = jest.fn() | ||
@@ -62,2 +62,59 @@ const after = jest.fn() | ||
test('"use" can add multiple before middleware', () => { | ||
const firstBefore = jest.fn() | ||
const firstMiddleware = () => ({ before: firstBefore }) | ||
const secondBefore = jest.fn() | ||
const secondMiddleware = () => ({ before: secondBefore }) | ||
const middlewares = [firstMiddleware(), secondMiddleware()] | ||
const handler = middy(jest.fn()) | ||
handler.use(middlewares) | ||
expect(handler.__middlewares.before[0]).toBe(firstBefore) | ||
expect(handler.__middlewares.before[1]).toBe(secondBefore) | ||
}) | ||
test('"use" can add multiple after middleware', () => { | ||
const firstAfter = jest.fn() | ||
const firstMiddleware = () => ({ after: firstAfter }) | ||
const secondAfter = jest.fn() | ||
const secondMiddleware = () => ({ after: secondAfter }) | ||
const middlewares = [firstMiddleware(), secondMiddleware()] | ||
const handler = middy(jest.fn()) | ||
handler.use(middlewares) | ||
// After middleware is in reverse order of being added | ||
expect(handler.__middlewares.after[1]).toBe(firstAfter) | ||
expect(handler.__middlewares.after[0]).toBe(secondAfter) | ||
}) | ||
test('"use" can add multiple error middleware', () => { | ||
const firstError = jest.fn() | ||
const firstMiddleware = () => ({ onError: firstError }) | ||
const secondError = jest.fn() | ||
const secondMiddleware = () => ({ onError: secondError }) | ||
const middlewares = [firstMiddleware(), secondMiddleware()] | ||
const handler = middy(jest.fn()) | ||
handler.use(middlewares) | ||
expect(handler.__middlewares.onError[0]).toBe(firstError) | ||
expect(handler.__middlewares.onError[1]).toBe(secondError) | ||
}) | ||
test('"use" can add single object with all types of middlewares', () => { | ||
const before = jest.fn() | ||
const after = jest.fn() | ||
const onError = jest.fn() | ||
const middleware = () => ({ | ||
before, | ||
after, | ||
onError | ||
}) | ||
const handler = middy(jest.fn()) | ||
handler.use(middleware()) | ||
expect(handler.__middlewares.before[0]).toBe(before) | ||
expect(handler.__middlewares.after[0]).toBe(after) | ||
expect(handler.__middlewares.onError[0]).toBe(onError) | ||
}) | ||
test('"before" should add a before middleware', () => { | ||
@@ -64,0 +121,0 @@ const beforeMiddleware = jest.fn() |
@@ -1,23 +0,28 @@ | ||
import { Callback, Context, Handler, ProxyResult } from 'aws-lambda'; | ||
import { Callback, Context, Handler, ProxyResult } from 'aws-lambda' | ||
type AsyncHandler = (event: any, context: Context) => Promise<ProxyResult | object>; | ||
type AsyncHandler = ( | ||
event: any, | ||
context: Context | ||
) => Promise<ProxyResult | object> | ||
declare var middy: { | ||
(handler: Handler | AsyncHandler): middy.IMiddy; | ||
}; | ||
(handler: Handler | AsyncHandler): middy.IMiddy | ||
} | ||
declare namespace middy { | ||
interface IMiddy extends Handler { | ||
use: IMiddyUseFunction; | ||
before: IMiddyMiddlewareFunction; | ||
after: IMiddyMiddlewareFunction; | ||
onError: IMiddyMiddlewareFunction; | ||
use: IMiddyUseFunction | ||
before: IMiddyMiddlewareFunction | ||
after: IMiddyMiddlewareFunction | ||
onError: IMiddyMiddlewareFunction | ||
} | ||
type IMiddyUseFunction = (config?: object) => IMiddy; | ||
type IMiddyUseFunction = ( | ||
middlewares?: IMiddyMiddlewareObject | IMiddyMiddlewareObject[] | ||
) => IMiddy | ||
interface IMiddyMiddlewareObject { | ||
before?: IMiddyMiddlewareFunction; | ||
after?: IMiddyMiddlewareFunction; | ||
onError?: IMiddyMiddlewareFunction; | ||
before?: IMiddyMiddlewareFunction | ||
after?: IMiddyMiddlewareFunction | ||
onError?: IMiddyMiddlewareFunction | ||
} | ||
@@ -28,12 +33,12 @@ | ||
next: IMiddyNextFunction | ||
) => void | Promise<any>; | ||
) => void | Promise<any> | ||
type IMiddyNextFunction = (error?: any) => void; | ||
type IMiddyNextFunction = (error?: any) => void | ||
interface IHandlerLambda<T = any, V = object> { | ||
event: T; | ||
context: Context; | ||
response: V; | ||
error: Error; | ||
callback: Callback; | ||
event: T | ||
context: Context | ||
response: V | ||
error: Error | ||
callback: Callback | ||
} | ||
@@ -40,0 +45,0 @@ } |
23
index.js
@@ -10,3 +10,4 @@ const isPromise = require('./isPromise') | ||
* @param {function} callback - the AWS Lambda callback from the original handler | ||
* @property {useFunction} use - attach a new middleware | ||
* @property {useFunction} use - attach one or more new middlewares | ||
* @property {applyMiddlewareFunction} applyMiddleware - attach a new middleware | ||
* @property {middlewareAttachFunction} before - attach a new *before-only* middleware | ||
@@ -23,2 +24,9 @@ * @property {middlewareAttachFunction} after - attach a new *after-only* middleware | ||
* @type {function} | ||
* @param {middlewareObject|middlewareObject[]} - the middleware object or array of middleware objects to attach | ||
* @return {middy} | ||
*/ | ||
/** | ||
* @typedef applyMiddlewareFunction | ||
* @type {function} | ||
* @param {middlewareObject} - the middleware object to attach | ||
@@ -200,3 +208,14 @@ * @return {middy} | ||
instance.use = (middleware) => { | ||
instance.use = (input) => { | ||
if (Array.isArray(input)) { | ||
input.forEach(middleware => instance.applyMiddleware(middleware)) | ||
return instance | ||
} else if (typeof input === 'object') { | ||
return instance.applyMiddleware(input) | ||
} else { | ||
throw new Error('Middy.use() accepts an object or an array of objects') | ||
} | ||
} | ||
instance.applyMiddleware = (middleware) => { | ||
if (typeof middleware !== 'object') { | ||
@@ -203,0 +222,0 @@ throw new Error('Middleware must be an object') |
{ | ||
"name": "@middy/core", | ||
"version": "1.0.0-alpha.22", | ||
"version": "1.0.0-alpha.23", | ||
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)", | ||
@@ -5,0 +5,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33203
10
871