npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

func-middleware

v3.2.0

Published

A custom middleware and interceptor for your functions. performs an action before the main function or after.

Downloads

85

Readme

func-middleware

A custom middleware and interceptor for your functions. performs an action before the main function or after.

Install

npm i func-middleware

API

- middleware(func, action)

- interceptor(func, action)

- capsule(func, [middlewareAction, interceptorAction])

Example

Pre-execution:

import { middleware } from 'func-middleware'

const action = () => {
  console.log('Hello, world!')
}

const sum = middleware((num1: number, num2: number) => {
  return num1 + num2
}, action)

const result = sum(2, 2)
console.log(result)

Output:

Hello, wordl!
4

Execution blocking (return false or block result):

import { middleware } from 'func-middleware'

const action = () => {
  if (/** blocking conditional */) {
    return false // false or any other value
  }
}

const sum = middleware((num1: number, num2: number) => {
  return num1 + num2
}, action)

const result = sum(2, 2)
console.log(result)

Output:

undefined

Parameter interception:

import { middleware } from 'func-middleware'
import { User } from './userEntity.ts'

const createUserDTO = (data: User) => {
  if (data.age < 18) {
    throw new Error('Users must be over 18 years old')
  }
}

const createUser = middleware((data: User) => {
  return new User(data)
}, createUserDTO)

const johnDoe = createUser({ uname: 'John Doe', age: 17 }) // Error: Users must be over 18 years old

const jones = createUser({ uname: 'jones', age: 21 }) // Success
console.log(jones)

Output:

User { uname: 'jones', age: 21 }

Parameter validation: (return the new parameter array)

Obs: The new parameter array must be compatible with the function parameters, so if there are two parameters you must return both parameters in the array, even if not modified.

import { middleware } from 'func-middleware'

const toLowerCase = (name1: string, name2: string) => {
  return [name1.toLowerCase(), name2]
}

const printNames = middleware((name1: string, name2: string) => {
  console.log(name1)
  console.log(name2)
}, toLowerCase)

printNames('JOHN', 'JONES')

Output:

john
JONES

Promise

The action can be a promise, in which case the return type of the main function will be changed to a promise.

Interceptor

The interceptor allows you to modify or interact with the result of a function after it has been executed. Receiving as parameters the result of the function and its parameters.

Example

Case 1:

import { interceptor } from 'func-middleware'

const action = (result: number) => {
  return result * 2
}

const sum = interceptor((num1: number, num2: number) => {
  return num1 + num2
}, action)

const result = sum(10, 10)
console.log(result)

Output:

40

Case 2:

import { interceptor } from 'func-middleware'

const action = (result: number) => {
  if (result > 10) {
    throw new Error('Result should be lt 10')
  }
}

const sum = interceptor((num1: number, num2: number) => {
  return num1 + num2
}, action)

const result = sum(10, 10) // Error: Result should be lt 10
console.log(result)

Case 3:

import { interceptor } from 'func-middleware'

const test = (result: number, num1: number, num2: number) => {
  if (result - (num1 + num2) === 0) {
    console.log('Success')
  }
  console.log('Error')
}

const sum = interceptor((num1: number, num2: number) => {
  return num1 + num2
}, test)

const result = sum(10, 10)
console.log(result)

Output:

Success
20

Capsule

The capsule is the combination of middleware and interceptor.

Example

Case 1:

import { capsule } from 'func-middleware'

const sum = (num1: number, num2: number) => {
  return num1 + num2
}

const sumCapsule = capsule(sum, [
  (num1, num2) => {
    // Middleware action
    console.log(num1, num2)
  },
  (res, num1, num2) => {
    // Interceptor action
    console.log(res, num1, num2)
  },
])

sumCapule(2, 2)

Output:

2 2
4 2 2