@nestbridge/core 2.6.1 → 3.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @nestbridge/core
2
2
 
3
+ ## 3.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 531bf3e: Declare the response shape of any interceptor in an optional `nestbridge.d.ts` or `nestbridge-env.d.ts` at the server `root` (equivalent names, both can be used together), and keep the types of the interceptors around an unsupported one.
8
+
9
+ Export one generic type alias per interceptor, named after the interceptor class (`export type ZodSerializerInterceptor<T> = T;`). `T` is the value coming from inside; an alias with no type parameter is a fixed shape that discards what is inside it. A matching alias wins over local analysis, the package shape-neutral rule and the unsupported diagnostic, and the interceptor is matched by its exported class name even when imported under another name. Only exported `type` aliases with zero or one type parameter are accepted; anything else exported from a file, `declare global` and `declare module` are reported as the new `invalid-interceptor-declaration` diagnostic. NestBridge writes `<outputDir>/<package name>/interceptors.d.ts`, re-exporting every alias with one line per file, and the controller declarations import the aliases they use from it. A name exported by both files is reported and used from neither. Creating, editing or deleting either file regenerates the declarations in watch mode.
10
+
11
+ An unsupported interceptor still reports its diagnostic, but it now contributes `unknown` to its route instead of turning the whole route into `AbortablePromise<unknown>`: the interceptors outside it still apply, so a declared `TransformInterceptor<T> = { data: T; meta: ... }` outside an unsupported interceptor gives `{ data: unknown; meta: ... }`. A route whose outermost result is `unknown` stays `AbortablePromise<unknown>`, and so does a route with a registration that cannot be placed in the chain (a spread `APP_INTERCEPTOR` provider, a `@UseInterceptors` argument that is not a class or `new X()`).
12
+
13
+ `@nestbridge/core` adds `analyzeControllers` option `interceptorDeclarationFilePaths`, the result field `interceptorDeclarations`, `generateInterceptorsDeclaration` (takes the declarations and the output path), the `INTERCEPTOR_DECLARATION_FILE_NAMES` constant and an optional `declaredInterceptors` list on `ResponseWrapperDetection`; `generateControllerDeclaration` takes an options argument with `interceptorsDeclarationFilePath`.
14
+
15
+ ### Patch Changes
16
+
17
+ - 531bf3e: Interceptors that come from a package are now treated as shape-neutral instead of failing the build with `unsupported-interceptor`.
18
+
19
+ An interceptor class whose import resolves into `node_modules`, or to a declaration or JavaScript file, such as `ZodSerializerInterceptor` from `nestjs-zod` or Nest's own `ClassSerializerInterceptor` and `CacheInterceptor`, produces no diagnostic and leaves the route's type as it would be without it, in every registration form (`APP_INTERCEPTOR`, `useGlobalInterceptors(new X(...))`, `@UseInterceptors`). A package interceptor that really does change the response shape is not reflected in the types. Local interceptors and workspace libraries that resolve to TypeScript source are analyzed as before, and an import that does not resolve is still reported.
20
+
21
+ ## 3.0.0
22
+
23
+ ### Minor Changes
24
+
25
+ - b5009b4: Response-shaping interceptors are now detected per route instead of as one app-wide wrapper, and anything that cannot be read is reported instead of silently ignored.
26
+
27
+ Each JSON route gets its own interceptor chain, outermost first: `APP_INTERCEPTOR` providers (`useClass` only, read from the root module and the modules it imports), then `app.useGlobalInterceptors(new X())`, then the controller's `@UseInterceptors`, then the method's `@UseInterceptors`. `intercept()` may hold any statements before its final `return`; the returned `next.handle()` may be piped through any number of operators. `map` operators compose in order, `tap`, `timeout` and `finalize` are shape-neutral, and a bare `return next.handle()` changes nothing. Shorthand properties (`{ data }`) and `-`, `*`, `/`, `%` arithmetic (typed `number`) are understood in `map` bodies. Message handlers and GraphQL resolvers are not wrapped yet.
28
+
29
+ An interceptor that cannot be analyzed (another operator, a `useFactory` / `useValue` / `useExisting` / spread provider, a class from a package, an unreadable `map` body, a `@UseInterceptors` argument that is not a class or `new X()`) is a new `unsupported-interceptor` diagnostic, reported once per interceptor at its source location. The JSON routes under it are declared as `AbortablePromise<unknown>`. A one-shot build fails on the diagnostic; watch mode warns.
30
+
31
+ Breaking changes to `@nestbridge/core`:
32
+
33
+ - `detectResponseWrapper` is removed. `analyzeControllers(files, { bootstrapFilePath })` now attaches an optional `responseWrapper` to each JSON method of the result and returns `interceptorFiles`, the files that took part in the detection.
34
+ - `generateControllerDeclaration(definition, outputFilePath)` no longer takes a response wrapper argument; it reads each method's own `responseWrapper`. A declaration can now hold several `__NestBridgeWrapped` helper aliases (`__NestBridgeWrapped`, `__NestBridgeWrapped2`, ...), one per distinct shape. Controllers without a response-shaping interceptor produce the same output as before.
35
+
36
+ `@nestbridge/unplugin` passes the bootstrap file to the controller analysis, re-analyzes when a file that contributed to interceptor detection changes, and registers those files and the bootstrap file with the bundler's watcher.
37
+
3
38
  ## 2.6.1
4
39
 
5
40
  No changes in this release.
package/README.md CHANGED
@@ -46,6 +46,23 @@ statically read `pattern`, and `payloadParameterIndex`. A pattern that is not
46
46
  statically known gives an `unsupported-message-pattern` diagnostic. Without
47
47
  the option, `messageHandlers` is empty.
48
48
 
49
+ With `analyzeControllers(filePaths, { bootstrapFilePath })`, each JSON method
50
+ also carries an optional `responseWrapper`: the response shape given by the
51
+ interceptors that apply to it (global `APP_INTERCEPTOR` providers, then
52
+ `useGlobalInterceptors(...)` in the bootstrap file, then the controller's and
53
+ the method's `@UseInterceptors`, outermost first). A method no interceptor
54
+ reshapes has no `responseWrapper`. An interceptor that cannot be read gives an
55
+ `unsupported-interceptor` diagnostic, once per interceptor, and it contributes
56
+ `unknown` to the chain (so a route whose outermost interceptor is unsupported gets a
57
+ `responseWrapper` of `unknown`). Interceptors named by an exported type alias in
58
+ the files given as `interceptorDeclarationFilePaths` (`nestbridge.d.ts` and
59
+ `nestbridge-env.d.ts`, read in that order) use that
60
+ alias instead, and the result lists them as `interceptorDeclarations`. `interceptorFiles` lists the files that took
61
+ part in that detection, so a watcher knows what to re-analyze. Message handlers
62
+ and GraphQL resolvers are not wrapped yet. See the [repository
63
+ README](../../README.md#auto-detected-response-shaping-interceptors) for the
64
+ full rules.
65
+
49
66
  ```ts
50
67
  import { analyzeControllers } from '@nestbridge/core';
51
68
 
@@ -74,7 +91,7 @@ const { resolvers, diagnostics } = analyzeResolvers([
74
91
  ]);
75
92
  ```
76
93
 
77
- ### `generateControllerDeclaration(definition, outputFilePath, responseWrapper?)`
94
+ ### `generateControllerDeclaration(definition, outputFilePath)`
78
95
 
79
96
  Turns a `ControllerDefinition` or a `ResolverDefinition` into the text of a
80
97
  `.d.ts` file — both share the same `{ name, sourceFile, methods }` shape, so
@@ -83,10 +100,10 @@ method using a type-only reference back to the real controller or resolver
83
100
  (`Parameters<__ServerX['method']>` / `RemoteResult<...>`, or
84
101
  `RemoteObservableResult<...>` / `RemoteStreamResult<...>` for observable and
85
102
  streamed methods), so argument and return types are projected from the real
86
- class's own type — never re-derived from the AST. Pass the result of
87
- `detectResponseWrapper` as `responseWrapper` to wrap the JSON-returning
88
- methods in the detected response shape (an `AbortablePromise` of it). The helper
89
- types are imported from `'nestbridge'`.
103
+ class's own type — never re-derived from the AST. A method with a
104
+ `responseWrapper` (see `analyzeControllers`) is declared as an
105
+ `AbortablePromise` of its own wrapped shape instead; methods without one are
106
+ unchanged. The helper types are imported from `'nestbridge'`.
90
107
 
91
108
  ```ts
92
109
  import { generateControllerDeclaration } from '@nestbridge/core';
@@ -94,23 +111,6 @@ import { generateControllerDeclaration } from '@nestbridge/core';
94
111
  const source = generateControllerDeclaration(controller, outputPath);
95
112
  ```
96
113
 
97
- ### `detectResponseWrapper(bootstrapFilePath)`
98
-
99
- Statically detects `app.useGlobalInterceptors(...)` registrations in the
100
- bootstrap file whose `intercept()` method reshapes every response through a
101
- single recognized `next.handle().pipe(map(...))` call, composing multiple
102
- chained interceptors in registration order, and returns the resulting
103
- wrapped-response type source. Returns `undefined` when no interceptor is
104
- registered, or the shape isn't exactly recognized — see the [repository
105
- README](../../README.md#auto-detected-global-response-wrapper) for the full
106
- rules.
107
-
108
- ```ts
109
- import { detectResponseWrapper } from '@nestbridge/core';
110
-
111
- const responseWrapper = detectResponseWrapper('src/main.ts');
112
- ```
113
-
114
114
  ### `detectErrorResponseShape(bootstrapFilePath)`
115
115
 
116
116
  Statically detects a single global `@Catch(...)` exception filter —
@@ -184,7 +184,7 @@ const routed = applyApplicationRouting(controllers, {
184
184
  ### `generateErrorBodyDeclaration(errorBodyTypeSource)`
185
185
 
186
186
  Turns the `errorBodyTypeSource` from `detectErrorResponseShape` into the text
187
- of the fixed `nestbridge-error-body.d.ts` declaration file, exporting a
187
+ of the fixed `error-body.d.ts` declaration file, exporting a
188
188
  `NestBridgeErrorBody` type.
189
189
 
190
190
  ```ts
@@ -26,7 +26,7 @@ interface ApplicationRouting {
26
26
  }
27
27
  //#endregion
28
28
  //#region src/diagnostics/nestbridge-diagnostic.d.ts
29
- type NestBridgeDiagnosticCode = "unsupported-route" | "unsupported-parameter" | "unsupported-argument" | "unsupported-return-type" | "circular-type" | "unsupported-global-prefix" | "unsupported-versioning" | "unsupported-message-pattern";
29
+ type NestBridgeDiagnosticCode = "unsupported-route" | "unsupported-parameter" | "unsupported-argument" | "unsupported-return-type" | "circular-type" | "unsupported-global-prefix" | "unsupported-versioning" | "unsupported-message-pattern" | "unsupported-interceptor" | "invalid-interceptor-declaration";
30
30
  interface NestBridgeDiagnostic {
31
31
  code: NestBridgeDiagnosticCode;
32
32
  title: string;
@@ -53,6 +53,18 @@ interface ControllerAnalysisOptions {
53
53
  * that only call HTTP routes never receive microservice code.
54
54
  */
55
55
  messagePatterns?: boolean;
56
+ /**
57
+ * The application bootstrap file. Global interceptors registered through
58
+ * `useGlobalInterceptors()` or `APP_INTERCEPTOR` providers of the modules it bootstraps are read
59
+ * from it. Without it, only `@UseInterceptors()` on controllers and routes is analyzed.
60
+ */
61
+ bootstrapFilePath?: string;
62
+ /**
63
+ * The server's interceptor declaration files (`INTERCEPTOR_DECLARATION_FILE_NAMES` under its
64
+ * root), in reading order. They declare the response shape of interceptors by name. Each one is
65
+ * read when it exists; a missing file changes nothing.
66
+ */
67
+ interceptorDeclarationFilePaths?: readonly string[];
56
68
  }
57
69
  //#endregion
58
70
  //#region src/diagnostics/format-diagnostic.d.ts
@@ -87,6 +99,20 @@ type ControllerParameterDefinition = (ControllerParameterBase & {
87
99
  //#region src/models/response-kind.d.ts
88
100
  type ResponseKind = "json" | "observable" | "stream";
89
101
  //#endregion
102
+ //#region src/models/response-wrapper-detection.d.ts
103
+ /**
104
+ * `wrappedResultTypeSource` refers to the awaited server result as `Awaited<Result>`. A source
105
+ * without that reference, such as `unknown`, does not depend on the server result at all.
106
+ */
107
+ interface ResponseWrapperDetection {
108
+ wrappedResultTypeSource: string;
109
+ /**
110
+ * The interceptor declarations the source refers to, each as `DECLARED_INTERCEPTOR_PREFIX`
111
+ * followed by its name. Absent when the source refers to none.
112
+ */
113
+ declaredInterceptors?: readonly string[];
114
+ }
115
+ //#endregion
90
116
  //#region src/models/controller-method-definition.d.ts
91
117
  interface ControllerMethodDefinition {
92
118
  name: string;
@@ -104,6 +130,11 @@ interface ControllerMethodDefinition {
104
130
  */
105
131
  serverOnlyParameterIndexes: number[];
106
132
  responseKind: ResponseKind;
133
+ /**
134
+ * The shape the interceptors applying to this JSON route give its response. Absent when none of
135
+ * them changes the shape.
136
+ */
137
+ responseWrapper?: ResponseWrapperDetection;
107
138
  line: number;
108
139
  column: number;
109
140
  }
@@ -140,10 +171,30 @@ interface ControllerDefinition {
140
171
  messageHandlers: MessageHandlerDefinition[];
141
172
  }
142
173
  //#endregion
174
+ //#region src/models/interceptor-declaration.d.ts
175
+ /**
176
+ * An exported type alias of one of the server's interceptor declaration files, named after an interceptor class.
177
+ * `typeParameterCount` is 1 when the alias receives the inner shape as `T`, and 0 when it is a
178
+ * fixed shape that ignores what is inside it.
179
+ */
180
+ interface InterceptorDeclaration {
181
+ name: string;
182
+ filePath: string;
183
+ typeParameterCount: 0 | 1;
184
+ }
185
+ //#endregion
143
186
  //#region src/models/controller-analysis-result.d.ts
144
187
  interface ControllerAnalysisResult {
145
188
  controllers: ControllerDefinition[];
146
189
  diagnostics: NestBridgeDiagnostic[];
190
+ /**
191
+ * Source files other than the controllers whose content decided the interceptor analysis:
192
+ * the bootstrap file, the modules scanned for `APP_INTERCEPTOR` providers and the interceptor
193
+ * classes. A change to any of them can change the declared response types.
194
+ */
195
+ interceptorFiles: string[];
196
+ /** The valid exported aliases of the interceptor declarations file, in source order. */
197
+ interceptorDeclarations: InterceptorDeclaration[];
147
198
  }
148
199
  //#endregion
149
200
  //#region src/models/error-response-shape-detection.d.ts
@@ -192,11 +243,6 @@ interface ResolverAnalysisResult {
192
243
  diagnostics: NestBridgeDiagnostic[];
193
244
  }
194
245
  //#endregion
195
- //#region src/models/response-wrapper-detection.d.ts
196
- interface ResponseWrapperDetection {
197
- wrappedResultTypeSource: string;
198
- }
199
- //#endregion
200
246
  //#region src/analysis/analyze-controllers.d.ts
201
247
  export declare const analyzeControllers: (filePaths: readonly string[], options?: ControllerAnalysisOptions) => ControllerAnalysisResult;
202
248
  //#endregion
@@ -212,17 +258,21 @@ export declare const detectErrorResponseShape: (bootstrapFilePath: string) => Er
212
258
  //#region src/analysis/detect-global-prefix.d.ts
213
259
  export declare const detectGlobalPrefix: (bootstrapFilePath: string) => BootstrapDetection<GlobalPrefix>;
214
260
  //#endregion
215
- //#region src/analysis/detect-response-wrapper.d.ts
216
- /**
217
- * Interceptors registered first wrap those registered after them, so the
218
- * composed type is built from the last-registered interceptor (closest to the
219
- * raw handler result) outward to the first-registered one.
220
- */
221
- export declare const detectResponseWrapper: (bootstrapFilePath: string) => ResponseWrapperDetection | undefined;
222
- //#endregion
223
261
  //#region src/analysis/detect-versioning.d.ts
224
262
  export declare const detectVersioning: (bootstrapFilePath: string) => BootstrapDetection<UriVersioning>;
225
263
  //#endregion
264
+ //#region src/analysis/interceptor-declaration-file-names.d.ts
265
+ export declare const INTERCEPTOR_DECLARATION_FILE_NAMES: readonly ["nestbridge.d.ts", "nestbridge-env.d.ts"];
266
+ //#endregion
267
+ //#region src/declarations/controller-declaration-options.d.ts
268
+ interface ControllerDeclarationOptions {
269
+ /**
270
+ * The generated `interceptors.d.ts` the declared interceptor aliases are imported from. Needed
271
+ * only by controllers whose routes use a declared interceptor.
272
+ */
273
+ interceptorsDeclarationFilePath?: string;
274
+ }
275
+ //#endregion
226
276
  //#region src/declarations/generate-controller-declaration.d.ts
227
277
  interface ClientDeclarationParameter {
228
278
  index: number;
@@ -231,6 +281,7 @@ interface ClientDeclarationParameter {
231
281
  interface ClientDeclarationMethod {
232
282
  name: string;
233
283
  responseKind?: ResponseKind;
284
+ responseWrapper?: ResponseWrapperDetection;
234
285
  parameters?: readonly ClientDeclarationParameter[];
235
286
  serverOnlyParameterIndexes?: readonly number[];
236
287
  }
@@ -241,13 +292,20 @@ interface ClientDeclarationSource {
241
292
  methods: readonly ClientDeclarationMethod[];
242
293
  messageHandlers?: readonly ClientDeclarationMessageHandler[];
243
294
  }
244
- export declare const generateControllerDeclaration: (controller: ClientDeclarationSource, outputFilePath: string, responseWrapper?: ResponseWrapperDetection) => string;
295
+ export declare const generateControllerDeclaration: (controller: ClientDeclarationSource, outputFilePath: string, options?: ControllerDeclarationOptions) => string;
245
296
  //#endregion
246
297
  //#region src/declarations/generate-error-body-declaration.d.ts
247
298
  export declare const generateErrorBodyDeclaration: (errorBodyTypeSource: string) => string;
248
299
  //#endregion
300
+ //#region src/declarations/generate-interceptors-declaration.d.ts
301
+ /**
302
+ * One re-export line per declaration file, in the order the files were read. A file that
303
+ * contributes no alias gets no line.
304
+ */
305
+ export declare const generateInterceptorsDeclaration: (declarations: readonly InterceptorDeclaration[], outputFilePath: string) => string;
306
+ //#endregion
249
307
  //#region src/version.d.ts
250
308
  export declare const VERSION: string;
251
309
  //#endregion
252
- export type { ApplicationRouting, BootstrapDetection, ControllerAnalysisOptions, ControllerAnalysisResult, ControllerDefinition, ControllerMethodDefinition, ControllerParameterDefinition, ErrorResponseShapeDetection, GlobalPrefix, GlobalPrefixExclusion, GraphqlOperationKind, HttpMethod, MessageHandlerDefinition, MessageKind, MessagePattern, NestBridgeDiagnostic, NestBridgeDiagnosticCode, ParameterSourceKind, ResolverAnalysisResult, ResolverArgumentDefinition, ResolverDefinition, ResolverMethodDefinition, ResponseKind, ResponseWrapperDetection, SelectionField, UriVersioning };
310
+ export type { ApplicationRouting, BootstrapDetection, ControllerAnalysisOptions, ControllerAnalysisResult, ControllerDeclarationOptions, ControllerDefinition, ControllerMethodDefinition, ControllerParameterDefinition, ErrorResponseShapeDetection, GlobalPrefix, GlobalPrefixExclusion, GraphqlOperationKind, HttpMethod, InterceptorDeclaration, MessageHandlerDefinition, MessageKind, MessagePattern, NestBridgeDiagnostic, NestBridgeDiagnosticCode, ParameterSourceKind, ResolverAnalysisResult, ResolverArgumentDefinition, ResolverDefinition, ResolverMethodDefinition, ResponseKind, ResponseWrapperDetection, SelectionField, UriVersioning };
253
311
  //# sourceMappingURL=nestbridge.core.d.ts.map