@orpc/nest 1.11.3 → 1.12.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.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <div align="center">
2
- <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
2
+ <image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
3
3
  </div>
4
4
 
5
5
  <h1></h1>
@@ -45,7 +45,7 @@
45
45
 
46
46
  ## Documentation
47
47
 
48
- You can find the full documentation [here](https://orpc.unnoq.com).
48
+ You can find the full documentation [here](https://orpc.dev).
49
49
 
50
50
  ## Packages
51
51
 
@@ -66,11 +66,11 @@ You can find the full documentation [here](https://orpc.unnoq.com).
66
66
 
67
67
  ## `@orpc/nest`
68
68
 
69
- Deeply integrate oRPC with [NestJS](https://nestjs.com/). Read the [documentation](https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest) for more information.
69
+ Deeply integrate oRPC with [NestJS](https://nestjs.com/). Read the [documentation](https://orpc.dev/docs/openapi/integrations/implement-contract-in-nest) for more information.
70
70
 
71
71
  ### Implement Contract
72
72
 
73
- An overview of how to implement an [oRPC contract](https://orpc.unnoq.com/docs/contract-first/define-contract) in NestJS.
73
+ An overview of how to implement an [oRPC contract](https://orpc.dev/docs/contract-first/define-contract) in NestJS.
74
74
 
75
75
  ```ts
76
76
  import { Implement, implement, ORPCError } from '@orpc/nest'
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;
@@ -21,11 +44,12 @@ declare class ORPCModule {
21
44
  /**
22
45
  * Decorator in controller handler to implement a oRPC contract.
23
46
  *
24
- * @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
47
+ * @see {@link https://orpc.dev/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
  }
@@ -43,9 +67,14 @@ interface PopulateContractRouterPathsOptions {
43
67
  * This utility automatically populates any missing paths
44
68
  * Using the router's keys + `/`.
45
69
  *
46
- * @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#define-your-contract NestJS Implement Contract Docs}
70
+ * @see {@link https://orpc.dev/docs/openapi/integrations/implement-contract-in-nest#define-your-contract NestJS Implement Contract Docs}
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;
@@ -21,11 +44,12 @@ declare class ORPCModule {
21
44
  /**
22
45
  * Decorator in controller handler to implement a oRPC contract.
23
46
  *
24
- * @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
47
+ * @see {@link https://orpc.dev/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
  }
@@ -43,9 +67,14 @@ interface PopulateContractRouterPathsOptions {
43
67
  * This utility automatically populates any missing paths
44
68
  * Using the router's keys + `/`.
45
69
  *
46
- * @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#define-your-contract NestJS Implement Contract Docs}
70
+ * @see {@link https://orpc.dev/docs/openapi/integrations/implement-contract-in-nest#define-your-contract NestJS Implement Contract Docs}
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,9 +1,9 @@
1
1
  {
2
2
  "name": "@orpc/nest",
3
3
  "type": "module",
4
- "version": "1.11.3",
4
+ "version": "1.12.1",
5
5
  "license": "MIT",
6
- "homepage": "https://orpc.unnoq.com",
6
+ "homepage": "https://orpc.dev",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/unnoq/orpc.git",
@@ -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.1",
43
+ "@orpc/client": "1.12.1",
44
+ "@orpc/openapi": "1.12.1",
45
+ "@orpc/openapi-client": "1.12.1",
46
+ "@orpc/shared": "1.12.1",
47
+ "@orpc/standard-server": "1.12.1",
48
+ "@orpc/standard-server-fastify": "1.12.1",
49
+ "@orpc/server": "1.12.1",
50
+ "@orpc/standard-server-node": "1.12.1"
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",