@orpc/shared 0.0.0-next.caefe3a → 0.0.0-next.cc4cb21
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 +68 -0
- package/dist/index.js +74 -94
- package/dist/src/index.d.ts +4 -5
- package/dist/src/interceptor.d.ts +0 -2
- package/dist/src/iterator.d.ts +2 -0
- package/dist/src/json.d.ts +1 -1
- package/dist/src/object.d.ts +8 -0
- package/dist/src/types.d.ts +3 -0
- package/dist/src/value.d.ts +1 -1
- package/package.json +1 -3
- package/dist/src/constants.d.ts +0 -3
- package/dist/src/proxy.d.ts +0 -3
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
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/shared">
|
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fshared?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/shared`
|
|
63
|
+
|
|
64
|
+
Provides shared utilities for oRPC packages.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,58 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// src/object.ts
|
|
2
|
+
function set(root, segments, value2) {
|
|
3
|
+
const ref = { root };
|
|
4
|
+
let currentRef = ref;
|
|
5
|
+
let preSegment = "root";
|
|
6
|
+
for (const segment of segments) {
|
|
7
|
+
currentRef = currentRef[preSegment];
|
|
8
|
+
preSegment = segment;
|
|
9
|
+
}
|
|
10
|
+
currentRef[preSegment] = value2;
|
|
11
|
+
return ref.root;
|
|
12
|
+
}
|
|
13
|
+
function get(root, segments) {
|
|
14
|
+
const ref = { root };
|
|
15
|
+
let currentRef = ref;
|
|
16
|
+
let preSegment = "root";
|
|
17
|
+
for (const segment of segments) {
|
|
18
|
+
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
currentRef = currentRef[preSegment];
|
|
22
|
+
preSegment = segment;
|
|
23
|
+
}
|
|
24
|
+
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
return currentRef[preSegment];
|
|
28
|
+
}
|
|
29
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
30
|
+
if (check(payload)) {
|
|
31
|
+
maps.push(segments);
|
|
32
|
+
values.push(payload);
|
|
33
|
+
} else if (Array.isArray(payload)) {
|
|
34
|
+
payload.forEach((v, i) => {
|
|
35
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
36
|
+
});
|
|
37
|
+
} else if (isObject(payload)) {
|
|
38
|
+
for (const key in payload) {
|
|
39
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { maps, values };
|
|
43
|
+
}
|
|
44
|
+
function isObject(value2) {
|
|
45
|
+
if (!value2 || typeof value2 !== "object") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const proto = Object.getPrototypeOf(value2);
|
|
49
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
50
|
+
}
|
|
51
|
+
function isTypescriptObject(value2) {
|
|
52
|
+
return !!value2 && (typeof value2 === "object" || typeof value2 === "function");
|
|
53
|
+
}
|
|
4
54
|
|
|
5
55
|
// src/error.ts
|
|
6
|
-
import { isPlainObject } from "is-what";
|
|
7
56
|
function toError(error) {
|
|
8
57
|
if (error instanceof Error) {
|
|
9
58
|
return error;
|
|
@@ -11,7 +60,7 @@ function toError(error) {
|
|
|
11
60
|
if (typeof error === "string") {
|
|
12
61
|
return new Error(error, { cause: error });
|
|
13
62
|
}
|
|
14
|
-
if (
|
|
63
|
+
if (isObject(error)) {
|
|
15
64
|
if ("message" in error && typeof error.message === "string") {
|
|
16
65
|
return new Error(error.message, { cause: error });
|
|
17
66
|
}
|
|
@@ -76,101 +125,33 @@ function onFinish(callback) {
|
|
|
76
125
|
}
|
|
77
126
|
async function intercept(interceptors, options, main) {
|
|
78
127
|
let index = 0;
|
|
79
|
-
const next = async (
|
|
128
|
+
const next = async (options2) => {
|
|
80
129
|
const interceptor = interceptors[index++];
|
|
81
130
|
if (!interceptor) {
|
|
82
|
-
return await main(
|
|
131
|
+
return await main(options2);
|
|
83
132
|
}
|
|
84
133
|
return await interceptor({
|
|
85
|
-
...
|
|
86
|
-
next
|
|
134
|
+
...options2,
|
|
135
|
+
next: (newOptions = options2) => next(newOptions)
|
|
87
136
|
});
|
|
88
137
|
};
|
|
89
|
-
return await next();
|
|
138
|
+
return await next(options);
|
|
90
139
|
}
|
|
91
140
|
|
|
92
|
-
// src/
|
|
93
|
-
function
|
|
94
|
-
if (
|
|
95
|
-
return
|
|
96
|
-
try {
|
|
97
|
-
return JSON.parse(text);
|
|
98
|
-
} catch {
|
|
99
|
-
return text;
|
|
141
|
+
// src/iterator.ts
|
|
142
|
+
function isAsyncIteratorObject(maybe) {
|
|
143
|
+
if (!maybe || typeof maybe !== "object") {
|
|
144
|
+
return false;
|
|
100
145
|
}
|
|
146
|
+
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
101
147
|
}
|
|
102
148
|
|
|
103
|
-
// src/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const ref = { root };
|
|
107
|
-
let currentRef = ref;
|
|
108
|
-
let preSegment = "root";
|
|
109
|
-
for (const segment of segments) {
|
|
110
|
-
currentRef = currentRef[preSegment];
|
|
111
|
-
preSegment = segment;
|
|
112
|
-
}
|
|
113
|
-
currentRef[preSegment] = value2;
|
|
114
|
-
return ref.root;
|
|
115
|
-
}
|
|
116
|
-
function get(root, segments) {
|
|
117
|
-
const ref = { root };
|
|
118
|
-
let currentRef = ref;
|
|
119
|
-
let preSegment = "root";
|
|
120
|
-
for (const segment of segments) {
|
|
121
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
122
|
-
return void 0;
|
|
123
|
-
}
|
|
124
|
-
currentRef = currentRef[preSegment];
|
|
125
|
-
preSegment = segment;
|
|
126
|
-
}
|
|
127
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
149
|
+
// src/json.ts
|
|
150
|
+
function parseEmptyableJSON(text) {
|
|
151
|
+
if (!text) {
|
|
128
152
|
return void 0;
|
|
129
153
|
}
|
|
130
|
-
return
|
|
131
|
-
}
|
|
132
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
133
|
-
if (check(payload)) {
|
|
134
|
-
maps.push(segments);
|
|
135
|
-
values.push(payload);
|
|
136
|
-
} else if (Array.isArray(payload)) {
|
|
137
|
-
payload.forEach((v, i) => {
|
|
138
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
139
|
-
});
|
|
140
|
-
} else if (isPlainObject2(payload)) {
|
|
141
|
-
for (const key in payload) {
|
|
142
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return { maps, values };
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// src/proxy.ts
|
|
149
|
-
function createCallableObject(obj, handler) {
|
|
150
|
-
const proxy = new Proxy(handler, {
|
|
151
|
-
has(target, key) {
|
|
152
|
-
return Reflect.has(obj, key) || Reflect.has(target, key);
|
|
153
|
-
},
|
|
154
|
-
ownKeys(target) {
|
|
155
|
-
return Array.from(new Set(Reflect.ownKeys(obj).concat(...Reflect.ownKeys(target))));
|
|
156
|
-
},
|
|
157
|
-
get(target, key) {
|
|
158
|
-
if (!Reflect.has(target, key) || Reflect.has(obj, key)) {
|
|
159
|
-
return Reflect.get(obj, key);
|
|
160
|
-
}
|
|
161
|
-
return Reflect.get(target, key);
|
|
162
|
-
},
|
|
163
|
-
defineProperty(_, key, descriptor) {
|
|
164
|
-
return Reflect.defineProperty(obj, key, descriptor);
|
|
165
|
-
},
|
|
166
|
-
set(_, key, value2) {
|
|
167
|
-
return Reflect.set(obj, key, value2);
|
|
168
|
-
},
|
|
169
|
-
deleteProperty(target, key) {
|
|
170
|
-
return Reflect.deleteProperty(target, key) && Reflect.deleteProperty(obj, key);
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
return proxy;
|
|
154
|
+
return JSON.parse(text);
|
|
174
155
|
}
|
|
175
156
|
|
|
176
157
|
// src/value.ts
|
|
@@ -182,18 +163,16 @@ function value(value2, ...args) {
|
|
|
182
163
|
}
|
|
183
164
|
|
|
184
165
|
// src/index.ts
|
|
185
|
-
import {
|
|
186
|
-
import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
|
|
166
|
+
import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash";
|
|
187
167
|
export {
|
|
188
|
-
ORPC_HANDLER_HEADER,
|
|
189
|
-
ORPC_HANDLER_VALUE,
|
|
190
|
-
createCallableObject,
|
|
191
168
|
findDeepMatches,
|
|
192
169
|
get,
|
|
193
170
|
group,
|
|
194
171
|
guard,
|
|
195
172
|
intercept,
|
|
196
|
-
|
|
173
|
+
isAsyncIteratorObject,
|
|
174
|
+
isObject,
|
|
175
|
+
isTypescriptObject,
|
|
197
176
|
mapEntries,
|
|
198
177
|
mapValues,
|
|
199
178
|
omit,
|
|
@@ -202,7 +181,8 @@ export {
|
|
|
202
181
|
onStart,
|
|
203
182
|
onSuccess,
|
|
204
183
|
once,
|
|
205
|
-
|
|
184
|
+
parseEmptyableJSON,
|
|
185
|
+
retry,
|
|
206
186
|
set,
|
|
207
187
|
toError,
|
|
208
188
|
trim,
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
export * from './chain';
|
|
2
|
-
export * from './constants';
|
|
3
2
|
export * from './error';
|
|
4
3
|
export * from './function';
|
|
5
4
|
export * from './interceptor';
|
|
5
|
+
export * from './iterator';
|
|
6
6
|
export * from './json';
|
|
7
7
|
export * from './object';
|
|
8
|
-
export * from './
|
|
8
|
+
export * from './types';
|
|
9
9
|
export * from './value';
|
|
10
|
-
export {
|
|
11
|
-
export {
|
|
12
|
-
export type * from 'type-fest';
|
|
10
|
+
export { group, guard, mapEntries, mapValues, omit, retry, trim } from 'radash';
|
|
11
|
+
export type { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
13
12
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Promisable } from 'type-fest';
|
|
2
|
-
import type { AnyFunction } from './function';
|
|
3
2
|
export type InterceptableOptions = Record<string, any>;
|
|
4
3
|
export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
5
4
|
next(options?: TOptions): Promise<TResult>;
|
|
@@ -21,7 +20,6 @@ export declare function onStart<TOptions extends {
|
|
|
21
20
|
export declare function onSuccess<TOptions extends {
|
|
22
21
|
next(): any;
|
|
23
22
|
}, TRest extends any[]>(callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
24
|
-
export type InferError<T extends AnyFunction> = T extends Interceptor<any, any, infer TError> ? TError : unknown;
|
|
25
23
|
/**
|
|
26
24
|
* Can used for interceptors or middlewares
|
|
27
25
|
*/
|
package/dist/src/json.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function parseEmptyableJSON(text: string): unknown;
|
|
2
2
|
//# sourceMappingURL=json.d.ts.map
|
package/dist/src/object.d.ts
CHANGED
|
@@ -5,4 +5,12 @@ export declare function findDeepMatches(check: (value: unknown) => boolean, payl
|
|
|
5
5
|
maps: Segment[][];
|
|
6
6
|
values: unknown[];
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Check if the value satisfy a `object` type in typescript
|
|
14
|
+
*/
|
|
15
|
+
export declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
8
16
|
//# sourceMappingURL=object.d.ts.map
|
package/dist/src/value.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Promisable } from 'type-fest';
|
|
2
2
|
export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
3
|
-
export declare function value<T
|
|
3
|
+
export declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
4
4
|
//# sourceMappingURL=value.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.cc4cb21",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -29,8 +29,6 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@standard-schema/spec": "1.0.0-beta.4",
|
|
33
|
-
"is-what": "^5.0.2",
|
|
34
32
|
"radash": "^12.1.0",
|
|
35
33
|
"type-fest": "^4.26.1"
|
|
36
34
|
},
|
package/dist/src/constants.d.ts
DELETED
package/dist/src/proxy.d.ts
DELETED