@orpc/nest 0.0.0-next.01ede75
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/LICENSE +21 -0
- package/README.md +130 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.mjs +191 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 oRPC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<h1></h1>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<a href="https://codecov.io/gh/unnoq/orpc">
|
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/@orpc/nest">
|
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fnest?logo=npm" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
|
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
|
+
</a>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
|
+
|
|
24
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Highlights
|
|
29
|
+
|
|
30
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
31
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
32
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
33
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
|
|
34
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
35
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
36
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
37
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
38
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
39
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
40
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
41
|
+
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
|
42
|
+
|
|
43
|
+
## Documentation
|
|
44
|
+
|
|
45
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
|
46
|
+
|
|
47
|
+
## Packages
|
|
48
|
+
|
|
49
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
50
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
51
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
52
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
53
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
54
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
55
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
56
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
57
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
58
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
59
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
60
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
|
+
|
|
62
|
+
## `@orpc/nest`
|
|
63
|
+
|
|
64
|
+
Deeply integrate oRPC with [NestJS](https://nestjs.com/). Read the [documentation](https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest) for more information.
|
|
65
|
+
|
|
66
|
+
### Implement Contract
|
|
67
|
+
|
|
68
|
+
An overview of how to implement an [oRPC contract](https://orpc.unnoq.com/docs/contract-first/define-contract) in NestJS.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { Implement, implement, ORPCError } from '@orpc/nest'
|
|
72
|
+
|
|
73
|
+
@Controller()
|
|
74
|
+
export class PlanetController {
|
|
75
|
+
/**
|
|
76
|
+
* Implement a standalone procedure
|
|
77
|
+
*/
|
|
78
|
+
@Implement(contract.planet.list)
|
|
79
|
+
list() {
|
|
80
|
+
return implement(contract.planet.list).handler(({ input }) => {
|
|
81
|
+
// Implement logic here
|
|
82
|
+
|
|
83
|
+
return []
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Implement entire a contract
|
|
89
|
+
*/
|
|
90
|
+
@Implement(contract.planet)
|
|
91
|
+
planet() {
|
|
92
|
+
return {
|
|
93
|
+
list: implement(contract.planet.list).handler(({ input }) => {
|
|
94
|
+
// Implement logic here
|
|
95
|
+
return []
|
|
96
|
+
}),
|
|
97
|
+
find: implement(contract.planet.find).handler(({ input }) => {
|
|
98
|
+
// Implement logic here
|
|
99
|
+
return {
|
|
100
|
+
id: 1,
|
|
101
|
+
name: 'Earth',
|
|
102
|
+
description: 'The planet Earth',
|
|
103
|
+
}
|
|
104
|
+
}),
|
|
105
|
+
create: implement(contract.planet.create).handler(({ input }) => {
|
|
106
|
+
// Implement logic here
|
|
107
|
+
return {
|
|
108
|
+
id: 1,
|
|
109
|
+
name: 'Earth',
|
|
110
|
+
description: 'The planet Earth',
|
|
111
|
+
}
|
|
112
|
+
}),
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// other handlers...
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Sponsors
|
|
121
|
+
|
|
122
|
+
<p align="center">
|
|
123
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
124
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
125
|
+
</a>
|
|
126
|
+
</p>
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DynamicModule, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
2
|
+
import { AnySchema, ContractRouter, HTTPPath, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
|
3
|
+
import { CreateProcedureClientOptions, Router } from '@orpc/server';
|
|
4
|
+
export { ImplementedProcedure, Implementer, ImplementerInternal, ImplementerInternalWithMiddlewares, ORPCError, ProcedureImplementer, RouterImplementer, RouterImplementerWithMiddlewares, implement, onError, onFinish, onStart, onSuccess } from '@orpc/server';
|
|
5
|
+
import { Promisable } from '@orpc/shared';
|
|
6
|
+
import { Observable } from 'rxjs';
|
|
7
|
+
import { SendStandardResponseOptions } from '@orpc/standard-server-node';
|
|
8
|
+
|
|
9
|
+
declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
|
10
|
+
interface ORPCModuleConfig extends CreateProcedureClientOptions<object, AnySchema, object, object, object>, SendStandardResponseOptions {
|
|
11
|
+
}
|
|
12
|
+
declare class ORPCModule {
|
|
13
|
+
static forRoot(config: ORPCModuleConfig): DynamicModule;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Decorator in controller handler to implement a oRPC contract.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
|
|
20
|
+
*/
|
|
21
|
+
declare function Implement<T extends ContractRouter<any>>(contract: T): <U extends Promisable<Router<T, Record<never, never>>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
|
|
22
|
+
declare class ImplementInterceptor implements NestInterceptor {
|
|
23
|
+
private readonly config;
|
|
24
|
+
constructor(config: ORPCModuleConfig | undefined);
|
|
25
|
+
intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function toNestPattern(path: HTTPPath): string;
|
|
29
|
+
type PopulatedContractRouterPaths<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, UErrors, UMeta> : {
|
|
30
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? PopulatedContractRouterPaths<T[K]> : never;
|
|
31
|
+
};
|
|
32
|
+
interface PopulateContractRouterPathsOptions {
|
|
33
|
+
path?: readonly string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* populateContractRouterPaths is completely optional,
|
|
37
|
+
* because the procedure's path is required for NestJS implementation.
|
|
38
|
+
* This utility automatically populates any missing paths
|
|
39
|
+
* Using the router's keys + `/`.
|
|
40
|
+
*
|
|
41
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#define-your-contract NestJS Implement Contract Docs}
|
|
42
|
+
*/
|
|
43
|
+
declare function populateContractRouterPaths<T extends AnyContractRouter>(router: T, options?: PopulateContractRouterPathsOptions): PopulatedContractRouterPaths<T>;
|
|
44
|
+
|
|
45
|
+
export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, populateContractRouterPaths, toNestPattern };
|
|
46
|
+
export type { ORPCModuleConfig, PopulateContractRouterPathsOptions, PopulatedContractRouterPaths };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DynamicModule, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
2
|
+
import { AnySchema, ContractRouter, HTTPPath, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
|
3
|
+
import { CreateProcedureClientOptions, Router } from '@orpc/server';
|
|
4
|
+
export { ImplementedProcedure, Implementer, ImplementerInternal, ImplementerInternalWithMiddlewares, ORPCError, ProcedureImplementer, RouterImplementer, RouterImplementerWithMiddlewares, implement, onError, onFinish, onStart, onSuccess } from '@orpc/server';
|
|
5
|
+
import { Promisable } from '@orpc/shared';
|
|
6
|
+
import { Observable } from 'rxjs';
|
|
7
|
+
import { SendStandardResponseOptions } from '@orpc/standard-server-node';
|
|
8
|
+
|
|
9
|
+
declare const ORPC_MODULE_CONFIG_SYMBOL: unique symbol;
|
|
10
|
+
interface ORPCModuleConfig extends CreateProcedureClientOptions<object, AnySchema, object, object, object>, SendStandardResponseOptions {
|
|
11
|
+
}
|
|
12
|
+
declare class ORPCModule {
|
|
13
|
+
static forRoot(config: ORPCModuleConfig): DynamicModule;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Decorator in controller handler to implement a oRPC contract.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
|
|
20
|
+
*/
|
|
21
|
+
declare function Implement<T extends ContractRouter<any>>(contract: T): <U extends Promisable<Router<T, Record<never, never>>>>(target: Record<PropertyKey, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void;
|
|
22
|
+
declare class ImplementInterceptor implements NestInterceptor {
|
|
23
|
+
private readonly config;
|
|
24
|
+
constructor(config: ORPCModuleConfig | undefined);
|
|
25
|
+
intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare function toNestPattern(path: HTTPPath): string;
|
|
29
|
+
type PopulatedContractRouterPaths<T extends AnyContractRouter> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrors, infer UMeta> ? ContractProcedure<UInputSchema, UOutputSchema, UErrors, UMeta> : {
|
|
30
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? PopulatedContractRouterPaths<T[K]> : never;
|
|
31
|
+
};
|
|
32
|
+
interface PopulateContractRouterPathsOptions {
|
|
33
|
+
path?: readonly string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* populateContractRouterPaths is completely optional,
|
|
37
|
+
* because the procedure's path is required for NestJS implementation.
|
|
38
|
+
* This utility automatically populates any missing paths
|
|
39
|
+
* Using the router's keys + `/`.
|
|
40
|
+
*
|
|
41
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/integrations/implement-contract-in-nest#define-your-contract NestJS Implement Contract Docs}
|
|
42
|
+
*/
|
|
43
|
+
declare function populateContractRouterPaths<T extends AnyContractRouter>(router: T, options?: PopulateContractRouterPathsOptions): PopulatedContractRouterPaths<T>;
|
|
44
|
+
|
|
45
|
+
export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, populateContractRouterPaths, toNestPattern };
|
|
46
|
+
export type { ORPCModuleConfig, PopulateContractRouterPathsOptions, PopulatedContractRouterPaths };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Module, Injectable, Inject, Optional, applyDecorators, Delete, Patch, Put, Post, Get, Head, UseInterceptors } from '@nestjs/common';
|
|
2
|
+
import { toORPCError } from '@orpc/client';
|
|
3
|
+
import { isContractProcedure, ContractProcedure, fallbackContractConfig } from '@orpc/contract';
|
|
4
|
+
import { standardizeHTTPPath, StandardOpenAPISerializer, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
|
|
5
|
+
import { StandardOpenAPICodec } from '@orpc/openapi/standard';
|
|
6
|
+
import { getRouter, unlazy, isProcedure, createProcedureClient, ORPCError } from '@orpc/server';
|
|
7
|
+
export { ORPCError, implement, onError, onFinish, onStart, onSuccess } from '@orpc/server';
|
|
8
|
+
import { toArray, get } from '@orpc/shared';
|
|
9
|
+
import { flattenHeader } from '@orpc/standard-server';
|
|
10
|
+
import { toStandardLazyRequest, sendStandardResponse } from '@orpc/standard-server-node';
|
|
11
|
+
import { mergeMap } from 'rxjs';
|
|
12
|
+
import { toHttpPath } from '@orpc/client/standard';
|
|
13
|
+
|
|
14
|
+
var __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __decorateClass$1 = (decorators, target, key, kind) => {
|
|
16
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target;
|
|
17
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
18
|
+
if (decorator = decorators[i])
|
|
19
|
+
result = (decorator(result)) || result;
|
|
20
|
+
return result;
|
|
21
|
+
};
|
|
22
|
+
const ORPC_MODULE_CONFIG_SYMBOL = Symbol("ORPC_MODULE_CONFIG");
|
|
23
|
+
let ORPCModule = class {
|
|
24
|
+
static forRoot(config) {
|
|
25
|
+
return {
|
|
26
|
+
module: ORPCModule,
|
|
27
|
+
providers: [
|
|
28
|
+
{
|
|
29
|
+
provide: ORPC_MODULE_CONFIG_SYMBOL,
|
|
30
|
+
useValue: config
|
|
31
|
+
},
|
|
32
|
+
ImplementInterceptor
|
|
33
|
+
],
|
|
34
|
+
exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor],
|
|
35
|
+
global: true
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
ORPCModule = __decorateClass$1([
|
|
40
|
+
Module({})
|
|
41
|
+
], ORPCModule);
|
|
42
|
+
|
|
43
|
+
function toNestPattern(path) {
|
|
44
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/*$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
45
|
+
}
|
|
46
|
+
function populateContractRouterPaths(router, options = {}) {
|
|
47
|
+
const path = toArray(options.path);
|
|
48
|
+
if (isContractProcedure(router)) {
|
|
49
|
+
if (router["~orpc"].route.path === void 0) {
|
|
50
|
+
return new ContractProcedure({
|
|
51
|
+
...router["~orpc"],
|
|
52
|
+
route: {
|
|
53
|
+
...router["~orpc"].route,
|
|
54
|
+
path: toHttpPath(path)
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return router;
|
|
59
|
+
}
|
|
60
|
+
const populated = {};
|
|
61
|
+
for (const key in router) {
|
|
62
|
+
populated[key] = populateContractRouterPaths(router[key], { ...options, path: [...path, key] });
|
|
63
|
+
}
|
|
64
|
+
return populated;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
68
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
69
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
70
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
71
|
+
if (decorator = decorators[i])
|
|
72
|
+
result = (decorator(result)) || result;
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
75
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
76
|
+
const MethodDecoratorMap = {
|
|
77
|
+
HEAD: Head,
|
|
78
|
+
GET: Get,
|
|
79
|
+
POST: Post,
|
|
80
|
+
PUT: Put,
|
|
81
|
+
PATCH: Patch,
|
|
82
|
+
DELETE: Delete
|
|
83
|
+
};
|
|
84
|
+
function Implement(contract) {
|
|
85
|
+
if (isContractProcedure(contract)) {
|
|
86
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
87
|
+
const path = contract["~orpc"].route.path;
|
|
88
|
+
if (path === void 0) {
|
|
89
|
+
throw new Error(`
|
|
90
|
+
@Implement decorator requires contract to have a 'path'.
|
|
91
|
+
Please define one using 'path' property on the '.route' method.
|
|
92
|
+
Or use "populateContractRouterPaths" utility to automatically fill in any missing paths.
|
|
93
|
+
`);
|
|
94
|
+
}
|
|
95
|
+
return (target, propertyKey, descriptor) => {
|
|
96
|
+
applyDecorators(
|
|
97
|
+
MethodDecoratorMap[method](toNestPattern(path)),
|
|
98
|
+
UseInterceptors(ImplementInterceptor)
|
|
99
|
+
)(target, propertyKey, descriptor);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return (target, propertyKey, descriptor) => {
|
|
103
|
+
for (const key in contract) {
|
|
104
|
+
let methodName = `${propertyKey}_${key}`;
|
|
105
|
+
let i = 0;
|
|
106
|
+
while (methodName in target) {
|
|
107
|
+
methodName = `${propertyKey}_${key}_${i++}`;
|
|
108
|
+
}
|
|
109
|
+
target[methodName] = async function(...args) {
|
|
110
|
+
const router = await descriptor.value.apply(this, args);
|
|
111
|
+
return getRouter(router, [key]);
|
|
112
|
+
};
|
|
113
|
+
for (const p of Reflect.getOwnMetadataKeys(target, propertyKey)) {
|
|
114
|
+
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target, propertyKey), target, methodName);
|
|
115
|
+
}
|
|
116
|
+
for (const p of Reflect.getOwnMetadataKeys(target.constructor, propertyKey)) {
|
|
117
|
+
Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target.constructor, propertyKey), target.constructor, methodName);
|
|
118
|
+
}
|
|
119
|
+
Implement(get(contract, [key]))(target, methodName, Object.getOwnPropertyDescriptor(target, methodName));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const codec = new StandardOpenAPICodec(
|
|
124
|
+
new StandardOpenAPISerializer(
|
|
125
|
+
new StandardOpenAPIJsonSerializer(),
|
|
126
|
+
new StandardBracketNotationSerializer()
|
|
127
|
+
)
|
|
128
|
+
);
|
|
129
|
+
let ImplementInterceptor = class {
|
|
130
|
+
constructor(config) {
|
|
131
|
+
this.config = config;
|
|
132
|
+
}
|
|
133
|
+
intercept(ctx, next) {
|
|
134
|
+
return next.handle().pipe(
|
|
135
|
+
mergeMap(async (impl) => {
|
|
136
|
+
const { default: procedure } = await unlazy(impl);
|
|
137
|
+
if (!isProcedure(procedure)) {
|
|
138
|
+
throw new Error(`
|
|
139
|
+
The return value of the @Implement controller handler must be a corresponding implemented router or procedure.
|
|
140
|
+
`);
|
|
141
|
+
}
|
|
142
|
+
const req = ctx.switchToHttp().getRequest();
|
|
143
|
+
const res = ctx.switchToHttp().getResponse();
|
|
144
|
+
const nodeReq = "raw" in req ? req.raw : req;
|
|
145
|
+
const nodeRes = "raw" in res ? res.raw : res;
|
|
146
|
+
const standardRequest = toStandardLazyRequest(nodeReq, nodeRes);
|
|
147
|
+
const fallbackStandardBody = standardRequest.body.bind(standardRequest);
|
|
148
|
+
standardRequest.body = () => Promise.resolve(req.body ?? fallbackStandardBody());
|
|
149
|
+
const standardResponse = await (async () => {
|
|
150
|
+
let isDecoding = false;
|
|
151
|
+
try {
|
|
152
|
+
const client = createProcedureClient(procedure, this.config);
|
|
153
|
+
isDecoding = true;
|
|
154
|
+
const input = await codec.decode(standardRequest, flattenParams(req.params), procedure);
|
|
155
|
+
isDecoding = false;
|
|
156
|
+
const output = await client(input, {
|
|
157
|
+
signal: standardRequest.signal,
|
|
158
|
+
lastEventId: flattenHeader(standardRequest.headers["last-event-id"])
|
|
159
|
+
});
|
|
160
|
+
return codec.encode(output, procedure);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
|
163
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
164
|
+
cause: e
|
|
165
|
+
}) : toORPCError(e);
|
|
166
|
+
return codec.encodeError(error);
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
169
|
+
await sendStandardResponse(nodeRes, standardResponse, this.config);
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
ImplementInterceptor = __decorateClass([
|
|
175
|
+
Injectable(),
|
|
176
|
+
__decorateParam(0, Inject(ORPC_MODULE_CONFIG_SYMBOL)),
|
|
177
|
+
__decorateParam(0, Optional())
|
|
178
|
+
], ImplementInterceptor);
|
|
179
|
+
function flattenParams(params) {
|
|
180
|
+
const flatten = {};
|
|
181
|
+
for (const [key, value] of Object.entries(params)) {
|
|
182
|
+
if (Array.isArray(value)) {
|
|
183
|
+
flatten[key] = value.join("/");
|
|
184
|
+
} else {
|
|
185
|
+
flatten[key] = value;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return flatten;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export { Implement as Impl, Implement, ImplementInterceptor, ORPCModule, ORPC_MODULE_CONFIG_SYMBOL, populateContractRouterPaths, toNestPattern };
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@orpc/nest",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0-next.01ede75",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://orpc.unnoq.com",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/unnoq/orpc.git",
|
|
10
|
+
"directory": "packages/nest"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"unnoq",
|
|
14
|
+
"orpc"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@nestjs/common": ">=11.0.0",
|
|
28
|
+
"@nestjs/core": ">=11.0.0",
|
|
29
|
+
"express": ">=5.0.0",
|
|
30
|
+
"fastify": ">=5.0.0",
|
|
31
|
+
"rxjs": ">=7.0.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependenciesMeta": {
|
|
34
|
+
"express": {
|
|
35
|
+
"optional": true
|
|
36
|
+
},
|
|
37
|
+
"fastify": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@orpc/client": "0.0.0-next.01ede75",
|
|
43
|
+
"@orpc/contract": "0.0.0-next.01ede75",
|
|
44
|
+
"@orpc/openapi-client": "0.0.0-next.01ede75",
|
|
45
|
+
"@orpc/server": "0.0.0-next.01ede75",
|
|
46
|
+
"@orpc/shared": "0.0.0-next.01ede75",
|
|
47
|
+
"@orpc/openapi": "0.0.0-next.01ede75",
|
|
48
|
+
"@orpc/standard-server-node": "0.0.0-next.01ede75",
|
|
49
|
+
"@orpc/standard-server": "0.0.0-next.01ede75"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@nestjs/common": "^11.1.4",
|
|
53
|
+
"@nestjs/core": "^11.1.4",
|
|
54
|
+
"@nestjs/platform-express": "^11.1.4",
|
|
55
|
+
"@nestjs/platform-fastify": "^11.1.4",
|
|
56
|
+
"@nestjs/testing": "^11.1.4",
|
|
57
|
+
"@ts-rest/core": "^3.52.1",
|
|
58
|
+
"@types/express": "^5.0.3",
|
|
59
|
+
"express": "^5.0.0",
|
|
60
|
+
"fastify": "^5.4.0",
|
|
61
|
+
"rxjs": "^7.8.1",
|
|
62
|
+
"supertest": "^7.1.3",
|
|
63
|
+
"zod": "^4.0.5"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "unbuild",
|
|
67
|
+
"build:watch": "pnpm run build --watch",
|
|
68
|
+
"type:check": "tsc -b",
|
|
69
|
+
"type:check:test": "tsc -p tsconfig.test.json --noEmit"
|
|
70
|
+
}
|
|
71
|
+
}
|