@orpc/nest 2.0.0-beta.47 → 2.0.0-beta.49
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 +6 -7
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -0
- package/dist/implement.d.ts +29 -0
- package/dist/implement.js +323 -0
- package/dist/index.d.ts +6 -83
- package/dist/index.js +8 -0
- package/dist/module.d.ts +54 -0
- package/dist/module.js +54 -0
- package/package.json +49 -45
- package/dist/index.d.mts +0 -85
- package/dist/index.mjs +0 -341
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)
|
|
@@ -191,15 +192,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
191
192
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
192
193
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
193
194
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
194
|
-
<td align="center"><a href="https://
|
|
195
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
195
196
|
</tr>
|
|
196
197
|
<tr>
|
|
197
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
198
198
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
199
199
|
</tr>
|
|
200
200
|
</table>
|
|
201
201
|
|
|
202
|
-
With thanks to [
|
|
202
|
+
With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
203
|
|
|
204
204
|
## Open Source Programs
|
|
205
205
|
|
|
@@ -209,7 +209,6 @@ With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
209
209
|
<tr>
|
|
210
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>
|
|
211
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>
|
|
212
|
-
<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>
|
|
213
212
|
</tr>
|
|
214
213
|
</table>
|
|
215
214
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
|
2
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
3
|
+
import type { RouterContract } from '@orpc/contract';
|
|
4
|
+
import type { ContractedRouter, DefaultInitialContext } from '@orpc/server';
|
|
5
|
+
import type { Promisable } from '@orpc/shared';
|
|
6
|
+
import type { Observable } from 'rxjs';
|
|
7
|
+
import type { ORPCModuleConfig } from './module.ts';
|
|
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
|
+
}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { Readable } from 'node:stream';
|
|
14
|
+
import * as NestCommon from '@nestjs/common';
|
|
15
|
+
import { applyDecorators, Delete, Get, Head, HttpCode, HttpException, Inject, Injectable, Optional, Options, Patch, Post, Put, StreamableFile, UseInterceptors, } from '@nestjs/common';
|
|
16
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
17
|
+
import { getPathMeta, ProcedureContract } from '@orpc/contract';
|
|
18
|
+
import { DEFAULT_OPENAPI_METHOD, getDynamicPathParams, getOpenAPIMeta } from '@orpc/openapi';
|
|
19
|
+
import { OpenAPIHandlerCodecCore } from '@orpc/openapi/standard';
|
|
20
|
+
import { DEFAULT_SUCCESS_STATUS, getRouter, Procedure, unlazy } from '@orpc/server';
|
|
21
|
+
import { StandardHandler } from '@orpc/server/standard';
|
|
22
|
+
import { isAsyncIteratorObject, mergeHttpPath, NullProtoObj, stringifyJSON, toArray, value, } from '@orpc/shared';
|
|
23
|
+
import { flattenStandardHeader, generateContentDisposition, getRequiredStandardBodyHint, } from '@standard-server/core';
|
|
24
|
+
import { toEventStream, toStandardLazyRequest } from '@standard-server/node';
|
|
25
|
+
import { mergeMap } from 'rxjs';
|
|
26
|
+
import { ORPC_MODULE_CONFIG_SYMBOL } from './constants.js';
|
|
27
|
+
// Namespace access so NestJS < 11.2 (no QueryMethod export) loads without a SyntaxError
|
|
28
|
+
const QueryMethod = NestCommon.QueryMethod;
|
|
29
|
+
const MethodDecoratorMap = {
|
|
30
|
+
HEAD: Head,
|
|
31
|
+
GET: Get,
|
|
32
|
+
POST: Post,
|
|
33
|
+
PUT: Put,
|
|
34
|
+
PATCH: Patch,
|
|
35
|
+
DELETE: Delete,
|
|
36
|
+
OPTIONS: Options,
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Decorator that implements an oRPC contract (procedure or router contract)
|
|
40
|
+
* on a NestJS controller method. It registers the corresponding NestJS routes
|
|
41
|
+
* and handles request decoding and response encoding for you.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* **Note**: Every procedure contract must define an `openapi.path` meta;
|
|
45
|
+
* use `populateRouterContractOpenAPIPaths` from `@orpc/openapi` to fill in missing paths.
|
|
46
|
+
* **Note**: The HTTP `QUERY` method requires NestJS v11.2+ (`QueryMethod`). Older NestJS versions throw; use `GET` instead.
|
|
47
|
+
*
|
|
48
|
+
* @see {@link https://orpc.dev/docs/integrations/nest#implement-your-contract | Implement oRPC contract with NestJS - Implement Your Contract}
|
|
49
|
+
*/
|
|
50
|
+
export function Implement(contract) {
|
|
51
|
+
if (contract instanceof ProcedureContract) {
|
|
52
|
+
return (target, propertyKey, descriptor) => {
|
|
53
|
+
applyDecorators(toNestRouteDecorator(contract), UseInterceptors(ImplementInterceptor))(target, propertyKey, descriptor);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return (target, propertyKey, descriptor) => {
|
|
57
|
+
// applied at the decorated method level so interceptor order follows decorator order,
|
|
58
|
+
// and synthesized methods inherit the full ordered list through the prototype chain
|
|
59
|
+
UseInterceptors(ImplementInterceptor)(target, propertyKey, descriptor);
|
|
60
|
+
implementRouterContract(contract, target, propertyKey, descriptor);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function toNestRouteDecorator(contract) {
|
|
64
|
+
const meta = getOpenAPIMeta(contract);
|
|
65
|
+
if (meta?.path === undefined) {
|
|
66
|
+
throw new TypeError(`
|
|
67
|
+
@Implement decorator requires contract to have a 'openapi.path' meta.
|
|
68
|
+
Please define one using '.meta(openapi({ path: '/example' }))'.
|
|
69
|
+
Or use "populateRouterContractOpenAPIPaths" from "@orpc/openapi" utility to automatically fill in any missing paths.
|
|
70
|
+
`);
|
|
71
|
+
}
|
|
72
|
+
const method = meta.method ?? DEFAULT_OPENAPI_METHOD;
|
|
73
|
+
const path = toNestPattern(meta.prefix ? mergeHttpPath(meta.prefix, meta.path) : meta.path);
|
|
74
|
+
const successStatus = meta.successStatus ?? DEFAULT_SUCCESS_STATUS;
|
|
75
|
+
if (method === 'QUERY') {
|
|
76
|
+
if (!QueryMethod) {
|
|
77
|
+
throw new TypeError(`
|
|
78
|
+
@Implement decorator does not support the 'QUERY' HTTP method because the installed version of NestJS does not support it.
|
|
79
|
+
The 'QUERY' HTTP method requires NestJS v11.2 or later. Alternatively, use 'GET' method.
|
|
80
|
+
`);
|
|
81
|
+
}
|
|
82
|
+
return applyDecorators(QueryMethod(path), HttpCode(successStatus));
|
|
83
|
+
}
|
|
84
|
+
return applyDecorators(MethodDecoratorMap[method](path), HttpCode(successStatus));
|
|
85
|
+
}
|
|
86
|
+
function implementRouterContract(contract, target, propertyKey, descriptor) {
|
|
87
|
+
for (const [key, childContract] of Object.entries(contract)) {
|
|
88
|
+
let methodName = `${propertyKey}_${key}`;
|
|
89
|
+
let i = 0;
|
|
90
|
+
while (methodName in target) {
|
|
91
|
+
methodName = `${propertyKey}_${key}_${i++}`;
|
|
92
|
+
}
|
|
93
|
+
target[methodName] = async function (...args) {
|
|
94
|
+
const router = await descriptor.value.apply(this, args);
|
|
95
|
+
return getRouter(router, [key]);
|
|
96
|
+
};
|
|
97
|
+
Object.setPrototypeOf(target[methodName], descriptor.value);
|
|
98
|
+
queueMicrotask(() => {
|
|
99
|
+
for (const p of Reflect.getOwnMetadataKeys(target, propertyKey)) {
|
|
100
|
+
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target, propertyKey), target, methodName);
|
|
101
|
+
}
|
|
102
|
+
for (const p of Reflect.getOwnMetadataKeys(target.constructor, propertyKey)) {
|
|
103
|
+
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target.constructor, propertyKey), target.constructor, methodName);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
const childDescriptor = Object.getOwnPropertyDescriptor(target, methodName);
|
|
107
|
+
if (childContract instanceof ProcedureContract) {
|
|
108
|
+
const routeDecorator = toNestRouteDecorator(childContract);
|
|
109
|
+
// applied after the deferred metadata copies so route metadata cannot be overridden
|
|
110
|
+
queueMicrotask(() => {
|
|
111
|
+
routeDecorator(target, methodName, childDescriptor);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
implementRouterContract(childContract, target, methodName, childDescriptor);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
let ImplementInterceptor = class ImplementInterceptor {
|
|
120
|
+
config;
|
|
121
|
+
codec;
|
|
122
|
+
handler;
|
|
123
|
+
toNestStandardLazyRequest;
|
|
124
|
+
httpAdapterHost;
|
|
125
|
+
constructor(config, httpAdapterHost) {
|
|
126
|
+
// @Optional() does not allow set default value so we need to do it here
|
|
127
|
+
this.config = config ?? {};
|
|
128
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
129
|
+
this.codec = new NestHandlerCodec(this.config);
|
|
130
|
+
this.handler = new StandardHandler(this.codec, this.config);
|
|
131
|
+
this.toNestStandardLazyRequest =
|
|
132
|
+
this.config.toNestStandardLazyRequest ??
|
|
133
|
+
((req, res) => {
|
|
134
|
+
const standardRequest = toStandardLazyRequest('raw' in req ? req.raw : req, 'raw' in res ? res.raw : res);
|
|
135
|
+
// if body already parsed by NestJS
|
|
136
|
+
if (req.body !== undefined) {
|
|
137
|
+
standardRequest.resolveBody = () => Promise.resolve(req.body);
|
|
138
|
+
}
|
|
139
|
+
standardRequest.params = req.params;
|
|
140
|
+
return standardRequest;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
intercept(ctx, next) {
|
|
144
|
+
return next.handle().pipe(mergeMap(async (impl) => {
|
|
145
|
+
const { default: procedure } = await unlazy(impl);
|
|
146
|
+
if (!(procedure instanceof Procedure)) {
|
|
147
|
+
throw new TypeError(`
|
|
148
|
+
The return value of the @Implement controller handler must be a corresponding implemented router or procedure.
|
|
149
|
+
`);
|
|
150
|
+
}
|
|
151
|
+
const req = ctx.switchToHttp().getRequest();
|
|
152
|
+
const res = ctx.switchToHttp().getResponse();
|
|
153
|
+
const standardRequest = this.toNestStandardLazyRequest(req, res);
|
|
154
|
+
const context = await value(this.config.context ?? {}, ctx);
|
|
155
|
+
const result = await this.handler.handle(standardRequest, {
|
|
156
|
+
context: this.codec.withMatch(context, { procedure, params: standardRequest.params }),
|
|
157
|
+
});
|
|
158
|
+
if (!result.matched) {
|
|
159
|
+
throw new TypeError('oRPC NestJS handler returned an unmatched result, which should never happen. Please check your plugins/interceptors or report a bug.');
|
|
160
|
+
}
|
|
161
|
+
const [nestBody, headers] = toNestHttpBody(result.response, standardRequest.signal, this.config.toNestResponse);
|
|
162
|
+
const httpAdapter = this.httpAdapterHost.httpAdapter;
|
|
163
|
+
httpAdapter.status(res, result.response.status);
|
|
164
|
+
for (const key of Object.keys(headers)) {
|
|
165
|
+
toArray(headers[key]).forEach((value, index) => {
|
|
166
|
+
if (index === 0) {
|
|
167
|
+
httpAdapter.setHeader(res, key, value);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
httpAdapter.appendHeader(res, key, value);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (nestBody instanceof HttpException) {
|
|
175
|
+
throw nestBody;
|
|
176
|
+
}
|
|
177
|
+
return nestBody;
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
ImplementInterceptor = __decorate([
|
|
182
|
+
Injectable(),
|
|
183
|
+
__param(0, Inject(ORPC_MODULE_CONFIG_SYMBOL)),
|
|
184
|
+
__param(0, Optional()),
|
|
185
|
+
__param(1, Inject(HttpAdapterHost)),
|
|
186
|
+
__metadata("design:paramtypes", [Object, HttpAdapterHost])
|
|
187
|
+
], ImplementInterceptor);
|
|
188
|
+
export { ImplementInterceptor };
|
|
189
|
+
/**
|
|
190
|
+
* Mirrors `toNodeHttpBody` from `@standard-server/node`, but returns a body NestJS can respond with.
|
|
191
|
+
*/
|
|
192
|
+
function toNestHttpBody(response, signal, options) {
|
|
193
|
+
const { status, body } = response;
|
|
194
|
+
const headers = { ...response.headers };
|
|
195
|
+
if (body instanceof ReadableStream) {
|
|
196
|
+
headers['standard-server'] ??= 'octet-stream';
|
|
197
|
+
headers['content-type'] ??= 'application/octet-stream';
|
|
198
|
+
return [toStreamableFile(Readable.fromWeb(body), headers, signal), headers];
|
|
199
|
+
}
|
|
200
|
+
if (body instanceof Blob) {
|
|
201
|
+
headers['standard-server'] ??= 'file';
|
|
202
|
+
headers['content-type'] ??= body.type;
|
|
203
|
+
headers['content-disposition'] ??= generateContentDisposition(body instanceof File ? body.name : 'blob');
|
|
204
|
+
// BunS3 can use NaN for the size
|
|
205
|
+
if (Number.isFinite(body.size)) {
|
|
206
|
+
headers['content-length'] = body.size.toString();
|
|
207
|
+
}
|
|
208
|
+
return [toStreamableFile(Readable.fromWeb(body.stream()), headers, signal), headers];
|
|
209
|
+
}
|
|
210
|
+
headers['standard-server'] = undefined;
|
|
211
|
+
headers['content-length'] = undefined;
|
|
212
|
+
if (body === undefined) {
|
|
213
|
+
headers['content-type'] = undefined;
|
|
214
|
+
return [undefined, headers];
|
|
215
|
+
}
|
|
216
|
+
if (body instanceof FormData) {
|
|
217
|
+
const response = new Response(body);
|
|
218
|
+
headers['content-type'] = response.headers.get('content-type');
|
|
219
|
+
return [toStreamableFile(Readable.fromWeb(response.body), headers, signal), headers];
|
|
220
|
+
}
|
|
221
|
+
if (body instanceof URLSearchParams) {
|
|
222
|
+
headers['content-type'] ??= 'application/x-www-form-urlencoded';
|
|
223
|
+
headers['standard-server'] = getRequiredStandardBodyHint('url-search-params', headers);
|
|
224
|
+
return [body.toString(), headers];
|
|
225
|
+
}
|
|
226
|
+
if (isAsyncIteratorObject(body)) {
|
|
227
|
+
headers['content-type'] ??= 'text/event-stream';
|
|
228
|
+
headers['standard-server'] = getRequiredStandardBodyHint('event-stream', headers);
|
|
229
|
+
return [toStreamableFile(toEventStream(body, options?.eventStream), headers, signal), headers];
|
|
230
|
+
}
|
|
231
|
+
// Prefer throwing an HttpException for more native error handling in NestJS.
|
|
232
|
+
if (status >= 300 && typeof body === 'object' && body !== null && !Array.isArray(body)) {
|
|
233
|
+
if (headers['content-type'] !== undefined) {
|
|
234
|
+
headers['standard-server'] = getRequiredStandardBodyHint('json', headers);
|
|
235
|
+
}
|
|
236
|
+
return [new HttpException(body, status), headers];
|
|
237
|
+
}
|
|
238
|
+
headers['content-type'] ??= 'application/json';
|
|
239
|
+
headers['standard-server'] = getRequiredStandardBodyHint('json', headers);
|
|
240
|
+
return [
|
|
241
|
+
typeof body === 'string' || body === null || headers['standard-server'] !== undefined
|
|
242
|
+
? stringifyJSON(body)
|
|
243
|
+
: body,
|
|
244
|
+
headers,
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
class NestHandlerCodec extends OpenAPIHandlerCodecCore {
|
|
248
|
+
CONTEXT_SYMBOL = Symbol('ORPC_NEST_HANDLER_CODEC_CONTEXT');
|
|
249
|
+
withMatch(context, match) {
|
|
250
|
+
return { ...context, [this.CONTEXT_SYMBOL]: match };
|
|
251
|
+
}
|
|
252
|
+
resolveProcedure(request, options) {
|
|
253
|
+
const match = options.context[this.CONTEXT_SYMBOL];
|
|
254
|
+
if (!match) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
const { procedure, params } = match;
|
|
258
|
+
return {
|
|
259
|
+
path: getPathMeta(procedure) ?? [],
|
|
260
|
+
procedure,
|
|
261
|
+
decodeInput: () => this.decodeInput({
|
|
262
|
+
procedure,
|
|
263
|
+
params: toORPCOpenAPIParams(procedure, params),
|
|
264
|
+
}, request),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Destroys the stream once the request is aborted, because Nest's Express adapter only pipes a StreamableFile
|
|
270
|
+
* and never destroys it on client disconnect, which would leave the underlying event iterator or stream uncanceled.
|
|
271
|
+
*/
|
|
272
|
+
function toStreamableFile(stream, headers, signal) {
|
|
273
|
+
const type = flattenStandardHeader(headers['content-type']);
|
|
274
|
+
headers['content-type'] = undefined;
|
|
275
|
+
if (signal?.aborted) {
|
|
276
|
+
stream.destroy();
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
signal?.addEventListener('abort', () => stream.destroy(), { once: true });
|
|
280
|
+
}
|
|
281
|
+
return new StreamableFile(stream, { type });
|
|
282
|
+
}
|
|
283
|
+
function flattenParamValue(value) {
|
|
284
|
+
return Array.isArray(value) ? value.join('/') : value;
|
|
285
|
+
}
|
|
286
|
+
function toORPCOpenAPIParams(contract, params) {
|
|
287
|
+
const meta = getOpenAPIMeta(contract);
|
|
288
|
+
if (!params || meta?.path === undefined || Object.keys(params).length === 0) {
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
// NullProtoObj prevents prototype injection when a param is named like `__proto__`
|
|
292
|
+
const orpcParams = new NullProtoObj();
|
|
293
|
+
// express and node use `path` while fastify uses `*` for rest matching
|
|
294
|
+
const restKey = Object.hasOwn(params, '*') ? '*' : 'path';
|
|
295
|
+
for (const [key, value] of Object.entries(params)) {
|
|
296
|
+
if (key === restKey) {
|
|
297
|
+
const restParams = getDynamicPathParams(meta.prefix ? mergeHttpPath(meta.prefix, meta.path) : meta.path)?.filter((c) => c.allowsSlash);
|
|
298
|
+
if (restParams?.length) {
|
|
299
|
+
for (const c of restParams) {
|
|
300
|
+
orpcParams[c.parameterName] = flattenParamValue(value);
|
|
301
|
+
}
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
orpcParams[key] = flattenParamValue(value);
|
|
306
|
+
}
|
|
307
|
+
return orpcParams;
|
|
308
|
+
}
|
|
309
|
+
function toNestPattern(path) {
|
|
310
|
+
const params = getDynamicPathParams(path);
|
|
311
|
+
if (!params?.length) {
|
|
312
|
+
return path;
|
|
313
|
+
}
|
|
314
|
+
for (let i = params.length - 1; i >= 0; i--) {
|
|
315
|
+
const param = params[i];
|
|
316
|
+
const pattern = param.allowsSlash ? `*` : `:${param.parameterName}`;
|
|
317
|
+
path =
|
|
318
|
+
path.slice(0, param.startIndex) +
|
|
319
|
+
pattern +
|
|
320
|
+
path.slice(param.startIndex + param.segment.length);
|
|
321
|
+
}
|
|
322
|
+
return path;
|
|
323
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,85 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
5
|
-
import { Observable } from 'rxjs';
|
|
6
|
-
import { OpenAPIHandlerCodecCoreOptions } from '@orpc/openapi/standard';
|
|
7
|
-
import { StandardHandlerOptions } from '@orpc/server/standard';
|
|
8
|
-
import { StandardLazyRequest } from '@standard-server/core';
|
|
9
|
-
import { ToEventStreamOptions } from '@standard-server/node';
|
|
10
|
-
import { HttpAdapterHost } from '@nestjs/core';
|
|
11
|
-
|
|
12
|
-
declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
|
1
|
+
export * from './constants.ts';
|
|
2
|
+
export * from './implement.ts';
|
|
3
|
+
export {
|
|
13
4
|
/**
|
|
14
|
-
*
|
|
15
|
-
* used as the request representation inside the `@orpc/nest` integration.
|
|
16
|
-
*
|
|
17
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#toneststandardlazyrequest-option | Implement oRPC contract with NestJS - toNestStandardLazyRequest option}
|
|
5
|
+
* @alias Implement
|
|
18
6
|
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* Route parameters extracted from the request path.
|
|
22
|
-
*/
|
|
23
|
-
params?: Record<string, string | string[]> | undefined;
|
|
24
|
-
}
|
|
25
|
-
type ORPCModuleConfig = OpenAPIHandlerCodecCoreOptions<DefaultInitialContext> & StandardHandlerOptions<DefaultInitialContext> & (object extends DefaultInitialContext ? {
|
|
26
|
-
context?: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]> | undefined;
|
|
27
|
-
} : {
|
|
28
|
-
context: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]>;
|
|
29
|
-
}) & {
|
|
30
|
-
/**
|
|
31
|
-
* Customize how to convert NestJS `req` and `res` to {@link NestStandardLazyRequest}.
|
|
32
|
-
* You might need to define this if you are not using express or fastify adapters.
|
|
33
|
-
*/
|
|
34
|
-
toNestStandardLazyRequest?: ((req: any, res: any) => NestStandardLazyRequest) | undefined;
|
|
35
|
-
/**
|
|
36
|
-
* Options for how to convert the Standard Response to a Nest Response (returning value), like event stream options, etc.
|
|
37
|
-
*/
|
|
38
|
-
toNestResponse?: {
|
|
39
|
-
/**
|
|
40
|
-
* Options for the event stream, like keep-alive settings, initial comment, etc.
|
|
41
|
-
*/
|
|
42
|
-
eventStream?: ToEventStreamOptions | undefined;
|
|
43
|
-
} | undefined;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* NestJS module that configures the `@orpc/nest` integration via `forRoot`
|
|
47
|
-
* or `forRootAsync`, providing options such as initial context, plugins,
|
|
48
|
-
* and interceptors to `@Implement` handlers.
|
|
49
|
-
*
|
|
50
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#configuration | Implement oRPC contract with NestJS - Configuration}
|
|
51
|
-
*/
|
|
52
|
-
declare class ORPCModule {
|
|
53
|
-
static forRoot(config: ORPCModuleConfig): DynamicModule;
|
|
54
|
-
static forRootAsync(options: {
|
|
55
|
-
imports?: any[] | undefined;
|
|
56
|
-
useFactory: (...args: any[]) => Promise<ORPCModuleConfig> | ORPCModuleConfig;
|
|
57
|
-
inject?: any[] | undefined;
|
|
58
|
-
}): DynamicModule;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Decorator that implements an oRPC contract (procedure or router contract)
|
|
63
|
-
* on a NestJS controller method. It registers the corresponding NestJS routes
|
|
64
|
-
* and handles request decoding and response encoding for you.
|
|
65
|
-
*
|
|
66
|
-
* @remarks
|
|
67
|
-
* **Note**: Every procedure contract must define an `openapi.path` meta;
|
|
68
|
-
* use `populateRouterContractOpenAPIPaths` from `@orpc/openapi` to fill in missing paths.
|
|
69
|
-
* **Note**: The HTTP `QUERY` method requires NestJS v11.2+ (`QueryMethod`). Older NestJS versions throw; use `GET` instead.
|
|
70
|
-
*
|
|
71
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#implement-your-contract | Implement oRPC contract with NestJS - Implement Your Contract}
|
|
72
|
-
*/
|
|
73
|
-
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;
|
|
74
|
-
declare class ImplementInterceptor implements NestInterceptor {
|
|
75
|
-
private readonly config;
|
|
76
|
-
private readonly codec;
|
|
77
|
-
private readonly handler;
|
|
78
|
-
private readonly toNestStandardLazyRequest;
|
|
79
|
-
private readonly httpAdapterHost;
|
|
80
|
-
constructor(config: ORPCModuleConfig | undefined, httpAdapterHost: HttpAdapterHost);
|
|
81
|
-
intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL };
|
|
85
|
-
export type { NestStandardLazyRequest, ORPCModuleConfig };
|
|
7
|
+
Implement as Impl, } from './implement.ts';
|
|
8
|
+
export * from './module.ts';
|
package/dist/index.js
ADDED
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
/**
|
|
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
|
+
}
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var ORPCModule_1;
|
|
8
|
+
import { Module } from '@nestjs/common';
|
|
9
|
+
import { ORPC_MODULE_CONFIG_SYMBOL } from './constants.js';
|
|
10
|
+
import { ImplementInterceptor } from './implement.js';
|
|
11
|
+
/**
|
|
12
|
+
* NestJS module that configures the `@orpc/nest` integration via `forRoot`
|
|
13
|
+
* or `forRootAsync`, providing options such as initial context, plugins,
|
|
14
|
+
* and interceptors to `@Implement` handlers.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.dev/docs/integrations/nest#configuration | Implement oRPC contract with NestJS - Configuration}
|
|
17
|
+
*/
|
|
18
|
+
let ORPCModule = ORPCModule_1 = class ORPCModule {
|
|
19
|
+
static forRoot(config) {
|
|
20
|
+
return {
|
|
21
|
+
module: ORPCModule_1,
|
|
22
|
+
providers: [
|
|
23
|
+
{
|
|
24
|
+
provide: ORPC_MODULE_CONFIG_SYMBOL,
|
|
25
|
+
useValue: config,
|
|
26
|
+
},
|
|
27
|
+
ImplementInterceptor,
|
|
28
|
+
],
|
|
29
|
+
exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor],
|
|
30
|
+
global: true,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
static forRootAsync(options) {
|
|
34
|
+
return {
|
|
35
|
+
module: ORPCModule_1,
|
|
36
|
+
imports: options.imports,
|
|
37
|
+
providers: [
|
|
38
|
+
{
|
|
39
|
+
provide: ORPC_MODULE_CONFIG_SYMBOL,
|
|
40
|
+
useFactory: options.useFactory,
|
|
41
|
+
inject: options.inject,
|
|
42
|
+
},
|
|
43
|
+
ImplementInterceptor,
|
|
44
|
+
],
|
|
45
|
+
exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor],
|
|
46
|
+
global: true,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
ORPCModule = ORPCModule_1 = __decorate([
|
|
51
|
+
Module({})
|
|
52
|
+
// oxlint-disable-next-line typescript/no-extraneous-class
|
|
53
|
+
], ORPCModule);
|
|
54
|
+
export { ORPCModule };
|
package/package.json
CHANGED
|
@@ -1,49 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/nest",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.47",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
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
|
-
"nest",
|
|
21
|
-
"contract-first",
|
|
22
|
-
"rpc",
|
|
23
|
-
"openapi",
|
|
24
|
-
"typescript",
|
|
25
|
-
"typesafe"
|
|
22
|
+
"funding": [
|
|
23
|
+
"https://github.com/sponsors/dinwwwh",
|
|
24
|
+
"https://opencollective.com/middleapi"
|
|
26
25
|
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
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.49",
|
|
41
|
+
"@orpc/contract": "2.0.0-beta.49",
|
|
42
|
+
"@orpc/openapi": "2.0.0-beta.49",
|
|
43
|
+
"@orpc/server": "2.0.0-beta.49",
|
|
44
|
+
"@orpc/shared": "2.0.0-beta.49",
|
|
45
|
+
"@oxc-project/runtime": "^0.155.0",
|
|
46
|
+
"@standard-server/core": "~0.15.0",
|
|
47
|
+
"@standard-server/node": "~0.15.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",
|
|
65
|
+
"@nestjs/platform-node": ">=12.3.0",
|
|
42
66
|
"express": ">=5.2.1",
|
|
43
67
|
"fastify": ">=5.8.5",
|
|
44
68
|
"rxjs": ">=7.8.2"
|
|
45
69
|
},
|
|
46
70
|
"peerDependenciesMeta": {
|
|
71
|
+
"@nestjs/platform-node": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
47
74
|
"express": {
|
|
48
75
|
"optional": true
|
|
49
76
|
},
|
|
@@ -51,28 +78,5 @@
|
|
|
51
78
|
"optional": true
|
|
52
79
|
}
|
|
53
80
|
},
|
|
54
|
-
"
|
|
55
|
-
"@orpc/client": "2.0.0-beta.47",
|
|
56
|
-
"@orpc/contract": "2.0.0-beta.47",
|
|
57
|
-
"@orpc/openapi": "2.0.0-beta.47",
|
|
58
|
-
"@orpc/server": "2.0.0-beta.47",
|
|
59
|
-
"@orpc/shared": "2.0.0-beta.47",
|
|
60
|
-
"@standard-server/core": "~0.13.0",
|
|
61
|
-
"@standard-server/node": "~0.13.0"
|
|
62
|
-
},
|
|
63
|
-
"devDependencies": {
|
|
64
|
-
"@fastify/cookie": "^11.1.3",
|
|
65
|
-
"@nestjs/common": "^12.1.3",
|
|
66
|
-
"@nestjs/core": "^12.1.3",
|
|
67
|
-
"@nestjs/platform-express": "^12.1.3",
|
|
68
|
-
"@nestjs/platform-fastify": "^12.1.3",
|
|
69
|
-
"@nestjs/testing": "^12.1.3",
|
|
70
|
-
"@types/express": "^5.0.6",
|
|
71
|
-
"express": "^5.3.0",
|
|
72
|
-
"fastify": "^5.12.7",
|
|
73
|
-
"rxjs": "^7.8.2"
|
|
74
|
-
},
|
|
75
|
-
"scripts": {
|
|
76
|
-
"type:check": "tsc -b"
|
|
77
|
-
}
|
|
81
|
+
"scripts": {}
|
|
78
82
|
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { ExecutionContext, DynamicModule, NestInterceptor, CallHandler } from '@nestjs/common';
|
|
2
|
-
import { RouterContract } from '@orpc/contract';
|
|
3
|
-
import { DefaultInitialContext, ContractedRouter } from '@orpc/server';
|
|
4
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
5
|
-
import { Observable } from 'rxjs';
|
|
6
|
-
import { OpenAPIHandlerCodecCoreOptions } from '@orpc/openapi/standard';
|
|
7
|
-
import { StandardHandlerOptions } from '@orpc/server/standard';
|
|
8
|
-
import { StandardLazyRequest } from '@standard-server/core';
|
|
9
|
-
import { ToEventStreamOptions } from '@standard-server/node';
|
|
10
|
-
import { HttpAdapterHost } from '@nestjs/core';
|
|
11
|
-
|
|
12
|
-
declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
|
13
|
-
/**
|
|
14
|
-
* A standard lazy request extended with NestJS route parameters,
|
|
15
|
-
* used as the request representation inside the `@orpc/nest` integration.
|
|
16
|
-
*
|
|
17
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#toneststandardlazyrequest-option | Implement oRPC contract with NestJS - toNestStandardLazyRequest option}
|
|
18
|
-
*/
|
|
19
|
-
interface NestStandardLazyRequest extends StandardLazyRequest {
|
|
20
|
-
/**
|
|
21
|
-
* Route parameters extracted from the request path.
|
|
22
|
-
*/
|
|
23
|
-
params?: Record<string, string | string[]> | undefined;
|
|
24
|
-
}
|
|
25
|
-
type ORPCModuleConfig = OpenAPIHandlerCodecCoreOptions<DefaultInitialContext> & StandardHandlerOptions<DefaultInitialContext> & (object extends DefaultInitialContext ? {
|
|
26
|
-
context?: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]> | undefined;
|
|
27
|
-
} : {
|
|
28
|
-
context: Value<Promisable<DefaultInitialContext>, [ctx: ExecutionContext]>;
|
|
29
|
-
}) & {
|
|
30
|
-
/**
|
|
31
|
-
* Customize how to convert NestJS `req` and `res` to {@link NestStandardLazyRequest}.
|
|
32
|
-
* You might need to define this if you are not using express or fastify adapters.
|
|
33
|
-
*/
|
|
34
|
-
toNestStandardLazyRequest?: ((req: any, res: any) => NestStandardLazyRequest) | undefined;
|
|
35
|
-
/**
|
|
36
|
-
* Options for how to convert the Standard Response to a Nest Response (returning value), like event stream options, etc.
|
|
37
|
-
*/
|
|
38
|
-
toNestResponse?: {
|
|
39
|
-
/**
|
|
40
|
-
* Options for the event stream, like keep-alive settings, initial comment, etc.
|
|
41
|
-
*/
|
|
42
|
-
eventStream?: ToEventStreamOptions | undefined;
|
|
43
|
-
} | undefined;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* NestJS module that configures the `@orpc/nest` integration via `forRoot`
|
|
47
|
-
* or `forRootAsync`, providing options such as initial context, plugins,
|
|
48
|
-
* and interceptors to `@Implement` handlers.
|
|
49
|
-
*
|
|
50
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#configuration | Implement oRPC contract with NestJS - Configuration}
|
|
51
|
-
*/
|
|
52
|
-
declare class ORPCModule {
|
|
53
|
-
static forRoot(config: ORPCModuleConfig): DynamicModule;
|
|
54
|
-
static forRootAsync(options: {
|
|
55
|
-
imports?: any[] | undefined;
|
|
56
|
-
useFactory: (...args: any[]) => Promise<ORPCModuleConfig> | ORPCModuleConfig;
|
|
57
|
-
inject?: any[] | undefined;
|
|
58
|
-
}): DynamicModule;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Decorator that implements an oRPC contract (procedure or router contract)
|
|
63
|
-
* on a NestJS controller method. It registers the corresponding NestJS routes
|
|
64
|
-
* and handles request decoding and response encoding for you.
|
|
65
|
-
*
|
|
66
|
-
* @remarks
|
|
67
|
-
* **Note**: Every procedure contract must define an `openapi.path` meta;
|
|
68
|
-
* use `populateRouterContractOpenAPIPaths` from `@orpc/openapi` to fill in missing paths.
|
|
69
|
-
* **Note**: The HTTP `QUERY` method requires NestJS v11.2+ (`QueryMethod`). Older NestJS versions throw; use `GET` instead.
|
|
70
|
-
*
|
|
71
|
-
* @see {@link https://orpc.dev/docs/integrations/nest#implement-your-contract | Implement oRPC contract with NestJS - Implement Your Contract}
|
|
72
|
-
*/
|
|
73
|
-
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;
|
|
74
|
-
declare class ImplementInterceptor implements NestInterceptor {
|
|
75
|
-
private readonly config;
|
|
76
|
-
private readonly codec;
|
|
77
|
-
private readonly handler;
|
|
78
|
-
private readonly toNestStandardLazyRequest;
|
|
79
|
-
private readonly httpAdapterHost;
|
|
80
|
-
constructor(config: ORPCModuleConfig | undefined, httpAdapterHost: HttpAdapterHost);
|
|
81
|
-
intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL };
|
|
85
|
-
export type { NestStandardLazyRequest, ORPCModuleConfig };
|
package/dist/index.mjs
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
import { Readable } from 'node:stream';
|
|
2
|
-
import * as NestCommon from '@nestjs/common';
|
|
3
|
-
import { Module, applyDecorators, UseInterceptors, HttpException, HttpCode, Options, Delete, Patch, Put, Post, Get, Head, StreamableFile, Injectable, Inject, Optional } from '@nestjs/common';
|
|
4
|
-
import { HttpAdapterHost } from '@nestjs/core';
|
|
5
|
-
import { ProcedureContract, getPathMeta } from '@orpc/contract';
|
|
6
|
-
import { getOpenAPIMeta, DEFAULT_OPENAPI_METHOD, getDynamicPathParams } from '@orpc/openapi';
|
|
7
|
-
import { OpenAPIHandlerCodecCore } from '@orpc/openapi/standard';
|
|
8
|
-
import { unlazy, Procedure, DEFAULT_SUCCESS_STATUS, getRouter } from '@orpc/server';
|
|
9
|
-
import { StandardHandler } from '@orpc/server/standard';
|
|
10
|
-
import { value, toArray, mergeHttpPath, isAsyncIteratorObject, stringifyJSON, NullProtoObj } from '@orpc/shared';
|
|
11
|
-
import { generateContentDisposition, getRequiredStandardBodyHint, flattenStandardHeader } from '@standard-server/core';
|
|
12
|
-
import { toStandardLazyRequest, toEventStream } from '@standard-server/node';
|
|
13
|
-
import { mergeMap } from 'rxjs';
|
|
14
|
-
|
|
15
|
-
var __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor;
|
|
16
|
-
var __decorateClass$1 = (decorators, target, key, kind) => {
|
|
17
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target;
|
|
18
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
19
|
-
if (decorator = decorators[i])
|
|
20
|
-
result = (decorator(result)) || result;
|
|
21
|
-
return result;
|
|
22
|
-
};
|
|
23
|
-
const ORPC_MODULE_CONFIG_SYMBOL = Symbol.for("ORPC_NEST_MODULE_CONFIG");
|
|
24
|
-
let ORPCModule = class {
|
|
25
|
-
static forRoot(config) {
|
|
26
|
-
return {
|
|
27
|
-
module: ORPCModule,
|
|
28
|
-
providers: [
|
|
29
|
-
{
|
|
30
|
-
provide: ORPC_MODULE_CONFIG_SYMBOL,
|
|
31
|
-
useValue: config
|
|
32
|
-
},
|
|
33
|
-
ImplementInterceptor
|
|
34
|
-
],
|
|
35
|
-
exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor],
|
|
36
|
-
global: true
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
static forRootAsync(options) {
|
|
40
|
-
return {
|
|
41
|
-
module: ORPCModule,
|
|
42
|
-
imports: options.imports,
|
|
43
|
-
providers: [
|
|
44
|
-
{
|
|
45
|
-
provide: ORPC_MODULE_CONFIG_SYMBOL,
|
|
46
|
-
useFactory: options.useFactory,
|
|
47
|
-
inject: options.inject
|
|
48
|
-
},
|
|
49
|
-
ImplementInterceptor
|
|
50
|
-
],
|
|
51
|
-
exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor],
|
|
52
|
-
global: true
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
ORPCModule = __decorateClass$1([
|
|
57
|
-
Module({})
|
|
58
|
-
], ORPCModule);
|
|
59
|
-
|
|
60
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
61
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
62
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
63
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
64
|
-
if (decorator = decorators[i])
|
|
65
|
-
result = (decorator(result)) || result;
|
|
66
|
-
return result;
|
|
67
|
-
};
|
|
68
|
-
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
69
|
-
const QueryMethod = NestCommon.QueryMethod;
|
|
70
|
-
const MethodDecoratorMap = {
|
|
71
|
-
HEAD: Head,
|
|
72
|
-
GET: Get,
|
|
73
|
-
POST: Post,
|
|
74
|
-
PUT: Put,
|
|
75
|
-
PATCH: Patch,
|
|
76
|
-
DELETE: Delete,
|
|
77
|
-
OPTIONS: Options
|
|
78
|
-
};
|
|
79
|
-
function Implement(contract) {
|
|
80
|
-
if (contract instanceof ProcedureContract) {
|
|
81
|
-
return (target, propertyKey, descriptor) => {
|
|
82
|
-
applyDecorators(
|
|
83
|
-
toNestRouteDecorator(contract),
|
|
84
|
-
UseInterceptors(ImplementInterceptor)
|
|
85
|
-
)(target, propertyKey, descriptor);
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return (target, propertyKey, descriptor) => {
|
|
89
|
-
UseInterceptors(ImplementInterceptor)(target, propertyKey, descriptor);
|
|
90
|
-
implementRouterContract(contract, target, propertyKey, descriptor);
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
function toNestRouteDecorator(contract) {
|
|
94
|
-
const meta = getOpenAPIMeta(contract);
|
|
95
|
-
if (meta?.path === void 0) {
|
|
96
|
-
throw new TypeError(`
|
|
97
|
-
@Implement decorator requires contract to have a 'openapi.path' meta.
|
|
98
|
-
Please define one using '.meta(openapi({ path: '/example' }))'.
|
|
99
|
-
Or use "populateRouterContractOpenAPIPaths" from "@orpc/openapi" utility to automatically fill in any missing paths.
|
|
100
|
-
`);
|
|
101
|
-
}
|
|
102
|
-
const method = meta.method ?? DEFAULT_OPENAPI_METHOD;
|
|
103
|
-
const path = toNestPattern(meta.prefix ? mergeHttpPath(meta.prefix, meta.path) : meta.path);
|
|
104
|
-
const successStatus = meta.successStatus ?? DEFAULT_SUCCESS_STATUS;
|
|
105
|
-
if (method === "QUERY") {
|
|
106
|
-
if (!QueryMethod) {
|
|
107
|
-
throw new TypeError(`
|
|
108
|
-
@Implement decorator does not support the 'QUERY' HTTP method because the installed version of NestJS does not support it.
|
|
109
|
-
The 'QUERY' HTTP method requires NestJS v11.2 or later. Alternatively, use 'GET' method.
|
|
110
|
-
`);
|
|
111
|
-
}
|
|
112
|
-
return applyDecorators(
|
|
113
|
-
QueryMethod(path),
|
|
114
|
-
HttpCode(successStatus)
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
return applyDecorators(
|
|
118
|
-
MethodDecoratorMap[method](path),
|
|
119
|
-
HttpCode(successStatus)
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
function implementRouterContract(contract, target, propertyKey, descriptor) {
|
|
123
|
-
for (const [key, childContract] of Object.entries(contract)) {
|
|
124
|
-
let methodName = `${propertyKey}_${key}`;
|
|
125
|
-
let i = 0;
|
|
126
|
-
while (methodName in target) {
|
|
127
|
-
methodName = `${propertyKey}_${key}_${i++}`;
|
|
128
|
-
}
|
|
129
|
-
target[methodName] = async function(...args) {
|
|
130
|
-
const router = await descriptor.value.apply(this, args);
|
|
131
|
-
return getRouter(router, [key]);
|
|
132
|
-
};
|
|
133
|
-
Object.setPrototypeOf(target[methodName], descriptor.value);
|
|
134
|
-
queueMicrotask(() => {
|
|
135
|
-
for (const p of Reflect.getOwnMetadataKeys(target, propertyKey)) {
|
|
136
|
-
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target, propertyKey), target, methodName);
|
|
137
|
-
}
|
|
138
|
-
for (const p of Reflect.getOwnMetadataKeys(target.constructor, propertyKey)) {
|
|
139
|
-
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target.constructor, propertyKey), target.constructor, methodName);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
const childDescriptor = Object.getOwnPropertyDescriptor(target, methodName);
|
|
143
|
-
if (childContract instanceof ProcedureContract) {
|
|
144
|
-
const routeDecorator = toNestRouteDecorator(childContract);
|
|
145
|
-
queueMicrotask(() => {
|
|
146
|
-
routeDecorator(target, methodName, childDescriptor);
|
|
147
|
-
});
|
|
148
|
-
} else {
|
|
149
|
-
implementRouterContract(childContract, target, methodName, childDescriptor);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
let ImplementInterceptor = class {
|
|
154
|
-
config;
|
|
155
|
-
codec;
|
|
156
|
-
handler;
|
|
157
|
-
toNestStandardLazyRequest;
|
|
158
|
-
httpAdapterHost;
|
|
159
|
-
constructor(config, httpAdapterHost) {
|
|
160
|
-
this.config = config ?? {};
|
|
161
|
-
this.httpAdapterHost = httpAdapterHost;
|
|
162
|
-
this.codec = new NestHandlerCodec(this.config);
|
|
163
|
-
this.handler = new StandardHandler(this.codec, this.config);
|
|
164
|
-
this.toNestStandardLazyRequest = this.config.toNestStandardLazyRequest ?? ((req, res) => {
|
|
165
|
-
const standardRequest = toStandardLazyRequest(
|
|
166
|
-
"raw" in req ? req.raw : req,
|
|
167
|
-
"raw" in res ? res.raw : res
|
|
168
|
-
);
|
|
169
|
-
if (req.body !== void 0) {
|
|
170
|
-
standardRequest.resolveBody = () => Promise.resolve(req.body);
|
|
171
|
-
}
|
|
172
|
-
standardRequest.params = req.params;
|
|
173
|
-
return standardRequest;
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
intercept(ctx, next) {
|
|
177
|
-
return next.handle().pipe(
|
|
178
|
-
mergeMap(async (impl) => {
|
|
179
|
-
const { default: procedure } = await unlazy(impl);
|
|
180
|
-
if (!(procedure instanceof Procedure)) {
|
|
181
|
-
throw new TypeError(`
|
|
182
|
-
The return value of the @Implement controller handler must be a corresponding implemented router or procedure.
|
|
183
|
-
`);
|
|
184
|
-
}
|
|
185
|
-
const req = ctx.switchToHttp().getRequest();
|
|
186
|
-
const res = ctx.switchToHttp().getResponse();
|
|
187
|
-
const standardRequest = this.toNestStandardLazyRequest(req, res);
|
|
188
|
-
const context = await value(this.config.context ?? {}, ctx);
|
|
189
|
-
const result = await this.handler.handle(standardRequest, {
|
|
190
|
-
context: this.codec.withMatch(context, { procedure, params: standardRequest.params })
|
|
191
|
-
});
|
|
192
|
-
if (!result.matched) {
|
|
193
|
-
throw new TypeError(
|
|
194
|
-
"oRPC NestJS handler returned an unmatched result, which should never happen. Please check your plugins/interceptors or report a bug."
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
const [nestBody, headers] = toNestHttpBody(result.response, standardRequest.signal, this.config.toNestResponse);
|
|
198
|
-
const httpAdapter = this.httpAdapterHost.httpAdapter;
|
|
199
|
-
httpAdapter.status(res, result.response.status);
|
|
200
|
-
for (const key of Object.keys(headers)) {
|
|
201
|
-
toArray(headers[key]).forEach((value2, index) => {
|
|
202
|
-
if (index === 0) {
|
|
203
|
-
httpAdapter.setHeader(res, key, value2);
|
|
204
|
-
} else {
|
|
205
|
-
httpAdapter.appendHeader(res, key, value2);
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
if (nestBody instanceof HttpException) {
|
|
210
|
-
throw nestBody;
|
|
211
|
-
}
|
|
212
|
-
return nestBody;
|
|
213
|
-
})
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
ImplementInterceptor = __decorateClass([
|
|
218
|
-
Injectable(),
|
|
219
|
-
__decorateParam(0, Inject(ORPC_MODULE_CONFIG_SYMBOL)),
|
|
220
|
-
__decorateParam(0, Optional()),
|
|
221
|
-
__decorateParam(1, Inject(HttpAdapterHost))
|
|
222
|
-
], ImplementInterceptor);
|
|
223
|
-
function toNestHttpBody(response, signal, options) {
|
|
224
|
-
const { status, body } = response;
|
|
225
|
-
const headers = { ...response.headers };
|
|
226
|
-
if (body instanceof ReadableStream) {
|
|
227
|
-
headers["standard-server"] ??= "octet-stream";
|
|
228
|
-
headers["content-type"] ??= "application/octet-stream";
|
|
229
|
-
return [toStreamableFile(Readable.fromWeb(body), headers, signal), headers];
|
|
230
|
-
}
|
|
231
|
-
if (body instanceof Blob) {
|
|
232
|
-
headers["standard-server"] ??= "file";
|
|
233
|
-
headers["content-type"] ??= body.type;
|
|
234
|
-
headers["content-disposition"] ??= generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
235
|
-
if (Number.isFinite(body.size)) {
|
|
236
|
-
headers["content-length"] = body.size.toString();
|
|
237
|
-
}
|
|
238
|
-
return [toStreamableFile(Readable.fromWeb(body.stream()), headers, signal), headers];
|
|
239
|
-
}
|
|
240
|
-
headers["standard-server"] = void 0;
|
|
241
|
-
headers["content-length"] = void 0;
|
|
242
|
-
if (body === void 0) {
|
|
243
|
-
headers["standard-server"] = getRequiredStandardBodyHint("none", headers);
|
|
244
|
-
return [void 0, headers];
|
|
245
|
-
}
|
|
246
|
-
if (body instanceof FormData) {
|
|
247
|
-
const response2 = new Response(body);
|
|
248
|
-
headers["content-type"] = response2.headers.get("content-type");
|
|
249
|
-
return [toStreamableFile(Readable.fromWeb(response2.body), headers, signal), headers];
|
|
250
|
-
}
|
|
251
|
-
if (body instanceof URLSearchParams) {
|
|
252
|
-
headers["content-type"] ??= "application/x-www-form-urlencoded";
|
|
253
|
-
headers["standard-server"] = getRequiredStandardBodyHint("url-search-params", headers);
|
|
254
|
-
return [body.toString(), headers];
|
|
255
|
-
}
|
|
256
|
-
if (isAsyncIteratorObject(body)) {
|
|
257
|
-
headers["content-type"] ??= "text/event-stream";
|
|
258
|
-
headers["standard-server"] = getRequiredStandardBodyHint("event-stream", headers);
|
|
259
|
-
return [toStreamableFile(toEventStream(body, options?.eventStream), headers, signal), headers];
|
|
260
|
-
}
|
|
261
|
-
if (status >= 300 && typeof body === "object" && body !== null && !Array.isArray(body)) {
|
|
262
|
-
if (headers["content-type"] !== void 0) {
|
|
263
|
-
headers["standard-server"] = getRequiredStandardBodyHint("json", headers);
|
|
264
|
-
}
|
|
265
|
-
return [new HttpException(body, status), headers];
|
|
266
|
-
}
|
|
267
|
-
headers["content-type"] ??= "application/json";
|
|
268
|
-
headers["standard-server"] = getRequiredStandardBodyHint("json", headers);
|
|
269
|
-
return [typeof body === "string" || body === null || headers["standard-server"] !== void 0 ? stringifyJSON(body) : body, headers];
|
|
270
|
-
}
|
|
271
|
-
class NestHandlerCodec extends OpenAPIHandlerCodecCore {
|
|
272
|
-
CONTEXT_SYMBOL = Symbol("ORPC_NEST_HANDLER_CODEC_CONTEXT");
|
|
273
|
-
withMatch(context, match) {
|
|
274
|
-
return { ...context, [this.CONTEXT_SYMBOL]: match };
|
|
275
|
-
}
|
|
276
|
-
resolveProcedure(request, options) {
|
|
277
|
-
const match = options.context[this.CONTEXT_SYMBOL];
|
|
278
|
-
if (!match) {
|
|
279
|
-
return void 0;
|
|
280
|
-
}
|
|
281
|
-
const { procedure, params } = match;
|
|
282
|
-
return {
|
|
283
|
-
path: getPathMeta(procedure) ?? [],
|
|
284
|
-
procedure,
|
|
285
|
-
decodeInput: () => this.decodeInput({
|
|
286
|
-
procedure,
|
|
287
|
-
params: toORPCOpenAPIParams(procedure, params)
|
|
288
|
-
}, request)
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
function toStreamableFile(stream, headers, signal) {
|
|
293
|
-
const type = flattenStandardHeader(headers["content-type"]);
|
|
294
|
-
headers["content-type"] = void 0;
|
|
295
|
-
if (signal?.aborted) {
|
|
296
|
-
stream.destroy();
|
|
297
|
-
} else {
|
|
298
|
-
signal?.addEventListener("abort", () => stream.destroy(), { once: true });
|
|
299
|
-
}
|
|
300
|
-
return new StreamableFile(stream, { type });
|
|
301
|
-
}
|
|
302
|
-
function flattenParamValue(value2) {
|
|
303
|
-
return Array.isArray(value2) ? value2.join("/") : value2;
|
|
304
|
-
}
|
|
305
|
-
function toORPCOpenAPIParams(contract, params) {
|
|
306
|
-
const meta = getOpenAPIMeta(contract);
|
|
307
|
-
if (!params || meta?.path === void 0 || Object.keys(params).length === 0) {
|
|
308
|
-
return void 0;
|
|
309
|
-
}
|
|
310
|
-
const orpcParams = new NullProtoObj();
|
|
311
|
-
const restKey = Object.hasOwn(params, "*") ? "*" : "path";
|
|
312
|
-
for (const [key, value2] of Object.entries(params)) {
|
|
313
|
-
if (key === restKey) {
|
|
314
|
-
const restParams = getDynamicPathParams(
|
|
315
|
-
meta.prefix ? mergeHttpPath(meta.prefix, meta.path) : meta.path
|
|
316
|
-
)?.filter((c) => c.allowsSlash);
|
|
317
|
-
if (restParams?.length) {
|
|
318
|
-
for (const c of restParams) {
|
|
319
|
-
orpcParams[c.parameterName] = flattenParamValue(value2);
|
|
320
|
-
}
|
|
321
|
-
continue;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
orpcParams[key] = flattenParamValue(value2);
|
|
325
|
-
}
|
|
326
|
-
return orpcParams;
|
|
327
|
-
}
|
|
328
|
-
function toNestPattern(path) {
|
|
329
|
-
const params = getDynamicPathParams(path);
|
|
330
|
-
if (!params?.length) {
|
|
331
|
-
return path;
|
|
332
|
-
}
|
|
333
|
-
for (let i = params.length - 1; i >= 0; i--) {
|
|
334
|
-
const param = params[i];
|
|
335
|
-
const pattern = param.allowsSlash ? `*` : `:${param.parameterName}`;
|
|
336
|
-
path = path.slice(0, param.startIndex) + pattern + path.slice(param.startIndex + param.segment.length);
|
|
337
|
-
}
|
|
338
|
-
return path;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL };
|