@koderlabs/tasks-sdk-nestjs 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/README.md +174 -5
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,9 +1,178 @@
1
- # Proprietary Software
1
+ # @koderlabs/tasks-sdk-nestjs
2
2
 
3
- KoderLabs proprietary. All rights reserved.
3
+ > Runtime: Node.js with NestJS 9+. Server-only. Does not use the core `@koderlabs/tasks-sdk` browser client — uses a server-side management key + direct `fetch` to the InstantTasks ingest API.
4
4
 
5
- See the bundled `LICENSE` for terms. No grant of any rights, express or
6
- implied, is conferred by access to or possession of this package. A
7
- separate signed written licence from KoderLabs is required for any use.
5
+ Production-grade observability for NestJS apps: errors, request context,
6
+ breadcrumbs, spans, HTTP tracing, console capture, TypeORM tracing.
7
+
8
+ ## Quick start
9
+
10
+ ```ts
11
+ import { InstantTasksModule, InstantTasksReportInterceptor } from '@koderlabs/tasks-sdk-nestjs';
12
+ import { APP_INTERCEPTOR } from '@nestjs/core';
13
+
14
+ @Module({
15
+ imports: [
16
+ InstantTasksModule.forRoot({
17
+ apiUrl: process.env.INSTANTTASKS_API_URL!,
18
+ projectId: process.env.INSTANTTASKS_PROJECT_ID!,
19
+ managementKey: process.env.INSTANTTASKS_MANAGEMENT_KEY!,
20
+ environment: process.env.NODE_ENV,
21
+ release: process.env.GIT_SHA,
22
+
23
+ // Opt-in observability — all default false
24
+ captureRequestContext: true,
25
+ captureHttp: true,
26
+ captureConsole: true,
27
+ captureSpans: true,
28
+ captureTypeOrm: true,
29
+ dataSource,
30
+ }),
31
+ ],
32
+ providers: [
33
+ { provide: APP_INTERCEPTOR, useClass: InstantTasksReportInterceptor },
34
+ ],
35
+ })
36
+ export class AppModule {}
37
+ ```
38
+
39
+ ## Options
40
+
41
+ | Option | Type | Default | Purpose |
42
+ |---|---|---|---|
43
+ | `apiUrl` | `string` | — | InstantTasks base URL (no trailing slash; `/api/v1` stripped) |
44
+ | `projectId` | `string` | — | Project UUID |
45
+ | `managementKey` | `string` | — | `sk_*` server key |
46
+ | `environment` | `string` | `process.env.NODE_ENV` | Free-form environment tag |
47
+ | `release` | `string` | `'prod'` | Release identifier (git sha) |
48
+ | `beforeSend` | `(e) => e \| null` | — | Last chance to mutate/drop |
49
+ | `dryRun` | `boolean` | `false` | Skip network — useful in tests |
50
+ | `debug` | `boolean` | `false` | Verbose logs |
51
+ | `captureRequestContext` | `boolean` | `false` | Install ALS middleware capturing method/route/url/ip/userAgent/userId per request |
52
+ | `captureHttp` | `boolean` | `false` | Patch `fetch` + `http.request` + `https.request`, emit `http` breadcrumbs |
53
+ | `captureConsole` | `boolean` | `false` | Patch `console.log/warn/error`, emit `console` breadcrumbs (output preserved; Nest Logger messages skipped) |
54
+ | `captureSpans` | `boolean` | `false` | Wrap every controller call in a `http.<method>.<route>` span attached to the request context |
55
+ | `captureTypeOrm` | `boolean` | `false` | Install a TypeORM `Logger` that emits `db` breadcrumbs + `db.query` spans |
56
+ | `dataSource` | `DataSource` | — | Required when `captureTypeOrm: true` |
57
+ | `ignoreUrls` | `Array<string \| RegExp>` | `[]` | URLs not to record as HTTP breadcrumbs (own ingest is added automatically) |
58
+ | `maxBreadcrumbs` | `number` | `50` | Per-request ring capacity |
59
+
60
+ ## How it composes
61
+
62
+ ```
63
+ HTTP request
64
+
65
+ RequestContextMiddleware → starts AsyncLocalStorage scope with empty ring
66
+
67
+ SpanInterceptor → records http.<method>.<route> span on success/error
68
+
69
+ your controller
70
+ ├─ fetch() → http breadcrumb (sanitized URL, status, durationMs)
71
+ ├─ console.log() → console breadcrumb (output still goes to stdout)
72
+ └─ TypeORM query → db breadcrumb + db.query span
73
+
74
+ ReportInterceptor (5xx) → Reporter.reportError() reads ALS, attaches:
75
+ { request, breadcrumbs, spans } → POST /api/v1/sdk/events
76
+ ```
77
+
78
+ Out-of-band errors (`uncaughtException`, `unhandledrejection`) still report,
79
+ but without request context — they fire outside any HTTP scope.
80
+
81
+ ## Manual breadcrumbs
82
+
83
+ ```ts
84
+ import { leaveBreadcrumb } from '@koderlabs/tasks-sdk-nestjs';
85
+
86
+ @Injectable()
87
+ export class MyService {
88
+ doThing() {
89
+ leaveBreadcrumb({ category: 'custom', message: 'started thing', data: { id: 42 } });
90
+ }
91
+ }
92
+ ```
93
+
94
+ `leaveBreadcrumb()` is a no-op outside an HTTP request scope.
95
+
96
+ ## Gotchas
97
+
98
+ ### AsyncLocalStorage doesn't follow detached callbacks
99
+
100
+ Code that escapes the request scope loses the ring:
101
+
102
+ - Bull / BullMQ processors run on a separate event loop tick — wrap your
103
+ handler in `requestContextStorage.run(ring, ...)` if you need breadcrumbs.
104
+ - `setTimeout` scheduled inside a request DOES inherit the scope.
105
+ - Custom EventEmitters that fire after the request response is sent still
106
+ see the scope (memory-leak risk if you hold the ring forever).
107
+
108
+ ### Console capture skips Nest Logger output
109
+
110
+ We pattern-match `[Nest]` / `NestApplication` / `RoutesResolver` in the
111
+ first argument and skip those messages. This prevents feedback loops when
112
+ `AllExceptionsFilter` logs via Nest Logger which eventually hits
113
+ `console.error`.
114
+
115
+ ### TypeORM Logger is exclusive
116
+
117
+ TypeORM allows exactly one logger per DataSource. `captureTypeOrm: true`
118
+ replaces whatever logger the host configured. If you have a custom logger,
119
+ wrap it instead:
120
+
121
+ ```ts
122
+ import { InstantTasksTypeOrmLogger } from '@koderlabs/tasks-sdk-nestjs';
123
+
124
+ class CombinedLogger extends InstantTasksTypeOrmLogger {
125
+ override logQuery(sql, params) {
126
+ super.logQuery(sql, params);
127
+ yourExistingLogger.logQuery(sql, params);
128
+ }
129
+ }
130
+ dataSource.setOptions({ logger: new CombinedLogger() });
131
+ ```
132
+
133
+ ### HTTP tracing patches globals
134
+
135
+ Patches `globalThis.fetch` and `require('http')`/`https`.`request`.
136
+ Idempotent via a Symbol sentinel — calling `installHttpTracing()` twice is
137
+ safe. Restores on `onModuleDestroy`.
138
+
139
+ Sanitization mirrors `@koderlabs/tasks-sdk-web-network`:
140
+ - `Authorization`, `Cookie`, `Set-Cookie`, `X-Auth-Token`,
141
+ `Proxy-Authorization` headers → `[Filtered]`
142
+ - `?token=…`, `?access_token=…`, `?api_key=…`, `?secret=…` query values → `[Filtered]`
143
+
144
+ The SDK's own ingest endpoint (`/api/v1/sdk/`) is added to `ignoreUrls`
145
+ automatically to avoid recursion.
146
+
147
+ ### Span interceptor only runs in HTTP context
148
+
149
+ Skipped for WebSocket, GraphQL, microservice transports. If you need spans
150
+ for those, wire your own interceptor and call `getCurrentContext()?.addSpan()`.
151
+
152
+ ## Tests
153
+
154
+ ```bash
155
+ pnpm --filter @koderlabs/tasks-sdk-nestjs test
156
+ ```
157
+
158
+ 19 tests cover: breadcrumb ring isolation, request-context middleware,
159
+ fetch tracing, console capture, span interceptor, end-to-end integration
160
+ with all flags wired.
161
+
162
+ ## Default-off principle
163
+
164
+ Every new capability defaults to `false`. With no flags set, behaviour is
165
+ identical to the MVP — only `uncaughtException`, `unhandledRejection`, and
166
+ 5xx HTTP responses get reported. Enable features incrementally.
167
+
168
+ ---
169
+
170
+ ## Suite overview
171
+
172
+ Full SDK suite map + platform availability matrix: [docs/sdk/overview.md](https://github.com/jawaidgadiwala/instant-tasks/blob/main/docs/sdk/overview.md).
173
+
174
+ ## License
175
+
176
+ KoderLabs proprietary. See [`LICENSE`](./LICENSE) for terms. Use of this package requires a separate signed written agreement with KoderLabs; access alone confers no rights.
8
177
 
9
178
  Licensing inquiries: jawaidgadiwala@gmail.com
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koderlabs/tasks-sdk-nestjs",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "NestJS module for InstantTasks — reporter + interceptor + breadcrumbs + req context + http/console/spans/typeorm tracing.",
5
5
  "keywords": [
6
6
  "instanttasks",
@@ -58,7 +58,7 @@
58
58
  "node": ">=20"
59
59
  },
60
60
  "publishConfig": {
61
- "access": "restricted"
61
+ "access": "public"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "tsup",