{"id":16355944,"url":"https://github.com/timobechtel/krog","last_synced_at":"2025-10-26T03:31:14.839Z","repository":{"id":117356913,"uuid":"462904979","full_name":"TimoBechtel/krog","owner":"TimoBechtel","description":"Add a hooks based plugin system to your library.","archived":false,"fork":false,"pushed_at":"2023-11-13T15:39:20.000Z","size":505,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-30T03:30:40.334Z","etag":null,"topics":["extension","hook","hooks","library","plugin"],"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/TimoBechtel.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-23T21:03:43.000Z","updated_at":"2023-08-17T19:30:57.000Z","dependencies_parsed_at":"2023-11-13T16:51:47.505Z","dependency_job_id":null,"html_url":"https://github.com/TimoBechtel/krog","commit_stats":{"total_commits":15,"total_committers":2,"mean_commits":7.5,"dds":"0.33333333333333337","last_synced_commit":"44e7143d0ebbdca5cb174cc471a99539fdf0e5e8"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimoBechtel%2Fkrog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimoBechtel%2Fkrog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimoBechtel%2Fkrog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimoBechtel%2Fkrog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimoBechtel","download_url":"https://codeload.github.com/TimoBechtel/krog/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238254124,"owners_count":19441790,"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":["extension","hook","hooks","library","plugin"],"created_at":"2024-10-11T01:42:13.234Z","updated_at":"2025-10-26T03:31:14.239Z","avatar_url":"https://github.com/TimoBechtel.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e🪝 \u003cbr/\u003ekrog\u003c/h1\u003e\n\u003ch3 align=\"center\"\u003eAdd a hooks-based plugin system to your library.\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\u003ci\u003e\u003ccode\u003e/krɔːɡ/\u003c/code\u003e, danish: \"hook\"\u003c/i\u003e\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#\" target=\"_blank\"\u003e\n    \u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/badge/License-MIT-yellow.svg\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  ·\n  \u003ca href=\"https://github.com/TimoBechtel/krog/issues\"\u003eReport Bug / Request Feature\u003c/a\u003e\n  ·\n\u003c/p\u003e\n\n## Table of Contents\n\n- [About](#about)\n  - [Features](#Features)\n- [Installation](#install)\n- [Usage](#usage)\n  - [As a library author](#As-a-library-author)\n  - [As a plugin author](#As-a-plugin-author)\n- [Used by](#used-by)\n- [Development / Contributing](#Development-Contributing)\n\n## About\n\n_krog_ adds typescript aware hooks to your library to allow for more flexible and powerful plugins.\n\n### Features ✨\n\n- supports async hooks\n- typescript support\n- data can be manipulated through hooks\n- full control over how plugins are configured / loaded\n\nExample:\n\n```typescript\nconst { data } = await hooks.call('before:write', {\n  args: { data: 'Hello World' },\n});\n```\n\n## Install\n\n```sh\npnpm add krog\n```\n\nor\n\n```sh\nnpm install krog\n```\n\n## Usage\n\n### As library author\n\n#### 1. Create hooks instance\n\n```js\nimport { createHooks } from 'krog';\n\nconst hooks = createHooks();\n```\n\nWith typescript, you can add types:\n\n```ts\nimport { createHooks, Hook } from 'krog';\n\nexport type AvailableHooks = {\n  // first type is the arguments type, second is the context type; both are optional\n  'before:write': Hook\u003c{ data: string }, { config: any }\u003e;\n};\n\nconst hooks = createHooks\u003cAvailableHooks\u003e();\n```\n\n#### 2. Register hooks\n\nNow anywhere in your code, register hooks, using `hooks.register(hookName, myFunction);` These may come from a plugin.\n\nNote: When multiple functions are registered to the same hook, they are called in the order they were registered.\n\nExamples:\n\n```js\n// register a single hook\nhooks.register('before:write', myHookFunction);\n```\n\n```js\n// register all hooks from a list of plugins\nplugins.forEach((plugin) =\u003e {\n  hooks.registerMany(plugin.hooks);\n});\n```\n\n```js\n// manually register each hook\nplugins.forEach((plugin) =\u003e {\n  hooks.register('before:write', plugin.beforeWrite);\n});\n```\n\n#### 3. Call hooks\n\n##### a) Wrap functions\n\nThe easiest way is to wrap existing functions using `hooks.wrap`.\n\nThis will then pass all arguments to the hook before running the initial function.\n\n```js\n// wrap your function (note the parenthesis at the end)\nconst wrappedPrinter = hooks.wrap('before:write', printer)();\n\n// call the wrapped function like normal\nwrappedPrinter('Hello World');\n```\n\nYou can also pass a context to your wrapped function:  \n(that is the reason for the parenthesis at the end)\n\n```js\n// create a function factory\nconst createInstance = hooks.wrap('before:write', myFunction);\n\n// create an instance of the function with a context\nconst myWrappedFunction = createInstance(myContext);\n\n// call the wrapped function like normal\nmyWrappedFunction(myArguments);\n```\n\n##### b) Call hooks directly\n\nYou can also call a hook anywhere in your code using `hooks.call`.\n\nYou can pass arguments to the hook, and get the result back.  \nYou can also pass a context object, which will be available in the hook function.\n\n```js\nconst { data } = await hooks.call('before:write', {\n  args: { data: dataBeforeHook },\n  context: myContext,\n});\n```\n\n#### 4. Unregister hooks\n\nYou can unregister hooks by:\n\n##### a) Calling the returned unregister function\n\n```js\nconst unregister = hooks.register('before:write', myHookFunction);\nunregister();\n\n// for registerMany\nconst unregisterMany = hooks.registerMany({\n  'before:write': myHookFunction,\n});\nunregisterMany();\n```\n\n##### b) Calling `hooks.unregister`\n\n```js\nhooks.register('before:write', myHookFunction);\n\n// unregister a specific callback for the 'before:write' hook\nhooks.unregister('before:write', myHookFunction);\n\n// unregister all callbacks for the 'before:write' hook\nhooks.unregister('before:write');\n```\n\n### As plugin author\n\n#### Create a plugin\n\nThe syntax depends on how the library handles plugins. If the library allows you to pass hooks directly, you may configure them similar like this:\n\n```js\nconst upperCasePlugin = {\n  hooks: {\n    'before:write': (args, context) =\u003e {\n      if (context.config.uppercase) {\n        // if you want to modify the data, you can return a new args object (context cannot be modified)\n        return {\n          data: args.data.toUpperCase(),\n        };\n      }\n      // if you don't return anything, the data will be unchanged\n    },\n  },\n};\n```\n\n### FAQs\n\n#### Differences between arguments and context\n\n- The **context** is an object that is passed to all hooks. It can be used to pass data or functions that can be used in hooks but should not be modified by a hook.\n\n- **arguments** on the other hand, are passed to the hook and can be modified by returning a modified version.\n\n## Used by\n\n- [socketdb](https://github.com/TimoBechtel/socketdb)\n- [stapigen](https://github.com/TimoBechtel/stapigen)\n\n(feel free to add your library by submitting a pull request)\n\n## Development / Contributing\n\n### Run tests\n\n```sh\npnpm run test\n```\n\n### Commit messages\n\nThis project uses semantic-release for automated release versions. So commits in this project follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0-beta.2/) guidelines. I recommend using [commitizen](https://github.com/commitizen/cz-cli) for automated commit messages.\n\n---\n\n_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimobechtel%2Fkrog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimobechtel%2Fkrog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimobechtel%2Fkrog/lists"}