@nestbridge/core 2.1.3 → 2.2.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,21 @@
1
1
  # @nestbridge/core
2
2
 
3
+ ## 2.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 98349c7: One client can now use controllers from several NestJS apps. `nestBridge()` accepts an array of options, one entry per app, and each entry takes its own `name`, `root`, `controllers`, `resolvers`, `baseURL`, `bootstrap`, `globalPrefix` and `versioning`. A named app writes its declarations to `<outputDir>/<name>/`, detects its own response wrapper and error body from its own bootstrap file, and tags every generated request with its `name` as the `service`. A single object still works as before.
8
+
9
+ The runtime picks each request's origin from `configureNestBridge({ services })`, then the origin the plugin registered for that app with the new `setNestBridgeServiceBaseURL(name, baseURL)`, then the global `setNestBridgeBaseURL()` value. `graphqlEndpoint` also accepts a record with one endpoint per app.
10
+
11
+ `@Controller({ path, version })` and `@Version(...)` are now read, and the global prefix (`setGlobalPrefix`, including `exclude`) and URI versioning (`enableVersioning`) are detected from the bootstrap file and composed into every request path with the new `detectGlobalPrefix`, `detectVersioning` and `applyApplicationRouting` from `@nestbridge/core`. A prefix or versioning setup that cannot be read statically is reported as an `unsupported-global-prefix` or `unsupported-versioning` diagnostic; the `globalPrefix` and `versioning` plugin options state the value explicitly, and `false` ignores it.
12
+
13
+ Upgrading: the global prefix and URI versioning are now part of every URL, so a prefix that was written into `baseURL` as a workaround would now be repeated. Keep `baseURL` to the origin, or set `globalPrefix: false` / `versioning: false`. A relative `root` now resolves against the bundler's root instead of `process.cwd()`. See `MIGRATION.md`.
14
+
15
+ ## 2.1.4
16
+
17
+ No changes in this release.
18
+
3
19
  ## 2.1.3
4
20
 
5
21
  No changes in this release.
package/README.md CHANGED
@@ -29,9 +29,15 @@ yarn add @nestbridge/core
29
29
  Parses each file, finds classes decorated with `@Controller(...)`, and
30
30
  extracts a plain `ControllerDefinition` per controller — its base path, and
31
31
  each `@Get`/`@Post`/`@Put`/`@Patch`/`@Delete` method's HTTP method, route
32
- path, and `@Param`/`@Query`/`@Body`/`@Headers` parameter mapping. Anything it
33
- can't safely resolve statically (a non-literal route path, an unsupported
34
- decorator) becomes a `NestBridgeDiagnostic` instead of a guess.
32
+ path, `version`, `@Param`/`@Query`/`@Body`/`@Headers` parameter mapping, and
33
+ `responseKind` (`'json'`, `'observable'` for an `Observable<T>` return type, or
34
+ `'stream'` for `StreamableFile`), read from the method's declared return type.
35
+ Both `@Controller('path')` and `@Controller({ path, version })` are read, and
36
+ a method's `@Version(...)` replaces its controller's version. A method's
37
+ `version` is a string, `null` for `VERSION_NEUTRAL`, or `undefined` when the
38
+ route states none (an array of versions uses its first entry). Anything it
39
+ can't safely resolve statically (a non-literal route path or version, an
40
+ unsupported decorator) becomes a `NestBridgeDiagnostic` instead of a guess.
35
41
 
36
42
  ```ts
37
43
  import { analyzeControllers } from '@nestbridge/core';
@@ -61,15 +67,19 @@ const { resolvers, diagnostics } = analyzeResolvers([
61
67
  ]);
62
68
  ```
63
69
 
64
- ### `generateControllerDeclaration(definition, outputFilePath)`
70
+ ### `generateControllerDeclaration(definition, outputFilePath, responseWrapper?)`
65
71
 
66
72
  Turns a `ControllerDefinition` or a `ResolverDefinition` into the text of a
67
73
  `.d.ts` file — both share the same `{ name, sourceFile, methods }` shape, so
68
74
  the same generator is used for both. The generated class re-declares each
69
75
  method using a type-only reference back to the real controller or resolver
70
- (`Parameters<__ServerX['method']>` / `RemoteResult<...>`), so argument and
71
- return types are projected from the real class's own type — never re-derived
72
- from the AST.
76
+ (`Parameters<__ServerX['method']>` / `RemoteResult<...>`, or
77
+ `RemoteObservableResult<...>` / `RemoteStreamResult<...>` for observable and
78
+ streamed methods), so argument and return types are projected from the real
79
+ class's own type — never re-derived from the AST. Pass the result of
80
+ `detectResponseWrapper` as `responseWrapper` to wrap the JSON-returning
81
+ methods in the detected response shape. The helper types are imported from
82
+ `'nestbridge'`.
73
83
 
74
84
  ```ts
75
85
  import { generateControllerDeclaration } from '@nestbridge/core';
@@ -111,6 +121,59 @@ import { detectErrorResponseShape } from '@nestbridge/core';
111
121
  const errorResponseShape = detectErrorResponseShape('src/main.ts');
112
122
  ```
113
123
 
124
+ ### `detectGlobalPrefix(bootstrapFilePath)`
125
+
126
+ Statically reads the app's `setGlobalPrefix(...)` call in the bootstrap file.
127
+ It returns a `BootstrapDetection<GlobalPrefix>`: `{ value, diagnostics }`, where
128
+ `value` is `{ prefix, exclude }` — `exclude` lists the routes the prefix skips
129
+ (string paths, and `{ path, method: RequestMethod.X }` entries as
130
+ `{ path, httpMethod }`) — or `undefined` when the file never sets a prefix. A
131
+ call that is conditional or made more than once, a prefix that is not a string
132
+ literal, or an exclusion it cannot read yields an `unsupported-global-prefix`
133
+ diagnostic and no value.
134
+
135
+ ```ts
136
+ import { detectGlobalPrefix } from '@nestbridge/core';
137
+
138
+ const { value: globalPrefix, diagnostics } = detectGlobalPrefix('src/main.ts');
139
+ ```
140
+
141
+ ### `detectVersioning(bootstrapFilePath)`
142
+
143
+ Statically reads the app's `enableVersioning(...)` call. It returns a
144
+ `BootstrapDetection<UriVersioning>` whose `value` is
145
+ `{ prefix, defaultVersion? }`: the version prefix (`'v'` unless the call sets
146
+ another string, or `false` for none, read as `''`) and the default version of
147
+ routes that state none. Only `VersioningType.URI` is URL-expressible; header
148
+ and media-type versioning, a conditional or repeated call, and non-literal
149
+ values yield an `unsupported-versioning` diagnostic and no value.
150
+
151
+ ```ts
152
+ import { detectVersioning } from '@nestbridge/core';
153
+
154
+ const { value: versioning, diagnostics } = detectVersioning('src/main.ts');
155
+ ```
156
+
157
+ ### `applyApplicationRouting(controllers, routing)`
158
+
159
+ Composes the application-level routing onto analyzed controllers, the way Nest
160
+ composes a route: `[global prefix][/<version prefix><version>][controller
161
+ path][method path]`. It returns new `ControllerDefinition`s whose methods'
162
+ `path` is the full request path; a prefix-excluded route skips the prefix but
163
+ keeps its version, and a `null` (`VERSION_NEUTRAL`) version adds no segment.
164
+ `routing` is an `ApplicationRouting` — `{ globalPrefix?, versioning? }`, the
165
+ two `value`s detected above, or any values you supply yourself.
166
+
167
+ ```ts
168
+ import { analyzeControllers, applyApplicationRouting } from '@nestbridge/core';
169
+
170
+ const { controllers } = analyzeControllers(files);
171
+ const routed = applyApplicationRouting(controllers, {
172
+ globalPrefix: { prefix: 'api', exclude: [{ path: 'health' }] },
173
+ versioning: { prefix: 'v', defaultVersion: '1' },
174
+ });
175
+ ```
176
+
114
177
  ### `generateErrorBodyDeclaration(errorBodyTypeSource)`
115
178
 
116
179
  Turns the `errorBodyTypeSource` from `detectErrorResponseShape` into the text
@@ -133,14 +196,21 @@ import { formatDiagnostic, NestBridgeDiagnosticError } from '@nestbridge/core';
133
196
  string (file, position, message). `NestBridgeDiagnosticError` wraps one or
134
197
  more diagnostics as a thrown `Error`.
135
198
 
199
+ A diagnostic's `code` is one of `unsupported-route` (a non-literal route path or
200
+ version), `unsupported-parameter`, `unsupported-argument`,
201
+ `unsupported-return-type`, `circular-type`, `unsupported-global-prefix` and
202
+ `unsupported-versioning`.
203
+
136
204
  ### Types
137
205
 
138
- `ControllerDefinition`, `ControllerMethodDefinition`,
139
- `ControllerParameterDefinition`, `HttpMethod`, `ParameterSourceKind`,
140
- `ResolverDefinition`, `ResolverMethodDefinition`,
141
- `ResolverArgumentDefinition`, `SelectionField`, `GraphqlOperationKind`,
142
- `NestBridgeDiagnostic`, `NestBridgeDiagnosticCode`, `ResponseWrapperDetection`,
143
- `ErrorResponseShapeDetection`.
206
+ `ControllerAnalysisResult`, `ControllerDefinition`,
207
+ `ControllerMethodDefinition`, `ControllerParameterDefinition`, `HttpMethod`,
208
+ `ParameterSourceKind`, `ResolverAnalysisResult`, `ResolverDefinition`,
209
+ `ResolverMethodDefinition`, `ResolverArgumentDefinition`, `SelectionField`,
210
+ `GraphqlOperationKind`, `ResponseKind`, `NestBridgeDiagnostic`,
211
+ `NestBridgeDiagnosticCode`, `ResponseWrapperDetection`,
212
+ `ErrorResponseShapeDetection`, `BootstrapDetection`, `GlobalPrefix`,
213
+ `GlobalPrefixExclusion`, `UriVersioning`, `ApplicationRouting`.
144
214
 
145
215
  ## License
146
216
 
@@ -1,5 +1,32 @@
1
+ //#region src/models/http-method.d.ts
2
+ type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
3
+ //#endregion
4
+ //#region src/models/global-prefix-exclusion.d.ts
5
+ interface GlobalPrefixExclusion {
6
+ path: string;
7
+ httpMethod?: HttpMethod;
8
+ }
9
+ //#endregion
10
+ //#region src/models/global-prefix.d.ts
11
+ interface GlobalPrefix {
12
+ prefix: string;
13
+ exclude: GlobalPrefixExclusion[];
14
+ }
15
+ //#endregion
16
+ //#region src/models/uri-versioning.d.ts
17
+ interface UriVersioning {
18
+ prefix: string;
19
+ defaultVersion?: string;
20
+ }
21
+ //#endregion
22
+ //#region src/models/application-routing.d.ts
23
+ interface ApplicationRouting {
24
+ globalPrefix?: GlobalPrefix;
25
+ versioning?: UriVersioning;
26
+ }
27
+ //#endregion
1
28
  //#region src/diagnostics/nestbridge-diagnostic.d.ts
2
- type NestBridgeDiagnosticCode = "unsupported-route" | "unsupported-parameter" | "unsupported-argument" | "unsupported-return-type" | "circular-type";
29
+ type NestBridgeDiagnosticCode = "unsupported-route" | "unsupported-parameter" | "unsupported-argument" | "unsupported-return-type" | "circular-type" | "unsupported-global-prefix" | "unsupported-versioning";
3
30
  interface NestBridgeDiagnostic {
4
31
  code: NestBridgeDiagnosticCode;
5
32
  title: string;
@@ -13,6 +40,12 @@ interface NestBridgeDiagnostic {
13
40
  column: number;
14
41
  }
15
42
  //#endregion
43
+ //#region src/models/bootstrap-detection.d.ts
44
+ interface BootstrapDetection<T> {
45
+ value: T | undefined;
46
+ diagnostics: NestBridgeDiagnostic[];
47
+ }
48
+ //#endregion
16
49
  //#region src/diagnostics/format-diagnostic.d.ts
17
50
  export declare const formatDiagnostic: (diagnostic: NestBridgeDiagnostic) => string;
18
51
  //#endregion
@@ -42,9 +75,6 @@ type ControllerParameterDefinition = (ControllerParameterBase & {
42
75
  name: string;
43
76
  });
44
77
  //#endregion
45
- //#region src/models/http-method.d.ts
46
- type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
47
- //#endregion
48
78
  //#region src/models/response-kind.d.ts
49
79
  type ResponseKind = "json" | "observable" | "stream";
50
80
  //#endregion
@@ -53,6 +83,11 @@ interface ControllerMethodDefinition {
53
83
  name: string;
54
84
  httpMethod: HttpMethod;
55
85
  path: string;
86
+ /**
87
+ * `undefined` means the route states no version, so the application default version applies;
88
+ * `null` is `VERSION_NEUTRAL`, which never adds a version to the URL.
89
+ */
90
+ version?: string | null;
56
91
  parameters: ControllerParameterDefinition[];
57
92
  responseKind: ResponseKind;
58
93
  line: number;
@@ -130,9 +165,15 @@ export declare const analyzeControllers: (filePaths: readonly string[]) => Contr
130
165
  //#region src/analysis/analyze-resolvers.d.ts
131
166
  export declare const analyzeResolvers: (filePaths: readonly string[]) => ResolverAnalysisResult;
132
167
  //#endregion
168
+ //#region src/analysis/apply-application-routing.d.ts
169
+ export declare const applyApplicationRouting: (controllers: readonly ControllerDefinition[], routing: ApplicationRouting) => ControllerDefinition[];
170
+ //#endregion
133
171
  //#region src/analysis/detect-error-response-shape.d.ts
134
172
  export declare const detectErrorResponseShape: (bootstrapFilePath: string) => ErrorResponseShapeDetection | undefined;
135
173
  //#endregion
174
+ //#region src/analysis/detect-global-prefix.d.ts
175
+ export declare const detectGlobalPrefix: (bootstrapFilePath: string) => BootstrapDetection<GlobalPrefix>;
176
+ //#endregion
136
177
  //#region src/analysis/detect-response-wrapper.d.ts
137
178
  /**
138
179
  * Interceptors registered first wrap those registered after them, so the
@@ -141,6 +182,9 @@ export declare const detectErrorResponseShape: (bootstrapFilePath: string) => Er
141
182
  */
142
183
  export declare const detectResponseWrapper: (bootstrapFilePath: string) => ResponseWrapperDetection | undefined;
143
184
  //#endregion
185
+ //#region src/analysis/detect-versioning.d.ts
186
+ export declare const detectVersioning: (bootstrapFilePath: string) => BootstrapDetection<UriVersioning>;
187
+ //#endregion
144
188
  //#region src/declarations/generate-controller-declaration.d.ts
145
189
  interface ClientDeclarationSource {
146
190
  name: string;
@@ -158,5 +202,5 @@ export declare const generateErrorBodyDeclaration: (errorBodyTypeSource: string)
158
202
  //#region src/version.d.ts
159
203
  export declare const VERSION: string;
160
204
  //#endregion
161
- export type { ControllerAnalysisResult, ControllerDefinition, ControllerMethodDefinition, ControllerParameterDefinition, ErrorResponseShapeDetection, GraphqlOperationKind, HttpMethod, NestBridgeDiagnostic, NestBridgeDiagnosticCode, ParameterSourceKind, ResolverAnalysisResult, ResolverArgumentDefinition, ResolverDefinition, ResolverMethodDefinition, ResponseKind, ResponseWrapperDetection, SelectionField };
205
+ export type { ApplicationRouting, BootstrapDetection, ControllerAnalysisResult, ControllerDefinition, ControllerMethodDefinition, ControllerParameterDefinition, ErrorResponseShapeDetection, GlobalPrefix, GlobalPrefixExclusion, GraphqlOperationKind, HttpMethod, NestBridgeDiagnostic, NestBridgeDiagnosticCode, ParameterSourceKind, ResolverAnalysisResult, ResolverArgumentDefinition, ResolverDefinition, ResolverMethodDefinition, ResponseKind, ResponseWrapperDetection, SelectionField, UriVersioning };
162
206
  //# sourceMappingURL=nestbridge.core.d.ts.map