@orpc/openapi-client 0.0.0-next.85e5dff → 0.0.0-next.8606faa
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 +22 -22
- package/dist/adapters/fetch/index.d.mts +13 -6
- package/dist/adapters/fetch/index.d.ts +13 -6
- package/dist/adapters/fetch/index.mjs +6 -9
- package/dist/adapters/standard/index.d.mts +2 -1
- package/dist/adapters/standard/index.d.ts +2 -1
- package/dist/adapters/standard/index.mjs +5 -3
- package/dist/helpers/index.d.mts +54 -0
- package/dist/helpers/index.d.ts +54 -0
- package/dist/helpers/index.mjs +7 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/shared/{openapi-client.V75iq1Nn.mjs → openapi-client.B2Q9qU5m.mjs} +34 -133
- package/dist/shared/openapi-client.Dgl8z2tb.mjs +39 -0
- package/dist/shared/{openapi-client.N18yafE2.d.mts → openapi-client.Dx4REA6z.d.mts} +39 -7
- package/dist/shared/{openapi-client.N18yafE2.d.ts → openapi-client.Dx4REA6z.d.ts} +39 -7
- package/dist/shared/openapi-client.t9fCAe3x.mjs +146 -0
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -17,32 +17,31 @@
|
|
|
17
17
|
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
18
|
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
19
|
</a>
|
|
20
|
+
<a href="https://deepwiki.com/unnoq/orpc">
|
|
21
|
+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
|
|
22
|
+
</a>
|
|
20
23
|
</div>
|
|
21
24
|
|
|
22
25
|
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
26
|
|
|
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
|
|
27
|
+
**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
28
|
|
|
26
29
|
---
|
|
27
30
|
|
|
28
31
|
## Highlights
|
|
29
32
|
|
|
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.
|
|
33
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
34
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
35
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
36
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
37
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
38
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
39
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
40
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
41
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
42
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
43
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
44
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
46
45
|
|
|
47
46
|
## Documentation
|
|
48
47
|
|
|
@@ -53,13 +52,14 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
53
52
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
53
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
54
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
55
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
56
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
57
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
56
58
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
57
|
-
- [@orpc/
|
|
58
|
-
- [@orpc/
|
|
59
|
-
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
60
|
-
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
|
59
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
60
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
61
61
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
62
|
-
- [@orpc/
|
|
62
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
63
63
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
64
64
|
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
65
65
|
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { ClientContext } from '@orpc/client';
|
|
2
|
-
import { AnyContractRouter } from '@orpc/contract';
|
|
3
2
|
import { LinkFetchClientOptions } from '@orpc/client/fetch';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { g as StandardOpenAPILinkOptions, h as StandardOpenAPILink } from '../../shared/openapi-client.Dx4REA6z.mjs';
|
|
5
|
+
import '@orpc/client/standard';
|
|
6
6
|
import '@orpc/shared';
|
|
7
7
|
import '@orpc/standard-server';
|
|
8
8
|
|
|
9
|
-
interface OpenAPILinkOptions<T extends ClientContext> extends
|
|
9
|
+
interface OpenAPILinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardOpenAPILinkOptions<T>, 'plugins'> {
|
|
10
10
|
}
|
|
11
|
-
|
|
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> {
|
|
12
18
|
constructor(contract: AnyContractRouter, options: OpenAPILinkOptions<T>);
|
|
13
19
|
}
|
|
14
20
|
|
|
15
|
-
export { OpenAPILink
|
|
21
|
+
export { OpenAPILink };
|
|
22
|
+
export type { OpenAPILinkOptions };
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { ClientContext } from '@orpc/client';
|
|
2
|
-
import { AnyContractRouter } from '@orpc/contract';
|
|
3
2
|
import { LinkFetchClientOptions } from '@orpc/client/fetch';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { g as StandardOpenAPILinkOptions, h as StandardOpenAPILink } from '../../shared/openapi-client.Dx4REA6z.js';
|
|
5
|
+
import '@orpc/client/standard';
|
|
6
6
|
import '@orpc/shared';
|
|
7
7
|
import '@orpc/standard-server';
|
|
8
8
|
|
|
9
|
-
interface OpenAPILinkOptions<T extends ClientContext> extends
|
|
9
|
+
interface OpenAPILinkOptions<T extends ClientContext> extends LinkFetchClientOptions<T>, Omit<StandardOpenAPILinkOptions<T>, 'plugins'> {
|
|
10
10
|
}
|
|
11
|
-
|
|
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> {
|
|
12
18
|
constructor(contract: AnyContractRouter, options: OpenAPILinkOptions<T>);
|
|
13
19
|
}
|
|
14
20
|
|
|
15
|
-
export { OpenAPILink
|
|
21
|
+
export { OpenAPILink };
|
|
22
|
+
export type { OpenAPILinkOptions };
|
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
import { LinkFetchClient } from '@orpc/client/fetch';
|
|
2
|
-
import { StandardLink } from '@orpc/client/standard';
|
|
3
|
-
import { a as StandardOpenAPIJsonSerializer, b as StandardOpenapiLinkCodec, c as StandardOpenAPISerializer, S as StandardBracketNotationSerializer } from '../../shared/openapi-client.V75iq1Nn.mjs';
|
|
4
2
|
import '@orpc/shared';
|
|
5
|
-
import '@orpc/client';
|
|
6
3
|
import '@orpc/contract';
|
|
4
|
+
import { a as StandardOpenAPILink } from '../../shared/openapi-client.B2Q9qU5m.mjs';
|
|
5
|
+
import '@orpc/client';
|
|
6
|
+
import '@orpc/client/standard';
|
|
7
7
|
import '@orpc/standard-server';
|
|
8
|
+
import '../../shared/openapi-client.t9fCAe3x.mjs';
|
|
8
9
|
|
|
9
|
-
class OpenAPILink extends
|
|
10
|
+
class OpenAPILink extends StandardOpenAPILink {
|
|
10
11
|
constructor(contract, options) {
|
|
11
|
-
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
12
|
-
const bracketNotationSerializer = new StandardBracketNotationSerializer();
|
|
13
|
-
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
14
|
-
const linkCodec = new StandardOpenapiLinkCodec(contract, serializer, options);
|
|
15
12
|
const linkClient = new LinkFetchClient(options);
|
|
16
|
-
super(
|
|
13
|
+
super(contract, linkClient, options);
|
|
17
14
|
}
|
|
18
15
|
}
|
|
19
16
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { S as StandardBracketNotationSerialized,
|
|
1
|
+
export { S as StandardBracketNotationSerialized, b as StandardBracketNotationSerializer, a as StandardBracketNotationSerializerOptions, d as StandardOpenAPICustomJsonSerializer, c as StandardOpenAPIJsonSerialized, f as StandardOpenAPIJsonSerializer, e as StandardOpenAPIJsonSerializerOptions, h as StandardOpenAPILink, g as StandardOpenAPILinkOptions, k as StandardOpenAPISerializeOptions, l as StandardOpenAPISerializer, j as StandardOpenapiLinkCodec, i as StandardOpenapiLinkCodecOptions } from '../../shared/openapi-client.Dx4REA6z.mjs';
|
|
2
|
+
export { getIssueMessage, parseFormData } from '../../helpers/index.mjs';
|
|
2
3
|
import { HTTPPath } from '@orpc/client';
|
|
3
4
|
import '@orpc/client/standard';
|
|
4
5
|
import '@orpc/contract';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { S as StandardBracketNotationSerialized,
|
|
1
|
+
export { S as StandardBracketNotationSerialized, b as StandardBracketNotationSerializer, a as StandardBracketNotationSerializerOptions, d as StandardOpenAPICustomJsonSerializer, c as StandardOpenAPIJsonSerialized, f as StandardOpenAPIJsonSerializer, e as StandardOpenAPIJsonSerializerOptions, h as StandardOpenAPILink, g as StandardOpenAPILinkOptions, k as StandardOpenAPISerializeOptions, l as StandardOpenAPISerializer, j as StandardOpenapiLinkCodec, i as StandardOpenapiLinkCodecOptions } from '../../shared/openapi-client.Dx4REA6z.js';
|
|
2
|
+
export { getIssueMessage, parseFormData } from '../../helpers/index.js';
|
|
2
3
|
import { HTTPPath } from '@orpc/client';
|
|
3
4
|
import '@orpc/client/standard';
|
|
4
5
|
import '@orpc/contract';
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
export { S as StandardBracketNotationSerializer
|
|
1
|
+
export { S as StandardBracketNotationSerializer } from '../../shared/openapi-client.t9fCAe3x.mjs';
|
|
2
|
+
export { g as getIssueMessage, p as parseFormData } from '../../shared/openapi-client.Dgl8z2tb.mjs';
|
|
3
|
+
export { S as StandardOpenAPIJsonSerializer, a as StandardOpenAPILink, c as StandardOpenAPISerializer, b as StandardOpenapiLinkCodec, g as getDynamicParams, s as standardizeHTTPPath } from '../../shared/openapi-client.B2Q9qU5m.mjs';
|
|
2
4
|
import '@orpc/shared';
|
|
3
|
-
import '@orpc/client';
|
|
4
|
-
import '@orpc/client/standard';
|
|
5
5
|
import '@orpc/contract';
|
|
6
|
+
import '@orpc/client/standard';
|
|
7
|
+
import '@orpc/client';
|
|
6
8
|
import '@orpc/standard-server';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parse a form data with bracket notation
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const form = new FormData()
|
|
7
|
+
* form.append('a', '1')
|
|
8
|
+
* form.append('user[name]', 'John')
|
|
9
|
+
* form.append('user[age]', '20')
|
|
10
|
+
* form.append('user[friends][]', 'Bob')
|
|
11
|
+
* form.append('user[friends][]', 'Alice')
|
|
12
|
+
* form.append('user[friends][]', 'Charlie')
|
|
13
|
+
* form.append('thumb', new Blob(['hello']), 'thumb.png')
|
|
14
|
+
*
|
|
15
|
+
* parseFormData(form)
|
|
16
|
+
* // {
|
|
17
|
+
* // a: '1',
|
|
18
|
+
* // user: {
|
|
19
|
+
* // name: 'John',
|
|
20
|
+
* // age: '20',
|
|
21
|
+
* // friends: ['Bob', 'Alice', 'Charlie'],
|
|
22
|
+
* // },
|
|
23
|
+
* // thumb: form.get('thumb'),
|
|
24
|
+
* // }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/bracket-notation Bracket Notation Docs}
|
|
28
|
+
*/
|
|
29
|
+
declare function parseFormData(form: FormData): any;
|
|
30
|
+
/**
|
|
31
|
+
* Get the issue message from the error.
|
|
32
|
+
*
|
|
33
|
+
* @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
|
|
34
|
+
* @param path - The path of the field that has the issue follow [bracket notation](https://orpc.unnoq.com/docs/openapi/bracket-notation)
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```tsx
|
|
38
|
+
* const { error, data, execute } = useServerAction(someAction)
|
|
39
|
+
*
|
|
40
|
+
* return <form action={(form) => execute(parseFormData(form))}>
|
|
41
|
+
* <input name="user[name]" type="text" />
|
|
42
|
+
* <p>{getIssueMessage(error, 'user[name]')}</p>
|
|
43
|
+
*
|
|
44
|
+
* <input name="user[age]" type="number" />
|
|
45
|
+
* <p>{getIssueMessage(error, 'user[age]')}</p>
|
|
46
|
+
*
|
|
47
|
+
* <input name="images[]" type="file" />
|
|
48
|
+
* <p>{getIssueMessage(error, 'images[]')}</p>
|
|
49
|
+
* </form>
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
declare function getIssueMessage(error: unknown, path: string): string | undefined;
|
|
53
|
+
|
|
54
|
+
export { getIssueMessage, parseFormData };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parse a form data with bracket notation
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const form = new FormData()
|
|
7
|
+
* form.append('a', '1')
|
|
8
|
+
* form.append('user[name]', 'John')
|
|
9
|
+
* form.append('user[age]', '20')
|
|
10
|
+
* form.append('user[friends][]', 'Bob')
|
|
11
|
+
* form.append('user[friends][]', 'Alice')
|
|
12
|
+
* form.append('user[friends][]', 'Charlie')
|
|
13
|
+
* form.append('thumb', new Blob(['hello']), 'thumb.png')
|
|
14
|
+
*
|
|
15
|
+
* parseFormData(form)
|
|
16
|
+
* // {
|
|
17
|
+
* // a: '1',
|
|
18
|
+
* // user: {
|
|
19
|
+
* // name: 'John',
|
|
20
|
+
* // age: '20',
|
|
21
|
+
* // friends: ['Bob', 'Alice', 'Charlie'],
|
|
22
|
+
* // },
|
|
23
|
+
* // thumb: form.get('thumb'),
|
|
24
|
+
* // }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/bracket-notation Bracket Notation Docs}
|
|
28
|
+
*/
|
|
29
|
+
declare function parseFormData(form: FormData): any;
|
|
30
|
+
/**
|
|
31
|
+
* Get the issue message from the error.
|
|
32
|
+
*
|
|
33
|
+
* @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
|
|
34
|
+
* @param path - The path of the field that has the issue follow [bracket notation](https://orpc.unnoq.com/docs/openapi/bracket-notation)
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```tsx
|
|
38
|
+
* const { error, data, execute } = useServerAction(someAction)
|
|
39
|
+
*
|
|
40
|
+
* return <form action={(form) => execute(parseFormData(form))}>
|
|
41
|
+
* <input name="user[name]" type="text" />
|
|
42
|
+
* <p>{getIssueMessage(error, 'user[name]')}</p>
|
|
43
|
+
*
|
|
44
|
+
* <input name="user[age]" type="number" />
|
|
45
|
+
* <p>{getIssueMessage(error, 'user[age]')}</p>
|
|
46
|
+
*
|
|
47
|
+
* <input name="images[]" type="file" />
|
|
48
|
+
* <p>{getIssueMessage(error, 'images[]')}</p>
|
|
49
|
+
* </form>
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
declare function getIssueMessage(error: unknown, path: string): string | undefined;
|
|
53
|
+
|
|
54
|
+
export { getIssueMessage, parseFormData };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import '@orpc/shared';
|
|
2
|
+
export { g as getIssueMessage, p as parseFormData } from '../shared/openapi-client.Dgl8z2tb.mjs';
|
|
3
|
+
import '@orpc/client/standard';
|
|
4
|
+
import '@orpc/client';
|
|
5
|
+
import '@orpc/contract';
|
|
6
|
+
import '@orpc/standard-server';
|
|
7
|
+
import '../shared/openapi-client.t9fCAe3x.mjs';
|
package/dist/index.d.mts
CHANGED
|
@@ -4,6 +4,11 @@ type JsonifiedValue<T> = T extends string ? T : T extends number ? T : T extends
|
|
|
4
4
|
[K in keyof T]: JsonifiedValue<T[K]>;
|
|
5
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
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
|
+
*/
|
|
7
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> : {
|
|
8
13
|
[K in keyof T]: T[K] extends NestedClient<any> ? JsonifiedClient<T[K]> : T[K];
|
|
9
14
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,11 @@ type JsonifiedValue<T> = T extends string ? T : T extends number ? T : T extends
|
|
|
4
4
|
[K in keyof T]: JsonifiedValue<T[K]>;
|
|
5
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
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
|
+
*/
|
|
7
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> : {
|
|
8
13
|
[K in keyof T]: T[K] extends NestedClient<any> ? JsonifiedClient<T[K]> : T[K];
|
|
9
14
|
};
|
|
@@ -1,128 +1,10 @@
|
|
|
1
|
+
import { toStandardHeaders, toHttpPath, getMalformedResponseErrorCode, StandardLink } from '@orpc/client/standard';
|
|
2
|
+
import { S as StandardBracketNotationSerializer } from './openapi-client.t9fCAe3x.mjs';
|
|
1
3
|
import { isObject, value, get, isAsyncIteratorObject } from '@orpc/shared';
|
|
2
|
-
import { isORPCErrorStatus,
|
|
3
|
-
import { toHttpPath } from '@orpc/client/standard';
|
|
4
|
+
import { isORPCErrorStatus, isORPCErrorJson, createORPCErrorFromJson, mapEventIterator, toORPCError } from '@orpc/client';
|
|
4
5
|
import { isContractProcedure, fallbackContractConfig, ORPCError } from '@orpc/contract';
|
|
5
6
|
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
6
7
|
|
|
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
8
|
class StandardOpenAPIJsonSerializer {
|
|
127
9
|
customSerializers;
|
|
128
10
|
constructor(options = {}) {
|
|
@@ -188,21 +70,23 @@ class StandardOpenapiLinkCodec {
|
|
|
188
70
|
this.serializer = serializer;
|
|
189
71
|
this.baseUrl = options.url;
|
|
190
72
|
this.headers = options.headers ?? {};
|
|
73
|
+
this.customErrorResponseBodyDecoder = options.customErrorResponseBodyDecoder;
|
|
191
74
|
}
|
|
192
75
|
baseUrl;
|
|
193
76
|
headers;
|
|
77
|
+
customErrorResponseBodyDecoder;
|
|
194
78
|
async encode(path, input, options) {
|
|
195
|
-
|
|
196
|
-
let headers = await value(this.headers, options, path, input);
|
|
79
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
197
80
|
if (options.lastEventId !== void 0) {
|
|
198
81
|
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
199
82
|
}
|
|
83
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
200
84
|
const procedure = get(this.contract, path);
|
|
201
85
|
if (!isContractProcedure(procedure)) {
|
|
202
86
|
throw new Error(`[StandardOpenapiLinkCodec] expect a contract procedure at ${path.join(".")}`);
|
|
203
87
|
}
|
|
204
88
|
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
205
|
-
return inputStructure === "compact" ? this.#encodeCompact(procedure, path, input, options, baseUrl
|
|
89
|
+
return inputStructure === "compact" ? this.#encodeCompact(procedure, path, input, options, baseUrl, headers) : this.#encodeDetailed(procedure, path, input, options, baseUrl, headers);
|
|
206
90
|
}
|
|
207
91
|
#encodeCompact(procedure, path, input, options, baseUrl, headers) {
|
|
208
92
|
let httpPath = standardizeHTTPPath(procedure["~orpc"].route.path ?? toHttpPath(path));
|
|
@@ -221,7 +105,8 @@ class StandardOpenapiLinkCodec {
|
|
|
221
105
|
httpBody = Object.keys(body).length ? body : void 0;
|
|
222
106
|
}
|
|
223
107
|
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
224
|
-
const url = new URL(
|
|
108
|
+
const url = new URL(baseUrl);
|
|
109
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${httpPath}`;
|
|
225
110
|
if (method === "GET") {
|
|
226
111
|
const serialized = this.serializer.serialize(httpBody, { outputFormat: "URLSearchParams" });
|
|
227
112
|
for (const [key, value2] of serialized) {
|
|
@@ -266,7 +151,8 @@ class StandardOpenapiLinkCodec {
|
|
|
266
151
|
mergedHeaders = mergeStandardHeaders(input.headers, headers);
|
|
267
152
|
}
|
|
268
153
|
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
269
|
-
const url = new URL(
|
|
154
|
+
const url = new URL(baseUrl);
|
|
155
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${httpPath}`;
|
|
270
156
|
if (input?.query !== void 0) {
|
|
271
157
|
const query = this.serializer.serialize(input.query, { outputFormat: "URLSearchParams" });
|
|
272
158
|
for (const [key, value2] of query) {
|
|
@@ -310,12 +196,16 @@ class StandardOpenapiLinkCodec {
|
|
|
310
196
|
}
|
|
311
197
|
})();
|
|
312
198
|
if (!isOk) {
|
|
313
|
-
|
|
314
|
-
|
|
199
|
+
const error = this.customErrorResponseBodyDecoder?.(deserialized, response);
|
|
200
|
+
if (error !== null && error !== void 0) {
|
|
201
|
+
throw error;
|
|
315
202
|
}
|
|
316
|
-
|
|
203
|
+
if (isORPCErrorJson(deserialized)) {
|
|
204
|
+
throw createORPCErrorFromJson(deserialized);
|
|
205
|
+
}
|
|
206
|
+
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
317
207
|
status: response.status,
|
|
318
|
-
data: deserialized
|
|
208
|
+
data: { ...response, body: deserialized }
|
|
319
209
|
});
|
|
320
210
|
}
|
|
321
211
|
const procedure = get(this.contract, path);
|
|
@@ -327,6 +217,7 @@ class StandardOpenapiLinkCodec {
|
|
|
327
217
|
return deserialized;
|
|
328
218
|
}
|
|
329
219
|
return {
|
|
220
|
+
status: response.status,
|
|
330
221
|
headers: response.headers,
|
|
331
222
|
body: deserialized
|
|
332
223
|
};
|
|
@@ -387,8 +278,8 @@ class StandardOpenAPISerializer {
|
|
|
387
278
|
return mapEventIterator(data, {
|
|
388
279
|
value: async (value) => value,
|
|
389
280
|
error: async (e) => {
|
|
390
|
-
if (e instanceof ErrorEvent &&
|
|
391
|
-
return
|
|
281
|
+
if (e instanceof ErrorEvent && isORPCErrorJson(e.data)) {
|
|
282
|
+
return createORPCErrorFromJson(e.data, { cause: e });
|
|
392
283
|
}
|
|
393
284
|
return e;
|
|
394
285
|
}
|
|
@@ -398,4 +289,14 @@ class StandardOpenAPISerializer {
|
|
|
398
289
|
}
|
|
399
290
|
}
|
|
400
291
|
|
|
401
|
-
|
|
292
|
+
class StandardOpenAPILink extends StandardLink {
|
|
293
|
+
constructor(contract, linkClient, options) {
|
|
294
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
295
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer({ maxBracketNotationArrayIndex: 4294967294 });
|
|
296
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
297
|
+
const linkCodec = new StandardOpenapiLinkCodec(contract, serializer, options);
|
|
298
|
+
super(linkCodec, linkClient, options);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export { StandardOpenAPIJsonSerializer as S, StandardOpenAPILink as a, StandardOpenapiLinkCodec as b, StandardOpenAPISerializer as c, getDynamicParams as g, standardizeHTTPPath as s };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isSchemaIssue } from '@orpc/contract';
|
|
2
|
+
import { isTypescriptObject } from '@orpc/shared';
|
|
3
|
+
import { S as StandardBracketNotationSerializer } from './openapi-client.t9fCAe3x.mjs';
|
|
4
|
+
|
|
5
|
+
function parseFormData(form) {
|
|
6
|
+
const serializer = new StandardBracketNotationSerializer();
|
|
7
|
+
return serializer.deserialize(Array.from(form.entries()));
|
|
8
|
+
}
|
|
9
|
+
function getIssueMessage(error, path) {
|
|
10
|
+
if (!isTypescriptObject(error) || !isTypescriptObject(error.data) || !Array.isArray(error.data.issues)) {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
const serializer = new StandardBracketNotationSerializer();
|
|
14
|
+
for (const issue of error.data.issues) {
|
|
15
|
+
if (!isSchemaIssue(issue)) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (issue.path === void 0) {
|
|
19
|
+
if (path === "") {
|
|
20
|
+
return issue.message;
|
|
21
|
+
}
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const issuePath = serializer.stringifyPath(
|
|
25
|
+
issue.path.map((segment) => typeof segment === "object" ? segment.key.toString() : segment.toString())
|
|
26
|
+
);
|
|
27
|
+
if (issuePath === path) {
|
|
28
|
+
return issue.message;
|
|
29
|
+
}
|
|
30
|
+
if (path.endsWith("[]") && issuePath.replace(/\[(?:0|[1-9]\d*)\]$/, "[]") === path) {
|
|
31
|
+
return issue.message;
|
|
32
|
+
}
|
|
33
|
+
if (path === "" && issuePath.match(/(?:0|[1-9]\d*)$/)) {
|
|
34
|
+
return issue.message;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { getIssueMessage as g, parseFormData as p };
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { ClientContext, ClientOptions } from '@orpc/client';
|
|
2
|
-
import { StandardLinkCodec, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
-
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
-
import { Segment, Value } from '@orpc/shared';
|
|
5
|
-
import { StandardHeaders,
|
|
2
|
+
import { StandardLinkCodec, StandardLinkOptions, StandardLink, StandardLinkClient } from '@orpc/client/standard';
|
|
3
|
+
import { ORPCError, AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { Segment, Value, Promisable } from '@orpc/shared';
|
|
5
|
+
import { StandardHeaders, StandardLazyResponse, StandardRequest } from '@orpc/standard-server';
|
|
6
6
|
|
|
7
7
|
type StandardBracketNotationSerialized = [string, unknown][];
|
|
8
|
+
interface StandardBracketNotationSerializerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Maximum allowed array index for bracket notation deserialization.
|
|
11
|
+
*
|
|
12
|
+
* This helps protect against memory exhaustion attacks where malicious input
|
|
13
|
+
* uses extremely large array indices (e.g., `?arr[4294967296]=value`).
|
|
14
|
+
*
|
|
15
|
+
* While bracket notation creates sparse arrays that handle large indices efficiently,
|
|
16
|
+
* downstream code might inadvertently convert these sparse arrays to dense arrays,
|
|
17
|
+
* potentially creating millions of undefined elements and causing memory issues.
|
|
18
|
+
*
|
|
19
|
+
* @note Only applies to deserialization.
|
|
20
|
+
* @default 9_999 (array with 10,000 elements)
|
|
21
|
+
*/
|
|
22
|
+
maxBracketNotationArrayIndex?: number;
|
|
23
|
+
}
|
|
8
24
|
declare class StandardBracketNotationSerializer {
|
|
25
|
+
private readonly maxArrayIndex;
|
|
26
|
+
constructor(options?: StandardBracketNotationSerializerOptions);
|
|
9
27
|
serialize(data: unknown, segments?: Segment[], result?: StandardBracketNotationSerialized): StandardBracketNotationSerialized;
|
|
10
28
|
deserialize(serialized: StandardBracketNotationSerialized): Record<string, unknown> | unknown[];
|
|
11
29
|
stringifyPath(segments: readonly Segment[]): string;
|
|
@@ -44,7 +62,7 @@ interface StandardOpenapiLinkCodecOptions<T extends ClientContext> {
|
|
|
44
62
|
/**
|
|
45
63
|
* Base url for all requests.
|
|
46
64
|
*/
|
|
47
|
-
url: Value<string | URL
|
|
65
|
+
url: Value<Promisable<string | URL>, [
|
|
48
66
|
options: ClientOptions<T>,
|
|
49
67
|
path: readonly string[],
|
|
50
68
|
input: unknown
|
|
@@ -52,11 +70,20 @@ interface StandardOpenapiLinkCodecOptions<T extends ClientContext> {
|
|
|
52
70
|
/**
|
|
53
71
|
* Inject headers to the request.
|
|
54
72
|
*/
|
|
55
|
-
headers?: Value<StandardHeaders
|
|
73
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [
|
|
56
74
|
options: ClientOptions<T>,
|
|
57
75
|
path: readonly string[],
|
|
58
76
|
input: unknown
|
|
59
77
|
]>;
|
|
78
|
+
/**
|
|
79
|
+
* Customize how a response body is decoded into an ORPC error.
|
|
80
|
+
* Useful when the default decoder cannot fully interpret
|
|
81
|
+
* your server's error format.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* - Return `null | undefined` to fallback to default behavior.
|
|
85
|
+
*/
|
|
86
|
+
customErrorResponseBodyDecoder?: (deserializedBody: unknown, response: StandardLazyResponse) => ORPCError<any, any> | null | undefined;
|
|
60
87
|
}
|
|
61
88
|
declare class StandardOpenapiLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
62
89
|
#private;
|
|
@@ -64,6 +91,7 @@ declare class StandardOpenapiLinkCodec<T extends ClientContext> implements Stand
|
|
|
64
91
|
private readonly serializer;
|
|
65
92
|
private readonly baseUrl;
|
|
66
93
|
private readonly headers;
|
|
94
|
+
private readonly customErrorResponseBodyDecoder;
|
|
67
95
|
constructor(contract: AnyContractRouter, serializer: StandardOpenAPISerializer, options: StandardOpenapiLinkCodecOptions<T>);
|
|
68
96
|
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
69
97
|
decode(response: StandardLazyResponse, _options: ClientOptions<T>, path: readonly string[]): Promise<unknown>;
|
|
@@ -71,5 +99,9 @@ declare class StandardOpenapiLinkCodec<T extends ClientContext> implements Stand
|
|
|
71
99
|
|
|
72
100
|
interface StandardOpenAPILinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardOpenapiLinkCodecOptions<T>, StandardOpenAPIJsonSerializerOptions {
|
|
73
101
|
}
|
|
102
|
+
declare class StandardOpenAPILink<T extends ClientContext> extends StandardLink<T> {
|
|
103
|
+
constructor(contract: AnyContractRouter, linkClient: StandardLinkClient<T>, options: StandardOpenAPILinkOptions<T>);
|
|
104
|
+
}
|
|
74
105
|
|
|
75
|
-
export {
|
|
106
|
+
export { StandardBracketNotationSerializer as b, StandardOpenAPIJsonSerializer as f, StandardOpenAPILink as h, StandardOpenapiLinkCodec as j, StandardOpenAPISerializer as l };
|
|
107
|
+
export type { StandardBracketNotationSerialized as S, StandardBracketNotationSerializerOptions as a, StandardOpenAPIJsonSerialized as c, StandardOpenAPICustomJsonSerializer as d, StandardOpenAPIJsonSerializerOptions as e, StandardOpenAPILinkOptions as g, StandardOpenapiLinkCodecOptions as i, StandardOpenAPISerializeOptions as k };
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { ClientContext, ClientOptions } from '@orpc/client';
|
|
2
|
-
import { StandardLinkCodec, StandardLinkOptions } from '@orpc/client/standard';
|
|
3
|
-
import { AnyContractRouter } from '@orpc/contract';
|
|
4
|
-
import { Segment, Value } from '@orpc/shared';
|
|
5
|
-
import { StandardHeaders,
|
|
2
|
+
import { StandardLinkCodec, StandardLinkOptions, StandardLink, StandardLinkClient } from '@orpc/client/standard';
|
|
3
|
+
import { ORPCError, AnyContractRouter } from '@orpc/contract';
|
|
4
|
+
import { Segment, Value, Promisable } from '@orpc/shared';
|
|
5
|
+
import { StandardHeaders, StandardLazyResponse, StandardRequest } from '@orpc/standard-server';
|
|
6
6
|
|
|
7
7
|
type StandardBracketNotationSerialized = [string, unknown][];
|
|
8
|
+
interface StandardBracketNotationSerializerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Maximum allowed array index for bracket notation deserialization.
|
|
11
|
+
*
|
|
12
|
+
* This helps protect against memory exhaustion attacks where malicious input
|
|
13
|
+
* uses extremely large array indices (e.g., `?arr[4294967296]=value`).
|
|
14
|
+
*
|
|
15
|
+
* While bracket notation creates sparse arrays that handle large indices efficiently,
|
|
16
|
+
* downstream code might inadvertently convert these sparse arrays to dense arrays,
|
|
17
|
+
* potentially creating millions of undefined elements and causing memory issues.
|
|
18
|
+
*
|
|
19
|
+
* @note Only applies to deserialization.
|
|
20
|
+
* @default 9_999 (array with 10,000 elements)
|
|
21
|
+
*/
|
|
22
|
+
maxBracketNotationArrayIndex?: number;
|
|
23
|
+
}
|
|
8
24
|
declare class StandardBracketNotationSerializer {
|
|
25
|
+
private readonly maxArrayIndex;
|
|
26
|
+
constructor(options?: StandardBracketNotationSerializerOptions);
|
|
9
27
|
serialize(data: unknown, segments?: Segment[], result?: StandardBracketNotationSerialized): StandardBracketNotationSerialized;
|
|
10
28
|
deserialize(serialized: StandardBracketNotationSerialized): Record<string, unknown> | unknown[];
|
|
11
29
|
stringifyPath(segments: readonly Segment[]): string;
|
|
@@ -44,7 +62,7 @@ interface StandardOpenapiLinkCodecOptions<T extends ClientContext> {
|
|
|
44
62
|
/**
|
|
45
63
|
* Base url for all requests.
|
|
46
64
|
*/
|
|
47
|
-
url: Value<string | URL
|
|
65
|
+
url: Value<Promisable<string | URL>, [
|
|
48
66
|
options: ClientOptions<T>,
|
|
49
67
|
path: readonly string[],
|
|
50
68
|
input: unknown
|
|
@@ -52,11 +70,20 @@ interface StandardOpenapiLinkCodecOptions<T extends ClientContext> {
|
|
|
52
70
|
/**
|
|
53
71
|
* Inject headers to the request.
|
|
54
72
|
*/
|
|
55
|
-
headers?: Value<StandardHeaders
|
|
73
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [
|
|
56
74
|
options: ClientOptions<T>,
|
|
57
75
|
path: readonly string[],
|
|
58
76
|
input: unknown
|
|
59
77
|
]>;
|
|
78
|
+
/**
|
|
79
|
+
* Customize how a response body is decoded into an ORPC error.
|
|
80
|
+
* Useful when the default decoder cannot fully interpret
|
|
81
|
+
* your server's error format.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* - Return `null | undefined` to fallback to default behavior.
|
|
85
|
+
*/
|
|
86
|
+
customErrorResponseBodyDecoder?: (deserializedBody: unknown, response: StandardLazyResponse) => ORPCError<any, any> | null | undefined;
|
|
60
87
|
}
|
|
61
88
|
declare class StandardOpenapiLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
62
89
|
#private;
|
|
@@ -64,6 +91,7 @@ declare class StandardOpenapiLinkCodec<T extends ClientContext> implements Stand
|
|
|
64
91
|
private readonly serializer;
|
|
65
92
|
private readonly baseUrl;
|
|
66
93
|
private readonly headers;
|
|
94
|
+
private readonly customErrorResponseBodyDecoder;
|
|
67
95
|
constructor(contract: AnyContractRouter, serializer: StandardOpenAPISerializer, options: StandardOpenapiLinkCodecOptions<T>);
|
|
68
96
|
encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
|
|
69
97
|
decode(response: StandardLazyResponse, _options: ClientOptions<T>, path: readonly string[]): Promise<unknown>;
|
|
@@ -71,5 +99,9 @@ declare class StandardOpenapiLinkCodec<T extends ClientContext> implements Stand
|
|
|
71
99
|
|
|
72
100
|
interface StandardOpenAPILinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, StandardOpenapiLinkCodecOptions<T>, StandardOpenAPIJsonSerializerOptions {
|
|
73
101
|
}
|
|
102
|
+
declare class StandardOpenAPILink<T extends ClientContext> extends StandardLink<T> {
|
|
103
|
+
constructor(contract: AnyContractRouter, linkClient: StandardLinkClient<T>, options: StandardOpenAPILinkOptions<T>);
|
|
104
|
+
}
|
|
74
105
|
|
|
75
|
-
export {
|
|
106
|
+
export { StandardBracketNotationSerializer as b, StandardOpenAPIJsonSerializer as f, StandardOpenAPILink as h, StandardOpenapiLinkCodec as j, StandardOpenAPISerializer as l };
|
|
107
|
+
export type { StandardBracketNotationSerialized as S, StandardBracketNotationSerializerOptions as a, StandardOpenAPIJsonSerialized as c, StandardOpenAPICustomJsonSerializer as d, StandardOpenAPIJsonSerializerOptions as e, StandardOpenAPILinkOptions as g, StandardOpenapiLinkCodecOptions as i, StandardOpenAPISerializeOptions as k };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { isObject, NullProtoObj } from '@orpc/shared';
|
|
2
|
+
|
|
3
|
+
class StandardBracketNotationSerializer {
|
|
4
|
+
maxArrayIndex;
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.maxArrayIndex = options.maxBracketNotationArrayIndex ?? 9999;
|
|
7
|
+
}
|
|
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, this.maxArrayIndex)) {
|
|
38
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
39
|
+
arrayPushStyles.delete(currentRef[nextSegment]);
|
|
40
|
+
currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
|
|
41
|
+
} else {
|
|
42
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
47
|
+
if (segment === "") {
|
|
48
|
+
if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
|
|
49
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
53
|
+
arrayPushStyles.delete(currentRef[nextSegment]);
|
|
54
|
+
currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
|
|
55
|
+
} else if (!isValidArrayIndex(segment, this.maxArrayIndex)) {
|
|
56
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
currentRef = currentRef[nextSegment];
|
|
62
|
+
nextSegment = segment;
|
|
63
|
+
});
|
|
64
|
+
if (Array.isArray(currentRef) && nextSegment === "") {
|
|
65
|
+
arrayPushStyles.add(currentRef);
|
|
66
|
+
currentRef.push(value);
|
|
67
|
+
} else if (nextSegment in currentRef) {
|
|
68
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
69
|
+
currentRef[nextSegment].push(value);
|
|
70
|
+
} else {
|
|
71
|
+
currentRef[nextSegment] = [currentRef[nextSegment], value];
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
currentRef[nextSegment] = value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return ref.value;
|
|
78
|
+
}
|
|
79
|
+
stringifyPath(segments) {
|
|
80
|
+
return segments.map((segment) => {
|
|
81
|
+
return segment.toString().replace(/[\\[\]]/g, (match) => {
|
|
82
|
+
switch (match) {
|
|
83
|
+
case "\\":
|
|
84
|
+
return "\\\\";
|
|
85
|
+
case "[":
|
|
86
|
+
return "\\[";
|
|
87
|
+
case "]":
|
|
88
|
+
return "\\]";
|
|
89
|
+
/* v8 ignore next 2 */
|
|
90
|
+
default:
|
|
91
|
+
return match;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}).reduce((result, segment, i) => {
|
|
95
|
+
if (i === 0) {
|
|
96
|
+
return segment;
|
|
97
|
+
}
|
|
98
|
+
return `${result}[${segment}]`;
|
|
99
|
+
}, "");
|
|
100
|
+
}
|
|
101
|
+
parsePath(path) {
|
|
102
|
+
const segments = [];
|
|
103
|
+
let inBrackets = false;
|
|
104
|
+
let currentSegment = "";
|
|
105
|
+
let backslashCount = 0;
|
|
106
|
+
for (let i = 0; i < path.length; i++) {
|
|
107
|
+
const char = path[i];
|
|
108
|
+
const nextChar = path[i + 1];
|
|
109
|
+
if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
|
|
110
|
+
if (nextChar === void 0) {
|
|
111
|
+
inBrackets = false;
|
|
112
|
+
}
|
|
113
|
+
segments.push(currentSegment);
|
|
114
|
+
currentSegment = "";
|
|
115
|
+
i++;
|
|
116
|
+
} else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
|
|
117
|
+
inBrackets = true;
|
|
118
|
+
segments.push(currentSegment);
|
|
119
|
+
currentSegment = "";
|
|
120
|
+
} else if (char === "\\") {
|
|
121
|
+
backslashCount++;
|
|
122
|
+
} else {
|
|
123
|
+
currentSegment += "\\".repeat(backslashCount / 2) + char;
|
|
124
|
+
backslashCount = 0;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return inBrackets || segments.length === 0 ? [path] : segments;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function isValidArrayIndex(value, maxIndex) {
|
|
131
|
+
return /^0$|^[1-9]\d*$/.test(value) && Number(value) <= maxIndex;
|
|
132
|
+
}
|
|
133
|
+
function arrayToObject(array) {
|
|
134
|
+
const obj = new NullProtoObj();
|
|
135
|
+
array.forEach((item, i) => {
|
|
136
|
+
obj[i] = item;
|
|
137
|
+
});
|
|
138
|
+
return obj;
|
|
139
|
+
}
|
|
140
|
+
function pushStyleArrayToObject(array) {
|
|
141
|
+
const obj = new NullProtoObj();
|
|
142
|
+
obj[""] = array.length === 1 ? array[0] : array;
|
|
143
|
+
return obj;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { StandardBracketNotationSerializer as S };
|
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.8606faa",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
"import": "./dist/index.mjs",
|
|
20
20
|
"default": "./dist/index.mjs"
|
|
21
21
|
},
|
|
22
|
+
"./helpers": {
|
|
23
|
+
"types": "./dist/helpers/index.d.mts",
|
|
24
|
+
"import": "./dist/helpers/index.mjs",
|
|
25
|
+
"default": "./dist/helpers/index.mjs"
|
|
26
|
+
},
|
|
22
27
|
"./standard": {
|
|
23
28
|
"types": "./dist/adapters/standard/index.d.mts",
|
|
24
29
|
"import": "./dist/adapters/standard/index.mjs",
|
|
@@ -34,13 +39,13 @@
|
|
|
34
39
|
"dist"
|
|
35
40
|
],
|
|
36
41
|
"dependencies": {
|
|
37
|
-
"@orpc/
|
|
38
|
-
"@orpc/
|
|
39
|
-
"@orpc/shared": "0.0.0-next.
|
|
40
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
42
|
+
"@orpc/client": "0.0.0-next.8606faa",
|
|
43
|
+
"@orpc/contract": "0.0.0-next.8606faa",
|
|
44
|
+
"@orpc/shared": "0.0.0-next.8606faa",
|
|
45
|
+
"@orpc/standard-server": "0.0.0-next.8606faa"
|
|
41
46
|
},
|
|
42
47
|
"devDependencies": {
|
|
43
|
-
"@orpc/server": "0.0.0-next.
|
|
48
|
+
"@orpc/server": "0.0.0-next.8606faa"
|
|
44
49
|
},
|
|
45
50
|
"scripts": {
|
|
46
51
|
"build": "unbuild",
|