@orpc/react 1.1.1 → 1.3.0
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 +1 -0
- package/dist/hooks/index.d.mts +2 -2
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.mjs +54 -42
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +1 -0
- package/package.json +7 -7
package/README.md
CHANGED
@@ -49,6 +49,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
49
49
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
50
50
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
51
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.
|
52
53
|
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
53
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).
|
54
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).
|
package/dist/hooks/index.d.mts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import { SafeResult, ORPCErrorJSON } from '@orpc/client';
|
2
2
|
import { ActionableClient, UnactionableError } from '@orpc/server';
|
3
|
-
import { Interceptor } from '@orpc/shared';
|
3
|
+
import { Interceptor, PromiseWithError } from '@orpc/shared';
|
4
4
|
|
5
5
|
interface UseServerActionOptions<TInput, TOutput, TError> {
|
6
6
|
interceptors?: Interceptor<{
|
7
7
|
input: TInput;
|
8
|
-
}, TOutput, TError
|
8
|
+
}, PromiseWithError<TOutput, TError>>[];
|
9
9
|
}
|
10
10
|
interface UseServerActionExecuteOptions<TInput, TOutput, TError> extends Pick<UseServerActionOptions<TInput, TOutput, TError>, 'interceptors'> {
|
11
11
|
}
|
package/dist/hooks/index.d.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
import { SafeResult, ORPCErrorJSON } from '@orpc/client';
|
2
2
|
import { ActionableClient, UnactionableError } from '@orpc/server';
|
3
|
-
import { Interceptor } from '@orpc/shared';
|
3
|
+
import { Interceptor, PromiseWithError } from '@orpc/shared';
|
4
4
|
|
5
5
|
interface UseServerActionOptions<TInput, TOutput, TError> {
|
6
6
|
interceptors?: Interceptor<{
|
7
7
|
input: TInput;
|
8
|
-
}, TOutput, TError
|
8
|
+
}, PromiseWithError<TOutput, TError>>[];
|
9
9
|
}
|
10
10
|
interface UseServerActionExecuteOptions<TInput, TOutput, TError> extends Pick<UseServerActionOptions<TInput, TOutput, TError>, 'interceptors'> {
|
11
11
|
}
|
package/dist/hooks/index.mjs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { safe, createORPCErrorFromJson } from '@orpc/client';
|
2
2
|
import { intercept, toArray } from '@orpc/shared';
|
3
|
-
import { useState, useCallback, useMemo } from 'react';
|
3
|
+
import { useState, useRef, useTransition, useCallback, useMemo } from 'react';
|
4
4
|
|
5
5
|
const INITIAL_STATE = {
|
6
6
|
data: void 0,
|
@@ -9,56 +9,68 @@ const INITIAL_STATE = {
|
|
9
9
|
isPending: false,
|
10
10
|
isSuccess: false,
|
11
11
|
isError: false,
|
12
|
-
status: "idle"
|
13
|
-
|
14
|
-
|
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"
|
15
22
|
};
|
16
23
|
function useServerAction(action, options = {}) {
|
17
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();
|
18
28
|
const reset = useCallback(() => {
|
19
|
-
|
29
|
+
executedAtRef.current = void 0;
|
30
|
+
setInput(void 0);
|
31
|
+
setState({ ...INITIAL_STATE });
|
20
32
|
}, []);
|
21
|
-
const execute = useCallback(async (
|
33
|
+
const execute = useCallback(async (input2, executeOptions = {}) => {
|
22
34
|
const executedAt = /* @__PURE__ */ new Date();
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
+
async ({ 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
|
+
});
|
40
59
|
}
|
41
|
-
|
42
|
-
})
|
43
|
-
));
|
44
|
-
setState({
|
45
|
-
data: result2.data,
|
46
|
-
error: result2.error,
|
47
|
-
isIdle: false,
|
48
|
-
isPending: false,
|
49
|
-
isSuccess: !result2.error,
|
50
|
-
isError: !!result2.error,
|
51
|
-
status: !result2.error ? "success" : "error",
|
52
|
-
executedAt,
|
53
|
-
input
|
60
|
+
resolve(result2);
|
61
|
+
});
|
54
62
|
});
|
55
|
-
return result2;
|
56
63
|
}, [action, ...toArray(options.interceptors)]);
|
57
|
-
const result = useMemo(() =>
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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]);
|
62
74
|
return result;
|
63
75
|
}
|
64
76
|
|
package/dist/index.d.mts
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
import { AnySchema } from '@orpc/contract';
|
2
2
|
import { Context, ErrorMap, Meta, Lazyable, Procedure, CreateProcedureClientOptions } from '@orpc/server';
|
3
3
|
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
export { getIssueMessage, parseFormData } from '@orpc/openapi-client/standard';
|
4
5
|
|
5
6
|
interface FormAction {
|
6
7
|
(form: FormData): Promise<void>;
|
7
8
|
}
|
8
|
-
declare const orpcErrorToNextHttpFallbackInterceptor: Interceptor<any, any
|
9
|
+
declare const orpcErrorToNextHttpFallbackInterceptor: Interceptor<any, Promise<any>>;
|
9
10
|
/**
|
10
11
|
* Create a server action accept a form data and deserialize with bracket notation.
|
11
12
|
*
|
package/dist/index.d.ts
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
import { AnySchema } from '@orpc/contract';
|
2
2
|
import { Context, ErrorMap, Meta, Lazyable, Procedure, CreateProcedureClientOptions } from '@orpc/server';
|
3
3
|
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
export { getIssueMessage, parseFormData } from '@orpc/openapi-client/standard';
|
4
5
|
|
5
6
|
interface FormAction {
|
6
7
|
(form: FormData): Promise<void>;
|
7
8
|
}
|
8
|
-
declare const orpcErrorToNextHttpFallbackInterceptor: Interceptor<any, any
|
9
|
+
declare const orpcErrorToNextHttpFallbackInterceptor: Interceptor<any, Promise<any>>;
|
9
10
|
/**
|
10
11
|
* Create a server action accept a form data and deserialize with bracket notation.
|
11
12
|
*
|
package/dist/index.mjs
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { ORPCError, createORPCErrorFromJson } from '@orpc/client';
|
2
2
|
import { StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
|
3
|
+
export { getIssueMessage, parseFormData } from '@orpc/openapi-client/standard';
|
3
4
|
import { createProcedureClient } from '@orpc/server';
|
4
5
|
import { onError, resolveMaybeOptionalOptions, toArray } from '@orpc/shared';
|
5
6
|
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/react",
|
3
3
|
"type": "module",
|
4
|
-
"version": "1.
|
4
|
+
"version": "1.3.0",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -34,15 +34,15 @@
|
|
34
34
|
"react": ">=18.0.0"
|
35
35
|
},
|
36
36
|
"dependencies": {
|
37
|
-
"@orpc/
|
38
|
-
"@orpc/client": "1.
|
39
|
-
"@orpc/
|
40
|
-
"@orpc/
|
41
|
-
"@orpc/
|
37
|
+
"@orpc/client": "1.3.0",
|
38
|
+
"@orpc/openapi-client": "1.3.0",
|
39
|
+
"@orpc/shared": "1.3.0",
|
40
|
+
"@orpc/contract": "1.3.0",
|
41
|
+
"@orpc/server": "1.3.0"
|
42
42
|
},
|
43
43
|
"devDependencies": {
|
44
44
|
"react": "^19.1.0",
|
45
|
-
"zod": "^3.
|
45
|
+
"zod": "^3.25.11"
|
46
46
|
},
|
47
47
|
"scripts": {
|
48
48
|
"build": "unbuild",
|