{"id":26506737,"url":"https://github.com/rizkyarifnur/ts-hooks","last_synced_at":"2026-05-08T18:36:05.589Z","repository":{"id":57380782,"uuid":"182801854","full_name":"RizkyArifNur/ts-hooks","owner":"RizkyArifNur","description":"Hooks as you want","archived":false,"fork":false,"pushed_at":"2019-05-09T03:21:33.000Z","size":48,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-18T15:11:55.101Z","etag":null,"topics":["decorators","hooks","tyepscript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RizkyArifNur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-22T14:13:17.000Z","updated_at":"2019-05-09T03:17:23.000Z","dependencies_parsed_at":"2022-09-26T16:41:17.526Z","dependency_job_id":null,"html_url":"https://github.com/RizkyArifNur/ts-hooks","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RizkyArifNur%2Fts-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RizkyArifNur%2Fts-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RizkyArifNur%2Fts-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RizkyArifNur%2Fts-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RizkyArifNur","download_url":"https://codeload.github.com/RizkyArifNur/ts-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244706526,"owners_count":20496571,"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":["decorators","hooks","tyepscript"],"created_at":"2025-03-20T22:56:13.374Z","updated_at":"2026-05-08T18:36:05.536Z","avatar_url":"https://github.com/RizkyArifNur.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-hooks\n\nHooks as you want\n\n\u003e Documentation for TS-Hooks\n\nCreate your own hooks like add validation before some function called, or trigger some stuff when the function called\nin every code as you want\n\n## Installation\n\n### NPM\n\n`npm install ts-hooks`\n\n### Yarn\n\n`yarn add ts-hooks`\n\n## Features\n\n- add hooks before function called\n- add hooks after function called\n- support multiple hooks\n- support for async function\n\n## Basic Concept\n\nIdea behind **ts-hooks** is to wrap your function into wrapper function, are you confuse ? yeah me too :D ,\ndon't worry, let's see the example below\n\n**Your function before wrapped**\n\n```typescript\nfunction iDontKnowTheName() {\n  console.log(\"Hey i don't what should i do!\")\n}\n```\n\n**Your function after wrapped by ts-hooks**\n\n```typescript\nfunction functionWrapper() {\n  hooksBefore()\n  const result = iDontKnowTheName()\n  hooksAfter()\n  return result\n}\n```\n\n## Basic Usage\n\nif you're not understand with the basic concept above, don't worry just forget it,\nlet's see how to use the **ts-hooks**\n\n\u003e :warning: **Important!** ts-hooks can be used only when your functions are inside a class\n\n### add hook before your target function called\n\n```typescript\n   /**\n   * add hooks before target function called\n   * NOTE : you can pass the Async hooks to the sync function, but\n   * your hooks will not awaited\n   * the first argument is array of hooks that will be called before the original function called\n   * the second argument is array of hooks that will be called after the original function called\n   * the last argument is optional, if you set it to `true` then the hooks function will\n   * executed like middleware, otherwise the hooks will executed like common function\n   */\n  @addHooks([firstSyncHook], [], false)\n  syncFunctionsWithHookBefore() {\n    console.log(\n      'this is the target function with single hook before this function called'\n    )\n  }\n```\n\nand the **firstSyncHook** must be a function like this\n\n```typescript\nfunction firstSyncHook() {\n  console.log('this is first synchronous hooks')\n}\n```\n\n### add hook after your target function called\n\n```typescript\n  /**\n   * add hooks after target function called\n   */\n  @addHooks([], [firstSyncHook], false)\n  syncFunctionWithHookAfter() {\n    console.log(\n      'this is the target function with single hook after this function called'\n    )\n  }\n```\n\n### add multiple hooks\n\n```typescript\n  /**\n   * add multiple hooks as much as you want,\n   * the ordering are matters\n   * in this case, `firstSyncHook` will executed first and after it done\n   * the `secondSyncHook` will be executed\n   */\n  @addHooks([firstSyncHook, secondSyncHook], [], false)\n  syncFunctionWithMultipleHooks() {\n    console.log('this is the target function with multiple hooks')\n  }\n```\n\n### pass arguments to target function\n\n```typescript\n  /**\n   * you can pass params just like a common functions\n   * and your hooks will have same params like the target function\n   */\n  @addHooks([hookWithParams], [], false)\n  functionWithHooksAndParams(param1: string, param2: number) {\n    console.log(\n      `this is the target function with hooks and params :{${param1} ${param2}} `\n    )\n  }\n```\n\nand the **hookWithParam** must be a function like this\n\n```typescript\nfunction hookWithParams(param1: string, param2: number) {\n  console.log(`this is synchronous hooks with params:{ ${param1} ${param2} }`)\n}\n```\n\n### hooks that will throw an Error?\n\n```typescript\n  /**\n   * if your hooks are throwing an error, the next step will not executed\n   * because hooks are run synchronously\n   */\n  @addHooks([hookWithError], [], false)\n  functionWithErrorHook() {\n    console.log('This statement will not executed')\n  }\n```\n\n## Basic usage for async function\n\nWe can add some hooks to async function too, yeay !\n\n```typescript\n  /**\n   * you can also handle the async function with hooks,\n   * and you can also pass the sync or Async hook to the Async function\n   * it will work fine :D\n   */\n  @addAsyncHooks([firstAsyncHook], [], false)\n  async functionWithAsyncHook() {\n    console.log('Yeay we can handle async function too :D !!')\n  }\n\n  /**\n   * this function are same like the Async function above\n   */\n  @addAsyncHooks([firstAsyncHook], [], false)\n  functionWhichReturnPromise() {\n    return new Promise(resolve =\u003e {\n      setTimeout(() =\u003e resolve('Yeay we can handle this too !!'))\n    })\n  }\n}\n```\n\n## Middleware concept\n\n\u003e :warning: Warning !!! this features maybe unstable\n\nts-hooks v.0.1.0-rc3 is already support the middleware concept, you can use it like middleware in express.js (see [express middleware](https://expressjs.com/en/guide/using-middleware.html))\nthe middleware concept will provide a **next callback** to your hooks function, if in your hooks function you called the **next()** callback it will execute the next process/function depends on the process/function stack\n\n### Example usage\n\nif you set your hooks like this\n\n```typescript\n  @addHooks([hookAsMiddleware], [], true)\n  functionWithHookAsMiddleware(param1: string) {\n    console.log('this is origin function with params : ', param1)\n  }\n```\n\nthen the **hookAsMiddleware** must be a function like this\n\n```typescript\n/**\n * import next type from `ts-hooks`\n */\nimport { Next } from 'ts-hooks'\n/**\n * remember that the first param of hook in middleware concept is always\n * `next`, and the next params is depends to the target function\n *\n * @param next callback function, if you invoked this callback, then the next process will be executed\n * otherwise the next process will not be executed\n * @param param1 this param is depend to the target function\n */\nfunction hookAsMiddleware(next: Next, param1: string) {\n  console.log(\n    'this is hook function that implement middleware concept with params : ',\n    param1\n  )\n  /**\n   * you can override the params that will be retrieved in the next process/function\n   */\n  next('override params from hooks')\n}\n```\n\nand if you execute it, it should be like this\n\n```\nthis is hook function that implement middleware concept with params :  origin params\nthis is origin function with params :  override params from hooks\n```\n\n## Process stack\n\n**ts-hooks** has a process stack or sequence of processes that will be executed,\nand it will be like this\n\n```\nfirstHooksBefore  \u003c- if this error, then the next process(secondHooksBefore, originFunction, etc) will not be executed, and returned value will be undefined/null\nsecondHooksBefore\noriginFunction\nfirstHooksAfter\nsecondHooksAfter\n```\n\nif you're using the middleware concept, the process stack will be like this\n\n```\nfirstHooksBefore  \u003c- if this error or the next() callback is not called, then the next process(secondHooksBefore, originFunction, etc) will not be executed, and returned value will be undefined/null\nsecondHooksBefore\noriginFunction\nfirstHooksAfter\nsecondHooksAfter\n```\n\nfor full example you can look at the [example](https://github.com/RizkyArifNur/ts-hooks/tree/master/example) folder in this repository,\n\nhope this repository can help you guys :blush:\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frizkyarifnur%2Fts-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frizkyarifnur%2Fts-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frizkyarifnur%2Fts-hooks/lists"}