@my-nestjs-kit/nodes 0.0.0-stage → 0.1.0-dev.20261129010337

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 sabationok
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,25 @@
1
- # Temporary Holding Version
1
+ # @my-nestjs-kit/nodes
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Owners get their providers by binding, not by import. Depends on `@my-nestjs-kit/core` (`AppHttpException`).
4
+
5
+ ```sh
6
+ npm i @my-nestjs-kit/nodes
7
+ ```
8
+
9
+ Peers: `@nestjs/common`, `@nestjs/core`, plus core's peers.
10
+
11
+ ```ts
12
+ @BindNode({ provider: 'liqPay', type: 'payments' })
13
+ @Injectable()
14
+ class LiqPayNode {}
15
+
16
+ NodesModule.forRoot(); // global NodesRegistry
17
+ registry.host<PaymentsMap>('payments').getOrFail('liqPay');
18
+ ```
19
+
20
+ `provider` + `type` bind both ways (`mirror: false` turns that off), `bindings` add any `{ host, key }`. The start
21
+ fails on a key taken twice and on a request-scoped or transient node; reading before a bound provider exists throws.
22
+
23
+ ## License
24
+
25
+ MIT
@@ -0,0 +1,43 @@
1
+ /** Where a node sits: its owner (`host`) and the key the owner looks it up by. */
2
+ export interface NodeBinding {
3
+ host: string;
4
+ key: string;
5
+ }
6
+ export interface BindNodeOptions {
7
+ /** Who provides the node (`liqPay`, `checkbox`, `local`). */
8
+ provider?: string;
9
+ /** What the node does (`webhooks`, `payments`, `sdk`). */
10
+ type?: string;
11
+ /**
12
+ * With both `provider` and `type`, the node binds both ways: `provider → type` and `type → provider`
13
+ * (`liqPay` finds it by `webhooks`, `webhooks` by `liqPay`). `false` turns that off. Default `true`.
14
+ */
15
+ mirror?: boolean;
16
+ /** Bindings beyond the mirrored pair. */
17
+ bindings?: NodeBinding | NodeBinding[];
18
+ }
19
+ /**
20
+ * Binds a provider to its owners. `NodesRegistry` finds every bound provider and hands each owner its
21
+ * nodes: the node knows its owners by name, an owner knows its nodes only by type.
22
+ * One decorator per class; a second one replaces the first.
23
+ *
24
+ * ```ts
25
+ * @Injectable()
26
+ * @BindNode({
27
+ * provider: 'liqPay',
28
+ * type: 'webhooks', // liqPay → webhooks, webhooks → liqPay
29
+ * bindings: [{ host: 'payments:webhooks', key: 'liqPay' }], // anything else
30
+ * })
31
+ * export class LiqPayWebhooksService {}
32
+ * ```
33
+ */
34
+ export declare function BindNode(options: BindNodeOptions): ClassDecorator;
35
+ export declare namespace BindNode {
36
+ var KEY: string;
37
+ }
38
+ /** The mirrored pair (when on) plus the extra bindings, without repeats. */
39
+ export declare function resolveNodeBindings({ provider, type, mirror, bindings, }: BindNodeOptions): NodeBinding[];
40
+ /** A class' `@BindNode` options, `undefined` for an unbound class. */
41
+ export declare function getNodeMeta(target: Function): BindNodeOptions | undefined;
42
+ /** A class' bindings, `[]` for an unbound class. */
43
+ export declare function getNodeBindings(target: Function): NodeBinding[];
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNodeBindings = exports.getNodeMeta = exports.resolveNodeBindings = exports.BindNode = void 0;
4
+ const core_1 = require("@nestjs/core");
5
+ const discoverable = core_1.DiscoveryService.createDecorator();
6
+ /**
7
+ * Binds a provider to its owners. `NodesRegistry` finds every bound provider and hands each owner its
8
+ * nodes: the node knows its owners by name, an owner knows its nodes only by type.
9
+ * One decorator per class; a second one replaces the first.
10
+ *
11
+ * ```ts
12
+ * @Injectable()
13
+ * @BindNode({
14
+ * provider: 'liqPay',
15
+ * type: 'webhooks', // liqPay → webhooks, webhooks → liqPay
16
+ * bindings: [{ host: 'payments:webhooks', key: 'liqPay' }], // anything else
17
+ * })
18
+ * export class LiqPayWebhooksService {}
19
+ * ```
20
+ */
21
+ function BindNode(options) {
22
+ if (!resolveNodeBindings(options).length)
23
+ throw new Error('@BindNode needs provider + type or at least one binding');
24
+ return discoverable(options);
25
+ }
26
+ exports.BindNode = BindNode;
27
+ BindNode.KEY = discoverable.KEY;
28
+ /** The mirrored pair (when on) plus the extra bindings, without repeats. */
29
+ function resolveNodeBindings({ provider, type, mirror = true, bindings = [], }) {
30
+ const all = [
31
+ ...(mirror && provider && type
32
+ ? [
33
+ { host: provider, key: type },
34
+ { host: type, key: provider },
35
+ ]
36
+ : []),
37
+ ...(Array.isArray(bindings) ? bindings : [bindings]),
38
+ ];
39
+ const seen = new Set();
40
+ return all.filter(({ host, key }) => {
41
+ const id = `${host}\u0000${key}`;
42
+ return !seen.has(id) && seen.add(id);
43
+ });
44
+ }
45
+ exports.resolveNodeBindings = resolveNodeBindings;
46
+ /** A class' `@BindNode` options, `undefined` for an unbound class. */
47
+ function getNodeMeta(target) {
48
+ return Reflect.getMetadata(BindNode.KEY, target);
49
+ }
50
+ exports.getNodeMeta = getNodeMeta;
51
+ /** A class' bindings, `[]` for an unbound class. */
52
+ function getNodeBindings(target) {
53
+ const meta = getNodeMeta(target);
54
+ return meta ? resolveNodeBindings(meta) : [];
55
+ }
56
+ exports.getNodeBindings = getNodeBindings;
@@ -0,0 +1,4 @@
1
+ export * from './bind-node.decorator';
2
+ export * from './nodes-host';
3
+ export * from './nodes.registry';
4
+ export * from './nodes.module';
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./bind-node.decorator"), exports);
18
+ __exportStar(require("./nodes-host"), exports);
19
+ __exportStar(require("./nodes.registry"), exports);
20
+ __exportStar(require("./nodes.module"), exports);
@@ -0,0 +1,21 @@
1
+ /** The owner's typed view of its nodes: `{ [key]: NodeClass }`. Types only, the runtime reads metadata. */
2
+ export type NodesMap = object;
3
+ export type NodeKey<M extends NodesMap> = Extract<keyof M, string>;
4
+ export interface NodesHostOptions {
5
+ /** Error of `getOrFail` for an unknown key. Default: 500 "Service is unavailable." */
6
+ notFound?: (host: string, key: string) => Error;
7
+ }
8
+ /**
9
+ * One owner's nodes. Reads the registry on every call, so it can be taken in a constructor and
10
+ * used once the app is up.
11
+ */
12
+ export declare class NodesHost<M extends NodesMap = Record<string, unknown>> {
13
+ readonly name: string;
14
+ private readonly entries;
15
+ private readonly options;
16
+ constructor(name: string, entries: () => ReadonlyMap<string, unknown>, options?: NodesHostOptions);
17
+ keys(): NodeKey<M>[];
18
+ has(key: string): boolean;
19
+ get<K extends NodeKey<M>>(key: K | string): M[K] | undefined;
20
+ getOrFail<K extends NodeKey<M>>(key: K | string): M[K];
21
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NodesHost = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const core_1 = require("@my-nestjs-kit/core");
6
+ /**
7
+ * One owner's nodes. Reads the registry on every call, so it can be taken in a constructor and
8
+ * used once the app is up.
9
+ */
10
+ class NodesHost {
11
+ constructor(name, entries, options = {}) {
12
+ this.name = name;
13
+ this.entries = entries;
14
+ this.options = options;
15
+ }
16
+ keys() {
17
+ return [...this.entries().keys()];
18
+ }
19
+ has(key) {
20
+ return this.entries().has(key);
21
+ }
22
+ get(key) {
23
+ return this.entries().get(key);
24
+ }
25
+ getOrFail(key) {
26
+ const node = this.get(key);
27
+ if (node)
28
+ return node;
29
+ throw (this.options.notFound?.(this.name, String(key)) ??
30
+ new core_1.AppHttpException({
31
+ message: 'Service is unavailable.',
32
+ statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
33
+ meta: { registry: this.name, service: String(key) },
34
+ }));
35
+ }
36
+ }
37
+ exports.NodesHost = NodesHost;
@@ -0,0 +1,5 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ /** Registers `NodesRegistry` once, globally. Owners inject the registry and read `host(name)`. */
3
+ export declare class NodesModule {
4
+ static forRoot(): DynamicModule;
5
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var NodesModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.NodesModule = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const core_1 = require("@nestjs/core");
13
+ const nodes_registry_1 = require("./nodes.registry");
14
+ /** Registers `NodesRegistry` once, globally. Owners inject the registry and read `host(name)`. */
15
+ let NodesModule = NodesModule_1 = class NodesModule {
16
+ static forRoot() {
17
+ return {
18
+ module: NodesModule_1,
19
+ global: true,
20
+ imports: [core_1.DiscoveryModule],
21
+ providers: [nodes_registry_1.NodesRegistry],
22
+ exports: [nodes_registry_1.NodesRegistry],
23
+ };
24
+ }
25
+ };
26
+ exports.NodesModule = NodesModule;
27
+ exports.NodesModule = NodesModule = NodesModule_1 = __decorate([
28
+ (0, common_1.Module)({})
29
+ ], NodesModule);
@@ -0,0 +1,19 @@
1
+ import { OnApplicationBootstrap } from '@nestjs/common';
2
+ import { DiscoveryService } from '@nestjs/core';
3
+ import { NodesHost, NodesHostOptions, NodesMap } from './nodes-host';
4
+ /**
5
+ * Collects `@BindNode` providers into `host → key → instance`. Built on first read, and at
6
+ * bootstrap at the latest, so a key taken twice fails the start instead of the first request.
7
+ */
8
+ export declare class NodesRegistry implements OnApplicationBootstrap {
9
+ private readonly discovery;
10
+ private readonly logger;
11
+ private _hosts?;
12
+ constructor(discovery: DiscoveryService);
13
+ onApplicationBootstrap(): void;
14
+ /** An owner's nodes. Typed by the owner: `nodes.host<{ email: EmailSender }>('notifications')`. */
15
+ host<M extends NodesMap = Record<string, unknown>>(name: string, options?: NodesHostOptions): NodesHost<M>;
16
+ hostNames(): string[];
17
+ private get hosts();
18
+ private collect;
19
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NodesRegistry = void 0;
13
+ const common_1 = require("@nestjs/common");
14
+ const core_1 = require("@nestjs/core");
15
+ const constants_1 = require("@nestjs/core/injector/constants");
16
+ const bind_node_decorator_1 = require("./bind-node.decorator");
17
+ const nodes_host_1 = require("./nodes-host");
18
+ const EMPTY = new Map();
19
+ /**
20
+ * Collects `@BindNode` providers into `host → key → instance`. Built on first read, and at
21
+ * bootstrap at the latest, so a key taken twice fails the start instead of the first request.
22
+ */
23
+ let NodesRegistry = class NodesRegistry {
24
+ constructor(discovery) {
25
+ this.discovery = discovery;
26
+ this.logger = new common_1.Logger('Nodes');
27
+ }
28
+ onApplicationBootstrap() {
29
+ const hosts = this.hosts;
30
+ if (hosts.size)
31
+ this.logger.log([...hosts]
32
+ .map(([host, nodes]) => `${host}[${[...nodes.keys()].join(', ')}]`)
33
+ .join(' '));
34
+ }
35
+ /** An owner's nodes. Typed by the owner: `nodes.host<{ email: EmailSender }>('notifications')`. */
36
+ host(name, options) {
37
+ return new nodes_host_1.NodesHost(name, () => this.hosts.get(name) ?? EMPTY, options);
38
+ }
39
+ hostNames() {
40
+ return [...this.hosts.keys()];
41
+ }
42
+ get hosts() {
43
+ return (this._hosts ??= this.collect());
44
+ }
45
+ collect() {
46
+ const hosts = new Map();
47
+ for (const wrapper of this.discovery.getProviders({
48
+ metadataKey: bind_node_decorator_1.BindNode.KEY,
49
+ })) {
50
+ const name = wrapper.metatype?.name ?? String(wrapper.token);
51
+ if (!wrapper.isDependencyTreeStatic())
52
+ throw new Error(`Node ${name} must be a singleton: request-scoped and transient providers can't be bound`);
53
+ // `instance` holds a bare prototype until the constructor ran, so it can't tell a created provider.
54
+ if (!wrapper.getInstanceByContextId(constants_1.STATIC_CONTEXT).isResolved)
55
+ throw new Error(`Nodes were read before ${name} was created: read them in a lifecycle hook or a handler, not in a constructor or factory`);
56
+ for (const { host, key } of (0, bind_node_decorator_1.getNodeBindings)(wrapper.instance.constructor)) {
57
+ const nodes = hosts.get(host) ?? new Map();
58
+ const taken = nodes.get(key);
59
+ if (taken)
60
+ throw new Error(`Node key "${host}:${key}" is taken by ${taken.constructor.name}, ${name} can't reuse it` +
61
+ (taken !== wrapper.instance &&
62
+ taken.constructor === wrapper.instance.constructor
63
+ ? ' (the class is provided in two modules)'
64
+ : ''));
65
+ hosts.set(host, nodes.set(key, wrapper.instance));
66
+ }
67
+ }
68
+ return hosts;
69
+ }
70
+ };
71
+ exports.NodesRegistry = NodesRegistry;
72
+ exports.NodesRegistry = NodesRegistry = __decorate([
73
+ (0, common_1.Injectable)(),
74
+ __metadata("design:paramtypes", [core_1.DiscoveryService])
75
+ ], NodesRegistry);
package/package.json CHANGED
@@ -1,6 +1,73 @@
1
1
  {
2
2
  "name": "@my-nestjs-kit/nodes",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-dev.20261129010337",
4
+ "description": "Provider nodes for NestJS: providers bind themselves to an owner with @BindNode, the owner reads them from NodesRegistry by type.",
5
+ "keywords": [
6
+ "nestjs",
7
+ "nest",
8
+ "registry",
9
+ "discovery",
10
+ "plugins"
11
+ ],
12
+ "license": "MIT",
13
+ "homepage": "https://github.com/sabationok/my-nestjs-kit/tree/main/packages/nodes#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/sabationok/my-nestjs-kit/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/sabationok/my-nestjs-kit.git",
20
+ "directory": "packages/nodes"
21
+ },
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "main": "dist/index.js",
29
+ "types": "dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "require": "./dist/index.js",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "./package.json": "./package.json"
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "!dist/**/*.spec.*",
41
+ "!dist/**/*.tsbuildinfo",
42
+ "README.md",
43
+ "LICENSE"
44
+ ],
45
+ "dependencies": {
46
+ "@my-nestjs-kit/core": "^0.1.0-dev.20261129010337"
47
+ },
48
+ "peerDependencies": {
49
+ "@nestjs/common": "^10.4.1",
50
+ "@nestjs/core": "^10.4.1"
51
+ },
52
+ "devDependencies": {
53
+ "@nestjs/common": "10.4.20",
54
+ "@nestjs/core": "10.4.1",
55
+ "@nestjs/swagger": "7.4.0",
56
+ "@nestjs/websockets": "10.4.1",
57
+ "@types/node": "20.11.28",
58
+ "class-transformer": "0.5.1",
59
+ "class-validator": "0.14.1",
60
+ "decimal.js": "10.4.3",
61
+ "ioredis": "5.8.2",
62
+ "lodash": "4.17.21",
63
+ "reflect-metadata": "0.2.2",
64
+ "rxjs": "7.8.1",
65
+ "typeorm": "0.3.28",
66
+ "typescript": "5.4.2"
67
+ },
68
+ "scripts": {
69
+ "build": "rm -rf dist && tsc -p tsconfig.json",
70
+ "build:watch": "tsc -p tsconfig.json --watch",
71
+ "typecheck": "tsc -p tsconfig.json --noEmit"
72
+ }
6
73
  }