{"id":15473023,"url":"https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions","last_synced_at":"2025-04-04T21:06:35.197Z","repository":{"id":35576240,"uuid":"218401578","full_name":"gr2m/javascript-plugin-architecture-with-typescript-definitions","owner":"gr2m","description":"Plugin architecture example with full TypeScript support","archived":false,"fork":false,"pushed_at":"2024-10-28T21:09:17.000Z","size":445,"stargazers_count":86,"open_issues_count":2,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T02:09:06.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gr2m.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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},"funding":{"github":"gr2m"}},"created_at":"2019-10-29T23:19:12.000Z","updated_at":"2025-02-23T13:30:02.000Z","dependencies_parsed_at":"2023-01-16T00:01:34.135Z","dependency_job_id":"4c06ea72-dffa-43a8-8b4c-ae6067d7b1ec","html_url":"https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions","commit_stats":{"total_commits":139,"total_committers":7,"mean_commits":"19.857142857142858","dds":0.7338129496402878,"last_synced_commit":"d6bea2fb42ecd961cc2580f4f745ad55b09b8774"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fjavascript-plugin-architecture-with-typescript-definitions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fjavascript-plugin-architecture-with-typescript-definitions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fjavascript-plugin-architecture-with-typescript-definitions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gr2m%2Fjavascript-plugin-architecture-with-typescript-definitions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gr2m","download_url":"https://codeload.github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247016400,"owners_count":20869650,"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-10-02T02:42:15.931Z","updated_at":"2025-04-04T21:06:35.180Z","avatar_url":"https://github.com/gr2m.png","language":"TypeScript","funding_links":["https://github.com/sponsors/gr2m"],"categories":[],"sub_categories":[],"readme":"# javascript-plugin-architecture-with-typescript-definitions\n\n\u003e Plugin architecture example with full TypeScript support\n\n[![@latest](https://img.shields.io/npm/v/javascript-plugin-architecture-with-typescript-definitions.svg)](https://www.npmjs.com/package/javascript-plugin-architecture-with-typescript-definitions)\n[![Build Status](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/workflows/Test/badge.svg)](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/actions/workflows/test.yml)\n\nThe goal of this repository is to provide a template for a simple plugin Architecture which allows plugin authors to extend the base API as well as extend its constructor options. A custom class can be composed of the core Base class, a set of plugins and default options and distributed as new package, with full TypeScript for added APIs and constructor options.\n\n## Usage\n\n[Try it in TypeScript's playground editor](https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgIQIYGcCmcC+cBmUEIcARAFaoBuGAxlMGDALRgA2ArgObAB2zqKLQAWwGJlowOUTMwDuY4cxgBPMJnT1GLACaZ8fMcAi90pANwAoS3tptB2EBB0c22CtToMmrTj36CImISUjLyispqGlo+ega8RiZmiJZwcLyoIBpgqLTYaFgpaWl84lD4udgA8kzGpkXFafgQEABccOgwDLxcVo04qbiWA5b4HLySdXAgKgBiLQAKfnwAFHydqBOY7QWYADRwELVJOxiYAHQ1MHXoAJQNMqG8DU0t7Sv3ALwAfIfHpudmhA4AAfEFkABGglIe0GOCsIzGE2uJmmKjQUCW3FW6xgmzypyw9wQg0e0meJMaUKg7y+v1I1JhcIR1loSXgAFl0Wc4J8UGdzgoYMIsf50CsANozeYQUV8A4zDFy3gAXVugsUABF9KhXDBxZSCG9IdDYThblZLGzTPBFTy+bxMHI4FzdiskED2gzUAAvUi4C2WO1YQEtD7mOAAekjHS6fC4Qe5Iep4ajMc63QTQA)\n\n### Export new class `MyBase` with two plugins and default options:\n\n```ts\n// import the Base class\nimport { Base } from \"javascript-plugin-architecture-with-typescript-definitions\";\n\n// import a set of plugins\nimport { myFooPlugin } from \"@example/my-foo-plugin\";\nimport { myBarPlugin } from \"./my-bar-plugin\";\n\nexport const MyBase = Base.withPlugins([myFooPlugin, myBarPlugin]).withDefaults(\n  {\n    foo: \"bar\",\n  },\n);\n```\n\nWhen importing `MyBase` and instantiating it, the `MyBase` constructor has type support for the new optional `foo` option as well as the `.foo()` and `.bar()` methods addded by the respective plugins.\n\n```ts\nimport { MyBase } from \"@example/my-base\";\n\nconst myBase = new MyBase({\n  // has full TypeScript intellisense\n  foo: \"bar\",\n});\nmyBase.foo(); // has full TypeScript intellisense\nmyBase.bar(); // has full TypeScript intellisense\n```\n\n### Create plugin which extends the API as well as the constructor options type\n\n```js\nimport { Base } from \"javascript-plugin-architecture-with-typescript-definitions\";\n\ndeclare module \"javascript-plugin-architecture-with-typescript-definitions\" {\n  namespace Base {\n    interface Options {\n      foo?: string;\n    }\n  }\n}\n\nexport function myFooPlugin(base: Base, options: Base.options) {\n  return {\n    foo() =\u003e options.foo || \"bar\",\n  }\n}\n```\n\n## API\n\n### static `.withPlugins(plugins)`\n\nReturns a new class with `.plugins` added to parent classes `.plugins` array. All plugins will be applied to instances.\n\n### static `.withDefaults(options)`\n\nReturns a new class with `.defaults` merged with the parent classes `.defaults` object. The defaults are applied to the options passed to the constructor when instantiated.\n\n### static `.plugins`\n\n`Base.plugins` is an empty array by default. It is extended on derived classes using `.withPlugins(plugins)`.\n\n### static `.defaults`\n\n`Base.defaults` is an empty object by default. It is extended on derived classes using `.withDefaults(plugins)`.\n\n### Constructor\n\nThe constructor accepts one argument which is optional by default\n\n```ts\nnew Base(options);\n```\n\nIf the `Base.Options` interface has been extended with required keys, then the `options` argument becomes required, and all required `Base.Options` keys must be set.\n\n### `.options`\n\nThe `.options` key is set on all instances. It's merged from from the constructor's `.defaults` object and the options passed to the constructor\n\n```js\nconst BaseWithOptions = Base.withDefaults({ foo: \"bar\" });\nconst instance = new BaseWithOptions();\ninstance.options; // {foo: 'bar'}\n```\n\nNote that in for TypeScript to recognize the new option, you have to extend the `Base.Option` intererface.\n\n### Other instance propreties and methods\n\nInstance properties and methods can be added using plugins. Example:\n\n```ts\nfunction myPlugin(base: Base, options: Base.options) {\n  return {\n    myMethod() {\n      /* do something here */\n    },\n    myProperty: \"\", // set to something useful\n  };\n}\nconst MyBase = Base.plugins([myPlugin]);\nconst myBase = new MyBase();\n\n// this method and property is now set\nmyBase.myMethod();\nmyBase.myProperty;\n```\n\n### TypeScript for a customized Base class\n\nIf you write your `d.ts` files by hand instead of generating them from TypeScript source code, you can use the `ExtendBaseWith` Generic to create a class with custom defaults and plugins. It can even inherit from another customized class.\n\n```ts\nimport {\n  Base,\n  ExtendBaseWith,\n} from \"javascript-plugin-architecture-with-typescript-definitions\";\n\nimport { myPlugin } from \"./my-plugin.js\";\n\nexport const MyBase: ExtendBaseWith\u003c\n  Base,\n  {\n    defaults: {\n      myPluginOption: string;\n    };\n    plugins: [typeof myPlugin];\n  }\n\u003e;\n\n// support import to be used as a class instance type\nexport type MyBase = typeof MyBase;\n```\n\nThe last line is important in order to make `MyBase` behave like a class type, making the following code possible:\n\n```ts\nimport { MyBase } from \"./index.js\";\n\nexport async function testInstanceType(client: MyBase) {\n  // types set correctly on `client`\n  client.myPlugin({ myPluginOption: \"foo\" });\n}\n```\n\n### Defaults\n\nTypeScript will not complain when chaining `.withDefaults()` calls endlessly: the static `.defaults` property will be set correctly. However, when instantiating from a class with 4+ chained `.withDefaults()` calls, then only the defaults from the first 3 calls are supported. See [#57](https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/pull/57) for details.\n\n## Credit\n\nThis plugin architecture was extracted from [`@octokit/core`](https://github.com/octokit/core.js). The implementation was made possible by help from [@karol-majewski](https://github.com/karol-majewski), [@dragomirtitian](https://github.com/dragomirtitian), [StackOverflow user \"hackape\"](https://stackoverflow.com/a/58706699/206879), and [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg).\n\n## LICENSE\n\n[ISC](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgr2m%2Fjavascript-plugin-architecture-with-typescript-definitions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgr2m%2Fjavascript-plugin-architecture-with-typescript-definitions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgr2m%2Fjavascript-plugin-architecture-with-typescript-definitions/lists"}