@ahoo-wang/fetcher-react 3.9.3 → 3.9.6
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 +200 -19
- package/README.zh-CN.md +200 -19
- package/dist/api/apiHooks.d.ts +78 -0
- package/dist/api/apiHooks.d.ts.map +1 -0
- package/dist/api/createExecuteApiHooks.d.ts +11 -8
- package/dist/api/createExecuteApiHooks.d.ts.map +1 -1
- package/dist/api/createQueryApiHooks.d.ts +97 -0
- package/dist/api/createQueryApiHooks.d.ts.map +1 -0
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/index.es.js +458 -429
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { UseQueryReturn, UseQueryOptions } from '../core';
|
|
2
|
+
import { FetcherError } from '@ahoo-wang/fetcher';
|
|
3
|
+
import { CreateQueryApiHooksOptions } from './createExecuteApiHooks';
|
|
4
|
+
import { HookName, QueryMethod, OnBeforeExecuteCallback } from './apiHooks';
|
|
5
|
+
/**
|
|
6
|
+
* Options for useApiMethodQuery hook.
|
|
7
|
+
* @template Q - The query type.
|
|
8
|
+
* @template TData - The return type of the API method (resolved).
|
|
9
|
+
* @template E - The error type.
|
|
10
|
+
*/
|
|
11
|
+
export interface UseApiMethodQueryOptions<Q, TData = any, E = FetcherError> extends Omit<UseQueryOptions<Q, TData, E>, 'execute'> {
|
|
12
|
+
/**
|
|
13
|
+
* Callback executed before query execution.
|
|
14
|
+
* Allows users to handle abortController and inspect/modify query parameters.
|
|
15
|
+
* Note: Query parameters can be modified in place for objects.
|
|
16
|
+
* @param abortController - The AbortController for the request.
|
|
17
|
+
* @param query - The query parameters passed to the API method (type-safe).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* onBeforeExecute: (abortController, query) => {
|
|
21
|
+
* // query is now typed as Q
|
|
22
|
+
* if (query && typeof query === 'object') {
|
|
23
|
+
* query.timestamp = Date.now();
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
*/
|
|
27
|
+
onBeforeExecute?: OnBeforeExecuteCallback<Q>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The return type of createQueryApiHooks.
|
|
31
|
+
* Creates a hook for each function method in the API object, prefixed with 'use' and capitalized.
|
|
32
|
+
* Each hook accepts query options and returns the useQuery interface with type-safe query management.
|
|
33
|
+
* @template API - The API object type.
|
|
34
|
+
* @template E - The error type for all hooks (defaults to FetcherError).
|
|
35
|
+
*/
|
|
36
|
+
export type QueryAPIHooks<API extends Record<string, any>, E = FetcherError> = {
|
|
37
|
+
[K in keyof API as API[K] extends QueryMethod ? HookName<string & K> : never]: API[K] extends QueryMethod<infer Q, infer R> ? (options?: UseApiMethodQueryOptions<Q, R, E>) => UseQueryReturn<Q, R, E> : never;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Creates type-safe React hooks for API query methods.
|
|
41
|
+
* Each API method that follows the query pattern is wrapped into a hook that extends useQuery.
|
|
42
|
+
* The generated hooks provide automatic query state management, auto-execution, and error handling.
|
|
43
|
+
*
|
|
44
|
+
* @template API - The API object type containing query methods.
|
|
45
|
+
* @param options - Configuration options including the API object.
|
|
46
|
+
* @returns An object containing hooks for each API query method.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Define your API service using decorators
|
|
51
|
+
* import { api, get, post, patch, path, body, autoGeneratedError } from '@ahoo-wang/fetcher-decorator';
|
|
52
|
+
*
|
|
53
|
+
* @api('/users')
|
|
54
|
+
* class UserApi {
|
|
55
|
+
* @get('')
|
|
56
|
+
* getUsers(query: UserListQuery, attributes?: Record<string, any>): Promise<User[]> {
|
|
57
|
+
* throw autoGeneratedError(query, attributes);
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* @get('/{id}')
|
|
61
|
+
* getUser(query: { id: string }, attributes?: Record<string, any>): Promise<User> {
|
|
62
|
+
* throw autoGeneratedError(query, attributes);
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* const apiHooks = createQueryApiHooks({ api: new UserApi() });
|
|
67
|
+
*
|
|
68
|
+
* function UserListComponent() {
|
|
69
|
+
* const { loading, result, error, execute, setQuery, getQuery } = apiHooks.useGetUsers({
|
|
70
|
+
* initialQuery: { page: 1, limit: 10 },
|
|
71
|
+
* autoExecute: true,
|
|
72
|
+
* onBeforeExecute: (abortController, query) => {
|
|
73
|
+
* // query is fully typed as UserListQuery
|
|
74
|
+
* console.log('Executing query:', query);
|
|
75
|
+
* },
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* const handlePageChange = (page: number) => {
|
|
79
|
+
* setQuery({ ...getQuery(), page }); // Automatically executes
|
|
80
|
+
* };
|
|
81
|
+
*
|
|
82
|
+
* if (loading) return <div>Loading...</div>;
|
|
83
|
+
* if (error) return <div>Error: {error.message}</div>;
|
|
84
|
+
*
|
|
85
|
+
* return (
|
|
86
|
+
* <div>
|
|
87
|
+
* <button onClick={() => handlePageChange(2)}>Go to page 2</button>
|
|
88
|
+
* {result?.map(user => (
|
|
89
|
+
* <div key={user.id}>{user.name}</div>
|
|
90
|
+
* ))}
|
|
91
|
+
* </div>
|
|
92
|
+
* );
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function createQueryApiHooks<API extends Record<string, any>, E = FetcherError>(options: CreateQueryApiHooksOptions<API>): QueryAPIHooks<API, E>;
|
|
97
|
+
//# sourceMappingURL=createQueryApiHooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createQueryApiHooks.d.ts","sourceRoot":"","sources":["../../src/api/createQueryApiHooks.ts"],"names":[],"mappings":"AAcA,OAAO,EAAY,cAAc,EAAE,eAAe,EAAa,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AC,CAAC,CAoBjE"}
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAaA,cAAc,yBAAyB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAaA,cAAc,YAAY,CAAC;AAC3B,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|