{"id":15007756,"url":"https://github.com/miguelzacca/func-middleware","last_synced_at":"2026-01-05T22:03:03.045Z","repository":{"id":253951821,"uuid":"845032297","full_name":"miguelzacca/func-middleware","owner":"miguelzacca","description":"A custom middleware and interceptor for your functions. performs an action before the main function or after.","archived":false,"fork":false,"pushed_at":"2024-09-15T03:47:59.000Z","size":3020,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-28T15:21:10.688Z","etag":null,"topics":["dto","func","func-middleware","function","interception","interceptor","middleware","npm","npm-package"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miguelzacca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-20T12:58:46.000Z","updated_at":"2024-09-15T03:48:02.000Z","dependencies_parsed_at":"2024-09-13T09:55:06.758Z","dependency_job_id":"2d961d77-7225-4fe5-aea8-711d5262d4b2","html_url":"https://github.com/miguelzacca/func-middleware","commit_stats":null,"previous_names":["miguelzacca/func-middleware"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miguelzacca%2Ffunc-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miguelzacca%2Ffunc-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miguelzacca%2Ffunc-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miguelzacca%2Ffunc-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miguelzacca","download_url":"https://codeload.github.com/miguelzacca/func-middleware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219856362,"owners_count":16556084,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dto","func","func-middleware","function","interception","interceptor","middleware","npm","npm-package"],"created_at":"2024-09-24T19:13:39.196Z","updated_at":"2025-10-30T11:32:03.111Z","avatar_url":"https://github.com/miguelzacca.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# func-middleware\n\nA custom middleware and interceptor for your functions. performs an action before the main function or after.\n\n## Install\n\n```bash\nnpm i func-middleware\n```\n\n## API\n\n#### - `middleware(func, action)`\n\n#### - `interceptor(func, action)`\n\n#### - `capsule(func, [middlewareAction, interceptorAction])`\n\n### Example\n\n**Pre-execution:**\n\n```js\nimport { middleware } from 'func-middleware'\n\nconst action = () =\u003e {\n  console.log('Hello, world!')\n}\n\nconst sum = middleware((num1: number, num2: number) =\u003e {\n  return num1 + num2\n}, action)\n\nconst result = sum(2, 2)\nconsole.log(result)\n```\n\nOutput:\n\n```txt\nHello, wordl!\n4\n```\n\n**Execution blocking (return false or block result):**\n\n```js\nimport { middleware } from 'func-middleware'\n\nconst action = () =\u003e {\n  if (/** blocking conditional */) {\n    return false // false or any other value\n  }\n}\n\nconst sum = middleware((num1: number, num2: number) =\u003e {\n  return num1 + num2\n}, action)\n\nconst result = sum(2, 2)\nconsole.log(result)\n```\n\nOutput:\n\n```txt\nundefined\n```\n\n**Parameter interception:**\n\n```js\nimport { middleware } from 'func-middleware'\nimport { User } from './userEntity.ts'\n\nconst createUserDTO = (data: User) =\u003e {\n  if (data.age \u003c 18) {\n    throw new Error('Users must be over 18 years old')\n  }\n}\n\nconst createUser = middleware((data: User) =\u003e {\n  return new User(data)\n}, createUserDTO)\n\nconst johnDoe = createUser({ uname: 'John Doe', age: 17 }) // Error: Users must be over 18 years old\n\nconst jones = createUser({ uname: 'jones', age: 21 }) // Success\nconsole.log(jones)\n```\n\nOutput:\n\n```txt\nUser { uname: 'jones', age: 21 }\n```\n\n**Parameter validation: (return the new parameter array)**\n\nObs: 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.\n\n```js\nimport { middleware } from 'func-middleware'\n\nconst toLowerCase = (name1: string, name2: string) =\u003e {\n  return [name1.toLowerCase(), name2]\n}\n\nconst printNames = middleware((name1: string, name2: string) =\u003e {\n  console.log(name1)\n  console.log(name2)\n}, toLowerCase)\n\nprintNames('JOHN', 'JONES')\n```\n\nOutput:\n\n```txt\njohn\nJONES\n```\n\n### Promise\n\nThe `action` can be a promise, in which case the return type of the main function will be changed to a promise.\n\n## Interceptor\n\nThe 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.\n\n### Example\n\n**Case 1:**\n\n```js\nimport { interceptor } from 'func-middleware'\n\nconst action = (result: number) =\u003e {\n  return result * 2\n}\n\nconst sum = interceptor((num1: number, num2: number) =\u003e {\n  return num1 + num2\n}, action)\n\nconst result = sum(10, 10)\nconsole.log(result)\n```\n\nOutput:\n\n```txt\n40\n```\n\n**Case 2:**\n\n```js\nimport { interceptor } from 'func-middleware'\n\nconst action = (result: number) =\u003e {\n  if (result \u003e 10) {\n    throw new Error('Result should be lt 10')\n  }\n}\n\nconst sum = interceptor((num1: number, num2: number) =\u003e {\n  return num1 + num2\n}, action)\n\nconst result = sum(10, 10) // Error: Result should be lt 10\nconsole.log(result)\n```\n\n**Case 3:**\n\n```js\nimport { interceptor } from 'func-middleware'\n\nconst test = (result: number, num1: number, num2: number) =\u003e {\n  if (result - (num1 + num2) === 0) {\n    console.log('Success')\n  }\n  console.log('Error')\n}\n\nconst sum = interceptor((num1: number, num2: number) =\u003e {\n  return num1 + num2\n}, test)\n\nconst result = sum(10, 10)\nconsole.log(result)\n```\n\nOutput:\n\n```txt\nSuccess\n20\n```\n\n## Capsule\n\nThe capsule is the combination of middleware and interceptor.\n\n### Example\n\n**Case 1:**\n\n```js\nimport { capsule } from 'func-middleware'\n\nconst sum = (num1: number, num2: number) =\u003e {\n  return num1 + num2\n}\n\nconst sumCapsule = capsule(sum, [\n  (num1, num2) =\u003e {\n    // Middleware action\n    console.log(num1, num2)\n  },\n  (res, num1, num2) =\u003e {\n    // Interceptor action\n    console.log(res, num1, num2)\n  },\n])\n\nsumCapule(2, 2)\n```\n\nOutput:\n\n```txt\n2 2\n4 2 2\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiguelzacca%2Ffunc-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiguelzacca%2Ffunc-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiguelzacca%2Ffunc-middleware/lists"}