UNPKG

nestjs-cls

Version:

A continuation-local storage module compatible with NestJS's dependency injection.

60 lines (59 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClsPluginBase = void 0; exports.getPluginHooksToken = getPluginHooksToken; exports.isPluginHooksToken = isPluginHooksToken; /** * Extend this class to create a new ClsPlugin * * It contains the basic structure for a plugin, including * some helper methods for common operations. */ class ClsPluginBase { /** * @param name {@link ClsPlugin.name} */ constructor(name) { this.name = name; this.imports = []; this.providers = []; this.exports = []; } get hooksProviderToken() { return getPluginHooksToken(this.name); } /** * Register the plugin hooks provider * * This is a shorthand for manually registering a provider * that returns the plugin hooks object provided under * the `hooksProviderToken` token. * * @example * ``` * this.registerHooks({ * inject: [OPTIONS], * useFactory: (options: PluginOptions<any>): ClsPluginHooks => ({ * afterSetup: (cls: ClsService) => { * cls.set('some-key', options.pluginData); * } * }) * }) * ``` */ registerHooks(opts) { this.providers.push({ provide: this.hooksProviderToken, inject: opts.inject, useFactory: opts.useFactory, }); this.exports.push(this.hooksProviderToken); } } exports.ClsPluginBase = ClsPluginBase; function getPluginHooksToken(name) { return `CLS_PLUGIN_HOOKS_${name}`; } function isPluginHooksToken(token) { return typeof token === 'string' && token.startsWith('CLS_PLUGIN_HOOKS_'); }