@orpc/react 0.0.0-next.f99e554 → 0.0.0-next.fa8d145
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 +153 -0
- package/dist/hooks/index.d.mts +69 -0
- package/dist/hooks/index.d.ts +69 -0
- package/dist/hooks/index.mjs +77 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.mjs +27 -0
- package/package.json +20 -17
- package/dist/index.js +0 -672
- package/dist/src/general-hooks.d.ts +0 -23
- package/dist/src/general-utils.d.ts +0 -40
- package/dist/src/index.d.ts +0 -9
- package/dist/src/orpc-path.d.ts +0 -5
- package/dist/src/procedure-hooks.d.ts +0 -30
- package/dist/src/procedure-utils.d.ts +0 -34
- package/dist/src/react-context.d.ts +0 -11
- package/dist/src/react-hooks.d.ts +0 -18
- package/dist/src/react-utils.d.ts +0 -18
- package/dist/src/react.d.ts +0 -15
- package/dist/src/tanstack-key.d.ts +0 -14
- package/dist/src/tanstack-query.d.ts +0 -19
- package/dist/src/types.d.ts +0 -5
- package/dist/src/use-queries/builder.d.ts +0 -19
- package/dist/src/use-queries/builders.d.ts +0 -14
- package/dist/src/use-queries/hook.d.ts +0 -12
package/README.md
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
3
|
+
</div>
|
4
|
+
|
5
|
+
<h1></h1>
|
6
|
+
|
7
|
+
<div align="center">
|
8
|
+
<a href="https://codecov.io/gh/unnoq/orpc">
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
|
10
|
+
</a>
|
11
|
+
<a href="https://www.npmjs.com/package/@orpc/react">
|
12
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Freact?logo=npm" />
|
13
|
+
</a>
|
14
|
+
<a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
|
15
|
+
<img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
|
16
|
+
</a>
|
17
|
+
<a href="https://discord.gg/TXEbwRBvQn">
|
18
|
+
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
19
|
+
</a>
|
20
|
+
</div>
|
21
|
+
|
22
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
23
|
+
|
24
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
25
|
+
|
26
|
+
---
|
27
|
+
|
28
|
+
## Highlights
|
29
|
+
|
30
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
31
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
32
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
33
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
|
34
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
35
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
36
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
37
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
38
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
39
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
40
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
41
|
+
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
42
|
+
|
43
|
+
## Documentation
|
44
|
+
|
45
|
+
You can find the full documentation [here](https://orpc.unnoq.com).
|
46
|
+
|
47
|
+
## Packages
|
48
|
+
|
49
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
50
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
51
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
52
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
|
53
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
54
|
+
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
55
|
+
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
56
|
+
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
57
|
+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
58
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
59
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
60
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
61
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
62
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
63
|
+
|
64
|
+
## `@orpc/react`
|
65
|
+
|
66
|
+
Provides utilities for integrating oRPC with React and React Server Actions. Read the [documentation](https://orpc.unnoq.com/docs/server-action) for more information.
|
67
|
+
|
68
|
+
### `useServerAction` Hook
|
69
|
+
|
70
|
+
The `useServerAction` hook simplifies invoking server actions in React.
|
71
|
+
|
72
|
+
```tsx
|
73
|
+
'use client'
|
74
|
+
|
75
|
+
import { isDefinedError, onError } from '@orpc/client'
|
76
|
+
import { useServerAction } from '@orpc/react'
|
77
|
+
|
78
|
+
export function MyComponent() {
|
79
|
+
const { execute, data, error, status } = useServerAction(someAction, {
|
80
|
+
interceptors: [
|
81
|
+
onError((error) => {
|
82
|
+
if (isDefinedError(error)) {
|
83
|
+
console.error(error.data)
|
84
|
+
// ^ Typed error data
|
85
|
+
}
|
86
|
+
}),
|
87
|
+
],
|
88
|
+
})
|
89
|
+
|
90
|
+
const action = async (form: FormData) => {
|
91
|
+
const name = form.get('name') as string
|
92
|
+
execute({ name })
|
93
|
+
}
|
94
|
+
|
95
|
+
return (
|
96
|
+
<form action={action}>
|
97
|
+
<input type="text" name="name" required />
|
98
|
+
<button type="submit">Submit</button>
|
99
|
+
{status === 'pending' && <p>Loading...</p>}
|
100
|
+
</form>
|
101
|
+
)
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
### `createFormAction` Utility
|
106
|
+
|
107
|
+
Creates a form action that can be used in React forms and deserializes with bracket notation.
|
108
|
+
|
109
|
+
```tsx
|
110
|
+
const dosomething = os
|
111
|
+
.input(
|
112
|
+
z.object({
|
113
|
+
user: z.object({
|
114
|
+
name: z.string(),
|
115
|
+
age: z.coerce.number(),
|
116
|
+
}),
|
117
|
+
})
|
118
|
+
)
|
119
|
+
.handler(({ input }) => {
|
120
|
+
console.log('Form action called!')
|
121
|
+
console.log(input)
|
122
|
+
})
|
123
|
+
|
124
|
+
export const redirectSomeWhereForm = createFormAction(dosomething, {
|
125
|
+
interceptors: [
|
126
|
+
onSuccess(async () => {
|
127
|
+
redirect('/some-where')
|
128
|
+
}),
|
129
|
+
],
|
130
|
+
})
|
131
|
+
|
132
|
+
export function MyComponent() {
|
133
|
+
return (
|
134
|
+
<form action={redirectSomeWhereForm}>
|
135
|
+
<input type="text" name="user[name]" required />
|
136
|
+
<input type="number" name="user[age]" required />
|
137
|
+
<button type="submit">Submit</button>
|
138
|
+
</form>
|
139
|
+
)
|
140
|
+
}
|
141
|
+
```
|
142
|
+
|
143
|
+
## Sponsors
|
144
|
+
|
145
|
+
<p align="center">
|
146
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
147
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
148
|
+
</a>
|
149
|
+
</p>
|
150
|
+
|
151
|
+
## License
|
152
|
+
|
153
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { SafeResult, ORPCErrorJSON } from '@orpc/client';
|
2
|
+
import { ActionableClient, UnactionableError } from '@orpc/server';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
|
5
|
+
interface UseServerActionOptions<TInput, TOutput, TError> {
|
6
|
+
interceptors?: Interceptor<{
|
7
|
+
input: TInput;
|
8
|
+
}, TOutput, TError>[];
|
9
|
+
}
|
10
|
+
interface UseServerActionExecuteOptions<TInput, TOutput, TError> extends Pick<UseServerActionOptions<TInput, TOutput, TError>, 'interceptors'> {
|
11
|
+
}
|
12
|
+
type UseServerActionExecuteRest<TInput, TOutput, TError> = undefined extends TInput ? [input?: TInput, options?: UseServerActionExecuteOptions<TInput, TOutput, TError>] : [input: TInput, options?: UseServerActionExecuteOptions<TInput, TOutput, TError>];
|
13
|
+
interface UseServerActionResultBase<TInput, TOutput, TError> {
|
14
|
+
reset: () => void;
|
15
|
+
execute: (...rest: UseServerActionExecuteRest<TInput, TOutput, TError>) => Promise<SafeResult<TOutput, TError>>;
|
16
|
+
}
|
17
|
+
interface UseServerActionIdleResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
18
|
+
input: undefined;
|
19
|
+
data: undefined;
|
20
|
+
error: null;
|
21
|
+
isIdle: true;
|
22
|
+
isPending: false;
|
23
|
+
isSuccess: false;
|
24
|
+
isError: false;
|
25
|
+
status: 'idle';
|
26
|
+
executedAt: undefined;
|
27
|
+
}
|
28
|
+
interface UseServerActionPendingResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
29
|
+
input: TInput;
|
30
|
+
data: undefined;
|
31
|
+
error: null;
|
32
|
+
isIdle: false;
|
33
|
+
isPending: true;
|
34
|
+
isSuccess: false;
|
35
|
+
isError: false;
|
36
|
+
status: 'pending';
|
37
|
+
executedAt: Date;
|
38
|
+
}
|
39
|
+
interface UseServerActionSuccessResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
40
|
+
input: TInput;
|
41
|
+
data: TOutput;
|
42
|
+
error: null;
|
43
|
+
isIdle: false;
|
44
|
+
isPending: false;
|
45
|
+
isSuccess: true;
|
46
|
+
isError: false;
|
47
|
+
status: 'success';
|
48
|
+
executedAt: Date;
|
49
|
+
}
|
50
|
+
interface UseServerActionErrorResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
51
|
+
input: TInput;
|
52
|
+
data: undefined;
|
53
|
+
error: TError;
|
54
|
+
isIdle: false;
|
55
|
+
isPending: false;
|
56
|
+
isSuccess: false;
|
57
|
+
isError: true;
|
58
|
+
status: 'error';
|
59
|
+
executedAt: Date;
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* Use a Server Action Hook
|
63
|
+
*
|
64
|
+
* @see {@link https://orpc.unnoq.com/docs/server-action#useserveraction-hook Server Action Hook Docs}
|
65
|
+
*/
|
66
|
+
declare function useServerAction<TInput, TOutput, TError extends ORPCErrorJSON<any, any>>(action: ActionableClient<TInput, TOutput, TError>, options?: NoInfer<UseServerActionOptions<TInput, TOutput, UnactionableError<TError>>>): UseServerActionIdleResult<TInput, TOutput, UnactionableError<TError>> | UseServerActionSuccessResult<TInput, TOutput, UnactionableError<TError>> | UseServerActionErrorResult<TInput, TOutput, UnactionableError<TError>> | UseServerActionPendingResult<TInput, TOutput, UnactionableError<TError>>;
|
67
|
+
|
68
|
+
export { useServerAction };
|
69
|
+
export type { UseServerActionErrorResult, UseServerActionExecuteOptions, UseServerActionExecuteRest, UseServerActionIdleResult, UseServerActionOptions, UseServerActionPendingResult, UseServerActionResultBase, UseServerActionSuccessResult };
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { SafeResult, ORPCErrorJSON } from '@orpc/client';
|
2
|
+
import { ActionableClient, UnactionableError } from '@orpc/server';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
|
5
|
+
interface UseServerActionOptions<TInput, TOutput, TError> {
|
6
|
+
interceptors?: Interceptor<{
|
7
|
+
input: TInput;
|
8
|
+
}, TOutput, TError>[];
|
9
|
+
}
|
10
|
+
interface UseServerActionExecuteOptions<TInput, TOutput, TError> extends Pick<UseServerActionOptions<TInput, TOutput, TError>, 'interceptors'> {
|
11
|
+
}
|
12
|
+
type UseServerActionExecuteRest<TInput, TOutput, TError> = undefined extends TInput ? [input?: TInput, options?: UseServerActionExecuteOptions<TInput, TOutput, TError>] : [input: TInput, options?: UseServerActionExecuteOptions<TInput, TOutput, TError>];
|
13
|
+
interface UseServerActionResultBase<TInput, TOutput, TError> {
|
14
|
+
reset: () => void;
|
15
|
+
execute: (...rest: UseServerActionExecuteRest<TInput, TOutput, TError>) => Promise<SafeResult<TOutput, TError>>;
|
16
|
+
}
|
17
|
+
interface UseServerActionIdleResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
18
|
+
input: undefined;
|
19
|
+
data: undefined;
|
20
|
+
error: null;
|
21
|
+
isIdle: true;
|
22
|
+
isPending: false;
|
23
|
+
isSuccess: false;
|
24
|
+
isError: false;
|
25
|
+
status: 'idle';
|
26
|
+
executedAt: undefined;
|
27
|
+
}
|
28
|
+
interface UseServerActionPendingResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
29
|
+
input: TInput;
|
30
|
+
data: undefined;
|
31
|
+
error: null;
|
32
|
+
isIdle: false;
|
33
|
+
isPending: true;
|
34
|
+
isSuccess: false;
|
35
|
+
isError: false;
|
36
|
+
status: 'pending';
|
37
|
+
executedAt: Date;
|
38
|
+
}
|
39
|
+
interface UseServerActionSuccessResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
40
|
+
input: TInput;
|
41
|
+
data: TOutput;
|
42
|
+
error: null;
|
43
|
+
isIdle: false;
|
44
|
+
isPending: false;
|
45
|
+
isSuccess: true;
|
46
|
+
isError: false;
|
47
|
+
status: 'success';
|
48
|
+
executedAt: Date;
|
49
|
+
}
|
50
|
+
interface UseServerActionErrorResult<TInput, TOutput, TError> extends UseServerActionResultBase<TInput, TOutput, TError> {
|
51
|
+
input: TInput;
|
52
|
+
data: undefined;
|
53
|
+
error: TError;
|
54
|
+
isIdle: false;
|
55
|
+
isPending: false;
|
56
|
+
isSuccess: false;
|
57
|
+
isError: true;
|
58
|
+
status: 'error';
|
59
|
+
executedAt: Date;
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* Use a Server Action Hook
|
63
|
+
*
|
64
|
+
* @see {@link https://orpc.unnoq.com/docs/server-action#useserveraction-hook Server Action Hook Docs}
|
65
|
+
*/
|
66
|
+
declare function useServerAction<TInput, TOutput, TError extends ORPCErrorJSON<any, any>>(action: ActionableClient<TInput, TOutput, TError>, options?: NoInfer<UseServerActionOptions<TInput, TOutput, UnactionableError<TError>>>): UseServerActionIdleResult<TInput, TOutput, UnactionableError<TError>> | UseServerActionSuccessResult<TInput, TOutput, UnactionableError<TError>> | UseServerActionErrorResult<TInput, TOutput, UnactionableError<TError>> | UseServerActionPendingResult<TInput, TOutput, UnactionableError<TError>>;
|
67
|
+
|
68
|
+
export { useServerAction };
|
69
|
+
export type { UseServerActionErrorResult, UseServerActionExecuteOptions, UseServerActionExecuteRest, UseServerActionIdleResult, UseServerActionOptions, UseServerActionPendingResult, UseServerActionResultBase, UseServerActionSuccessResult };
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import { safe, createORPCErrorFromJson } from '@orpc/client';
|
2
|
+
import { intercept, toArray } from '@orpc/shared';
|
3
|
+
import { useState, useRef, useTransition, useCallback, useMemo } from 'react';
|
4
|
+
|
5
|
+
const INITIAL_STATE = {
|
6
|
+
data: void 0,
|
7
|
+
error: null,
|
8
|
+
isIdle: true,
|
9
|
+
isPending: false,
|
10
|
+
isSuccess: false,
|
11
|
+
isError: false,
|
12
|
+
status: "idle"
|
13
|
+
};
|
14
|
+
const PENDING_STATE = {
|
15
|
+
data: void 0,
|
16
|
+
error: null,
|
17
|
+
isIdle: false,
|
18
|
+
isPending: true,
|
19
|
+
isSuccess: false,
|
20
|
+
isError: false,
|
21
|
+
status: "pending"
|
22
|
+
};
|
23
|
+
function useServerAction(action, options = {}) {
|
24
|
+
const [state, setState] = useState(INITIAL_STATE);
|
25
|
+
const executedAtRef = useRef(void 0);
|
26
|
+
const [input, setInput] = useState(void 0);
|
27
|
+
const [isPending, startTransition] = useTransition();
|
28
|
+
const reset = useCallback(() => {
|
29
|
+
executedAtRef.current = void 0;
|
30
|
+
setInput(void 0);
|
31
|
+
setState({ ...INITIAL_STATE });
|
32
|
+
}, []);
|
33
|
+
const execute = useCallback(async (input2, executeOptions = {}) => {
|
34
|
+
const executedAt = /* @__PURE__ */ new Date();
|
35
|
+
executedAtRef.current = executedAt;
|
36
|
+
setInput(input2);
|
37
|
+
return new Promise((resolve) => {
|
38
|
+
startTransition(async () => {
|
39
|
+
const result2 = await safe(intercept(
|
40
|
+
[...toArray(options.interceptors), ...toArray(executeOptions.interceptors)],
|
41
|
+
{ input: input2 },
|
42
|
+
({ input: input3 }) => action(input3).then(([error, data]) => {
|
43
|
+
if (error) {
|
44
|
+
throw createORPCErrorFromJson(error);
|
45
|
+
}
|
46
|
+
return data;
|
47
|
+
})
|
48
|
+
));
|
49
|
+
if (executedAtRef.current === executedAt) {
|
50
|
+
setState({
|
51
|
+
data: result2.data,
|
52
|
+
error: result2.error,
|
53
|
+
isIdle: false,
|
54
|
+
isPending: false,
|
55
|
+
isSuccess: !result2.error,
|
56
|
+
isError: !!result2.error,
|
57
|
+
status: !result2.error ? "success" : "error"
|
58
|
+
});
|
59
|
+
}
|
60
|
+
resolve(result2);
|
61
|
+
});
|
62
|
+
});
|
63
|
+
}, [action, ...toArray(options.interceptors)]);
|
64
|
+
const result = useMemo(() => {
|
65
|
+
const currentState = isPending && executedAtRef.current !== void 0 ? PENDING_STATE : state;
|
66
|
+
return {
|
67
|
+
...currentState,
|
68
|
+
executedAt: executedAtRef.current,
|
69
|
+
input,
|
70
|
+
reset,
|
71
|
+
execute
|
72
|
+
};
|
73
|
+
}, [isPending, state, input, reset, execute]);
|
74
|
+
return result;
|
75
|
+
}
|
76
|
+
|
77
|
+
export { useServerAction };
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
import { AnySchema } from '@orpc/contract';
|
2
|
+
import { Context, ErrorMap, Meta, Lazyable, Procedure, CreateProcedureClientOptions } from '@orpc/server';
|
3
|
+
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
|
5
|
+
interface FormAction {
|
6
|
+
(form: FormData): Promise<void>;
|
7
|
+
}
|
8
|
+
declare const orpcErrorToNextHttpFallbackInterceptor: Interceptor<any, any, any>;
|
9
|
+
/**
|
10
|
+
* Create a server action accept a form data and deserialize with bracket notation.
|
11
|
+
*
|
12
|
+
* @see {@link https://orpc.unnoq.com/docs/server-action#createformaction-utility Create Form Action Utility Docs}
|
13
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/bracket-notation Bracket Notation Docs}
|
14
|
+
*/
|
15
|
+
declare function createFormAction<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, Record<never, never>>>): FormAction;
|
16
|
+
|
17
|
+
export { createFormAction, orpcErrorToNextHttpFallbackInterceptor };
|
18
|
+
export type { FormAction };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
import { AnySchema } from '@orpc/contract';
|
2
|
+
import { Context, ErrorMap, Meta, Lazyable, Procedure, CreateProcedureClientOptions } from '@orpc/server';
|
3
|
+
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
|
5
|
+
interface FormAction {
|
6
|
+
(form: FormData): Promise<void>;
|
7
|
+
}
|
8
|
+
declare const orpcErrorToNextHttpFallbackInterceptor: Interceptor<any, any, any>;
|
9
|
+
/**
|
10
|
+
* Create a server action accept a form data and deserialize with bracket notation.
|
11
|
+
*
|
12
|
+
* @see {@link https://orpc.unnoq.com/docs/server-action#createformaction-utility Create Form Action Utility Docs}
|
13
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/bracket-notation Bracket Notation Docs}
|
14
|
+
*/
|
15
|
+
declare function createFormAction<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, Record<never, never>>>): FormAction;
|
16
|
+
|
17
|
+
export { createFormAction, orpcErrorToNextHttpFallbackInterceptor };
|
18
|
+
export type { FormAction };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { ORPCError, createORPCErrorFromJson } from '@orpc/client';
|
2
|
+
import { StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
|
3
|
+
import { createProcedureClient } from '@orpc/server';
|
4
|
+
import { onError, resolveMaybeOptionalOptions, toArray } from '@orpc/shared';
|
5
|
+
|
6
|
+
const orpcErrorToNextHttpFallbackInterceptor = onError((error) => {
|
7
|
+
if (error instanceof ORPCError && [401, 403, 404].includes(error.status)) {
|
8
|
+
const nextError = createORPCErrorFromJson(error.toJSON());
|
9
|
+
nextError.cause = error;
|
10
|
+
nextError.digest = `NEXT_HTTP_ERROR_FALLBACK;${error.status}`;
|
11
|
+
throw nextError;
|
12
|
+
}
|
13
|
+
});
|
14
|
+
function createFormAction(lazyableProcedure, ...rest) {
|
15
|
+
const options = resolveMaybeOptionalOptions(rest);
|
16
|
+
const client = createProcedureClient(lazyableProcedure, {
|
17
|
+
...options,
|
18
|
+
interceptors: [orpcErrorToNextHttpFallbackInterceptor, ...toArray(options.interceptors)]
|
19
|
+
});
|
20
|
+
const bracketNotation = new StandardBracketNotationSerializer();
|
21
|
+
return async (form) => {
|
22
|
+
const input = bracketNotation.deserialize([...form]);
|
23
|
+
await client(input);
|
24
|
+
};
|
25
|
+
}
|
26
|
+
|
27
|
+
export { createFormAction, orpcErrorToNextHttpFallbackInterceptor };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/react",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.fa8d145",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -11,38 +11,41 @@
|
|
11
11
|
},
|
12
12
|
"keywords": [
|
13
13
|
"unnoq",
|
14
|
-
"orpc"
|
14
|
+
"orpc",
|
15
|
+
"react",
|
16
|
+
"nextjs"
|
15
17
|
],
|
16
18
|
"exports": {
|
17
19
|
".": {
|
18
|
-
"types": "./dist/
|
19
|
-
"import": "./dist/index.
|
20
|
-
"default": "./dist/index.
|
20
|
+
"types": "./dist/index.d.mts",
|
21
|
+
"import": "./dist/index.mjs",
|
22
|
+
"default": "./dist/index.mjs"
|
21
23
|
},
|
22
|
-
"
|
23
|
-
"types": "./dist/
|
24
|
+
"./hooks": {
|
25
|
+
"types": "./dist/hooks/index.d.mts",
|
26
|
+
"import": "./dist/hooks/index.mjs",
|
27
|
+
"default": "./dist/hooks/index.mjs"
|
24
28
|
}
|
25
29
|
},
|
26
30
|
"files": [
|
27
|
-
"!**/*.map",
|
28
|
-
"!**/*.tsbuildinfo",
|
29
31
|
"dist"
|
30
32
|
],
|
31
33
|
"peerDependencies": {
|
32
|
-
"
|
33
|
-
"react": ">=18.3.0",
|
34
|
-
"@orpc/client": "0.0.0-next.f99e554",
|
35
|
-
"@orpc/contract": "0.0.0-next.f99e554",
|
36
|
-
"@orpc/server": "0.0.0-next.f99e554"
|
34
|
+
"react": ">=18.0.0"
|
37
35
|
},
|
38
36
|
"dependencies": {
|
39
|
-
"@orpc/
|
37
|
+
"@orpc/client": "0.0.0-next.fa8d145",
|
38
|
+
"@orpc/contract": "0.0.0-next.fa8d145",
|
39
|
+
"@orpc/openapi-client": "0.0.0-next.fa8d145",
|
40
|
+
"@orpc/server": "0.0.0-next.fa8d145",
|
41
|
+
"@orpc/shared": "0.0.0-next.fa8d145"
|
40
42
|
},
|
41
43
|
"devDependencies": {
|
42
|
-
"
|
44
|
+
"react": "^19.1.0",
|
45
|
+
"zod": "3.25.0-beta.20250516T005923"
|
43
46
|
},
|
44
47
|
"scripts": {
|
45
|
-
"build": "
|
48
|
+
"build": "unbuild",
|
46
49
|
"build:watch": "pnpm run build --watch",
|
47
50
|
"type:check": "tsc -b"
|
48
51
|
}
|