@nestbridge/core 2.6.1 → 3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @nestbridge/core
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 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.
8
+
9
+ 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.
10
+
11
+ 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.
12
+
13
+ Breaking changes to `@nestbridge/core`:
14
+
15
+ - `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.
16
+ - `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.
17
+
18
+ `@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.
19
+
3
20
  ## 2.6.1
4
21
 
5
22
  No changes in this release.
package/README.md CHANGED
@@ -46,6 +46,19 @@ 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 its methods get
56
+ a `responseWrapper` of `unknown`. `interceptorFiles` lists the files that took
57
+ part in that detection, so a watcher knows what to re-analyze. Message handlers
58
+ and GraphQL resolvers are not wrapped yet. See the [repository
59
+ README](../../README.md#auto-detected-response-shaping-interceptors) for the
60
+ full rules.
61
+
49
62
  ```ts
50
63
  import { analyzeControllers } from '@nestbridge/core';
51
64
 
@@ -74,7 +87,7 @@ const { resolvers, diagnostics } = analyzeResolvers([
74
87
  ]);
75
88
  ```
76
89
 
77
- ### `generateControllerDeclaration(definition, outputFilePath, responseWrapper?)`
90
+ ### `generateControllerDeclaration(definition, outputFilePath)`
78
91
 
79
92
  Turns a `ControllerDefinition` or a `ResolverDefinition` into the text of a
80
93
  `.d.ts` file — both share the same `{ name, sourceFile, methods }` shape, so
@@ -83,10 +96,10 @@ method using a type-only reference back to the real controller or resolver
83
96
  (`Parameters<__ServerX['method']>` / `RemoteResult<...>`, or
84
97
  `RemoteObservableResult<...>` / `RemoteStreamResult<...>` for observable and
85
98
  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'`.
99
+ class's own type — never re-derived from the AST. A method with a
100
+ `responseWrapper` (see `analyzeControllers`) is declared as an
101
+ `AbortablePromise` of its own wrapped shape instead; methods without one are
102
+ unchanged. The helper types are imported from `'nestbridge'`.
90
103
 
91
104
  ```ts
92
105
  import { generateControllerDeclaration } from '@nestbridge/core';
@@ -94,23 +107,6 @@ import { generateControllerDeclaration } from '@nestbridge/core';
94
107
  const source = generateControllerDeclaration(controller, outputPath);
95
108
  ```
96
109
 
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
110
  ### `detectErrorResponseShape(bootstrapFilePath)`
115
111
 
116
112
  Statically detects a single global `@Catch(...)` exception filter —
@@ -184,7 +180,7 @@ const routed = applyApplicationRouting(controllers, {
184
180
  ### `generateErrorBodyDeclaration(errorBodyTypeSource)`
185
181
 
186
182
  Turns the `errorBodyTypeSource` from `detectErrorResponseShape` into the text
187
- of the fixed `nestbridge-error-body.d.ts` declaration file, exporting a
183
+ of the fixed `error-body.d.ts` declaration file, exporting a
188
184
  `NestBridgeErrorBody` type.
189
185
 
190
186
  ```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";
30
30
  interface NestBridgeDiagnostic {
31
31
  code: NestBridgeDiagnosticCode;
32
32
  title: string;
@@ -53,6 +53,12 @@ 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;
56
62
  }
57
63
  //#endregion
58
64
  //#region src/diagnostics/format-diagnostic.d.ts
@@ -87,6 +93,15 @@ type ControllerParameterDefinition = (ControllerParameterBase & {
87
93
  //#region src/models/response-kind.d.ts
88
94
  type ResponseKind = "json" | "observable" | "stream";
89
95
  //#endregion
96
+ //#region src/models/response-wrapper-detection.d.ts
97
+ /**
98
+ * `wrappedResultTypeSource` refers to the awaited server result as `Awaited<Result>`. A source
99
+ * without that reference, such as `unknown`, does not depend on the server result at all.
100
+ */
101
+ interface ResponseWrapperDetection {
102
+ wrappedResultTypeSource: string;
103
+ }
104
+ //#endregion
90
105
  //#region src/models/controller-method-definition.d.ts
91
106
  interface ControllerMethodDefinition {
92
107
  name: string;
@@ -104,6 +119,11 @@ interface ControllerMethodDefinition {
104
119
  */
105
120
  serverOnlyParameterIndexes: number[];
106
121
  responseKind: ResponseKind;
122
+ /**
123
+ * The shape the interceptors applying to this JSON route give its response. Absent when none of
124
+ * them changes the shape.
125
+ */
126
+ responseWrapper?: ResponseWrapperDetection;
107
127
  line: number;
108
128
  column: number;
109
129
  }
@@ -144,6 +164,12 @@ interface ControllerDefinition {
144
164
  interface ControllerAnalysisResult {
145
165
  controllers: ControllerDefinition[];
146
166
  diagnostics: NestBridgeDiagnostic[];
167
+ /**
168
+ * Source files other than the controllers whose content decided the interceptor analysis:
169
+ * the bootstrap file, the modules scanned for `APP_INTERCEPTOR` providers and the interceptor
170
+ * classes. A change to any of them can change the declared response types.
171
+ */
172
+ interceptorFiles: string[];
147
173
  }
148
174
  //#endregion
149
175
  //#region src/models/error-response-shape-detection.d.ts
@@ -192,11 +218,6 @@ interface ResolverAnalysisResult {
192
218
  diagnostics: NestBridgeDiagnostic[];
193
219
  }
194
220
  //#endregion
195
- //#region src/models/response-wrapper-detection.d.ts
196
- interface ResponseWrapperDetection {
197
- wrappedResultTypeSource: string;
198
- }
199
- //#endregion
200
221
  //#region src/analysis/analyze-controllers.d.ts
201
222
  export declare const analyzeControllers: (filePaths: readonly string[], options?: ControllerAnalysisOptions) => ControllerAnalysisResult;
202
223
  //#endregion
@@ -212,14 +233,6 @@ export declare const detectErrorResponseShape: (bootstrapFilePath: string) => Er
212
233
  //#region src/analysis/detect-global-prefix.d.ts
213
234
  export declare const detectGlobalPrefix: (bootstrapFilePath: string) => BootstrapDetection<GlobalPrefix>;
214
235
  //#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
236
  //#region src/analysis/detect-versioning.d.ts
224
237
  export declare const detectVersioning: (bootstrapFilePath: string) => BootstrapDetection<UriVersioning>;
225
238
  //#endregion
@@ -231,6 +244,7 @@ interface ClientDeclarationParameter {
231
244
  interface ClientDeclarationMethod {
232
245
  name: string;
233
246
  responseKind?: ResponseKind;
247
+ responseWrapper?: ResponseWrapperDetection;
234
248
  parameters?: readonly ClientDeclarationParameter[];
235
249
  serverOnlyParameterIndexes?: readonly number[];
236
250
  }
@@ -241,7 +255,7 @@ interface ClientDeclarationSource {
241
255
  methods: readonly ClientDeclarationMethod[];
242
256
  messageHandlers?: readonly ClientDeclarationMessageHandler[];
243
257
  }
244
- export declare const generateControllerDeclaration: (controller: ClientDeclarationSource, outputFilePath: string, responseWrapper?: ResponseWrapperDetection) => string;
258
+ export declare const generateControllerDeclaration: (controller: ClientDeclarationSource, outputFilePath: string) => string;
245
259
  //#endregion
246
260
  //#region src/declarations/generate-error-body-declaration.d.ts
247
261
  export declare const generateErrorBodyDeclaration: (errorBodyTypeSource: string) => string;