@orpc/nest 1.11.3 → 1.12.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/dist/index.d.mts CHANGED
@@ -1,13 +1,36 @@
1
- import { DynamicModule, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
2
1
  import { AnySchema, ContractRouter, HTTPPath, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
- import { CreateProcedureClientOptions, Router } from '@orpc/server';
4
- export { ImplementedProcedure, Implementer, ImplementerInternal, ImplementerInternalWithMiddlewares, ORPCError, ProcedureImplementer, RouterImplementer, RouterImplementerWithMiddlewares, implement, onError, onFinish, onStart, onSuccess } from '@orpc/server';
5
- import { Promisable } from '@orpc/shared';
6
- import { Observable } from 'rxjs';
2
+ import { CreateProcedureClientOptions, Router, Context, BuilderConfig, Implementer } from '@orpc/server';
3
+ export { ImplementedProcedure, Implementer, ImplementerInternal, ImplementerInternalWithMiddlewares, ORPCError, ProcedureImplementer, RouterImplementer, RouterImplementerWithMiddlewares, onError, onFinish, onStart, onSuccess } from '@orpc/server';
4
+ import { DynamicModule, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
5
+ import { StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions } from '@orpc/openapi-client/standard';
6
+ import { StandardOpenAPICodecOptions } from '@orpc/openapi/standard';
7
+ import { StandardHandlerOptions } from '@orpc/server/standard';
8
+ import { Interceptor, Promisable } from '@orpc/shared';
9
+ import { StandardResponse } from '@orpc/standard-server';
7
10
  import { SendStandardResponseOptions } from '@orpc/standard-server-node';
11
+ import { Observable } from 'rxjs';
8
12
 
9
13
  declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
10
- interface ORPCModuleConfig extends CreateProcedureClientOptions<object, AnySchema, object, object, object>, SendStandardResponseOptions {
14
+ /**
15
+ * You can extend this interface to add global context properties.
16
+ * @example
17
+ * ```ts
18
+ * declare module '@orpc/nest' {
19
+ * interface ORPCGlobalContext {
20
+ * user: { id: string; name: string }
21
+ * }
22
+ * }
23
+ * ```
24
+ */
25
+ interface ORPCGlobalContext {
26
+ }
27
+ interface ORPCModuleConfig extends CreateProcedureClientOptions<ORPCGlobalContext, AnySchema, object, object, object>, SendStandardResponseOptions, StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions, StandardOpenAPICodecOptions {
28
+ plugins?: StandardHandlerOptions<ORPCGlobalContext>['plugins'];
29
+ sendResponseInterceptors?: Interceptor<{
30
+ request: any;
31
+ response: any;
32
+ standardResponse: StandardResponse;
33
+ }, unknown>[];
11
34
  }
12
35
  declare class ORPCModule {
13
36
  static forRoot(config: ORPCModuleConfig): DynamicModule;
@@ -23,9 +46,10 @@ declare class ORPCModule {
23
46
  *
24
47
  * @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
25
48
  */
26
- declare function Implement<T extends ContractRouter<any>>(contract: T): <U extends Promisable<Router<T, Record<never, never>>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
49
+ declare function Implement<T extends ContractRouter<any>>(contract: T): <U extends Promisable<Router<T, ORPCGlobalContext>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
27
50
  declare class ImplementInterceptor implements NestInterceptor {
28
51
  private readonly config;
52
+ private readonly codec;
29
53
  constructor(config: ORPCModuleConfig | undefined);
30
54
  intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
31
55
  }
@@ -47,5 +71,10 @@ interface PopulateContractRouterPathsOptions {
47
71
  */
48
72
  declare function populateContractRouterPaths<T extends AnyContractRouter>(router: T, options?: PopulateContractRouterPathsOptions): PopulatedContractRouterPaths<T>;
49
73
 
50
- export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, populateContractRouterPaths, toNestPattern };
51
- export type { ORPCModuleConfig, PopulateContractRouterPathsOptions, PopulatedContractRouterPaths };
74
+ /**
75
+ * Alias for `implement` from `@orpc/server` with default context set to `ORPCGlobalContext`
76
+ */
77
+ declare function implement<T extends AnyContractRouter, TContext extends Context = ORPCGlobalContext>(contract: T, config?: BuilderConfig): Implementer<T, TContext, TContext>;
78
+
79
+ export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, implement, populateContractRouterPaths, toNestPattern };
80
+ export type { ORPCGlobalContext, ORPCModuleConfig, PopulateContractRouterPathsOptions, PopulatedContractRouterPaths };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,36 @@
1
- import { DynamicModule, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
2
1
  import { AnySchema, ContractRouter, HTTPPath, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
- import { CreateProcedureClientOptions, Router } from '@orpc/server';
4
- export { ImplementedProcedure, Implementer, ImplementerInternal, ImplementerInternalWithMiddlewares, ORPCError, ProcedureImplementer, RouterImplementer, RouterImplementerWithMiddlewares, implement, onError, onFinish, onStart, onSuccess } from '@orpc/server';
5
- import { Promisable } from '@orpc/shared';
6
- import { Observable } from 'rxjs';
2
+ import { CreateProcedureClientOptions, Router, Context, BuilderConfig, Implementer } from '@orpc/server';
3
+ export { ImplementedProcedure, Implementer, ImplementerInternal, ImplementerInternalWithMiddlewares, ORPCError, ProcedureImplementer, RouterImplementer, RouterImplementerWithMiddlewares, onError, onFinish, onStart, onSuccess } from '@orpc/server';
4
+ import { DynamicModule, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
5
+ import { StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions } from '@orpc/openapi-client/standard';
6
+ import { StandardOpenAPICodecOptions } from '@orpc/openapi/standard';
7
+ import { StandardHandlerOptions } from '@orpc/server/standard';
8
+ import { Interceptor, Promisable } from '@orpc/shared';
9
+ import { StandardResponse } from '@orpc/standard-server';
7
10
  import { SendStandardResponseOptions } from '@orpc/standard-server-node';
11
+ import { Observable } from 'rxjs';
8
12
 
9
13
  declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
10
- interface ORPCModuleConfig extends CreateProcedureClientOptions<object, AnySchema, object, object, object>, SendStandardResponseOptions {
14
+ /**
15
+ * You can extend this interface to add global context properties.
16
+ * @example
17
+ * ```ts
18
+ * declare module '@orpc/nest' {
19
+ * interface ORPCGlobalContext {
20
+ * user: { id: string; name: string }
21
+ * }
22
+ * }
23
+ * ```
24
+ */
25
+ interface ORPCGlobalContext {
26
+ }
27
+ interface ORPCModuleConfig extends CreateProcedureClientOptions<ORPCGlobalContext, AnySchema, object, object, object>, SendStandardResponseOptions, StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions, StandardOpenAPICodecOptions {
28
+ plugins?: StandardHandlerOptions<ORPCGlobalContext>['plugins'];
29
+ sendResponseInterceptors?: Interceptor<{
30
+ request: any;
31
+ response: any;
32
+ standardResponse: StandardResponse;
33
+ }, unknown>[];
11
34
  }
12
35
  declare class ORPCModule {
13
36
  static forRoot(config: ORPCModuleConfig): DynamicModule;
@@ -23,9 +46,10 @@ declare class ORPCModule {
23
46
  *
24
47
  * @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
25
48
  */
26
- declare function Implement<T extends ContractRouter<any>>(contract: T): <U extends Promisable<Router<T, Record<never, never>>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
49
+ declare function Implement<T extends ContractRouter<any>>(contract: T): <U extends Promisable<Router<T, ORPCGlobalContext>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
27
50
  declare class ImplementInterceptor implements NestInterceptor {
28
51
  private readonly config;
52
+ private readonly codec;
29
53
  constructor(config: ORPCModuleConfig | undefined);
30
54
  intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
31
55
  }
@@ -47,5 +71,10 @@ interface PopulateContractRouterPathsOptions {
47
71
  */
48
72
  declare function populateContractRouterPaths<T extends AnyContractRouter>(router: T, options?: PopulateContractRouterPathsOptions): PopulatedContractRouterPaths<T>;
49
73
 
50
- export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, populateContractRouterPaths, toNestPattern };
51
- export type { ORPCModuleConfig, PopulateContractRouterPathsOptions, PopulatedContractRouterPaths };
74
+ /**
75
+ * Alias for `implement` from `@orpc/server` with default context set to `ORPCGlobalContext`
76
+ */
77
+ declare function implement<T extends AnyContractRouter, TContext extends Context = ORPCGlobalContext>(contract: T, config?: BuilderConfig): Implementer<T, TContext, TContext>;
78
+
79
+ export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, implement, populateContractRouterPaths, toNestPattern };
80
+ export type { ORPCGlobalContext, ORPCModuleConfig, PopulateContractRouterPathsOptions, PopulatedContractRouterPaths };
package/dist/index.mjs CHANGED
@@ -1,12 +1,11 @@
1
- import { Module, Injectable, Inject, Optional, applyDecorators, Delete, Patch, Put, Post, Get, Head, UseInterceptors } from '@nestjs/common';
2
- import { toORPCError } from '@orpc/client';
1
+ import { getRouter, unlazy, isProcedure, implement as implement$1 } from '@orpc/server';
2
+ export { ORPCError, onError, onFinish, onStart, onSuccess } from '@orpc/server';
3
+ import { Module, Injectable, Inject, Optional, applyDecorators, Delete, Patch, Put, Post, Get, Head, HttpCode, UseInterceptors } from '@nestjs/common';
3
4
  import { isContractProcedure, ContractProcedure, fallbackContractConfig } from '@orpc/contract';
4
5
  import { standardizeHTTPPath, StandardOpenAPISerializer, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
5
6
  import { StandardOpenAPICodec } from '@orpc/openapi/standard';
6
- import { getRouter, unlazy, isProcedure, createProcedureClient, ORPCError } from '@orpc/server';
7
- export { ORPCError, implement, onError, onFinish, onStart, onSuccess } from '@orpc/server';
8
- import { toArray, get } from '@orpc/shared';
9
- import { flattenHeader } from '@orpc/standard-server';
7
+ import { StandardHandler } from '@orpc/server/standard';
8
+ import { toArray, get, intercept } from '@orpc/shared';
10
9
  import * as StandardServerFastify from '@orpc/standard-server-fastify';
11
10
  import * as StandardServerNode from '@orpc/standard-server-node';
12
11
  import { mergeMap } from 'rxjs';
@@ -112,6 +111,7 @@ function Implement(contract) {
112
111
  return (target, propertyKey, descriptor) => {
113
112
  applyDecorators(
114
113
  MethodDecoratorMap[method](toNestPattern(path)),
114
+ HttpCode(fallbackContractConfig("defaultSuccessStatus", contract["~orpc"].route.successStatus)),
115
115
  UseInterceptors(ImplementInterceptor)
116
116
  )(target, propertyKey, descriptor);
117
117
  };
@@ -137,15 +137,18 @@ function Implement(contract) {
137
137
  }
138
138
  };
139
139
  }
140
- const codec = new StandardOpenAPICodec(
141
- new StandardOpenAPISerializer(
142
- new StandardOpenAPIJsonSerializer(),
143
- new StandardBracketNotationSerializer()
144
- )
145
- );
146
140
  let ImplementInterceptor = class {
141
+ config;
142
+ codec;
147
143
  constructor(config) {
148
- this.config = config;
144
+ this.config = config ?? {};
145
+ this.codec = new StandardOpenAPICodec(
146
+ new StandardOpenAPISerializer(
147
+ new StandardOpenAPIJsonSerializer(this.config),
148
+ new StandardBracketNotationSerializer(this.config)
149
+ ),
150
+ this.config
151
+ );
149
152
  }
150
153
  intercept(ctx, next) {
151
154
  return next.handle().pipe(
@@ -159,30 +162,31 @@ let ImplementInterceptor = class {
159
162
  const req = ctx.switchToHttp().getRequest();
160
163
  const res = ctx.switchToHttp().getResponse();
161
164
  const standardRequest = "raw" in req ? StandardServerFastify.toStandardLazyRequest(req, res) : StandardServerNode.toStandardLazyRequest(req, res);
162
- const standardResponse = await (async () => {
163
- let isDecoding = false;
164
- try {
165
- const client = createProcedureClient(procedure, this.config);
166
- isDecoding = true;
167
- const input = await codec.decode(standardRequest, flattenParams(req.params), procedure);
168
- isDecoding = false;
169
- const output = await client(input, {
170
- signal: standardRequest.signal,
171
- lastEventId: flattenHeader(standardRequest.headers["last-event-id"])
172
- });
173
- return codec.encode(output, procedure);
174
- } catch (e) {
175
- const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
176
- message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
177
- cause: e
178
- }) : toORPCError(e);
179
- return codec.encodeError(error);
180
- }
181
- })();
182
- if ("raw" in res) {
183
- await StandardServerFastify.sendStandardResponse(res, standardResponse, this.config);
184
- } else {
185
- await StandardServerNode.sendStandardResponse(res, standardResponse, this.config);
165
+ const handler = new StandardHandler(procedure, {
166
+ init: () => {
167
+ },
168
+ match: () => Promise.resolve({ path: toArray(this.config.path), procedure, params: flattenParams(req.params) })
169
+ }, this.codec, {
170
+ // Since plugins can modify options directly, so we need to clone to avoid affecting other handlers/requests
171
+ // TODO: improve plugins system to avoid this cloning
172
+ clientInterceptors: [...toArray(this.config.interceptors)],
173
+ plugins: [...toArray(this.config.plugins)]
174
+ });
175
+ const result = await handler.handle(standardRequest, {
176
+ context: this.config.context
177
+ });
178
+ if (result.matched) {
179
+ return intercept(
180
+ toArray(this.config.sendResponseInterceptors),
181
+ { request: req, response: res, standardResponse: result.response },
182
+ async ({ response, standardResponse }) => {
183
+ if ("raw" in response) {
184
+ await StandardServerFastify.sendStandardResponse(response, standardResponse, this.config);
185
+ } else {
186
+ await StandardServerNode.sendStandardResponse(response, standardResponse, this.config);
187
+ }
188
+ }
189
+ );
186
190
  }
187
191
  })
188
192
  );
@@ -205,4 +209,8 @@ function flattenParams(params) {
205
209
  return flatten;
206
210
  }
207
211
 
208
- export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, populateContractRouterPaths, toNestPattern };
212
+ function implement(contract, config = {}) {
213
+ return implement$1(contract, config);
214
+ }
215
+
216
+ export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, implement, populateContractRouterPaths, toNestPattern };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/nest",
3
3
  "type": "module",
4
- "version": "1.11.3",
4
+ "version": "1.12.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -39,23 +39,23 @@
39
39
  }
40
40
  },
41
41
  "dependencies": {
42
- "@orpc/client": "1.11.3",
43
- "@orpc/contract": "1.11.3",
44
- "@orpc/openapi": "1.11.3",
45
- "@orpc/openapi-client": "1.11.3",
46
- "@orpc/server": "1.11.3",
47
- "@orpc/shared": "1.11.3",
48
- "@orpc/standard-server": "1.11.3",
49
- "@orpc/standard-server-fastify": "1.11.3",
50
- "@orpc/standard-server-node": "1.11.3"
42
+ "@orpc/contract": "1.12.0",
43
+ "@orpc/client": "1.12.0",
44
+ "@orpc/openapi": "1.12.0",
45
+ "@orpc/shared": "1.12.0",
46
+ "@orpc/openapi-client": "1.12.0",
47
+ "@orpc/server": "1.12.0",
48
+ "@orpc/standard-server": "1.12.0",
49
+ "@orpc/standard-server-fastify": "1.12.0",
50
+ "@orpc/standard-server-node": "1.12.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@fastify/cookie": "^11.0.2",
54
- "@nestjs/common": "^11.1.8",
55
- "@nestjs/core": "^11.1.8",
56
- "@nestjs/platform-express": "^11.1.8",
57
- "@nestjs/platform-fastify": "^11.1.8",
58
- "@nestjs/testing": "^11.1.8",
54
+ "@nestjs/common": "^11.1.9",
55
+ "@nestjs/core": "^11.1.9",
56
+ "@nestjs/platform-express": "^11.1.9",
57
+ "@nestjs/platform-fastify": "^11.1.9",
58
+ "@nestjs/testing": "^11.1.9",
59
59
  "@ts-rest/core": "^3.52.1",
60
60
  "@types/express": "^5.0.5",
61
61
  "express": "^5.0.0",