{"id":28684909,"url":"https://github.com/node-modules/sdk-base","last_synced_at":"2025-06-14T03:07:48.637Z","repository":{"id":20294551,"uuid":"23568126","full_name":"node-modules/sdk-base","owner":"node-modules","description":"a base class for sdk with default error handler","archived":false,"fork":false,"pushed_at":"2024-12-22T03:21:27.000Z","size":69,"stargazers_count":62,"open_issues_count":2,"forks_count":8,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-26T14:03:08.800Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-modules.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":"2014-09-02T06:46:39.000Z","updated_at":"2024-12-22T03:21:29.000Z","dependencies_parsed_at":"2023-01-13T20:53:42.079Z","dependency_job_id":null,"html_url":"https://github.com/node-modules/sdk-base","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fsdk-base","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fsdk-base/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fsdk-base/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fsdk-base/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-modules","download_url":"https://codeload.github.com/node-modules/sdk-base/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fsdk-base/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":257186217,"owners_count":22503417,"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":"2025-06-14T03:07:40.690Z","updated_at":"2025-06-14T03:07:48.626Z","avatar_url":"https://github.com/node-modules.png","language":"TypeScript","readme":"# sdk-base\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/node-modules/sdk-base/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/sdk-base/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/sdk-base.svg?style=flat)](https://nodejs.org/en/download/)\n\n[npm-image]: https://img.shields.io/npm/v/sdk-base.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/sdk-base\n[codecov-image]: https://codecov.io/github/node-modules/sdk-base/coverage.svg?branch=master\n[codecov-url]: https://codecov.io/github/node-modules/sdk-base?branch=master\n[snyk-image]: https://snyk.io/test/npm/sdk-base/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/sdk-base\n[download-image]: https://img.shields.io/npm/dm/sdk-base.svg?style=flat-square\n[download-url]: https://npmjs.org/package/sdk-base\n\nA base class for sdk with some common \u0026 useful functions.\n\n## Installation\n\n```bash\nnpm install sdk-base\n```\n\n## Usage\n\nConstructor argument:\n\n- {Object} options\n  - {String} [initMethod] - the async init method name, the method should be a function return promise. If set, will execute the function in the constructor.\n  - {AsyncLocalStorage} [localStorage] - async localStorage instance.\n\n  ```js\n  const { Base } = require('sdk-base');\n\n  class Client extends Base {\n    constructor() {\n      super({\n        initMethod: 'init',\n        localStorage: app.ctxStorage,\n      });\n    }\n\n    async init() {\n      // put your async init logic here\n    }\n    // support async function too\n    // async init() {\n    //   // put your async init logic here\n    // }\n  }\n\n  (async function main() {\n    const client = new Client();\n    // wait client ready, if init failed, client will throw an error.\n    await client.ready();\n\n    // support async event listener\n    client.on('data', async function (data) {\n      // put your async process logic here\n      //\n      // @example\n      // ----------\n      // await submit(data);\n    });\n\n    client.emit('data', { foo: 'bar' });\n\n  })().catch(err =\u003e { console.error(err); });\n  ```\n\n### API\n\n- `.ready(flagOrFunction)` flagOrFunction is optional, and the argument type can be Boolean, Error or Function.\n\n  ```js\n  // init ready\n  client.ready(true);\n  // init failed\n  client.ready(new Error('init failed'));\n\n  // listen client ready\n  client.ready(err =\u003e {\n    if (err) {\n      console.log('client init failed');\n      console.error(err);\n      return;\n    }\n    console.log('client is ready');\n  });\n\n  // support promise style call\n  client.ready()\n    .then(() =\u003e { ... })\n    .catch(err =\u003e { ... });\n\n  // support async function style call\n  await client.ready();\n  ```\n\n- `async readyOrTimeout(milliseconds)` ready or timeout, after milliseconds not ready will throw TimeoutError\n\n  ```js\n  await client.readyOrTimeout(100);\n  ```\n\n- `.isReady getter` detect client start ready or not.\n- `.on(event, listener)` wrap the [EventEmitter.prototype.on(event, listener)](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener), the only difference is to support adding async function listener on events, except 'error' event.\n- `once(event, listener)` wrap the [EventEmitter.prototype.once(event, listener)](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener), the only difference is to support adding async function listener on events, except 'error' event.\n- `prependListener(event, listener)` wrap the [EventEmitter.prototype.prependListener(event, listener)](https://nodejs.org/api/events.html#events_emitter_prependlistener_eventname_listener), the only difference is to support adding async function listener on events, except 'error' event.\n- `prependOnceListener(event, listener)` wrap the [EventEmitter.prototype.prependOnceListener(event, listener)](https://nodejs.org/api/events.html#events_emitter_prependoncelistener_eventname_listener), the only difference is to support adding generator listener on events, except 'error' event.\n- `addListener(event, listener)` wrap the [EventEmitter.prototype.addListener(event, listener)](https://nodejs.org/api/events.html#events_emitter_addlistener_eventname_listener), the only difference is to support adding async function listener on events, except 'error' event.\n\n  ```js\n  client.on('data', async function(data) {\n    // your async process logic here\n  });\n  client.once('foo', async function(bar) {\n    // ...\n  });\n\n  // listen error event\n  client.on('error', err =\u003e {\n    console.error(err.stack);\n  });\n  ```\n\n- `.await(event)`: [await an event](https://github.com/cojs/await-event), return a promise, and it will resolve(reject if event is `error`) once this event emmited.\n\n  ```js\n  const data = await client.await('data');\n  ```\n\n- `._close()`: The `_close()` method is called by `close`.\nIt can be overridden by child class, but should not be called directly. It must return promise or generator.\n\n- `.close()`: The `close()` method is used to close the instance.\n\n## Breaking changes between v4 and v5\n\n- Drop `.awaitFirst(events)` support\n- Drop generator function support\n- Don't catch event listener inside error\n\n### License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=node-modules/sdk-base)](https://github.com/node-modules/sdk-base/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fsdk-base","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-modules%2Fsdk-base","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fsdk-base/lists"}