@go-go-scope/adapter-nestjs 2.0.0

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/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @go-go-scope/adapter-nestjs
2
+
3
+ NestJS adapter for go-go-scope - provides dependency injection integration with `@Task` decorator.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @go-go-scope/adapter-nestjs @nestjs/common
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { Module } from '@nestjs/common'
15
+ import { GoGoScopeModule, Task } from '@go-go-scope/adapter-nestjs'
16
+
17
+ @Module({
18
+ imports: [GoGoScopeModule.forRoot({ metrics: true })],
19
+ providers: [UserService],
20
+ })
21
+ class AppModule {}
22
+
23
+ @Injectable()
24
+ class UserService {
25
+ @Task({ retry: 'exponential', timeout: 5000 })
26
+ async getUser(id: string) {
27
+ return fetchUser(id)
28
+ }
29
+ }
30
+ ```
31
+
32
+ ## License
33
+
34
+ MIT
@@ -0,0 +1,83 @@
1
+ import * as go_go_scope from 'go-go-scope';
2
+ import { Scope, scope } from 'go-go-scope';
3
+ import { OnModuleDestroy } from '@nestjs/common';
4
+
5
+ declare const GOGO_ROOT_SCOPE: unique symbol;
6
+ declare const GOGO_REQUEST_SCOPE: unique symbol;
7
+ declare const GOGO_MODULE_OPTIONS: unique symbol;
8
+ interface GoGoScopeModuleOptions {
9
+ /** Root scope name */
10
+ name?: string;
11
+ /** Enable metrics collection */
12
+ metrics?: boolean;
13
+ /** Default timeout for all requests */
14
+ timeout?: number;
15
+ }
16
+ /**
17
+ * Service providing access to the root application scope
18
+ */
19
+ declare class GoGoScopeService implements OnModuleDestroy {
20
+ readonly rootScope: Scope;
21
+ constructor(_options?: GoGoScopeModuleOptions);
22
+ /**
23
+ * Create a new child scope from the root
24
+ */
25
+ createScope(name: string, options?: Omit<Parameters<typeof scope>[0], "parent">): Scope;
26
+ /**
27
+ * Get current metrics from the root scope
28
+ */
29
+ getMetrics(): go_go_scope.ScopeMetrics | undefined;
30
+ onModuleDestroy(): Promise<void>;
31
+ }
32
+ /**
33
+ * Request-scoped service that provides a unique scope per HTTP request
34
+ */
35
+ declare class GoGoRequestScopeService implements OnModuleDestroy {
36
+ readonly requestScope: Scope;
37
+ constructor(_scopeService: GoGoScopeService, _options?: GoGoScopeModuleOptions);
38
+ /**
39
+ * Get the underlying scope for advanced operations
40
+ */
41
+ getScope(): Scope;
42
+ onModuleDestroy(): Promise<void>;
43
+ }
44
+ /**
45
+ * Decorator to execute a method within a task
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * @Injectable()
50
+ * class UserService {
51
+ * @Task({ retry: 'exponential', timeout: 5000 })
52
+ * async getUser(id: string) {
53
+ * return fetchUser(id)
54
+ * }
55
+ * }
56
+ * ```
57
+ */
58
+ declare function Task(options?: Parameters<Scope["task"]>[1]): (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
59
+ /**
60
+ * NestJS module for go-go-scope integration
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * @Module({
65
+ * imports: [GoGoScopeModule.forRoot({ metrics: true })],
66
+ * providers: [UserService],
67
+ * })
68
+ * class AppModule {}
69
+ * ```
70
+ */
71
+ declare class GoGoScopeModule {
72
+ static forRoot(options?: GoGoScopeModuleOptions): {
73
+ module: typeof GoGoScopeModule;
74
+ providers: (typeof GoGoScopeService | typeof GoGoRequestScopeService | {
75
+ provide: symbol;
76
+ useValue: GoGoScopeModuleOptions;
77
+ })[];
78
+ exports: (typeof GoGoScopeService | typeof GoGoRequestScopeService)[];
79
+ };
80
+ }
81
+
82
+ export { GOGO_MODULE_OPTIONS, GOGO_REQUEST_SCOPE, GOGO_ROOT_SCOPE, GoGoRequestScopeService, GoGoScopeModule, GoGoScopeService, Task };
83
+ export type { GoGoScopeModuleOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,118 @@
1
+ import { Injectable, Optional, Inject, Scope, Module } from '@nestjs/common';
2
+ import { scope } from 'go-go-scope';
3
+
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __decorateClass = (decorators, target, key, kind) => {
6
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
7
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
8
+ if (decorator = decorators[i])
9
+ result = (decorator(result)) || result;
10
+ return result;
11
+ };
12
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
13
+ const GOGO_ROOT_SCOPE = Symbol("GOGO_ROOT_SCOPE");
14
+ const GOGO_REQUEST_SCOPE = Symbol("GOGO_REQUEST_SCOPE");
15
+ const GOGO_MODULE_OPTIONS = Symbol("GOGO_MODULE_OPTIONS");
16
+ let GoGoScopeService = class {
17
+ rootScope;
18
+ constructor(_options = {}) {
19
+ this.rootScope = scope({
20
+ name: _options.name ?? "nestjs-app",
21
+ metrics: _options.metrics,
22
+ timeout: _options.timeout
23
+ });
24
+ }
25
+ /**
26
+ * Create a new child scope from the root
27
+ */
28
+ createScope(name, options) {
29
+ return scope({
30
+ parent: this.rootScope,
31
+ name,
32
+ ...options
33
+ });
34
+ }
35
+ /**
36
+ * Get current metrics from the root scope
37
+ */
38
+ getMetrics() {
39
+ return this.rootScope.metrics();
40
+ }
41
+ async onModuleDestroy() {
42
+ await this.rootScope[Symbol.asyncDispose]().catch(() => {
43
+ });
44
+ }
45
+ };
46
+ GoGoScopeService = __decorateClass([
47
+ Injectable(),
48
+ __decorateParam(0, Optional()),
49
+ __decorateParam(0, Inject(GOGO_MODULE_OPTIONS))
50
+ ], GoGoScopeService);
51
+ let GoGoRequestScopeService = class {
52
+ requestScope;
53
+ constructor(_scopeService, _options = {}) {
54
+ this.requestScope = scope({
55
+ parent: _scopeService.rootScope,
56
+ name: `request-${Date.now()}`,
57
+ timeout: _options.timeout
58
+ });
59
+ }
60
+ /**
61
+ * Get the underlying scope for advanced operations
62
+ */
63
+ getScope() {
64
+ return this.requestScope;
65
+ }
66
+ async onModuleDestroy() {
67
+ await this.requestScope[Symbol.asyncDispose]().catch(() => {
68
+ });
69
+ }
70
+ };
71
+ GoGoRequestScopeService = __decorateClass([
72
+ Injectable({ scope: Scope.REQUEST }),
73
+ __decorateParam(1, Optional()),
74
+ __decorateParam(1, Inject(GOGO_MODULE_OPTIONS))
75
+ ], GoGoRequestScopeService);
76
+ function Task(options) {
77
+ return (_target, _propertyKey, descriptor) => {
78
+ const originalMethod = descriptor.value;
79
+ descriptor.value = async function(...args) {
80
+ const scopeService = this.goGoRequestScopeService;
81
+ const rootScopeService = this.goGoScopeService;
82
+ const taskScope = scopeService?.getScope() ?? rootScopeService?.rootScope;
83
+ if (!taskScope) {
84
+ throw new Error(
85
+ "No scope available. Ensure GoGoScopeModule is imported."
86
+ );
87
+ }
88
+ const [err, result] = await taskScope.task(
89
+ () => originalMethod.apply(this, args),
90
+ options ?? {}
91
+ );
92
+ if (err) throw err;
93
+ return result;
94
+ };
95
+ return descriptor;
96
+ };
97
+ }
98
+ let GoGoScopeModule = class {
99
+ static forRoot(options = {}) {
100
+ return {
101
+ module: GoGoScopeModule,
102
+ providers: [
103
+ {
104
+ provide: GOGO_MODULE_OPTIONS,
105
+ useValue: options
106
+ },
107
+ GoGoScopeService,
108
+ GoGoRequestScopeService
109
+ ],
110
+ exports: [GoGoScopeService, GoGoRequestScopeService]
111
+ };
112
+ }
113
+ };
114
+ GoGoScopeModule = __decorateClass([
115
+ Module({})
116
+ ], GoGoScopeModule);
117
+
118
+ export { GOGO_MODULE_OPTIONS, GOGO_REQUEST_SCOPE, GOGO_ROOT_SCOPE, GoGoRequestScopeService, GoGoScopeModule, GoGoScopeService, Task };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@go-go-scope/adapter-nestjs",
3
+ "version": "2.0.0",
4
+ "description": "NestJS adapter for go-go-scope - DI integration with @Task decorator",
5
+ "type": "module",
6
+ "main": "./dist/index.mjs",
7
+ "types": "./dist/index.d.mts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.mts",
11
+ "default": "./dist/index.mjs"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "build": "pkgroll --clean-dist",
16
+ "lint": "biome check --write src/",
17
+ "test": "vitest run --passWithNoTests",
18
+ "clean": "rm -rf dist"
19
+ },
20
+ "keywords": [
21
+ "nestjs",
22
+ "adapter",
23
+ "go-go-scope",
24
+ "structured-concurrency",
25
+ "decorator"
26
+ ],
27
+ "engines": {
28
+ "node": ">=24.0.0"
29
+ },
30
+ "author": "thelinuxlich",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/thelinuxlich/go-go-scope.git",
35
+ "directory": "packages/adapter-nestjs"
36
+ },
37
+ "files": [
38
+ "dist/",
39
+ "README.md"
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "dependencies": {
45
+ "go-go-scope": "workspace:*"
46
+ },
47
+ "peerDependencies": {
48
+ "@nestjs/common": "^11.1.14"
49
+ },
50
+ "devDependencies": {
51
+ "@biomejs/biome": "^2.4.4",
52
+ "@nestjs/common": "^11.1.14",
53
+ "@types/node": "^24",
54
+ "pkgroll": "^2.26.3",
55
+ "typescript": "^5.9.3",
56
+ "vitest": "^4.0.18"
57
+ }
58
+ }