@orpc/server 0.0.0-next.8f9385e → 0.0.0-next.905e81c
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 +118 -0
- package/dist/chunk-47YYO5JS.js +32 -0
- package/dist/chunk-77FU7QSO.js +181 -0
- package/dist/chunk-MHVECKBC.js +421 -0
- package/dist/chunk-WQNNSBXW.js +120 -0
- package/dist/fetch.js +6 -102
- package/dist/hono.js +34 -0
- package/dist/index.js +341 -322
- package/dist/next.js +31 -0
- package/dist/node.js +31 -0
- package/dist/plugins.js +11 -0
- package/dist/src/adapters/fetch/index.d.ts +3 -0
- package/dist/src/adapters/fetch/rpc-handler.d.ts +11 -0
- package/dist/src/adapters/fetch/types.d.ts +14 -0
- package/dist/src/adapters/hono/index.d.ts +3 -0
- package/dist/src/adapters/hono/middleware.d.ts +12 -0
- package/dist/src/adapters/next/index.d.ts +3 -0
- package/dist/src/adapters/next/serve.d.ts +19 -0
- package/dist/src/adapters/node/index.d.ts +3 -0
- package/dist/src/adapters/node/rpc-handler.d.ts +11 -0
- package/dist/src/adapters/node/types.d.ts +22 -0
- package/dist/src/adapters/standard/handler.d.ts +53 -0
- package/dist/src/adapters/standard/index.d.ts +6 -0
- package/dist/src/adapters/standard/rpc-codec.d.ts +16 -0
- package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
- package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
- package/dist/src/adapters/standard/types.d.ts +21 -0
- package/dist/src/builder-variants.d.ts +75 -0
- package/dist/src/builder.d.ts +48 -39
- package/dist/src/config.d.ts +6 -0
- package/dist/src/context.d.ts +8 -0
- package/dist/src/error.d.ts +12 -0
- package/dist/src/hidden.d.ts +8 -0
- package/dist/src/implementer-procedure.d.ts +33 -0
- package/dist/src/implementer-variants.d.ts +18 -0
- package/dist/src/implementer.d.ts +29 -0
- package/dist/src/index.d.ts +19 -9
- package/dist/src/lazy-utils.d.ts +6 -0
- package/dist/src/lazy.d.ts +22 -0
- package/dist/src/middleware-decorated.d.ts +11 -0
- package/dist/src/middleware-utils.d.ts +5 -0
- package/dist/src/middleware.d.ts +30 -18
- package/dist/src/plugins/base.d.ts +11 -0
- package/dist/src/plugins/cors.d.ts +19 -0
- package/dist/src/plugins/index.d.ts +4 -0
- package/dist/src/plugins/response-headers.d.ts +10 -0
- package/dist/src/procedure-client.d.ts +33 -0
- package/dist/src/procedure-decorated.d.ts +24 -0
- package/dist/src/procedure-utils.d.ts +19 -0
- package/dist/src/procedure.d.ts +27 -25
- package/dist/src/router-accessible-lazy.d.ts +8 -0
- package/dist/src/router-client.d.ts +12 -0
- package/dist/src/router.d.ts +26 -16
- package/dist/src/utils.d.ts +23 -2
- package/dist/standard.js +13 -0
- package/package.json +37 -9
- package/dist/chunk-TDFYNRZV.js +0 -190
- package/dist/src/fetch/handle.d.ts +0 -7
- package/dist/src/fetch/handler.d.ts +0 -3
- package/dist/src/fetch/index.d.ts +0 -4
- package/dist/src/fetch/types.d.ts +0 -35
- package/dist/src/procedure-builder.d.ts +0 -31
- package/dist/src/procedure-caller.d.ts +0 -19
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/router-builder.d.ts +0 -22
- package/dist/src/router-caller.d.ts +0 -22
- package/dist/src/router-implementer.d.ts +0 -20
- package/dist/src/types.d.ts +0 -8
package/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 />
|
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/server">
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fserver?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, ensuring a smooth and enjoyable developer experience.
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
## Highlights
|
29
|
+
|
30
|
+
- **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
|
31
|
+
- **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
|
32
|
+
- **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
|
33
|
+
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
|
34
|
+
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
|
35
|
+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue), Pinia Colada, and more.
|
36
|
+
- **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
|
37
|
+
- **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
|
38
|
+
- **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
|
39
|
+
- **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
|
40
|
+
- **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
|
41
|
+
- **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
|
42
|
+
- **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
|
43
|
+
- **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
|
44
|
+
- **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
|
45
|
+
- **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
|
46
|
+
|
47
|
+
## Documentation
|
48
|
+
|
49
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
50
|
+
|
51
|
+
## Packages
|
52
|
+
|
53
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
54
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
55
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
56
|
+
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
57
|
+
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
58
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
59
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
60
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
61
|
+
|
62
|
+
## `@orpc/server`
|
63
|
+
|
64
|
+
Build your API or implement API contract. Read the [documentation](https://orpc.unnoq.com/docs/getting-started) for more information.
|
65
|
+
|
66
|
+
```ts
|
67
|
+
import type { IncomingHttpHeaders } from 'node:http'
|
68
|
+
import { ORPCError, os } from '@orpc/server'
|
69
|
+
import { z } from 'zod'
|
70
|
+
|
71
|
+
const PlanetSchema = z.object({
|
72
|
+
id: z.number().int().min(1),
|
73
|
+
name: z.string(),
|
74
|
+
description: z.string().optional(),
|
75
|
+
})
|
76
|
+
|
77
|
+
export const listPlanet = os
|
78
|
+
.input(
|
79
|
+
z.object({
|
80
|
+
limit: z.number().int().min(1).max(100).optional(),
|
81
|
+
cursor: z.number().int().min(0).default(0),
|
82
|
+
}),
|
83
|
+
)
|
84
|
+
.handler(async ({ input }) => {
|
85
|
+
// your list code here
|
86
|
+
return [{ id: 1, name: 'name' }]
|
87
|
+
})
|
88
|
+
|
89
|
+
export const findPlanet = os
|
90
|
+
.input(PlanetSchema.pick({ id: true }))
|
91
|
+
.handler(async ({ input }) => {
|
92
|
+
// your find code here
|
93
|
+
return { id: 1, name: 'name' }
|
94
|
+
})
|
95
|
+
|
96
|
+
export const createPlanet = os
|
97
|
+
.$context<{ headers: IncomingHttpHeaders }>()
|
98
|
+
.use(({ context, next }) => {
|
99
|
+
const user = parseJWT(context.headers.authorization?.split(' ')[1])
|
100
|
+
|
101
|
+
if (user) {
|
102
|
+
return next({ context: { user } })
|
103
|
+
}
|
104
|
+
|
105
|
+
throw new ORPCError('UNAUTHORIZED')
|
106
|
+
})
|
107
|
+
.input(PlanetSchema.omit({ id: true }))
|
108
|
+
.handler(async ({ input, context }) => {
|
109
|
+
// your create code here
|
110
|
+
return { id: 1, name: 'name' }
|
111
|
+
})
|
112
|
+
|
113
|
+
export const router = { planet: { list: listPlanet, find: findPlanet, create: createPlanet } }
|
114
|
+
```
|
115
|
+
|
116
|
+
## License
|
117
|
+
|
118
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import {
|
2
|
+
RPCCodec,
|
3
|
+
RPCMatcher,
|
4
|
+
StandardHandler
|
5
|
+
} from "./chunk-77FU7QSO.js";
|
6
|
+
|
7
|
+
// src/adapters/fetch/rpc-handler.ts
|
8
|
+
import { toFetchResponse, toStandardRequest } from "@orpc/server-standard-fetch";
|
9
|
+
var RPCHandler = class {
|
10
|
+
standardHandler;
|
11
|
+
constructor(router, options) {
|
12
|
+
const matcher = options?.matcher ?? new RPCMatcher();
|
13
|
+
const codec = options?.codec ?? new RPCCodec();
|
14
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
15
|
+
}
|
16
|
+
async handle(request, ...rest) {
|
17
|
+
const standardRequest = toStandardRequest(request);
|
18
|
+
const result = await this.standardHandler.handle(standardRequest, ...rest);
|
19
|
+
if (!result.matched) {
|
20
|
+
return result;
|
21
|
+
}
|
22
|
+
return {
|
23
|
+
matched: true,
|
24
|
+
response: toFetchResponse(result.response)
|
25
|
+
};
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
export {
|
30
|
+
RPCHandler
|
31
|
+
};
|
32
|
+
//# sourceMappingURL=chunk-47YYO5JS.js.map
|
@@ -0,0 +1,181 @@
|
|
1
|
+
import {
|
2
|
+
convertPathToHttpPath,
|
3
|
+
createContractedProcedure,
|
4
|
+
createProcedureClient,
|
5
|
+
eachContractProcedure,
|
6
|
+
getRouterChild,
|
7
|
+
isProcedure,
|
8
|
+
unlazy
|
9
|
+
} from "./chunk-MHVECKBC.js";
|
10
|
+
import {
|
11
|
+
CompositePlugin
|
12
|
+
} from "./chunk-WQNNSBXW.js";
|
13
|
+
|
14
|
+
// src/adapters/standard/handler.ts
|
15
|
+
import { ORPCError, toORPCError } from "@orpc/client";
|
16
|
+
import { intercept, trim } from "@orpc/shared";
|
17
|
+
var StandardHandler = class {
|
18
|
+
constructor(router, matcher, codec, options = {}) {
|
19
|
+
this.matcher = matcher;
|
20
|
+
this.codec = codec;
|
21
|
+
this.options = options;
|
22
|
+
this.plugin = new CompositePlugin(options.plugins);
|
23
|
+
this.plugin.init(this.options);
|
24
|
+
this.matcher.init(router);
|
25
|
+
}
|
26
|
+
plugin;
|
27
|
+
handle(request, ...[options]) {
|
28
|
+
return intercept(
|
29
|
+
this.options.rootInterceptors ?? [],
|
30
|
+
{
|
31
|
+
request,
|
32
|
+
...options,
|
33
|
+
context: options?.context ?? {}
|
34
|
+
// context is optional only when all fields are optional so we can safely force it to have a context
|
35
|
+
},
|
36
|
+
async (interceptorOptions) => {
|
37
|
+
let isDecoding = false;
|
38
|
+
try {
|
39
|
+
return await intercept(
|
40
|
+
this.options.interceptors ?? [],
|
41
|
+
interceptorOptions,
|
42
|
+
async (interceptorOptions2) => {
|
43
|
+
const method = interceptorOptions2.request.method;
|
44
|
+
const url = interceptorOptions2.request.url;
|
45
|
+
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
46
|
+
const match = await this.matcher.match(method, pathname);
|
47
|
+
if (!match) {
|
48
|
+
return { matched: false, response: void 0 };
|
49
|
+
}
|
50
|
+
const client = createProcedureClient(match.procedure, {
|
51
|
+
context: interceptorOptions2.context,
|
52
|
+
path: match.path,
|
53
|
+
interceptors: this.options.clientInterceptors
|
54
|
+
});
|
55
|
+
isDecoding = true;
|
56
|
+
const input = await this.codec.decode(request, match.params, match.procedure);
|
57
|
+
isDecoding = false;
|
58
|
+
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
59
|
+
const output = await client(input, { signal: request.signal, lastEventId });
|
60
|
+
const response = this.codec.encode(output, match.procedure);
|
61
|
+
return {
|
62
|
+
matched: true,
|
63
|
+
response
|
64
|
+
};
|
65
|
+
}
|
66
|
+
);
|
67
|
+
} catch (e) {
|
68
|
+
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
69
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
70
|
+
cause: e
|
71
|
+
}) : toORPCError(e);
|
72
|
+
const response = this.codec.encodeError(error);
|
73
|
+
return {
|
74
|
+
matched: true,
|
75
|
+
response
|
76
|
+
};
|
77
|
+
}
|
78
|
+
}
|
79
|
+
);
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
// src/adapters/standard/rpc-codec.ts
|
84
|
+
import { RPCSerializer } from "@orpc/client/rpc";
|
85
|
+
var RPCCodec = class {
|
86
|
+
serializer;
|
87
|
+
constructor(options = {}) {
|
88
|
+
this.serializer = options.serializer ?? new RPCSerializer();
|
89
|
+
}
|
90
|
+
async decode(request, _params, _procedure) {
|
91
|
+
const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
92
|
+
return this.serializer.deserialize(serialized);
|
93
|
+
}
|
94
|
+
encode(output, _procedure) {
|
95
|
+
return {
|
96
|
+
status: 200,
|
97
|
+
headers: {},
|
98
|
+
body: this.serializer.serialize(output)
|
99
|
+
};
|
100
|
+
}
|
101
|
+
encodeError(error) {
|
102
|
+
return {
|
103
|
+
status: error.status,
|
104
|
+
headers: {},
|
105
|
+
body: this.serializer.serialize(error.toJSON())
|
106
|
+
};
|
107
|
+
}
|
108
|
+
};
|
109
|
+
|
110
|
+
// src/adapters/standard/rpc-matcher.ts
|
111
|
+
var RPCMatcher = class {
|
112
|
+
tree = {};
|
113
|
+
pendingRouters = [];
|
114
|
+
init(router, path = []) {
|
115
|
+
const laziedOptions = eachContractProcedure({
|
116
|
+
router,
|
117
|
+
path
|
118
|
+
}, ({ path: path2, contract }) => {
|
119
|
+
const httpPath = convertPathToHttpPath(path2);
|
120
|
+
if (isProcedure(contract)) {
|
121
|
+
this.tree[httpPath] = {
|
122
|
+
path: path2,
|
123
|
+
contract,
|
124
|
+
procedure: contract,
|
125
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
126
|
+
router
|
127
|
+
};
|
128
|
+
} else {
|
129
|
+
this.tree[httpPath] = {
|
130
|
+
path: path2,
|
131
|
+
contract,
|
132
|
+
procedure: void 0,
|
133
|
+
router
|
134
|
+
};
|
135
|
+
}
|
136
|
+
});
|
137
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
138
|
+
...option,
|
139
|
+
httpPathPrefix: convertPathToHttpPath(option.path)
|
140
|
+
})));
|
141
|
+
}
|
142
|
+
async match(_method, pathname) {
|
143
|
+
if (this.pendingRouters.length) {
|
144
|
+
const newPendingRouters = [];
|
145
|
+
for (const pendingRouter of this.pendingRouters) {
|
146
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
147
|
+
const { default: router } = await unlazy(pendingRouter.lazied);
|
148
|
+
this.init(router, pendingRouter.path);
|
149
|
+
} else {
|
150
|
+
newPendingRouters.push(pendingRouter);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
this.pendingRouters = newPendingRouters;
|
154
|
+
}
|
155
|
+
const match = this.tree[pathname];
|
156
|
+
if (!match) {
|
157
|
+
return void 0;
|
158
|
+
}
|
159
|
+
if (!match.procedure) {
|
160
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
|
161
|
+
if (!isProcedure(maybeProcedure)) {
|
162
|
+
throw new Error(`
|
163
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
|
164
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
165
|
+
`);
|
166
|
+
}
|
167
|
+
match.procedure = createContractedProcedure(match.contract, maybeProcedure);
|
168
|
+
}
|
169
|
+
return {
|
170
|
+
path: match.path,
|
171
|
+
procedure: match.procedure
|
172
|
+
};
|
173
|
+
}
|
174
|
+
};
|
175
|
+
|
176
|
+
export {
|
177
|
+
StandardHandler,
|
178
|
+
RPCCodec,
|
179
|
+
RPCMatcher
|
180
|
+
};
|
181
|
+
//# sourceMappingURL=chunk-77FU7QSO.js.map
|