@vladimirdev635/gql-client-react 0.0.61 → 0.0.62
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/index.d.ts +2 -2
- package/index.js +1 -2
- package/loading-state.d.ts +3 -0
- package/loading-state.js +1 -0
- package/package.json +1 -1
- package/tests/useLazyOperation.spec.js +10 -6
- package/tests/useOperation.spec.jsx +9 -5
- package/tests/useSubscription.spec.jsx +19 -15
- package/types.d.ts +6 -6
- package/use-lazy-operation/hook.d.ts +3 -0
- package/{useLazyOperation.jsx → use-lazy-operation/hook.js} +3 -3
- package/use-lazy-operation/index.d.ts +2 -0
- package/use-lazy-operation/index.js +1 -0
- package/use-lazy-operation/types.d.ts +14 -0
- package/use-lazy-operation/types.js +1 -0
- package/useOperation.d.ts +2 -5
- package/useOperation.jsx +2 -2
- package/useSubscription.d.ts +2 -2
- package/useSubscription.jsx +2 -2
- package/useLazyOperation.d.ts +0 -19
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type OperationLoadingState, type OperationSuccessState, type OperationFailureState, type OperationState,
|
|
2
|
-
export { type LazyOperationInitialState, type LazyOperationState, type UseLazyOperationReturnType,
|
|
1
|
+
export { type OperationLoadingState, type OperationSuccessState, type OperationFailureState, type OperationState, useOperation, } from './useOperation.jsx';
|
|
2
|
+
export { type LazyOperationInitialState, type LazyOperationState, type UseLazyOperationReturnType, type LazyOperationExecuteReturnType, } from './use-lazy-operation/index.js';
|
|
3
3
|
export { useSubscription } from './useSubscription.jsx';
|
package/index.js
CHANGED
package/loading-state.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const loadingState = Object.freeze({ state: 'loading' });
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useLazyOperation } from '../
|
|
1
|
+
import { useLazyOperation } from '../use-lazy-operation/index.js';
|
|
2
2
|
import { describe, expect, it } from 'vitest';
|
|
3
3
|
import { renderHook, act } from '@testing-library/react';
|
|
4
4
|
import assert from 'assert';
|
|
@@ -11,10 +11,12 @@ describe('useLazyOperation', () => {
|
|
|
11
11
|
expect(result.current[1].state.state).toBe('initial');
|
|
12
12
|
});
|
|
13
13
|
it('Should return loading state after invoking, then success state', async () => {
|
|
14
|
-
const executor =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
const executor = {
|
|
15
|
+
executeSync: async () => ({
|
|
16
|
+
result: { a: 1 },
|
|
17
|
+
response: new Response()
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
18
20
|
const { result } = renderHook(() => useLazyOperation(executor, testOperation));
|
|
19
21
|
let promise;
|
|
20
22
|
act(() => {
|
|
@@ -31,7 +33,9 @@ describe('useLazyOperation', () => {
|
|
|
31
33
|
});
|
|
32
34
|
it('Should return loading state after invoking, then failure state', async () => {
|
|
33
35
|
const error = new Error('Network error');
|
|
34
|
-
const executor =
|
|
36
|
+
const executor = {
|
|
37
|
+
executeSync: async () => { throw error; },
|
|
38
|
+
};
|
|
35
39
|
const { result } = renderHook(() => useLazyOperation(executor, testOperation));
|
|
36
40
|
let promise;
|
|
37
41
|
act(() => {
|
|
@@ -5,10 +5,12 @@ import assert from 'assert';
|
|
|
5
5
|
import { testOperation } from './utils.js';
|
|
6
6
|
describe('useOperation', () => {
|
|
7
7
|
it('Should return loading state and then success state', async () => {
|
|
8
|
-
const executor =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const executor = {
|
|
9
|
+
executeSync: async () => ({
|
|
10
|
+
result: { a: 1 },
|
|
11
|
+
response: new Response()
|
|
12
|
+
}),
|
|
13
|
+
};
|
|
12
14
|
const { result } = renderHook((props) => useOperation(...props), {
|
|
13
15
|
initialProps: [executor, testOperation, {}, {}]
|
|
14
16
|
});
|
|
@@ -21,7 +23,9 @@ describe('useOperation', () => {
|
|
|
21
23
|
});
|
|
22
24
|
it('Should return loading state and then failure state', async () => {
|
|
23
25
|
const error = new Error('Network error');
|
|
24
|
-
const executor =
|
|
26
|
+
const executor = {
|
|
27
|
+
executeSync: async () => { throw error; }
|
|
28
|
+
};
|
|
25
29
|
const { result } = renderHook((props) => useOperation(...props), {
|
|
26
30
|
initialProps: [executor, testOperation, {}, {}]
|
|
27
31
|
});
|
|
@@ -5,20 +5,22 @@ import assert from 'assert';
|
|
|
5
5
|
import { testSubscription } from './utils.js';
|
|
6
6
|
describe('useSubscription', () => {
|
|
7
7
|
it('Should return loading state and then success state', async () => {
|
|
8
|
-
const executor =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
const executor = {
|
|
9
|
+
executeSubscription: async () => {
|
|
10
|
+
const readableStream = new ReadableStream();
|
|
11
|
+
const response = new Response(readableStream);
|
|
12
|
+
return {
|
|
13
|
+
result: {
|
|
14
|
+
stream: (async function* () {
|
|
15
|
+
yield { number: 1 };
|
|
16
|
+
yield { number: 2 };
|
|
17
|
+
yield { number: 3 };
|
|
18
|
+
})(),
|
|
19
|
+
close: () => { }
|
|
20
|
+
},
|
|
21
|
+
response
|
|
22
|
+
};
|
|
23
|
+
},
|
|
22
24
|
};
|
|
23
25
|
const { result } = renderHook((props) => useSubscription(...props), {
|
|
24
26
|
initialProps: [executor, testSubscription, {}, {}]
|
|
@@ -36,7 +38,9 @@ describe('useSubscription', () => {
|
|
|
36
38
|
});
|
|
37
39
|
it('Should return loading state and then failure state', async () => {
|
|
38
40
|
const error = new Error('Network error');
|
|
39
|
-
const executor =
|
|
41
|
+
const executor = {
|
|
42
|
+
executeSubscription: async () => { throw error; }
|
|
43
|
+
};
|
|
40
44
|
const { result } = renderHook((props) => useSubscription(...props), {
|
|
41
45
|
initialProps: [executor, testSubscription, {}, {}]
|
|
42
46
|
});
|
package/types.d.ts
CHANGED
|
@@ -22,12 +22,12 @@ export interface ExecuteResult<TResult> {
|
|
|
22
22
|
result: TResult;
|
|
23
23
|
response: Response;
|
|
24
24
|
}
|
|
25
|
-
export
|
|
25
|
+
export interface SubOpAsyncIterable<TResult> extends Disposable {
|
|
26
26
|
stream: AsyncIterable<TResult, void, unknown>;
|
|
27
27
|
close: () => void;
|
|
28
|
-
}
|
|
29
|
-
export
|
|
30
|
-
<
|
|
31
|
-
<
|
|
32
|
-
}
|
|
28
|
+
}
|
|
29
|
+
export interface IExecutor<TRequestContext extends RequestContext> {
|
|
30
|
+
executeSync<T extends SyncOperation<unknown, unknown>>(operation: T, variables: OperationVariables<T>, requestContext: TRequestContext): Promise<ExecuteResult<OperationResult<T>>>;
|
|
31
|
+
executeSubscription<T extends SubscriptionOperation<unknown, unknown>>(operation: T, variables: OperationVariables<T>, requestContext: TRequestContext, controller: AbortController): Promise<ExecuteResult<SubOpAsyncIterable<OperationResult<T>>>>;
|
|
32
|
+
}
|
|
33
33
|
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { IExecutor, OperationResult, OperationVariables, RequestContext, SyncOperation } from '../types.js';
|
|
2
|
+
import { UseLazyOperationReturnType } from './types.js';
|
|
3
|
+
export declare function useLazyOperation<T extends SyncOperation<unknown, unknown>, TRequestContext extends RequestContext>(executor: IExecutor<TRequestContext>, operation: T): UseLazyOperationReturnType<OperationVariables<T>, OperationResult<T>, TRequestContext>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { useCallback, useState } from 'react';
|
|
2
|
-
import { loadingState } from '
|
|
3
|
-
|
|
2
|
+
import { loadingState } from '../loading-state.js';
|
|
3
|
+
const lazyInitialState = Object.freeze({ state: 'initial' });
|
|
4
4
|
async function execute(executor, operation, variables, requestContext, setState) {
|
|
5
5
|
let newState;
|
|
6
6
|
try {
|
|
7
|
-
const result = await executor(operation, variables, requestContext);
|
|
7
|
+
const result = await executor.executeSync(operation, variables, requestContext);
|
|
8
8
|
newState = { state: 'success', ...result };
|
|
9
9
|
}
|
|
10
10
|
catch (error) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useLazyOperation, } from './hook.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RequestContext } from '../types.js';
|
|
2
|
+
import { type OperationFailureState, type OperationState, type OperationSuccessState } from '../useOperation.jsx';
|
|
3
|
+
export interface LazyOperationInitialState {
|
|
4
|
+
state: 'initial';
|
|
5
|
+
}
|
|
6
|
+
export type LazyOperationState<TResult> = OperationState<TResult> | LazyOperationInitialState;
|
|
7
|
+
export type LazyOperationExecuteReturnType<TResult> = Promise<OperationSuccessState<TResult> | OperationFailureState>;
|
|
8
|
+
export type UseLazyOperationReturnType<TVariables, TResult, TRequestContext extends RequestContext> = [
|
|
9
|
+
(variables: TVariables, requestContext: TRequestContext) => LazyOperationExecuteReturnType<TResult>,
|
|
10
|
+
{
|
|
11
|
+
state: LazyOperationState<TResult>;
|
|
12
|
+
reset: () => void;
|
|
13
|
+
}
|
|
14
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/useOperation.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ExecuteResult,
|
|
1
|
+
import type { ExecuteResult, IExecutor, OperationResult, OperationVariables, RequestContext, SyncOperation } from './types.js';
|
|
2
2
|
export interface OperationLoadingState {
|
|
3
3
|
state: 'loading';
|
|
4
4
|
}
|
|
@@ -10,7 +10,4 @@ export interface OperationFailureState {
|
|
|
10
10
|
error: Error;
|
|
11
11
|
}
|
|
12
12
|
export type OperationState<TResult> = OperationLoadingState | OperationSuccessState<TResult> | OperationFailureState;
|
|
13
|
-
export declare
|
|
14
|
-
readonly state: "loading";
|
|
15
|
-
}>;
|
|
16
|
-
export declare function useOperation<T extends SyncOperation<unknown, unknown>, TRequestContext extends RequestContext>(executor: Executor<TRequestContext>, operation: T, variables: OperationVariables<T>, requestContext: TRequestContext): OperationState<OperationResult<T>>;
|
|
13
|
+
export declare function useOperation<T extends SyncOperation<unknown, unknown>, TRequestContext extends RequestContext>(executor: IExecutor<TRequestContext>, operation: T, variables: OperationVariables<T>, requestContext: TRequestContext): OperationState<OperationResult<T>>;
|
package/useOperation.jsx
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useEffect, useMemo, useState } from 'react';
|
|
2
2
|
import hash from 'object-hash';
|
|
3
|
-
|
|
3
|
+
import { loadingState } from './loading-state.js';
|
|
4
4
|
export function useOperation(executor, operation, variables, requestContext) {
|
|
5
5
|
const [state, setState] = useState(loadingState);
|
|
6
6
|
const memoizedVariables = useMemo(() => variables, [hash(variables)]);
|
|
7
7
|
const memoizedRequestContext = useMemo(() => requestContext, [hash(requestContext)]);
|
|
8
8
|
useEffect(() => {
|
|
9
|
-
executor(operation, variables, requestContext)
|
|
9
|
+
executor.executeSync(operation, variables, requestContext)
|
|
10
10
|
.then(result => setState({ state: 'success', ...result }))
|
|
11
11
|
.catch(error => setState({ state: 'failure', error }));
|
|
12
12
|
return () => setState(loadingState);
|
package/useSubscription.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IExecutor, OperationResult, OperationVariables, RequestContext, SubOpAsyncIterable, SubscriptionOperation } from './types.js';
|
|
2
2
|
import { type OperationState } from './useOperation.jsx';
|
|
3
|
-
export declare function useSubscription<T extends SubscriptionOperation<unknown, unknown>, TRequestContext extends RequestContext>(executor:
|
|
3
|
+
export declare function useSubscription<T extends SubscriptionOperation<unknown, unknown>, TRequestContext extends RequestContext>(executor: IExecutor<TRequestContext>, operation: T, variables: OperationVariables<T>, requestContext: TRequestContext): OperationState<SubOpAsyncIterable<OperationResult<T>>>;
|
package/useSubscription.jsx
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useEffect, useMemo, useState } from 'react';
|
|
2
2
|
import hash from 'object-hash';
|
|
3
|
-
import { loadingState } from './
|
|
3
|
+
import { loadingState } from './loading-state.js';
|
|
4
4
|
export function useSubscription(executor, operation, variables, requestContext) {
|
|
5
5
|
const [state, setState] = useState(loadingState);
|
|
6
6
|
const memoizedVariables = useMemo(() => variables, [hash(variables)]);
|
|
7
7
|
const memoizedRequestContext = useMemo(() => requestContext, [hash(requestContext)]);
|
|
8
8
|
useEffect(() => {
|
|
9
|
-
executor(operation, variables, requestContext)
|
|
9
|
+
executor.executeSubscription(operation, variables, requestContext, new AbortController())
|
|
10
10
|
.then(result => setState({ state: 'success', ...result }))
|
|
11
11
|
.catch(error => setState({ state: 'failure', error }));
|
|
12
12
|
return () => {
|
package/useLazyOperation.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { Executor, OperationResult, OperationVariables, RequestContext, SyncOperation } from './types.js';
|
|
2
|
-
import { type OperationFailureState, type OperationState, type OperationSuccessState } from './useOperation.jsx';
|
|
3
|
-
export interface LazyOperationInitialState {
|
|
4
|
-
state: 'initial';
|
|
5
|
-
}
|
|
6
|
-
export type LazyOperationState<TResult> = OperationState<TResult> | LazyOperationInitialState;
|
|
7
|
-
export declare const lazyInitialState: Readonly<{
|
|
8
|
-
readonly state: "initial";
|
|
9
|
-
}>;
|
|
10
|
-
type LazyOperationExecuteReturnType<TResult> = Promise<OperationSuccessState<TResult> | OperationFailureState>;
|
|
11
|
-
export type UseLazyOperationReturnType<TVariables, TResult, TRequestContext extends RequestContext> = [
|
|
12
|
-
(variables: TVariables, requestContext: TRequestContext) => LazyOperationExecuteReturnType<TResult>,
|
|
13
|
-
{
|
|
14
|
-
state: LazyOperationState<TResult>;
|
|
15
|
-
reset: () => void;
|
|
16
|
-
}
|
|
17
|
-
];
|
|
18
|
-
export declare function useLazyOperation<T extends SyncOperation<unknown, unknown>, TRequestContext extends RequestContext>(executor: Executor<TRequestContext>, operation: T): UseLazyOperationReturnType<OperationVariables<T>, OperationResult<T>, TRequestContext>;
|
|
19
|
-
export {};
|