{"id":22485528,"url":"https://github.com/arkerone/hookify-object","last_synced_at":"2025-08-02T18:32:46.777Z","repository":{"id":55657240,"uuid":"321114607","full_name":"arkerone/hookify-object","owner":"arkerone","description":"Wrap an object with ES6 proxy to add hooks capabilities","archived":false,"fork":false,"pushed_at":"2020-12-14T21:02:05.000Z","size":30,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-01T02:39:04.891Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/arkerone.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-13T16:57:23.000Z","updated_at":"2022-07-22T09:59:23.000Z","dependencies_parsed_at":"2022-08-15T05:41:00.446Z","dependency_job_id":null,"html_url":"https://github.com/arkerone/hookify-object","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkerone%2Fhookify-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkerone%2Fhookify-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkerone%2Fhookify-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkerone%2Fhookify-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arkerone","download_url":"https://codeload.github.com/arkerone/hookify-object/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228499980,"owners_count":17929990,"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":[],"created_at":"2024-12-06T17:12:48.147Z","updated_at":"2024-12-06T17:14:37.648Z","avatar_url":"https://github.com/arkerone.png","language":"JavaScript","readme":"# hookify-object\n\nWrap an object with ES6 proxy to add hooks capabilities through an event emitter.\n\n[![npm version](https://badge.fury.io/js/hookify-object.svg)](https://badge.fury.io/js/hookify-object)  [![codecov](https://codecov.io/gh/arkerone/hookify-object/branch/main/graph/badge.svg?token=oKiP9Xm6yv)](https://codecov.io/gh/arkerone/hookify-object)\n\n## Installation\n\n$ npm install --save hookify-object\n\n## Usage\n\n`Hookify-object` provides severals hooks for an object :\n\n- Before/after call a method;\n- After resolve/reject a promise;\n- Before/after set a object property;\n- Before/after delete a property.\n\n### Example\n\nHere is a basic usage to get the execution time of the object's methods :\n\n    const hookify = require('hookify-object')  \n      \n    const obj = {  \n      process () {  \n        /* ... */  \n      },  \n    }  \n      \n    const objWithHooks = hookify(obj)  \n      \n    objWithHooks.hooks.on('beforeCall', (context) =\u003e {  \n      const { name } = context  \n      console.time(name)  \n    })  \n      \n    objWithHooks.hooks.on('afterCall', (context) =\u003e {  \n      const { name } = context  \n      console.timeEnd(name)  \n    })  \n      \n    objWithHooks.process() \n\n## API\n\n### hookify(target)\n\nAdd hook capabilities to a target object.\n\n#### Parameters\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `target`    |   `object`    |       `-`       | `The object on which we want to add the hook capabilities` |  \n\n#### Return value\n\n| Type | Description |  \n|:---------------:|:---------------:|  \n| `Proxy` | `The hook wrapper of the target object` |  \n\n#### Example\n\n    const hookify = require('hookify-object')   \n          \n    const obj = {}    \n            \n    const objWithHooks = hookify(obj)  \n\n### proxy.hooks.on(hookName, handler)\n\nAttach a handler for the hook named `hookName`.\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `hookName`    |   `string`    |       `-`       | `The hook's name` |  \n|    `handler`    |   `Function`    |       `-`       | `The handler function for the specified hook (see bellow)` |  \n\n#### handler (context)\n\nCheck each hook to know the structure of the `context` object.\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `context`    |   `object`    |       `-`       | `Contains the context of the hook` |  \n\n### Hooks list\n\n#### beforeCall[:methodName]\n\nCalled before calling a method. You can specify a unique method via the `methodName` option.\n\n#### Example\n\n    objWithHooks.hooks.on('beforeCall', (context) =\u003e {  \n      /* Call before the call of any method */  \n    })  \n      \n    objWithHooks.hooks.on('beforeCall:test', (context) =\u003e {  \n      /* Call before the call of the \"test\" method */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the called method` |  \n|    `params`    |   `Array`    |       `-`       | `The parameters of the called method` |  \n\n#### afterCall[:methodName]\n\nCalled after calling a method. You can specify a unique method via the `methodName` option.\n\n#### Example\n\n    objWithHooks.hooks.on('afterCall', (context) =\u003e {  \n      /* Call after the call of any method */  \n    })  \n      \n    objWithHooks.hooks.on('afterCall:test', (context) =\u003e {  \n      /* Call after the call of the \"test\" method */  \n    }) \n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the called method` |  \n|    `params`    |   `Array`    |       `-`       | `The parameters of the called method` |  \n|    `result`    |   `*`    |       `-`       | `The returned value of the called method` |  \n\n#### afterResolve[:methodName]\n\nCalled when the promise returned by the method has resolved. You can specify a unique method via the `methodName`\noption.\n\n#### Example\n\n    objWithHooks.hooks.on('afterResolve', (context) =\u003e {  \n      /* Call after resolve the promise of any method */  \n    })  \n      \n    objWithHooks.hooks.on('afterResolve:testAsync', (context) =\u003e {  \n      /* Call after resolve the promise of the \"testAsync\" method */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the called method` |  \n|    `params`    |   `Array`    |       `-`       | `The parameters of the called method` |  \n|    `result`    |   `*`    |       `-`       | `The returned value of the promise` |  \n\n#### afterReject[:methodName]\n\nCalled when the promise returned by the method has rejected. You can specify a unique method via the `methodName`\noption.\n\n#### Example\n\n    objWithHooks.hooks.on('afterReject', (context) =\u003e {  \n      /* Call after reject the promise of any method */  \n    })  \n      \n    objWithHooks.hooks.on('afterReject:testAsync', (context) =\u003e {  \n      /* Call after reject the promise of the \"testAsync\" method */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the called method` |  \n|    `params`    |   `Array`    |       `-`       | `The parameters of the called method` |  \n|    `errors`    |   `Array`    |       `-`       | `The returned errors of the promise` |  \n\n#### beforeSet[:propertyName]\n\nCalled before setting a property value. You can specify a unique property via the `propertyName` option.\n\n#### Example\n\n    objWithHooks.hooks.on('beforeSet', (context) =\u003e {  \n      /* Call before set any property */  \n    })  \n      \n    objWithHooks.hooks.on('beforeSet:value', (context) =\u003e {  \n      /* Call before set the property \"value\" */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the property` |  \n|    `value`    |   `*`    |       `-`       | `The new value of the property to set` |  \n\n#### afterSet:[propertyName]\n\nCalled after setting a property value. You can specify a unique property via the `propertyName` option.\n\n#### Example\n\n    objWithHooks.hooks.on('afterSet', (context) =\u003e {  \n      /* Call after set any property */  \n    })  \n      \n    objWithHooks.hooks.on('afterSet:value', (context) =\u003e {  \n      /* Call after set the property \"value\" */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the property` |  \n|    `value`    |   `*`    |       `-`       | `The new value of the property to set` |  \n\n#### beforeDelete[:propertyName]\n\nCalled before deleting a property via the `delete` instruction. You can specify a unique property via the `propertyName`\noption.\n\n#### Example\n\n    objWithHooks.hooks.on('beforeDelete', (context) =\u003e {  \n      /* Call before delete any property */  \n    })  \n      \n    objWithHooks.hooks.on('beforeDelete:value', (context) =\u003e {  \n      /* Call before delete the property \"value\" */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the property` |  \n\n#### afterDelete[:propertyName]\n\nCalled after deleting a property via the `delete` instruction. You can specify a unique property via the `propertyName`\noption.\n\n#### Example\n\n    objWithHooks.hooks.on('afterDelete', (context) =\u003e {  \n      /* Call after delete any property */  \n    })  \n      \n    objWithHooks.hooks.on('afterDelete:value', (context) =\u003e {  \n      /* Call after delete the property \"value\" */  \n    })\n\n#### context object\n\n|       Name        |      Type       |     Default     | Description                                     |  \n| :---------------: | :-------------: | :-------------: | :---------------------------------------------- |  \n|    `self`    |   `object`    |       `-`       | `The object wrapped by the hook proxy` |  \n|    `name`    |   `string`    |       `-`       | `The name of the property` |\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farkerone%2Fhookify-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farkerone%2Fhookify-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farkerone%2Fhookify-object/lists"}