@orpc/openapi-client 0.0.0-next.ea0903c → 0.0.0-next.eea495c
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 +14 -17
- package/dist/adapters/fetch/index.d.mts +22 -0
- package/dist/adapters/fetch/index.d.ts +22 -0
- package/dist/adapters/fetch/index.mjs +16 -0
- package/dist/adapters/standard/index.d.mts +6 -35
- package/dist/adapters/standard/index.d.ts +6 -35
- package/dist/adapters/standard/index.mjs +6 -239
- package/dist/index.d.mts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/shared/openapi-client.D89vdV2Y.mjs +413 -0
- package/dist/shared/openapi-client.D_hC2pAM.d.mts +79 -0
- package/dist/shared/openapi-client.D_hC2pAM.d.ts +79 -0
- package/package.json +13 -4
package/README.md
CHANGED
|
@@ -21,28 +21,24 @@
|
|
|
21
21
|
|
|
22
22
|
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
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
|
|
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
25
|
|
|
26
26
|
---
|
|
27
27
|
|
|
28
28
|
## Highlights
|
|
29
29
|
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
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.
|
|
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), 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.
|
|
46
42
|
|
|
47
43
|
## Documentation
|
|
48
44
|
|
|
@@ -53,6 +49,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
53
49
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
50
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
51
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
52
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
56
53
|
- [@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
54
|
- [@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
55
|
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ClientContext } from '@orpc/client';
|
|
2
|
+
import { LinkFetchClientOptions } from '@orpc/client/fetch';
|
|
3
|
+
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { f as StandardOpenAPILinkOptions, g as StandardOpenAPILink } from '../../shared/openapi-client.D_hC2pAM.mjs';
|
|
5
|
+
import '@orpc/client/standard';
|
|
6
|
+
import '@orpc/shared';
|
|
7
|
+
import '@orpc/standard-server';
|
|
8
|
+
|
|
9
|
+
interface OpenAPILinkOptions<T extends ClientContext> extends StandardOpenAPILinkOptions<T>, LinkFetchClientOptions<T> {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The OpenAPI Link for fetch runtime communicates with the server that follow the OpenAPI specification.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/client/openapi-link OpenAPI Link Docs}
|
|
15
|
+
* @see {@link https://swagger.io/specification/ OpenAPI Specification}
|
|
16
|
+
*/
|
|
17
|
+
declare class OpenAPILink<T extends ClientContext> extends StandardOpenAPILink<T> {
|
|
18
|
+
constructor(contract: AnyContractRouter, options: OpenAPILinkOptions<T>);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { OpenAPILink };
|
|
22
|
+
export type { OpenAPILinkOptions };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ClientContext } from '@orpc/client';
|
|
2
|
+
import { LinkFetchClientOptions } from '@orpc/client/fetch';
|
|
3
|
+
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { f as StandardOpenAPILinkOptions, g as StandardOpenAPILink } from '../../shared/openapi-client.D_hC2pAM.js';
|
|
5
|
+
import '@orpc/client/standard';
|
|
6
|
+
import '@orpc/shared';
|
|
7
|
+
import '@orpc/standard-server';
|
|
8
|
+
|
|
9
|
+
interface OpenAPILinkOptions<T extends ClientContext> extends StandardOpenAPILinkOptions<T>, LinkFetchClientOptions<T> {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The OpenAPI Link for fetch runtime communicates with the server that follow the OpenAPI specification.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/client/openapi-link OpenAPI Link Docs}
|
|
15
|
+
* @see {@link https://swagger.io/specification/ OpenAPI Specification}
|
|
16
|
+
*/
|
|
17
|
+
declare class OpenAPILink<T extends ClientContext> extends StandardOpenAPILink<T> {
|
|
18
|
+
constructor(contract: AnyContractRouter, options: OpenAPILinkOptions<T>);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { OpenAPILink };
|
|
22
|
+
export type { OpenAPILinkOptions };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LinkFetchClient } from '@orpc/client/fetch';
|
|
2
|
+
import '@orpc/shared';
|
|
3
|
+
import { b as StandardOpenAPILink } from '../../shared/openapi-client.D89vdV2Y.mjs';
|
|
4
|
+
import '@orpc/client';
|
|
5
|
+
import '@orpc/client/standard';
|
|
6
|
+
import '@orpc/contract';
|
|
7
|
+
import '@orpc/standard-server';
|
|
8
|
+
|
|
9
|
+
class OpenAPILink extends StandardOpenAPILink {
|
|
10
|
+
constructor(contract, options) {
|
|
11
|
+
const linkClient = new LinkFetchClient(options);
|
|
12
|
+
super(contract, linkClient, options);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { OpenAPILink };
|
|
@@ -1,38 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
export { S as StandardBracketNotationSerialized, a as StandardBracketNotationSerializer, c as StandardOpenAPICustomJsonSerializer, b as StandardOpenAPIJsonSerialized, e as StandardOpenAPIJsonSerializer, d as StandardOpenAPIJsonSerializerOptions, g as StandardOpenAPILink, f as StandardOpenAPILinkOptions, j as StandardOpenAPISerializeOptions, k as StandardOpenAPISerializer, i as StandardOpenapiLinkCodec, h as StandardOpenapiLinkCodecOptions } from '../../shared/openapi-client.D_hC2pAM.mjs';
|
|
2
2
|
import { HTTPPath } from '@orpc/client';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
deserialize(serialized: StandardBracketNotationSerialized): Record<string, unknown> | unknown[];
|
|
8
|
-
stringifyPath(segments: readonly Segment[]): string;
|
|
9
|
-
parsePath(path: string): string[];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type StandardOpenAPIJsonSerialized = [json: unknown, hasBlob: boolean];
|
|
13
|
-
interface StandardOpenAPICustomJsonSerializer {
|
|
14
|
-
condition(data: unknown): boolean;
|
|
15
|
-
serialize(data: any): unknown;
|
|
16
|
-
}
|
|
17
|
-
interface StandardOpenAPIJsonSerializerOptions {
|
|
18
|
-
customJsonSerializers?: readonly StandardOpenAPICustomJsonSerializer[];
|
|
19
|
-
}
|
|
20
|
-
declare class StandardOpenAPIJsonSerializer {
|
|
21
|
-
private readonly customSerializers;
|
|
22
|
-
constructor(options?: StandardOpenAPIJsonSerializerOptions);
|
|
23
|
-
serialize(data: unknown, hasBlobRef?: {
|
|
24
|
-
value: boolean;
|
|
25
|
-
}): StandardOpenAPIJsonSerialized;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
declare class StandardOpenAPISerializer {
|
|
29
|
-
#private;
|
|
30
|
-
private readonly jsonSerializer;
|
|
31
|
-
private readonly bracketNotation;
|
|
32
|
-
constructor(jsonSerializer: StandardOpenAPIJsonSerializer, bracketNotation: StandardBracketNotationSerializer);
|
|
33
|
-
serialize(data: unknown): unknown;
|
|
34
|
-
deserialize(data: unknown): unknown;
|
|
35
|
-
}
|
|
3
|
+
import '@orpc/client/standard';
|
|
4
|
+
import '@orpc/contract';
|
|
5
|
+
import '@orpc/shared';
|
|
6
|
+
import '@orpc/standard-server';
|
|
36
7
|
|
|
37
8
|
/**
|
|
38
9
|
* @internal
|
|
@@ -46,4 +17,4 @@ declare function getDynamicParams(path: HTTPPath | undefined): {
|
|
|
46
17
|
name: string;
|
|
47
18
|
}[] | undefined;
|
|
48
19
|
|
|
49
|
-
export {
|
|
20
|
+
export { getDynamicParams, standardizeHTTPPath };
|
|
@@ -1,38 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
export { S as StandardBracketNotationSerialized, a as StandardBracketNotationSerializer, c as StandardOpenAPICustomJsonSerializer, b as StandardOpenAPIJsonSerialized, e as StandardOpenAPIJsonSerializer, d as StandardOpenAPIJsonSerializerOptions, g as StandardOpenAPILink, f as StandardOpenAPILinkOptions, j as StandardOpenAPISerializeOptions, k as StandardOpenAPISerializer, i as StandardOpenapiLinkCodec, h as StandardOpenapiLinkCodecOptions } from '../../shared/openapi-client.D_hC2pAM.js';
|
|
2
2
|
import { HTTPPath } from '@orpc/client';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
deserialize(serialized: StandardBracketNotationSerialized): Record<string, unknown> | unknown[];
|
|
8
|
-
stringifyPath(segments: readonly Segment[]): string;
|
|
9
|
-
parsePath(path: string): string[];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type StandardOpenAPIJsonSerialized = [json: unknown, hasBlob: boolean];
|
|
13
|
-
interface StandardOpenAPICustomJsonSerializer {
|
|
14
|
-
condition(data: unknown): boolean;
|
|
15
|
-
serialize(data: any): unknown;
|
|
16
|
-
}
|
|
17
|
-
interface StandardOpenAPIJsonSerializerOptions {
|
|
18
|
-
customJsonSerializers?: readonly StandardOpenAPICustomJsonSerializer[];
|
|
19
|
-
}
|
|
20
|
-
declare class StandardOpenAPIJsonSerializer {
|
|
21
|
-
private readonly customSerializers;
|
|
22
|
-
constructor(options?: StandardOpenAPIJsonSerializerOptions);
|
|
23
|
-
serialize(data: unknown, hasBlobRef?: {
|
|
24
|
-
value: boolean;
|
|
25
|
-
}): StandardOpenAPIJsonSerialized;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
declare class StandardOpenAPISerializer {
|
|
29
|
-
#private;
|
|
30
|
-
private readonly jsonSerializer;
|
|
31
|
-
private readonly bracketNotation;
|
|
32
|
-
constructor(jsonSerializer: StandardOpenAPIJsonSerializer, bracketNotation: StandardBracketNotationSerializer);
|
|
33
|
-
serialize(data: unknown): unknown;
|
|
34
|
-
deserialize(data: unknown): unknown;
|
|
35
|
-
}
|
|
3
|
+
import '@orpc/client/standard';
|
|
4
|
+
import '@orpc/contract';
|
|
5
|
+
import '@orpc/shared';
|
|
6
|
+
import '@orpc/standard-server';
|
|
36
7
|
|
|
37
8
|
/**
|
|
38
9
|
* @internal
|
|
@@ -46,4 +17,4 @@ declare function getDynamicParams(path: HTTPPath | undefined): {
|
|
|
46
17
|
name: string;
|
|
47
18
|
}[] | undefined;
|
|
48
19
|
|
|
49
|
-
export {
|
|
20
|
+
export { getDynamicParams, standardizeHTTPPath };
|
|
@@ -1,239 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (Array.isArray(data)) {
|
|
8
|
-
data.forEach((item, i) => {
|
|
9
|
-
this.serialize(item, [...segments, i], result);
|
|
10
|
-
});
|
|
11
|
-
} else if (isObject(data)) {
|
|
12
|
-
for (const key in data) {
|
|
13
|
-
this.serialize(data[key], [...segments, key], result);
|
|
14
|
-
}
|
|
15
|
-
} else {
|
|
16
|
-
result.push([this.stringifyPath(segments), data]);
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
20
|
-
deserialize(serialized) {
|
|
21
|
-
if (serialized.length === 0) {
|
|
22
|
-
return {};
|
|
23
|
-
}
|
|
24
|
-
const arrayPushStyles = /* @__PURE__ */ new WeakSet();
|
|
25
|
-
const ref = { value: [] };
|
|
26
|
-
for (const [path, value] of serialized) {
|
|
27
|
-
const segments = this.parsePath(path);
|
|
28
|
-
let currentRef = ref;
|
|
29
|
-
let nextSegment = "value";
|
|
30
|
-
segments.forEach((segment, i) => {
|
|
31
|
-
if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
|
|
32
|
-
currentRef[nextSegment] = [];
|
|
33
|
-
}
|
|
34
|
-
if (i !== segments.length - 1) {
|
|
35
|
-
if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment)) {
|
|
36
|
-
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
if (Array.isArray(currentRef[nextSegment])) {
|
|
40
|
-
if (segment === "") {
|
|
41
|
-
if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
|
|
42
|
-
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
46
|
-
currentRef[nextSegment] = { "": currentRef[nextSegment].at(-1) };
|
|
47
|
-
} else if (!isValidArrayIndex(segment)) {
|
|
48
|
-
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
currentRef = currentRef[nextSegment];
|
|
54
|
-
nextSegment = segment;
|
|
55
|
-
});
|
|
56
|
-
if (Array.isArray(currentRef)) {
|
|
57
|
-
if (nextSegment === "") {
|
|
58
|
-
arrayPushStyles.add(currentRef);
|
|
59
|
-
currentRef.push(value);
|
|
60
|
-
} else {
|
|
61
|
-
currentRef[Number(nextSegment)] = value;
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
currentRef[nextSegment] = value;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return ref.value;
|
|
68
|
-
}
|
|
69
|
-
stringifyPath(segments) {
|
|
70
|
-
return segments.map((segment) => {
|
|
71
|
-
return segment.toString().replace(/[\\[\]]/g, (match) => {
|
|
72
|
-
switch (match) {
|
|
73
|
-
case "\\":
|
|
74
|
-
return "\\\\";
|
|
75
|
-
case "[":
|
|
76
|
-
return "\\[";
|
|
77
|
-
case "]":
|
|
78
|
-
return "\\]";
|
|
79
|
-
/* v8 ignore next 2 */
|
|
80
|
-
default:
|
|
81
|
-
return match;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}).reduce((result, segment, i) => {
|
|
85
|
-
if (i === 0) {
|
|
86
|
-
return segment;
|
|
87
|
-
}
|
|
88
|
-
return `${result}[${segment}]`;
|
|
89
|
-
}, "");
|
|
90
|
-
}
|
|
91
|
-
parsePath(path) {
|
|
92
|
-
const segments = [];
|
|
93
|
-
let inBrackets = false;
|
|
94
|
-
let currentSegment = "";
|
|
95
|
-
let backslashCount = 0;
|
|
96
|
-
for (let i = 0; i < path.length; i++) {
|
|
97
|
-
const char = path[i];
|
|
98
|
-
const nextChar = path[i + 1];
|
|
99
|
-
if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
|
|
100
|
-
if (nextChar === void 0) {
|
|
101
|
-
inBrackets = false;
|
|
102
|
-
}
|
|
103
|
-
segments.push(currentSegment);
|
|
104
|
-
currentSegment = "";
|
|
105
|
-
i++;
|
|
106
|
-
} else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
|
|
107
|
-
inBrackets = true;
|
|
108
|
-
segments.push(currentSegment);
|
|
109
|
-
currentSegment = "";
|
|
110
|
-
} else if (char === "\\") {
|
|
111
|
-
backslashCount++;
|
|
112
|
-
} else {
|
|
113
|
-
currentSegment += "\\".repeat(backslashCount / 2) + char;
|
|
114
|
-
backslashCount = 0;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return inBrackets || segments.length === 0 ? [path] : segments;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function isValidArrayIndex(value) {
|
|
121
|
-
return /^0$|^[1-9]\d*$/.test(value);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
class StandardOpenAPIJsonSerializer {
|
|
125
|
-
customSerializers;
|
|
126
|
-
constructor(options = {}) {
|
|
127
|
-
this.customSerializers = options.customJsonSerializers ?? [];
|
|
128
|
-
}
|
|
129
|
-
serialize(data, hasBlobRef = { value: false }) {
|
|
130
|
-
for (const custom of this.customSerializers) {
|
|
131
|
-
if (custom.condition(data)) {
|
|
132
|
-
const result = this.serialize(custom.serialize(data), hasBlobRef);
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (data instanceof Blob) {
|
|
137
|
-
hasBlobRef.value = true;
|
|
138
|
-
return [data, hasBlobRef.value];
|
|
139
|
-
}
|
|
140
|
-
if (data instanceof Set) {
|
|
141
|
-
return this.serialize(Array.from(data), hasBlobRef);
|
|
142
|
-
}
|
|
143
|
-
if (data instanceof Map) {
|
|
144
|
-
return this.serialize(Array.from(data.entries()), hasBlobRef);
|
|
145
|
-
}
|
|
146
|
-
if (Array.isArray(data)) {
|
|
147
|
-
const json = data.map((v) => v === void 0 ? null : this.serialize(v, hasBlobRef)[0]);
|
|
148
|
-
return [json, hasBlobRef.value];
|
|
149
|
-
}
|
|
150
|
-
if (isObject(data)) {
|
|
151
|
-
const json = {};
|
|
152
|
-
for (const k in data) {
|
|
153
|
-
if (k === "toJSON" && typeof data[k] === "function") {
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
json[k] = this.serialize(data[k], hasBlobRef)[0];
|
|
157
|
-
}
|
|
158
|
-
return [json, hasBlobRef.value];
|
|
159
|
-
}
|
|
160
|
-
if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
|
|
161
|
-
return [data.toString(), hasBlobRef.value];
|
|
162
|
-
}
|
|
163
|
-
if (data instanceof Date) {
|
|
164
|
-
return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
|
|
165
|
-
}
|
|
166
|
-
if (Number.isNaN(data)) {
|
|
167
|
-
return [null, hasBlobRef.value];
|
|
168
|
-
}
|
|
169
|
-
return [data, hasBlobRef.value];
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
class StandardOpenAPISerializer {
|
|
174
|
-
constructor(jsonSerializer, bracketNotation) {
|
|
175
|
-
this.jsonSerializer = jsonSerializer;
|
|
176
|
-
this.bracketNotation = bracketNotation;
|
|
177
|
-
}
|
|
178
|
-
serialize(data) {
|
|
179
|
-
if (isAsyncIteratorObject(data)) {
|
|
180
|
-
return mapEventIterator(data, {
|
|
181
|
-
value: async (value) => this.#serialize(value, false),
|
|
182
|
-
error: async (e) => {
|
|
183
|
-
return new ErrorEvent({
|
|
184
|
-
data: this.#serialize(toORPCError(e).toJSON(), false),
|
|
185
|
-
cause: e
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
return this.#serialize(data, true);
|
|
191
|
-
}
|
|
192
|
-
#serialize(data, enableFormData) {
|
|
193
|
-
if (data instanceof Blob || data === void 0) {
|
|
194
|
-
return data;
|
|
195
|
-
}
|
|
196
|
-
const [json, hasBlob] = this.jsonSerializer.serialize(data);
|
|
197
|
-
if (!enableFormData || !hasBlob) {
|
|
198
|
-
return json;
|
|
199
|
-
}
|
|
200
|
-
const form = new FormData();
|
|
201
|
-
for (const [path, value] of this.bracketNotation.serialize(json)) {
|
|
202
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
203
|
-
form.append(path, value.toString());
|
|
204
|
-
} else if (value instanceof Blob) {
|
|
205
|
-
form.append(path, value);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return form;
|
|
209
|
-
}
|
|
210
|
-
deserialize(data) {
|
|
211
|
-
if (data instanceof URLSearchParams || data instanceof FormData) {
|
|
212
|
-
return this.bracketNotation.deserialize(Array.from(data.entries()));
|
|
213
|
-
}
|
|
214
|
-
if (isAsyncIteratorObject(data)) {
|
|
215
|
-
return mapEventIterator(data, {
|
|
216
|
-
value: async (value) => value,
|
|
217
|
-
error: async (e) => {
|
|
218
|
-
if (e instanceof ErrorEvent && ORPCError.isValidJSON(e.data)) {
|
|
219
|
-
return ORPCError.fromJSON(e.data, { cause: e });
|
|
220
|
-
}
|
|
221
|
-
return e;
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
return data;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function standardizeHTTPPath(path) {
|
|
230
|
-
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
231
|
-
}
|
|
232
|
-
function getDynamicParams(path) {
|
|
233
|
-
return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
|
|
234
|
-
raw: v,
|
|
235
|
-
name: v.match(/\{\+?([^}]+)\}/)[1]
|
|
236
|
-
})) : void 0;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export { StandardBracketNotationSerializer, StandardOpenAPIJsonSerializer, StandardOpenAPISerializer, getDynamicParams, standardizeHTTPPath };
|
|
1
|
+
export { S as StandardBracketNotationSerializer, a as StandardOpenAPIJsonSerializer, b as StandardOpenAPILink, d as StandardOpenAPISerializer, c as StandardOpenapiLinkCodec, g as getDynamicParams, s as standardizeHTTPPath } from '../../shared/openapi-client.D89vdV2Y.mjs';
|
|
2
|
+
import '@orpc/client/standard';
|
|
3
|
+
import '@orpc/shared';
|
|
4
|
+
import '@orpc/client';
|
|
5
|
+
import '@orpc/contract';
|
|
6
|
+
import '@orpc/standard-server';
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
import { NestedClient, Client, ORPCError } from '@orpc/client';
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
type JsonifiedValue<T> = T extends string ? T : T extends number ? T : T extends boolean ? T : T extends null ? T : T extends undefined ? T : T extends Array<unknown> ? JsonifiedArray<T> : T extends Record<string, unknown> ? {
|
|
4
|
+
[K in keyof T]: JsonifiedValue<T[K]>;
|
|
5
|
+
} : T extends Date ? string : T extends bigint ? string : T extends File ? File : T extends Blob ? Blob : T extends RegExp ? string : T extends URL ? string : T extends Map<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends Set<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown;
|
|
6
|
+
type JsonifiedArray<T extends Array<unknown>> = T extends readonly [] ? [] : T extends readonly [infer U, ...infer V] ? [U extends undefined ? null : JsonifiedValue<U>, ...JsonifiedArray<V>] : T extends Array<infer U> ? Array<JsonifiedValue<U>> : unknown;
|
|
7
|
+
/**
|
|
8
|
+
* Convert types that JSON not support to corresponding json types
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/client/openapi-link OpenAPI Link Docs}
|
|
11
|
+
*/
|
|
12
|
+
type JsonifiedClient<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, UError extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : UError> : {
|
|
13
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? JsonifiedClient<T[K]> : T[K];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type { JsonifiedArray, JsonifiedClient, JsonifiedValue };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
import { NestedClient, Client, ORPCError } from '@orpc/client';
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
type JsonifiedValue<T> = T extends string ? T : T extends number ? T : T extends boolean ? T : T extends null ? T : T extends undefined ? T : T extends Array<unknown> ? JsonifiedArray<T> : T extends Record<string, unknown> ? {
|
|
4
|
+
[K in keyof T]: JsonifiedValue<T[K]>;
|
|
5
|
+
} : T extends Date ? string : T extends bigint ? string : T extends File ? File : T extends Blob ? Blob : T extends RegExp ? string : T extends URL ? string : T extends Map<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends Set<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown;
|
|
6
|
+
type JsonifiedArray<T extends Array<unknown>> = T extends readonly [] ? [] : T extends readonly [infer U, ...infer V] ? [U extends undefined ? null : JsonifiedValue<U>, ...JsonifiedArray<V>] : T extends Array<infer U> ? Array<JsonifiedValue<U>> : unknown;
|
|
7
|
+
/**
|
|
8
|
+
* Convert types that JSON not support to corresponding json types
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/client/openapi-link OpenAPI Link Docs}
|
|
11
|
+
*/
|
|
12
|
+
type JsonifiedClient<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, UError extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : UError> : {
|
|
13
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? JsonifiedClient<T[K]> : T[K];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type { JsonifiedArray, JsonifiedClient, JsonifiedValue };
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { toHttpPath, getMalformedResponseErrorCode, StandardLink } from '@orpc/client/standard';
|
|
2
|
+
import { isObject, value, get, isAsyncIteratorObject } from '@orpc/shared';
|
|
3
|
+
import { isORPCErrorStatus, isORPCErrorJson, createORPCErrorFromJson, mapEventIterator, toORPCError } from '@orpc/client';
|
|
4
|
+
import { isContractProcedure, fallbackContractConfig, ORPCError } from '@orpc/contract';
|
|
5
|
+
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
6
|
+
|
|
7
|
+
class StandardBracketNotationSerializer {
|
|
8
|
+
serialize(data, segments = [], result = []) {
|
|
9
|
+
if (Array.isArray(data)) {
|
|
10
|
+
data.forEach((item, i) => {
|
|
11
|
+
this.serialize(item, [...segments, i], result);
|
|
12
|
+
});
|
|
13
|
+
} else if (isObject(data)) {
|
|
14
|
+
for (const key in data) {
|
|
15
|
+
this.serialize(data[key], [...segments, key], result);
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
result.push([this.stringifyPath(segments), data]);
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
deserialize(serialized) {
|
|
23
|
+
if (serialized.length === 0) {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
const arrayPushStyles = /* @__PURE__ */ new WeakSet();
|
|
27
|
+
const ref = { value: [] };
|
|
28
|
+
for (const [path, value] of serialized) {
|
|
29
|
+
const segments = this.parsePath(path);
|
|
30
|
+
let currentRef = ref;
|
|
31
|
+
let nextSegment = "value";
|
|
32
|
+
segments.forEach((segment, i) => {
|
|
33
|
+
if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
|
|
34
|
+
currentRef[nextSegment] = [];
|
|
35
|
+
}
|
|
36
|
+
if (i !== segments.length - 1) {
|
|
37
|
+
if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment)) {
|
|
38
|
+
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
42
|
+
if (segment === "") {
|
|
43
|
+
if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
|
|
44
|
+
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
48
|
+
currentRef[nextSegment] = { "": currentRef[nextSegment].at(-1) };
|
|
49
|
+
} else if (!isValidArrayIndex(segment)) {
|
|
50
|
+
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
currentRef = currentRef[nextSegment];
|
|
56
|
+
nextSegment = segment;
|
|
57
|
+
});
|
|
58
|
+
if (Array.isArray(currentRef)) {
|
|
59
|
+
if (nextSegment === "") {
|
|
60
|
+
arrayPushStyles.add(currentRef);
|
|
61
|
+
currentRef.push(value);
|
|
62
|
+
} else {
|
|
63
|
+
currentRef[Number(nextSegment)] = value;
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
currentRef[nextSegment] = value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return ref.value;
|
|
70
|
+
}
|
|
71
|
+
stringifyPath(segments) {
|
|
72
|
+
return segments.map((segment) => {
|
|
73
|
+
return segment.toString().replace(/[\\[\]]/g, (match) => {
|
|
74
|
+
switch (match) {
|
|
75
|
+
case "\\":
|
|
76
|
+
return "\\\\";
|
|
77
|
+
case "[":
|
|
78
|
+
return "\\[";
|
|
79
|
+
case "]":
|
|
80
|
+
return "\\]";
|
|
81
|
+
/* v8 ignore next 2 */
|
|
82
|
+
default:
|
|
83
|
+
return match;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}).reduce((result, segment, i) => {
|
|
87
|
+
if (i === 0) {
|
|
88
|
+
return segment;
|
|
89
|
+
}
|
|
90
|
+
return `${result}[${segment}]`;
|
|
91
|
+
}, "");
|
|
92
|
+
}
|
|
93
|
+
parsePath(path) {
|
|
94
|
+
const segments = [];
|
|
95
|
+
let inBrackets = false;
|
|
96
|
+
let currentSegment = "";
|
|
97
|
+
let backslashCount = 0;
|
|
98
|
+
for (let i = 0; i < path.length; i++) {
|
|
99
|
+
const char = path[i];
|
|
100
|
+
const nextChar = path[i + 1];
|
|
101
|
+
if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
|
|
102
|
+
if (nextChar === void 0) {
|
|
103
|
+
inBrackets = false;
|
|
104
|
+
}
|
|
105
|
+
segments.push(currentSegment);
|
|
106
|
+
currentSegment = "";
|
|
107
|
+
i++;
|
|
108
|
+
} else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
|
|
109
|
+
inBrackets = true;
|
|
110
|
+
segments.push(currentSegment);
|
|
111
|
+
currentSegment = "";
|
|
112
|
+
} else if (char === "\\") {
|
|
113
|
+
backslashCount++;
|
|
114
|
+
} else {
|
|
115
|
+
currentSegment += "\\".repeat(backslashCount / 2) + char;
|
|
116
|
+
backslashCount = 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return inBrackets || segments.length === 0 ? [path] : segments;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function isValidArrayIndex(value) {
|
|
123
|
+
return /^0$|^[1-9]\d*$/.test(value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
class StandardOpenAPIJsonSerializer {
|
|
127
|
+
customSerializers;
|
|
128
|
+
constructor(options = {}) {
|
|
129
|
+
this.customSerializers = options.customJsonSerializers ?? [];
|
|
130
|
+
}
|
|
131
|
+
serialize(data, hasBlobRef = { value: false }) {
|
|
132
|
+
for (const custom of this.customSerializers) {
|
|
133
|
+
if (custom.condition(data)) {
|
|
134
|
+
const result = this.serialize(custom.serialize(data), hasBlobRef);
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (data instanceof Blob) {
|
|
139
|
+
hasBlobRef.value = true;
|
|
140
|
+
return [data, hasBlobRef.value];
|
|
141
|
+
}
|
|
142
|
+
if (data instanceof Set) {
|
|
143
|
+
return this.serialize(Array.from(data), hasBlobRef);
|
|
144
|
+
}
|
|
145
|
+
if (data instanceof Map) {
|
|
146
|
+
return this.serialize(Array.from(data.entries()), hasBlobRef);
|
|
147
|
+
}
|
|
148
|
+
if (Array.isArray(data)) {
|
|
149
|
+
const json = data.map((v) => v === void 0 ? null : this.serialize(v, hasBlobRef)[0]);
|
|
150
|
+
return [json, hasBlobRef.value];
|
|
151
|
+
}
|
|
152
|
+
if (isObject(data)) {
|
|
153
|
+
const json = {};
|
|
154
|
+
for (const k in data) {
|
|
155
|
+
if (k === "toJSON" && typeof data[k] === "function") {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
json[k] = this.serialize(data[k], hasBlobRef)[0];
|
|
159
|
+
}
|
|
160
|
+
return [json, hasBlobRef.value];
|
|
161
|
+
}
|
|
162
|
+
if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
|
|
163
|
+
return [data.toString(), hasBlobRef.value];
|
|
164
|
+
}
|
|
165
|
+
if (data instanceof Date) {
|
|
166
|
+
return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
|
|
167
|
+
}
|
|
168
|
+
if (Number.isNaN(data)) {
|
|
169
|
+
return [null, hasBlobRef.value];
|
|
170
|
+
}
|
|
171
|
+
return [data, hasBlobRef.value];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function standardizeHTTPPath(path) {
|
|
176
|
+
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
177
|
+
}
|
|
178
|
+
function getDynamicParams(path) {
|
|
179
|
+
return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
|
|
180
|
+
raw: v,
|
|
181
|
+
name: v.match(/\{\+?([^}]+)\}/)[1]
|
|
182
|
+
})) : void 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
class StandardOpenapiLinkCodec {
|
|
186
|
+
constructor(contract, serializer, options) {
|
|
187
|
+
this.contract = contract;
|
|
188
|
+
this.serializer = serializer;
|
|
189
|
+
this.baseUrl = options.url;
|
|
190
|
+
this.headers = options.headers ?? {};
|
|
191
|
+
}
|
|
192
|
+
baseUrl;
|
|
193
|
+
headers;
|
|
194
|
+
async encode(path, input, options) {
|
|
195
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
196
|
+
let headers = await value(this.headers, options, path, input);
|
|
197
|
+
if (options.lastEventId !== void 0) {
|
|
198
|
+
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
199
|
+
}
|
|
200
|
+
const procedure = get(this.contract, path);
|
|
201
|
+
if (!isContractProcedure(procedure)) {
|
|
202
|
+
throw new Error(`[StandardOpenapiLinkCodec] expect a contract procedure at ${path.join(".")}`);
|
|
203
|
+
}
|
|
204
|
+
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
205
|
+
return inputStructure === "compact" ? this.#encodeCompact(procedure, path, input, options, baseUrl, headers) : this.#encodeDetailed(procedure, path, input, options, baseUrl, headers);
|
|
206
|
+
}
|
|
207
|
+
#encodeCompact(procedure, path, input, options, baseUrl, headers) {
|
|
208
|
+
let httpPath = standardizeHTTPPath(procedure["~orpc"].route.path ?? toHttpPath(path));
|
|
209
|
+
let httpBody = input;
|
|
210
|
+
const dynamicParams = getDynamicParams(httpPath);
|
|
211
|
+
if (dynamicParams?.length) {
|
|
212
|
+
if (!isObject(input)) {
|
|
213
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input shape for "compact" structure when has dynamic params at ${path.join(".")}.`);
|
|
214
|
+
}
|
|
215
|
+
const body = { ...input };
|
|
216
|
+
for (const param of dynamicParams) {
|
|
217
|
+
const value2 = input[param.name];
|
|
218
|
+
httpPath = httpPath.replace(param.raw, `/${encodeURIComponent(`${this.serializer.serialize(value2)}`)}`);
|
|
219
|
+
delete body[param.name];
|
|
220
|
+
}
|
|
221
|
+
httpBody = Object.keys(body).length ? body : void 0;
|
|
222
|
+
}
|
|
223
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
224
|
+
const url = new URL(baseUrl);
|
|
225
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${httpPath}`;
|
|
226
|
+
if (method === "GET") {
|
|
227
|
+
const serialized = this.serializer.serialize(httpBody, { outputFormat: "URLSearchParams" });
|
|
228
|
+
for (const [key, value2] of serialized) {
|
|
229
|
+
url.searchParams.append(key, value2);
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
url,
|
|
233
|
+
method,
|
|
234
|
+
headers,
|
|
235
|
+
body: void 0,
|
|
236
|
+
signal: options.signal
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
url,
|
|
241
|
+
method,
|
|
242
|
+
headers,
|
|
243
|
+
body: this.serializer.serialize(httpBody),
|
|
244
|
+
signal: options.signal
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
#encodeDetailed(procedure, path, input, options, baseUrl, headers) {
|
|
248
|
+
let httpPath = standardizeHTTPPath(procedure["~orpc"].route.path ?? toHttpPath(path));
|
|
249
|
+
const dynamicParams = getDynamicParams(httpPath);
|
|
250
|
+
if (!isObject(input) && input !== void 0) {
|
|
251
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input shape for "detailed" structure at ${path.join(".")}.`);
|
|
252
|
+
}
|
|
253
|
+
if (dynamicParams?.length) {
|
|
254
|
+
if (!isObject(input?.params)) {
|
|
255
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input.params shape for "detailed" structure when has dynamic params at ${path.join(".")}.`);
|
|
256
|
+
}
|
|
257
|
+
for (const param of dynamicParams) {
|
|
258
|
+
const value2 = input.params[param.name];
|
|
259
|
+
httpPath = httpPath.replace(param.raw, `/${encodeURIComponent(`${this.serializer.serialize(value2)}`)}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
let mergedHeaders = headers;
|
|
263
|
+
if (input?.headers !== void 0) {
|
|
264
|
+
if (!isObject(input.headers)) {
|
|
265
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input.headers shape for "detailed" structure at ${path.join(".")}.`);
|
|
266
|
+
}
|
|
267
|
+
mergedHeaders = mergeStandardHeaders(input.headers, headers);
|
|
268
|
+
}
|
|
269
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
270
|
+
const url = new URL(baseUrl);
|
|
271
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${httpPath}`;
|
|
272
|
+
if (input?.query !== void 0) {
|
|
273
|
+
const query = this.serializer.serialize(input.query, { outputFormat: "URLSearchParams" });
|
|
274
|
+
for (const [key, value2] of query) {
|
|
275
|
+
url.searchParams.append(key, value2);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (method === "GET") {
|
|
279
|
+
return {
|
|
280
|
+
url,
|
|
281
|
+
method,
|
|
282
|
+
headers: mergedHeaders,
|
|
283
|
+
body: void 0,
|
|
284
|
+
signal: options.signal
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
url,
|
|
289
|
+
method,
|
|
290
|
+
headers: mergedHeaders,
|
|
291
|
+
body: this.serializer.serialize(input?.body),
|
|
292
|
+
signal: options.signal
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
async decode(response, _options, path) {
|
|
296
|
+
const isOk = !isORPCErrorStatus(response.status);
|
|
297
|
+
const deserialized = await (async () => {
|
|
298
|
+
let isBodyOk = false;
|
|
299
|
+
try {
|
|
300
|
+
const body = await response.body();
|
|
301
|
+
isBodyOk = true;
|
|
302
|
+
return this.serializer.deserialize(body);
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (!isBodyOk) {
|
|
305
|
+
throw new Error("Cannot parse response body, please check the response body and content-type.", {
|
|
306
|
+
cause: error
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
throw new Error("Invalid OpenAPI response format.", {
|
|
310
|
+
cause: error
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
})();
|
|
314
|
+
if (!isOk) {
|
|
315
|
+
if (isORPCErrorJson(deserialized)) {
|
|
316
|
+
throw createORPCErrorFromJson(deserialized);
|
|
317
|
+
}
|
|
318
|
+
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
319
|
+
status: response.status,
|
|
320
|
+
data: { ...response, body: deserialized }
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
const procedure = get(this.contract, path);
|
|
324
|
+
if (!isContractProcedure(procedure)) {
|
|
325
|
+
throw new Error(`[StandardOpenapiLinkCodec] expect a contract procedure at ${path.join(".")}`);
|
|
326
|
+
}
|
|
327
|
+
const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
|
|
328
|
+
if (outputStructure === "compact") {
|
|
329
|
+
return deserialized;
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
headers: response.headers,
|
|
333
|
+
body: deserialized
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
class StandardOpenAPISerializer {
|
|
339
|
+
constructor(jsonSerializer, bracketNotation) {
|
|
340
|
+
this.jsonSerializer = jsonSerializer;
|
|
341
|
+
this.bracketNotation = bracketNotation;
|
|
342
|
+
}
|
|
343
|
+
serialize(data, options = {}) {
|
|
344
|
+
if (isAsyncIteratorObject(data) && !options.outputFormat) {
|
|
345
|
+
return mapEventIterator(data, {
|
|
346
|
+
value: async (value) => this.#serialize(value, { outputFormat: "plain" }),
|
|
347
|
+
error: async (e) => {
|
|
348
|
+
return new ErrorEvent({
|
|
349
|
+
data: this.#serialize(toORPCError(e).toJSON(), { outputFormat: "plain" }),
|
|
350
|
+
cause: e
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return this.#serialize(data, options);
|
|
356
|
+
}
|
|
357
|
+
#serialize(data, options) {
|
|
358
|
+
const [json, hasBlob] = this.jsonSerializer.serialize(data);
|
|
359
|
+
if (options.outputFormat === "plain") {
|
|
360
|
+
return json;
|
|
361
|
+
}
|
|
362
|
+
if (options.outputFormat === "URLSearchParams") {
|
|
363
|
+
const params = new URLSearchParams();
|
|
364
|
+
for (const [path, value] of this.bracketNotation.serialize(json)) {
|
|
365
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
366
|
+
params.append(path, value.toString());
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return params;
|
|
370
|
+
}
|
|
371
|
+
if (json instanceof Blob || json === void 0 || !hasBlob) {
|
|
372
|
+
return json;
|
|
373
|
+
}
|
|
374
|
+
const form = new FormData();
|
|
375
|
+
for (const [path, value] of this.bracketNotation.serialize(json)) {
|
|
376
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
377
|
+
form.append(path, value.toString());
|
|
378
|
+
} else if (value instanceof Blob) {
|
|
379
|
+
form.append(path, value);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return form;
|
|
383
|
+
}
|
|
384
|
+
deserialize(data) {
|
|
385
|
+
if (data instanceof URLSearchParams || data instanceof FormData) {
|
|
386
|
+
return this.bracketNotation.deserialize(Array.from(data.entries()));
|
|
387
|
+
}
|
|
388
|
+
if (isAsyncIteratorObject(data)) {
|
|
389
|
+
return mapEventIterator(data, {
|
|
390
|
+
value: async (value) => value,
|
|
391
|
+
error: async (e) => {
|
|
392
|
+
if (e instanceof ErrorEvent && isORPCErrorJson(e.data)) {
|
|
393
|
+
return createORPCErrorFromJson(e.data, { cause: e });
|
|
394
|
+
}
|
|
395
|
+
return e;
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
return data;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
class StandardOpenAPILink extends StandardLink {
|
|
404
|
+
constructor(contract, linkClient, options) {
|
|
405
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
406
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer();
|
|
407
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
408
|
+
const linkCodec = new StandardOpenapiLinkCodec(contract, serializer, options);
|
|
409
|
+
super(linkCodec, linkClient, options);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export { StandardBracketNotationSerializer as S, StandardOpenAPIJsonSerializer as a, StandardOpenAPILink as b, StandardOpenapiLinkCodec as c, StandardOpenAPISerializer as d, getDynamicParams as g, standardizeHTTPPath as s };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ClientContext, ClientOptions } from '@orpc/client';
|
|
2
|
+
import { StandardLinkCodec, StandardLinkOptions, StandardLink, StandardLinkClient } from '@orpc/client/standard';
|
|
3
|
+
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { Segment, Value } from '@orpc/shared';
|
|
5
|
+
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
6
|
+
|
|
7
|
+
type StandardBracketNotationSerialized = [string, unknown][];
|
|
8
|
+
declare class StandardBracketNotationSerializer {
|
|
9
|
+
serialize(data: unknown, segments?: Segment[], result?: StandardBracketNotationSerialized): StandardBracketNotationSerialized;
|
|
10
|
+
deserialize(serialized: StandardBracketNotationSerialized): Record<string, unknown> | unknown[];
|
|
11
|
+
stringifyPath(segments: readonly Segment[]): string;
|
|
12
|
+
parsePath(path: string): string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type StandardOpenAPIJsonSerialized = [json: unknown, hasBlob: boolean];
|
|
16
|
+
interface StandardOpenAPICustomJsonSerializer {
|
|
17
|
+
condition(data: unknown): boolean;
|
|
18
|
+
serialize(data: any): unknown;
|
|
19
|
+
}
|
|
20
|
+
interface StandardOpenAPIJsonSerializerOptions {
|
|
21
|
+
customJsonSerializers?: readonly StandardOpenAPICustomJsonSerializer[];
|
|
22
|
+
}
|
|
23
|
+
declare class StandardOpenAPIJsonSerializer {
|
|
24
|
+
private readonly customSerializers;
|
|
25
|
+
constructor(options?: StandardOpenAPIJsonSerializerOptions);
|
|
26
|
+
serialize(data: unknown, hasBlobRef?: {
|
|
27
|
+
value: boolean;
|
|
28
|
+
}): StandardOpenAPIJsonSerialized;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface StandardOpenAPISerializeOptions {
|
|
32
|
+
outputFormat?: 'plain' | 'URLSearchParams';
|
|
33
|
+
}
|
|
34
|
+
declare class StandardOpenAPISerializer {
|
|
35
|
+
#private;
|
|
36
|
+
private readonly jsonSerializer;
|
|
37
|
+
private readonly bracketNotation;
|
|
38
|
+
constructor(jsonSerializer: StandardOpenAPIJsonSerializer, bracketNotation: StandardBracketNotationSerializer);
|
|
39
|
+
serialize(data: unknown, options?: StandardOpenAPISerializeOptions): unknown;
|
|
40
|
+
deserialize(data: unknown): unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface StandardOpenapiLinkCodecOptions<T extends ClientContext> {
|
|
44
|
+
/**
|
|
45
|
+
* Base url for all requests.
|
|
46
|
+
*/
|
|
47
|
+
url: Value<string | URL, [
|
|
48
|
+
options: ClientOptions<T>,
|
|
49
|
+
path: readonly string[],
|
|
50
|
+
input: unknown
|
|
51
|
+
]>;
|
|
52
|
+
/**
|
|
53
|
+
* Inject headers to the request.
|
|
54
|
+
*/
|
|
55
|
+
headers?: Value<StandardHeaders, [
|
|
56
|
+
options: ClientOptions<T>,
|
|
57
|
+
path: readonly string[],
|
|
58
|
+
input: unknown
|
|
59
|
+
]>;
|
|
60
|
+
}
|
|
61
|
+
declare class StandardOpenapiLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
62
|
+
#private;
|
|
63
|
+
private readonly contract;
|
|
64
|
+
private readonly serializer;
|
|
65
|
+
private readonly baseUrl;
|
|
66
|
+
private readonly headers;
|
|
67
|
+
constructor(contract: AnyContractRouter, serializer: StandardOpenAPISerializer, options: StandardOpenapiLinkCodecOptions<T>);
|
|
68
|
+
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
69
|
+
decode(response: StandardLazyResponse, _options: ClientOptions<T>, path: readonly string[]): Promise<unknown>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface StandardOpenAPILinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardOpenapiLinkCodecOptions<T>, StandardOpenAPIJsonSerializerOptions {
|
|
73
|
+
}
|
|
74
|
+
declare class StandardOpenAPILink<T extends ClientContext> extends StandardLink<T> {
|
|
75
|
+
constructor(contract: AnyContractRouter, linkClient: StandardLinkClient<T>, options: StandardOpenAPILinkOptions<T>);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { StandardBracketNotationSerializer as a, StandardOpenAPIJsonSerializer as e, StandardOpenAPILink as g, StandardOpenapiLinkCodec as i, StandardOpenAPISerializer as k };
|
|
79
|
+
export type { StandardBracketNotationSerialized as S, StandardOpenAPIJsonSerialized as b, StandardOpenAPICustomJsonSerializer as c, StandardOpenAPIJsonSerializerOptions as d, StandardOpenAPILinkOptions as f, StandardOpenapiLinkCodecOptions as h, StandardOpenAPISerializeOptions as j };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ClientContext, ClientOptions } from '@orpc/client';
|
|
2
|
+
import { StandardLinkCodec, StandardLinkOptions, StandardLink, StandardLinkClient } from '@orpc/client/standard';
|
|
3
|
+
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { Segment, Value } from '@orpc/shared';
|
|
5
|
+
import { StandardHeaders, StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
|
|
6
|
+
|
|
7
|
+
type StandardBracketNotationSerialized = [string, unknown][];
|
|
8
|
+
declare class StandardBracketNotationSerializer {
|
|
9
|
+
serialize(data: unknown, segments?: Segment[], result?: StandardBracketNotationSerialized): StandardBracketNotationSerialized;
|
|
10
|
+
deserialize(serialized: StandardBracketNotationSerialized): Record<string, unknown> | unknown[];
|
|
11
|
+
stringifyPath(segments: readonly Segment[]): string;
|
|
12
|
+
parsePath(path: string): string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type StandardOpenAPIJsonSerialized = [json: unknown, hasBlob: boolean];
|
|
16
|
+
interface StandardOpenAPICustomJsonSerializer {
|
|
17
|
+
condition(data: unknown): boolean;
|
|
18
|
+
serialize(data: any): unknown;
|
|
19
|
+
}
|
|
20
|
+
interface StandardOpenAPIJsonSerializerOptions {
|
|
21
|
+
customJsonSerializers?: readonly StandardOpenAPICustomJsonSerializer[];
|
|
22
|
+
}
|
|
23
|
+
declare class StandardOpenAPIJsonSerializer {
|
|
24
|
+
private readonly customSerializers;
|
|
25
|
+
constructor(options?: StandardOpenAPIJsonSerializerOptions);
|
|
26
|
+
serialize(data: unknown, hasBlobRef?: {
|
|
27
|
+
value: boolean;
|
|
28
|
+
}): StandardOpenAPIJsonSerialized;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface StandardOpenAPISerializeOptions {
|
|
32
|
+
outputFormat?: 'plain' | 'URLSearchParams';
|
|
33
|
+
}
|
|
34
|
+
declare class StandardOpenAPISerializer {
|
|
35
|
+
#private;
|
|
36
|
+
private readonly jsonSerializer;
|
|
37
|
+
private readonly bracketNotation;
|
|
38
|
+
constructor(jsonSerializer: StandardOpenAPIJsonSerializer, bracketNotation: StandardBracketNotationSerializer);
|
|
39
|
+
serialize(data: unknown, options?: StandardOpenAPISerializeOptions): unknown;
|
|
40
|
+
deserialize(data: unknown): unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface StandardOpenapiLinkCodecOptions<T extends ClientContext> {
|
|
44
|
+
/**
|
|
45
|
+
* Base url for all requests.
|
|
46
|
+
*/
|
|
47
|
+
url: Value<string | URL, [
|
|
48
|
+
options: ClientOptions<T>,
|
|
49
|
+
path: readonly string[],
|
|
50
|
+
input: unknown
|
|
51
|
+
]>;
|
|
52
|
+
/**
|
|
53
|
+
* Inject headers to the request.
|
|
54
|
+
*/
|
|
55
|
+
headers?: Value<StandardHeaders, [
|
|
56
|
+
options: ClientOptions<T>,
|
|
57
|
+
path: readonly string[],
|
|
58
|
+
input: unknown
|
|
59
|
+
]>;
|
|
60
|
+
}
|
|
61
|
+
declare class StandardOpenapiLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
62
|
+
#private;
|
|
63
|
+
private readonly contract;
|
|
64
|
+
private readonly serializer;
|
|
65
|
+
private readonly baseUrl;
|
|
66
|
+
private readonly headers;
|
|
67
|
+
constructor(contract: AnyContractRouter, serializer: StandardOpenAPISerializer, options: StandardOpenapiLinkCodecOptions<T>);
|
|
68
|
+
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
69
|
+
decode(response: StandardLazyResponse, _options: ClientOptions<T>, path: readonly string[]): Promise<unknown>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface StandardOpenAPILinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardOpenapiLinkCodecOptions<T>, StandardOpenAPIJsonSerializerOptions {
|
|
73
|
+
}
|
|
74
|
+
declare class StandardOpenAPILink<T extends ClientContext> extends StandardLink<T> {
|
|
75
|
+
constructor(contract: AnyContractRouter, linkClient: StandardLinkClient<T>, options: StandardOpenAPILinkOptions<T>);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { StandardBracketNotationSerializer as a, StandardOpenAPIJsonSerializer as e, StandardOpenAPILink as g, StandardOpenapiLinkCodec as i, StandardOpenAPISerializer as k };
|
|
79
|
+
export type { StandardBracketNotationSerialized as S, StandardOpenAPIJsonSerialized as b, StandardOpenAPICustomJsonSerializer as c, StandardOpenAPIJsonSerializerOptions as d, StandardOpenAPILinkOptions as f, StandardOpenapiLinkCodecOptions as h, StandardOpenAPISerializeOptions as j };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/openapi-client",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.eea495c",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -23,15 +23,24 @@
|
|
|
23
23
|
"types": "./dist/adapters/standard/index.d.mts",
|
|
24
24
|
"import": "./dist/adapters/standard/index.mjs",
|
|
25
25
|
"default": "./dist/adapters/standard/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./fetch": {
|
|
28
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
|
29
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
|
30
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
|
26
31
|
}
|
|
27
32
|
},
|
|
28
33
|
"files": [
|
|
29
34
|
"dist"
|
|
30
35
|
],
|
|
31
36
|
"dependencies": {
|
|
32
|
-
"@orpc/client": "0.0.0-next.
|
|
33
|
-
"@orpc/
|
|
34
|
-
"@orpc/
|
|
37
|
+
"@orpc/client": "0.0.0-next.eea495c",
|
|
38
|
+
"@orpc/contract": "0.0.0-next.eea495c",
|
|
39
|
+
"@orpc/shared": "0.0.0-next.eea495c",
|
|
40
|
+
"@orpc/standard-server": "0.0.0-next.eea495c"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@orpc/server": "0.0.0-next.eea495c"
|
|
35
44
|
},
|
|
36
45
|
"scripts": {
|
|
37
46
|
"build": "unbuild",
|