{"id":22222120,"url":"https://github.com/fabiospampinato/call-hooks","last_synced_at":"2025-07-05T20:33:06.996Z","repository":{"id":57192961,"uuid":"188293961","full_name":"fabiospampinato/call-hooks","owner":"fabiospampinato","description":"Function for adding before/after/call/arguments/result hooks to another function.","archived":false,"fork":false,"pushed_at":"2024-04-04T22:12:31.000Z","size":13,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-01T12:39:10.293Z","etag":null,"topics":["call","function","hooks","intercept","override"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/fabiospampinato.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},"funding":{"github":"fabiospampinato","custom":"https://www.paypal.me/fabiospampinato"}},"created_at":"2019-05-23T19:26:25.000Z","updated_at":"2023-12-11T16:11:41.000Z","dependencies_parsed_at":"2024-04-04T23:25:08.681Z","dependency_job_id":null,"html_url":"https://github.com/fabiospampinato/call-hooks","commit_stats":{"total_commits":13,"total_committers":2,"mean_commits":6.5,"dds":0.07692307692307687,"last_synced_commit":"ef36a3b76bbdfe3a42dad7c9054513912c97c39f"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcall-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcall-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcall-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcall-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabiospampinato","download_url":"https://codeload.github.com/fabiospampinato/call-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227817017,"owners_count":17824200,"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":["call","function","hooks","intercept","override"],"created_at":"2024-12-02T23:16:59.583Z","updated_at":"2025-07-05T20:33:06.986Z","avatar_url":"https://github.com/fabiospampinato.png","language":"JavaScript","funding_links":["https://github.com/sponsors/fabiospampinato","https://www.paypal.me/fabiospampinato"],"categories":[],"sub_categories":[],"readme":"# Call Hooks\n\nFunction for adding before/after/call/args/result hooks to another function.\n\nIt supports both regular and async functions, even if they may throw.\n\n## Install\n\n```sh\nnpm install call-hooks\n```\n\n## Usage\n\nThis function has the following interface:\n\n```ts\nfunction callHooks ( fn: Function, hooks: Hooks ): Function;\n```\n\nThese are the supported hooks:\n\n```ts\n{\n  args: ( args: any[] ) =\u003e any[],\n  before: ( args: any[] ) =\u003e void,\n  call: ( args: any[] ) =\u003e Result,\n  result: ( args: any[], result: any ) =\u003e any,\n  after: ( args: any[], result: any | Error ) =\u003e void\n}\n```\n\n## Hooks\n\nHooks are called in the following order:\n\n##### Args\n\nThe `args` hook is the first one called, its returned value will be the actual arguments object used:\n\n```ts\nimport hooks from 'call-hooks';\nimport fn from './my_fn';\n\nconst fnWithHooks = hooks ( fn, {\n  args ( args ) {\n    if ( args.length === 1 ) return args; // Not overriding the arguments\n    return [...args, true]; // Overriding the arguments\n  }\n});\n```\n\n##### Before\n\nThe `before` hook is called before the wrapped function will be executed:\n\n```ts\nimport hooks from 'call-hooks';\nimport fn from './my_fn';\n\nconst fnWithHooks = hooks ( fn, {\n  before ( args ) {\n    console.log ( 'Called with arguments:', args );\n  }\n});\n```\n\n##### Call\n\nThe `call` hook is called instead of the wrapped function:\n\n```ts\nimport hooks from 'call-hooks';\nimport fn from './my_fn';\n\nconst fnWithHooks = hooks ( fn, {\n  call ( args ) {\n    if ( args.length === 0 ) return null; // Overriding the function\n    return fn.apply ( this, args ); // Not overriding the function\n  }\n});\n```\n\n##### Result\n\nThe `result` hook is called after the wrapped function has been executed, its returned value will be the actual return value:\n\n```ts\nimport hooks from 'call-hooks';\nimport fn from './my_fn';\n\nconst fnWithHooks = hooks ( fn, {\n  result ( args, result ) {\n    if ( isNaN ( result ) ) return 0; // Overriding the returned value\n    return result; // Not overriding the returned value\n  }\n});\n```\n\n##### After\n\nThe `after` hook is the last one called:\n\n```ts\nimport hooks from 'call-hooks';\nimport fn from './my_fn';\n\nconst fnWithHooks = hooks ( fn, {\n  after ( args, result ) {\n    console.log ( 'Returned result:', result );\n  }\n});\n```\n\n## License\n\nMIT © Fabio Spampinato\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiospampinato%2Fcall-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabiospampinato%2Fcall-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiospampinato%2Fcall-hooks/lists"}