@orpc/nest 2.0.0-beta.48 → 2.0.0-beta.50
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 +4 -4
- package/dist/_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorate.js +8 -0
- package/dist/_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorateMetadata.js +5 -0
- package/dist/_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorateParam.js +7 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +3 -0
- package/dist/implement.d.ts +29 -0
- package/dist/{implement.mjs → implement.js} +68 -102
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/module.d.ts +54 -0
- package/dist/{module.mjs → module.js} +5 -4
- package/package.json +45 -47
- package/dist/constants.d.mts +0 -1
- package/dist/constants.mjs +0 -1
- package/dist/implement.d.mts +0 -29
- package/dist/index.d.mts +0 -8
- package/dist/index.mjs +0 -6
- package/dist/module.d.mts +0 -47
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/nest">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fnest?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
208
209
|
<tr>
|
|
209
210
|
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
211
|
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
-
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
212
212
|
</tr>
|
|
213
213
|
</table>
|
|
214
214
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// \0@oxc-project+runtime@0.157.0/helpers/esm/decorate.js
|
|
2
|
+
function __decorate(decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
}
|
|
8
|
+
export { __decorate as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ORPCModuleConfig } from "./module.js";
|
|
2
|
+
import { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
|
|
3
|
+
import { HttpAdapterHost } from "@nestjs/core";
|
|
4
|
+
import { RouterContract } from "@orpc/contract";
|
|
5
|
+
import { ContractedRouter, DefaultInitialContext } from "@orpc/server";
|
|
6
|
+
import { Promisable } from "@orpc/shared";
|
|
7
|
+
import { Observable } from "rxjs";
|
|
8
|
+
/**
|
|
9
|
+
* Decorator that implements an oRPC contract (procedure or router contract)
|
|
10
|
+
* on a NestJS controller method. It registers the corresponding NestJS routes
|
|
11
|
+
* and handles request decoding and response encoding for you.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* **Note**: Every procedure contract must define an `openapi.path` meta;
|
|
15
|
+
* use `populateRouterContractOpenAPIPaths` from `@orpc/openapi` to fill in missing paths.
|
|
16
|
+
* **Note**: The HTTP `QUERY` method requires NestJS v11.2+ (`QueryMethod`). Older NestJS versions throw; use `GET` instead.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://orpc.dev/docs/integrations/nest#implement-your-contract | Implement oRPC contract with NestJS - Implement Your Contract}
|
|
19
|
+
*/
|
|
20
|
+
export declare function Implement<T extends RouterContract>(contract: T): <U extends Promisable<ContractedRouter<T, DefaultInitialContext>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
|
|
21
|
+
export declare class ImplementInterceptor implements NestInterceptor {
|
|
22
|
+
private readonly config;
|
|
23
|
+
private readonly codec;
|
|
24
|
+
private readonly handler;
|
|
25
|
+
private readonly toNestStandardLazyRequest;
|
|
26
|
+
private readonly httpAdapterHost;
|
|
27
|
+
constructor(config: ORPCModuleConfig | undefined, httpAdapterHost: HttpAdapterHost);
|
|
28
|
+
intercept(ctx: ExecutionContext, next: CallHandler): Observable<any>;
|
|
29
|
+
}
|
|
@@ -1,20 +1,21 @@
|
|
|
1
|
+
import { ORPC_MODULE_CONFIG_SYMBOL } from "./constants.js";
|
|
2
|
+
import __decorateMetadata from "./_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorateMetadata.js";
|
|
3
|
+
import __decorateParam from "./_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorateParam.js";
|
|
4
|
+
import __decorate from "./_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorate.js";
|
|
1
5
|
import { Readable } from "node:stream";
|
|
2
6
|
import * as NestCommon from "@nestjs/common";
|
|
3
|
-
import {
|
|
7
|
+
import { Delete, Get, Head, HttpCode, HttpException, Inject, Injectable, Optional, Options, Patch, Post, Put, StreamableFile, UseInterceptors, applyDecorators } from "@nestjs/common";
|
|
4
8
|
import { HttpAdapterHost } from "@nestjs/core";
|
|
5
|
-
import {
|
|
9
|
+
import { ProcedureContract, getPathMeta } from "@orpc/contract";
|
|
6
10
|
import { DEFAULT_OPENAPI_METHOD, getDynamicPathParams, getOpenAPIMeta } from "@orpc/openapi";
|
|
7
11
|
import { OpenAPIHandlerCodecCore } from "@orpc/openapi/standard";
|
|
8
|
-
import { DEFAULT_SUCCESS_STATUS,
|
|
12
|
+
import { DEFAULT_SUCCESS_STATUS, Procedure, getRouter, unlazy } from "@orpc/server";
|
|
9
13
|
import { StandardHandler } from "@orpc/server/standard";
|
|
10
|
-
import { isAsyncIteratorObject,
|
|
14
|
+
import { NullProtoObj, isAsyncIteratorObject, listenAbort, mergeHttpPath, stringifyJSON, toArray, value } from "@orpc/shared";
|
|
11
15
|
import { flattenStandardHeader, generateContentDisposition, getRequiredStandardBodyHint } from "@standard-server/core";
|
|
12
16
|
import { toEventStream, toStandardLazyRequest } from "@standard-server/node";
|
|
13
17
|
import { mergeMap } from "rxjs";
|
|
14
|
-
|
|
15
|
-
import _decorateParam from "@oxc-project/runtime/helpers/decorateParam";
|
|
16
|
-
import _decorate from "@oxc-project/runtime/helpers/decorate";
|
|
17
|
-
|
|
18
|
+
// src/implement.ts
|
|
18
19
|
const QueryMethod = NestCommon.QueryMethod;
|
|
19
20
|
const MethodDecoratorMap = {
|
|
20
21
|
HEAD: Head,
|
|
@@ -25,39 +26,42 @@ const MethodDecoratorMap = {
|
|
|
25
26
|
DELETE: Delete,
|
|
26
27
|
OPTIONS: Options
|
|
27
28
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Decorator that implements an oRPC contract (procedure or router contract)
|
|
31
|
+
* on a NestJS controller method. It registers the corresponding NestJS routes
|
|
32
|
+
* and handles request decoding and response encoding for you.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* **Note**: Every procedure contract must define an `openapi.path` meta;
|
|
36
|
+
* use `populateRouterContractOpenAPIPaths` from `@orpc/openapi` to fill in missing paths.
|
|
37
|
+
* **Note**: The HTTP `QUERY` method requires NestJS v11.2+ (`QueryMethod`). Older NestJS versions throw; use `GET` instead.
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://orpc.dev/docs/integrations/nest#implement-your-contract | Implement oRPC contract with NestJS - Implement Your Contract}
|
|
40
|
+
*/
|
|
41
|
+
function Implement(contract) {
|
|
42
|
+
if (contract instanceof ProcedureContract) return (target, propertyKey, descriptor) => {
|
|
43
|
+
applyDecorators(toNestRouteDecorator(contract), UseInterceptors(ImplementInterceptor))(target, propertyKey, descriptor);
|
|
44
|
+
};
|
|
35
45
|
return (target, propertyKey, descriptor) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
46
|
UseInterceptors(ImplementInterceptor)(target, propertyKey, descriptor);
|
|
39
47
|
implementRouterContract(contract, target, propertyKey, descriptor);
|
|
40
48
|
};
|
|
41
49
|
}
|
|
42
50
|
function toNestRouteDecorator(contract) {
|
|
43
51
|
const meta = getOpenAPIMeta(contract);
|
|
44
|
-
if (meta?.path ===
|
|
45
|
-
throw new TypeError(`
|
|
52
|
+
if (meta?.path === void 0) throw new TypeError(`
|
|
46
53
|
@Implement decorator requires contract to have a 'openapi.path' meta.
|
|
47
54
|
Please define one using '.meta(openapi({ path: '/example' }))'.
|
|
48
55
|
Or use "populateRouterContractOpenAPIPaths" from "@orpc/openapi" utility to automatically fill in any missing paths.
|
|
49
56
|
`);
|
|
50
|
-
}
|
|
51
57
|
const method = meta.method ?? DEFAULT_OPENAPI_METHOD;
|
|
52
58
|
const path = toNestPattern(meta.prefix ? mergeHttpPath(meta.prefix, meta.path) : meta.path);
|
|
53
59
|
const successStatus = meta.successStatus ?? DEFAULT_SUCCESS_STATUS;
|
|
54
60
|
if (method === "QUERY") {
|
|
55
|
-
if (!QueryMethod)
|
|
56
|
-
throw new TypeError(`
|
|
61
|
+
if (!QueryMethod) throw new TypeError(`
|
|
57
62
|
@Implement decorator does not support the 'QUERY' HTTP method because the installed version of NestJS does not support it.
|
|
58
63
|
The 'QUERY' HTTP method requires NestJS v11.2 or later. Alternatively, use 'GET' method.
|
|
59
64
|
`);
|
|
60
|
-
}
|
|
61
65
|
return applyDecorators(QueryMethod(path), HttpCode(successStatus));
|
|
62
66
|
}
|
|
63
67
|
return applyDecorators(MethodDecoratorMap[method](path), HttpCode(successStatus));
|
|
@@ -66,32 +70,23 @@ function implementRouterContract(contract, target, propertyKey, descriptor) {
|
|
|
66
70
|
for (const [key, childContract] of Object.entries(contract)) {
|
|
67
71
|
let methodName = `${propertyKey}_${key}`;
|
|
68
72
|
let i = 0;
|
|
69
|
-
while (methodName in target) {
|
|
70
|
-
methodName = `${propertyKey}_${key}_${i++}`;
|
|
71
|
-
}
|
|
73
|
+
while (methodName in target) methodName = `${propertyKey}_${key}_${i++}`;
|
|
72
74
|
target[methodName] = async function(...args) {
|
|
73
75
|
const router = await descriptor.value.apply(this, args);
|
|
74
76
|
return getRouter(router, [key]);
|
|
75
77
|
};
|
|
76
78
|
Object.setPrototypeOf(target[methodName], descriptor.value);
|
|
77
79
|
queueMicrotask(() => {
|
|
78
|
-
for (const p of Reflect.getOwnMetadataKeys(target, propertyKey))
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
for (const p of Reflect.getOwnMetadataKeys(target.constructor, propertyKey)) {
|
|
82
|
-
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target.constructor, propertyKey), target.constructor, methodName);
|
|
83
|
-
}
|
|
80
|
+
for (const p of Reflect.getOwnMetadataKeys(target, propertyKey)) Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target, propertyKey), target, methodName);
|
|
81
|
+
for (const p of Reflect.getOwnMetadataKeys(target.constructor, propertyKey)) Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target.constructor, propertyKey), target.constructor, methodName);
|
|
84
82
|
});
|
|
85
83
|
const childDescriptor = Object.getOwnPropertyDescriptor(target, methodName);
|
|
86
84
|
if (childContract instanceof ProcedureContract) {
|
|
87
85
|
const routeDecorator = toNestRouteDecorator(childContract);
|
|
88
|
-
|
|
89
86
|
queueMicrotask(() => {
|
|
90
87
|
routeDecorator(target, methodName, childDescriptor);
|
|
91
88
|
});
|
|
92
|
-
} else
|
|
93
|
-
implementRouterContract(childContract, target, methodName, childDescriptor);
|
|
94
|
-
}
|
|
89
|
+
} else implementRouterContract(childContract, target, methodName, childDescriptor);
|
|
95
90
|
}
|
|
96
91
|
}
|
|
97
92
|
let ImplementInterceptor = class ImplementInterceptor {
|
|
@@ -101,17 +96,13 @@ let ImplementInterceptor = class ImplementInterceptor {
|
|
|
101
96
|
toNestStandardLazyRequest;
|
|
102
97
|
httpAdapterHost;
|
|
103
98
|
constructor(config, httpAdapterHost) {
|
|
104
|
-
// @Optional() does not allow set default value so we need to do it here
|
|
105
99
|
this.config = config ?? {};
|
|
106
100
|
this.httpAdapterHost = httpAdapterHost;
|
|
107
101
|
this.codec = new NestHandlerCodec(this.config);
|
|
108
102
|
this.handler = new StandardHandler(this.codec, this.config);
|
|
109
103
|
this.toNestStandardLazyRequest = this.config.toNestStandardLazyRequest ?? ((req, res) => {
|
|
110
104
|
const standardRequest = toStandardLazyRequest("raw" in req ? req.raw : req, "raw" in res ? res.raw : res);
|
|
111
|
-
|
|
112
|
-
if (req.body !== undefined) {
|
|
113
|
-
standardRequest.resolveBody = () => Promise.resolve(req.body);
|
|
114
|
-
}
|
|
105
|
+
if (req.body !== void 0) standardRequest.resolveBody = () => Promise.resolve(req.body);
|
|
115
106
|
standardRequest.params = req.params;
|
|
116
107
|
return standardRequest;
|
|
117
108
|
});
|
|
@@ -119,11 +110,9 @@ let ImplementInterceptor = class ImplementInterceptor {
|
|
|
119
110
|
intercept(ctx, next) {
|
|
120
111
|
return next.handle().pipe(mergeMap(async (impl) => {
|
|
121
112
|
const { default: procedure } = await unlazy(impl);
|
|
122
|
-
if (!(procedure instanceof Procedure))
|
|
123
|
-
throw new TypeError(`
|
|
113
|
+
if (!(procedure instanceof Procedure)) throw new TypeError(`
|
|
124
114
|
The return value of the @Implement controller handler must be a corresponding implemented router or procedure.
|
|
125
115
|
`);
|
|
126
|
-
}
|
|
127
116
|
const req = ctx.switchToHttp().getRequest();
|
|
128
117
|
const res = ctx.switchToHttp().getResponse();
|
|
129
118
|
const standardRequest = this.toNestStandardLazyRequest(req, res);
|
|
@@ -132,36 +121,29 @@ let ImplementInterceptor = class ImplementInterceptor {
|
|
|
132
121
|
procedure,
|
|
133
122
|
params: standardRequest.params
|
|
134
123
|
}) });
|
|
135
|
-
if (!result.matched)
|
|
136
|
-
throw new TypeError("oRPC NestJS handler returned an unmatched result, which should never happen. Please check your plugins/interceptors or report a bug.");
|
|
137
|
-
}
|
|
124
|
+
if (!result.matched) throw new TypeError("oRPC NestJS handler returned an unmatched result, which should never happen. Please check your plugins/interceptors or report a bug.");
|
|
138
125
|
const [nestBody, headers] = toNestHttpBody(result.response, standardRequest.signal, this.config.toNestResponse);
|
|
139
126
|
const httpAdapter = this.httpAdapterHost.httpAdapter;
|
|
140
127
|
httpAdapter.status(res, result.response.status);
|
|
141
|
-
for (const key of Object.keys(headers)) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
httpAdapter.appendHeader(res, key, value);
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
if (nestBody instanceof HttpException) {
|
|
151
|
-
throw nestBody;
|
|
152
|
-
}
|
|
128
|
+
for (const key of Object.keys(headers)) toArray(headers[key]).forEach((value, index) => {
|
|
129
|
+
if (index === 0) httpAdapter.setHeader(res, key, value);
|
|
130
|
+
else httpAdapter.appendHeader(res, key, value);
|
|
131
|
+
});
|
|
132
|
+
if (nestBody instanceof HttpException) throw nestBody;
|
|
153
133
|
return nestBody;
|
|
154
134
|
}));
|
|
155
135
|
}
|
|
156
136
|
};
|
|
157
|
-
ImplementInterceptor =
|
|
137
|
+
ImplementInterceptor = __decorate([
|
|
158
138
|
Injectable(),
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
139
|
+
__decorateParam(0, Inject(ORPC_MODULE_CONFIG_SYMBOL)),
|
|
140
|
+
__decorateParam(0, Optional()),
|
|
141
|
+
__decorateParam(1, Inject(HttpAdapterHost)),
|
|
142
|
+
__decorateMetadata("design:paramtypes", [Object, typeof HttpAdapterHost === "undefined" ? Object : HttpAdapterHost])
|
|
162
143
|
], ImplementInterceptor);
|
|
163
|
-
|
|
164
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Mirrors `toNodeHttpBody` from `@standard-server/node`, but returns a body NestJS can respond with.
|
|
146
|
+
*/
|
|
165
147
|
function toNestHttpBody(response, signal, options) {
|
|
166
148
|
const { status, body } = response;
|
|
167
149
|
const headers = { ...response.headers };
|
|
@@ -174,17 +156,14 @@ function toNestHttpBody(response, signal, options) {
|
|
|
174
156
|
headers["standard-server"] ??= "file";
|
|
175
157
|
headers["content-type"] ??= body.type;
|
|
176
158
|
headers["content-disposition"] ??= generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
177
|
-
|
|
178
|
-
if (Number.isFinite(body.size)) {
|
|
179
|
-
headers["content-length"] = body.size.toString();
|
|
180
|
-
}
|
|
159
|
+
if (Number.isFinite(body.size)) headers["content-length"] = body.size.toString();
|
|
181
160
|
return [toStreamableFile(Readable.fromWeb(body.stream()), headers, signal), headers];
|
|
182
161
|
}
|
|
183
|
-
headers["standard-server"] =
|
|
184
|
-
headers["content-length"] =
|
|
185
|
-
if (body ===
|
|
186
|
-
headers["content-type"] =
|
|
187
|
-
return [
|
|
162
|
+
headers["standard-server"] = void 0;
|
|
163
|
+
headers["content-length"] = void 0;
|
|
164
|
+
if (body === void 0) {
|
|
165
|
+
headers["content-type"] = void 0;
|
|
166
|
+
return [void 0, headers];
|
|
188
167
|
}
|
|
189
168
|
if (body instanceof FormData) {
|
|
190
169
|
const response = new Response(body);
|
|
@@ -201,18 +180,15 @@ function toNestHttpBody(response, signal, options) {
|
|
|
201
180
|
headers["standard-server"] = getRequiredStandardBodyHint("event-stream", headers);
|
|
202
181
|
return [toStreamableFile(toEventStream(body, options?.eventStream), headers, signal), headers];
|
|
203
182
|
}
|
|
204
|
-
|
|
205
183
|
if (status >= 300 && typeof body === "object" && body !== null && !Array.isArray(body)) {
|
|
206
|
-
if (headers["content-type"] !==
|
|
207
|
-
headers["standard-server"] = getRequiredStandardBodyHint("json", headers);
|
|
208
|
-
}
|
|
184
|
+
if (headers["content-type"] !== void 0) headers["standard-server"] = getRequiredStandardBodyHint("json", headers);
|
|
209
185
|
return [new HttpException(body, status), headers];
|
|
210
186
|
}
|
|
211
187
|
headers["content-type"] ??= "application/json";
|
|
212
188
|
headers["standard-server"] = getRequiredStandardBodyHint("json", headers);
|
|
213
|
-
return [typeof body === "string" || body === null || headers["standard-server"] !==
|
|
189
|
+
return [typeof body === "string" || body === null || headers["standard-server"] !== void 0 ? stringifyJSON(body) : body, headers];
|
|
214
190
|
}
|
|
215
|
-
|
|
191
|
+
var NestHandlerCodec = class extends OpenAPIHandlerCodecCore {
|
|
216
192
|
CONTEXT_SYMBOL = Symbol("ORPC_NEST_HANDLER_CODEC_CONTEXT");
|
|
217
193
|
withMatch(context, match) {
|
|
218
194
|
return {
|
|
@@ -222,9 +198,7 @@ class NestHandlerCodec extends OpenAPIHandlerCodecCore {
|
|
|
222
198
|
}
|
|
223
199
|
resolveProcedure(request, options) {
|
|
224
200
|
const match = options.context[this.CONTEXT_SYMBOL];
|
|
225
|
-
if (!match)
|
|
226
|
-
return undefined;
|
|
227
|
-
}
|
|
201
|
+
if (!match) return;
|
|
228
202
|
const { procedure, params } = match;
|
|
229
203
|
return {
|
|
230
204
|
path: getPathMeta(procedure) ?? [],
|
|
@@ -235,16 +209,15 @@ class NestHandlerCodec extends OpenAPIHandlerCodecCore {
|
|
|
235
209
|
}, request)
|
|
236
210
|
};
|
|
237
211
|
}
|
|
238
|
-
}
|
|
239
|
-
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Destroys the stream once the request is aborted, because Nest's Express adapter only pipes a StreamableFile
|
|
215
|
+
* and never destroys it on client disconnect, which would leave the underlying event iterator or stream uncanceled.
|
|
216
|
+
*/
|
|
240
217
|
function toStreamableFile(stream, headers, signal) {
|
|
241
218
|
const type = flattenStandardHeader(headers["content-type"]);
|
|
242
|
-
headers["content-type"] =
|
|
243
|
-
|
|
244
|
-
stream.destroy();
|
|
245
|
-
} else {
|
|
246
|
-
signal?.addEventListener("abort", () => stream.destroy(), { once: true });
|
|
247
|
-
}
|
|
219
|
+
headers["content-type"] = void 0;
|
|
220
|
+
listenAbort(signal, () => stream.destroy());
|
|
248
221
|
return new StreamableFile(stream, { type });
|
|
249
222
|
}
|
|
250
223
|
function flattenParamValue(value) {
|
|
@@ -252,20 +225,14 @@ function flattenParamValue(value) {
|
|
|
252
225
|
}
|
|
253
226
|
function toORPCOpenAPIParams(contract, params) {
|
|
254
227
|
const meta = getOpenAPIMeta(contract);
|
|
255
|
-
if (!params || meta?.path ===
|
|
256
|
-
return undefined;
|
|
257
|
-
}
|
|
258
|
-
|
|
228
|
+
if (!params || meta?.path === void 0 || Object.keys(params).length === 0) return;
|
|
259
229
|
const orpcParams = new NullProtoObj();
|
|
260
|
-
|
|
261
230
|
const restKey = Object.hasOwn(params, "*") ? "*" : "path";
|
|
262
231
|
for (const [key, value] of Object.entries(params)) {
|
|
263
232
|
if (key === restKey) {
|
|
264
233
|
const restParams = getDynamicPathParams(meta.prefix ? mergeHttpPath(meta.prefix, meta.path) : meta.path)?.filter((c) => c.allowsSlash);
|
|
265
234
|
if (restParams?.length) {
|
|
266
|
-
for (const c of restParams)
|
|
267
|
-
orpcParams[c.parameterName] = flattenParamValue(value);
|
|
268
|
-
}
|
|
235
|
+
for (const c of restParams) orpcParams[c.parameterName] = flattenParamValue(value);
|
|
269
236
|
continue;
|
|
270
237
|
}
|
|
271
238
|
}
|
|
@@ -275,9 +242,7 @@ function toORPCOpenAPIParams(contract, params) {
|
|
|
275
242
|
}
|
|
276
243
|
function toNestPattern(path) {
|
|
277
244
|
const params = getDynamicPathParams(path);
|
|
278
|
-
if (!params?.length)
|
|
279
|
-
return path;
|
|
280
|
-
}
|
|
245
|
+
if (!params?.length) return path;
|
|
281
246
|
for (let i = params.length - 1; i >= 0; i--) {
|
|
282
247
|
const param = params[i];
|
|
283
248
|
const pattern = param.allowsSlash ? `*` : `:${param.parameterName}`;
|
|
@@ -285,3 +250,4 @@ function toNestPattern(path) {
|
|
|
285
250
|
}
|
|
286
251
|
return path;
|
|
287
252
|
}
|
|
253
|
+
export { Implement, ImplementInterceptor };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ORPC_MODULE_CONFIG_SYMBOL } from "./constants.js";
|
|
2
|
+
import { Implement, ImplementInterceptor } from "./implement.js";
|
|
3
|
+
import { NestStandardLazyRequest, ORPCModule, ORPCModuleConfig } from "./module.js";
|
|
4
|
+
export { Implement as Impl, Implement, ImplementInterceptor, NestStandardLazyRequest, ORPCModule, ORPCModuleConfig, ORPC_MODULE_CONFIG_SYMBOL };
|
package/dist/index.js
ADDED
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { DynamicModule, ExecutionContext } from "@nestjs/common";
|
|
2
|
+
import { OpenAPIHandlerCodecCoreOptions } from "@orpc/openapi/standard";
|
|
3
|
+
import { DefaultInitialContext } from "@orpc/server";
|
|
4
|
+
import { StandardHandlerOptions } from "@orpc/server/standard";
|
|
5
|
+
import { Promisable, Value } from "@orpc/shared";
|
|
6
|
+
import { StandardLazyRequest } from "@standard-server/core";
|
|
7
|
+
import { ToEventStreamOptions } from "@standard-server/node";
|
|
8
|
+
/**
|
|
9
|
+
* A standard lazy request extended with NestJS route parameters,
|
|
10
|
+
* used as the request representation inside the `@orpc/nest` integration.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/integrations/nest#toneststandardlazyrequest-option | Implement oRPC contract with NestJS - toNestStandardLazyRequest option}
|
|
13
|
+
*/
|
|
14
|
+
export interface NestStandardLazyRequest extends StandardLazyRequest {
|
|
15
|
+
/**
|
|
16
|
+
* Route parameters extracted from the request path.
|
|
17
|
+
*/
|
|
18
|
+
params?: Record<string, string | string[]> | undefined;
|
|
19
|
+
}
|
|
20
|
+
export type ORPCModuleConfig = OpenAPIHandlerCodecCoreOptions<DefaultInitialContext> & StandardHandlerOptions<DefaultInitialContext> & (object extends DefaultInitialContext ? {
|
|
21
|
+
context?: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]> | undefined;
|
|
22
|
+
} : {
|
|
23
|
+
context: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]>;
|
|
24
|
+
}) & {
|
|
25
|
+
/**
|
|
26
|
+
* Customize how to convert NestJS `req` and `res` to {@link NestStandardLazyRequest}.
|
|
27
|
+
* You might need to define this if you are not using the express, fastify or node adapters.
|
|
28
|
+
*/
|
|
29
|
+
toNestStandardLazyRequest?: ((req: any, res: any) => NestStandardLazyRequest) | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Options for how to convert the Standard Response to a Nest Response (returning value), like event stream options, etc.
|
|
32
|
+
*/
|
|
33
|
+
toNestResponse?: {
|
|
34
|
+
/**
|
|
35
|
+
* Options for the event stream, like keep-alive settings, initial comment, etc.
|
|
36
|
+
*/
|
|
37
|
+
eventStream?: ToEventStreamOptions | undefined;
|
|
38
|
+
} | undefined;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* NestJS module that configures the `@orpc/nest` integration via `forRoot`
|
|
42
|
+
* or `forRootAsync`, providing options such as initial context, plugins,
|
|
43
|
+
* and interceptors to `@Implement` handlers.
|
|
44
|
+
*
|
|
45
|
+
* @see {@link https://orpc.dev/docs/integrations/nest#configuration | Implement oRPC contract with NestJS - Configuration}
|
|
46
|
+
*/
|
|
47
|
+
export declare class ORPCModule {
|
|
48
|
+
static forRoot(config: ORPCModuleConfig): DynamicModule;
|
|
49
|
+
static forRootAsync(options: {
|
|
50
|
+
imports?: any[] | undefined;
|
|
51
|
+
useFactory: (...args: any[]) => Promise<ORPCModuleConfig> | ORPCModuleConfig;
|
|
52
|
+
inject?: any[] | undefined;
|
|
53
|
+
}): DynamicModule;
|
|
54
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { ORPC_MODULE_CONFIG_SYMBOL } from "./constants.js";
|
|
2
|
+
import { ImplementInterceptor } from "./implement.js";
|
|
3
|
+
import __decorate from "./_virtual/_@oxc-project_runtime@0.157.0/helpers/esm/decorate.js";
|
|
1
4
|
import { Module } from "@nestjs/common";
|
|
2
|
-
|
|
3
|
-
import { ImplementInterceptor } from "./implement.mjs";
|
|
4
|
-
import _decorate from "@oxc-project/runtime/helpers/decorate";
|
|
5
|
+
// src/module.ts
|
|
5
6
|
var _ORPCModule;
|
|
6
7
|
let ORPCModule = _ORPCModule = class ORPCModule {
|
|
7
8
|
static forRoot(config) {
|
|
@@ -29,5 +30,5 @@ let ORPCModule = _ORPCModule = class ORPCModule {
|
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
32
|
};
|
|
32
|
-
ORPCModule = _ORPCModule =
|
|
33
|
+
ORPCModule = _ORPCModule = __decorate([Module({})], ORPCModule);
|
|
33
34
|
export { ORPCModule };
|
package/package.json
CHANGED
|
@@ -1,41 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/nest",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.48",
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
5
4
|
"description": "NestJS integration for oRPC: implement typesafe contracts in NestJS controllers",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"contract-first",
|
|
7
|
+
"nest",
|
|
8
|
+
"nestjs",
|
|
9
|
+
"openapi",
|
|
10
|
+
"orpc",
|
|
11
|
+
"rpc",
|
|
12
|
+
"typesafe",
|
|
13
|
+
"typescript"
|
|
10
14
|
],
|
|
11
15
|
"homepage": "https://orpc.dev",
|
|
16
|
+
"license": "MIT",
|
|
12
17
|
"repository": {
|
|
13
18
|
"type": "git",
|
|
14
19
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
20
|
"directory": "packages/nest"
|
|
16
21
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"openapi",
|
|
24
|
-
"typescript",
|
|
25
|
-
"typesafe"
|
|
22
|
+
"funding": [
|
|
23
|
+
"https://github.com/sponsors/dinwwwh",
|
|
24
|
+
"https://opencollective.com/middleapi"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
26
28
|
],
|
|
29
|
+
"type": "module",
|
|
27
30
|
"sideEffects": false,
|
|
28
31
|
"exports": {
|
|
29
32
|
"./package.json": "./package.json",
|
|
30
33
|
".": {
|
|
31
|
-
"types": "./dist/index.d.
|
|
32
|
-
"import": "./dist/index.
|
|
33
|
-
"default": "./dist/index.
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"default": "./dist/index.js"
|
|
34
37
|
}
|
|
35
38
|
},
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@orpc/client": "2.0.0-beta.50",
|
|
41
|
+
"@orpc/contract": "2.0.0-beta.50",
|
|
42
|
+
"@orpc/openapi": "2.0.0-beta.50",
|
|
43
|
+
"@orpc/server": "2.0.0-beta.50",
|
|
44
|
+
"@orpc/shared": "2.0.0-beta.50",
|
|
45
|
+
"@oxc-project/runtime": "^0.157.0",
|
|
46
|
+
"@standard-server/core": "~0.16.0",
|
|
47
|
+
"@standard-server/node": "~0.16.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@fastify/cookie": "^11.1.5",
|
|
51
|
+
"@nestjs/common": "^12.3.2",
|
|
52
|
+
"@nestjs/core": "^12.3.2",
|
|
53
|
+
"@nestjs/platform-express": "^12.3.2",
|
|
54
|
+
"@nestjs/platform-fastify": "^12.3.2",
|
|
55
|
+
"@nestjs/platform-node": "^12.3.2",
|
|
56
|
+
"@nestjs/testing": "^12.3.2",
|
|
57
|
+
"@types/express": "^5.0.6",
|
|
58
|
+
"express": "^5.3.0",
|
|
59
|
+
"fastify": "^5.12.8",
|
|
60
|
+
"rxjs": "^7.8.2"
|
|
61
|
+
},
|
|
39
62
|
"peerDependencies": {
|
|
40
63
|
"@nestjs/common": ">=11.1.27",
|
|
41
64
|
"@nestjs/core": ">=11.1.27",
|
|
@@ -55,30 +78,5 @@
|
|
|
55
78
|
"optional": true
|
|
56
79
|
}
|
|
57
80
|
},
|
|
58
|
-
"
|
|
59
|
-
"@orpc/client": "2.0.0-beta.48",
|
|
60
|
-
"@orpc/contract": "2.0.0-beta.48",
|
|
61
|
-
"@orpc/openapi": "2.0.0-beta.48",
|
|
62
|
-
"@orpc/server": "2.0.0-beta.48",
|
|
63
|
-
"@orpc/shared": "2.0.0-beta.48",
|
|
64
|
-
"@oxc-project/runtime": "^0.155.0",
|
|
65
|
-
"@standard-server/core": "~0.14.0",
|
|
66
|
-
"@standard-server/node": "~0.14.0"
|
|
67
|
-
},
|
|
68
|
-
"devDependencies": {
|
|
69
|
-
"@fastify/cookie": "^11.1.5",
|
|
70
|
-
"@nestjs/common": "^12.3.2",
|
|
71
|
-
"@nestjs/core": "^12.3.2",
|
|
72
|
-
"@nestjs/platform-express": "^12.3.2",
|
|
73
|
-
"@nestjs/platform-fastify": "^12.3.2",
|
|
74
|
-
"@nestjs/platform-node": "^12.3.2",
|
|
75
|
-
"@nestjs/testing": "^12.3.2",
|
|
76
|
-
"@types/express": "^5.0.6",
|
|
77
|
-
"express": "^5.3.0",
|
|
78
|
-
"fastify": "^5.12.7",
|
|
79
|
-
"rxjs": "^7.8.2"
|
|
80
|
-
},
|
|
81
|
-
"scripts": {
|
|
82
|
-
"type:check": "tsc -b"
|
|
83
|
-
}
|
|
81
|
+
"scripts": {}
|
|
84
82
|
}
|
package/dist/constants.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
package/dist/constants.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const ORPC_MODULE_CONFIG_SYMBOL = /* @__PURE__ */ Symbol.for("ORPC_NEST_MODULE_CONFIG");
|
package/dist/implement.d.mts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { CallHandler, ExecutionContext, NestInterceptor } from "@nestjs/common";
|
|
2
|
-
import type { RouterContract } from "@orpc/contract";
|
|
3
|
-
import type { ContractedRouter, DefaultInitialContext } from "@orpc/server";
|
|
4
|
-
import type { Promisable } from "@orpc/shared";
|
|
5
|
-
import type { Observable } from "rxjs";
|
|
6
|
-
import type { ORPCModuleConfig } from "./module.mjs";
|
|
7
|
-
import { HttpAdapterHost } from "@nestjs/core";
|
|
8
|
-
/**
|
|
9
|
-
* Decorator that implements an oRPC contract (procedure or router contract)
|
|
10
|
-
* on a NestJS controller method. It registers the corresponding NestJS routes
|
|
11
|
-
* and handles request decoding and response encoding for you.
|
|
12
|
-
*
|
|
13
|
-
* @remarks
|
|
14
|
-
* **Note**: Every procedure contract must define an `openapi.path` meta;
|
|
15
|
-
* use `populateRouterContractOpenAPIPaths` from `@orpc/openapi` to fill in missing paths.
|
|
16
|
-
* **Note**: The HTTP `QUERY` method requires NestJS v11.2+ (`QueryMethod`). Older NestJS versions throw; use `GET` instead.
|
|
17
|
-
*
|
|
18
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#implement-your-contract | Implement oRPC contract with NestJS - Implement Your Contract}
|
|
19
|
-
*/
|
|
20
|
-
export declare function Implement<T extends RouterContract>(contract: T): <U extends Promisable<ContractedRouter<T, DefaultInitialContext>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
|
|
21
|
-
export declare class ImplementInterceptor implements NestInterceptor {
|
|
22
|
-
private readonly config;
|
|
23
|
-
private readonly codec;
|
|
24
|
-
private readonly handler;
|
|
25
|
-
private readonly toNestStandardLazyRequest;
|
|
26
|
-
private readonly httpAdapterHost;
|
|
27
|
-
constructor(config: ORPCModuleConfig | undefined, httpAdapterHost: HttpAdapterHost);
|
|
28
|
-
intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
|
|
29
|
-
}
|
package/dist/index.d.mts
DELETED
package/dist/index.mjs
DELETED
package/dist/module.d.mts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { DynamicModule, ExecutionContext } from "@nestjs/common";
|
|
2
|
-
import type { OpenAPIHandlerCodecCoreOptions } from "@orpc/openapi/standard";
|
|
3
|
-
import type { DefaultInitialContext } from "@orpc/server";
|
|
4
|
-
import type { StandardHandlerOptions } from "@orpc/server/standard";
|
|
5
|
-
import type { Promisable, Value } from "@orpc/shared";
|
|
6
|
-
import type { StandardLazyRequest } from "@standard-server/core";
|
|
7
|
-
import type { ToEventStreamOptions } from "@standard-server/node";
|
|
8
|
-
/**
|
|
9
|
-
* A standard lazy request extended with NestJS route parameters,
|
|
10
|
-
* used as the request representation inside the `@orpc/nest` integration.
|
|
11
|
-
*
|
|
12
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#toneststandardlazyrequest-option | Implement oRPC contract with NestJS - toNestStandardLazyRequest option}
|
|
13
|
-
*/
|
|
14
|
-
export interface NestStandardLazyRequest extends StandardLazyRequest {
|
|
15
|
-
/**
|
|
16
|
-
* Route parameters extracted from the request path.
|
|
17
|
-
*/
|
|
18
|
-
params?: Record<string, string | string[]> | undefined;
|
|
19
|
-
}
|
|
20
|
-
export type ORPCModuleConfig = OpenAPIHandlerCodecCoreOptions<DefaultInitialContext> & StandardHandlerOptions<DefaultInitialContext> & (object extends DefaultInitialContext ? {
|
|
21
|
-
context?: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]> | undefined;
|
|
22
|
-
} : {
|
|
23
|
-
context: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]>;
|
|
24
|
-
}) & {
|
|
25
|
-
/**
|
|
26
|
-
* Customize how to convert NestJS `req` and `res` to {@link NestStandardLazyRequest}.
|
|
27
|
-
* You might need to define this if you are not using the express, fastify or node adapters.
|
|
28
|
-
*/
|
|
29
|
-
toNestStandardLazyRequest?: ((req: any, res: any) => NestStandardLazyRequest) | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Options for how to convert the Standard Response to a Nest Response (returning value), like event stream options, etc.
|
|
32
|
-
*/
|
|
33
|
-
toNestResponse?: {
|
|
34
|
-
/**
|
|
35
|
-
* Options for the event stream, like keep-alive settings, initial comment, etc.
|
|
36
|
-
*/
|
|
37
|
-
eventStream?: ToEventStreamOptions | undefined;
|
|
38
|
-
} | undefined;
|
|
39
|
-
};
|
|
40
|
-
export declare class ORPCModule {
|
|
41
|
-
static forRoot(config: ORPCModuleConfig): DynamicModule;
|
|
42
|
-
static forRootAsync(options: {
|
|
43
|
-
imports?: any[] | undefined;
|
|
44
|
-
useFactory: (...args: any[]) => Promise<ORPCModuleConfig> | ORPCModuleConfig;
|
|
45
|
-
inject?: any[] | undefined;
|
|
46
|
-
}): DynamicModule;
|
|
47
|
-
}
|