@opengeoweb/api 9.9.0 → 9.10.1
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.esm.js +10 -6
- package/package.json +1 -1
- package/src/lib/components/apihooks/useApi.d.ts +31 -5
package/index.esm.js
CHANGED
|
@@ -424,9 +424,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
424
424
|
var generateRandomId = function generateRandomId() {
|
|
425
425
|
return "-".concat(Math.random().toString(36).substr(2, 9));
|
|
426
426
|
};
|
|
427
|
-
var useApi = function useApi(
|
|
428
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
429
|
-
apiCall, params) {
|
|
427
|
+
var useApi = function useApi(apiCall, params, callbacks) {
|
|
430
428
|
var _useIsMounted = useIsMounted(),
|
|
431
429
|
isMounted = _useIsMounted.isMounted;
|
|
432
430
|
var _React$useState = React.useState(generateRandomId()),
|
|
@@ -445,9 +443,12 @@ apiCall, params) {
|
|
|
445
443
|
result = _React$useState8[0],
|
|
446
444
|
setResult = _React$useState8[1];
|
|
447
445
|
var debouncedParams = useDebounce(JSON.stringify(params), 300);
|
|
446
|
+
// trick so that we don't have to include this in the useEffect dependency array (as it is likely that callbacks changes on every render)
|
|
447
|
+
var callbacksRef = React.useRef();
|
|
448
|
+
callbacksRef.current = callbacks;
|
|
448
449
|
var fetchApiData = function fetchApiData(_params) {
|
|
449
450
|
return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
450
|
-
var response;
|
|
451
|
+
var _a, _b, _c, _d, response, data;
|
|
451
452
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
452
453
|
while (1) switch (_context.prev = _context.next) {
|
|
453
454
|
case 0:
|
|
@@ -459,9 +460,11 @@ apiCall, params) {
|
|
|
459
460
|
case 5:
|
|
460
461
|
response = _context.sent;
|
|
461
462
|
if (isMounted.current) {
|
|
462
|
-
|
|
463
|
+
data = Array.isArray(response) ? response.map(function (d) {
|
|
463
464
|
return d.data;
|
|
464
|
-
}) : response.data
|
|
465
|
+
}) : response.data;
|
|
466
|
+
(_b = (_a = callbacksRef.current) === null || _a === void 0 ? void 0 : _a.onSuccess) === null || _b === void 0 ? void 0 : _b.call(_a, data);
|
|
467
|
+
setResult(data);
|
|
465
468
|
setIsloading(false);
|
|
466
469
|
setError(null);
|
|
467
470
|
}
|
|
@@ -474,6 +477,7 @@ apiCall, params) {
|
|
|
474
477
|
if (axios.isCancel(_context.t0)) {
|
|
475
478
|
setIsloading(false);
|
|
476
479
|
} else {
|
|
480
|
+
(_d = (_c = callbacksRef.current) === null || _c === void 0 ? void 0 : _c.onError) === null || _d === void 0 ? void 0 : _d.call(_c, _context.t0);
|
|
477
481
|
setError(_context.t0);
|
|
478
482
|
setIsloading(false);
|
|
479
483
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
1
|
import { ConfigType } from '@opengeoweb/shared';
|
|
2
2
|
export type ApiParams = string | ConfigType | Record<string, string | undefined> | null;
|
|
3
|
-
|
|
4
|
-
isLoading: boolean;
|
|
5
|
-
error: Error;
|
|
6
|
-
result: any;
|
|
3
|
+
interface BaseApiHookProps {
|
|
7
4
|
fetchApiData?: (params: ApiParams) => Promise<void>;
|
|
8
5
|
clearResults?: () => void;
|
|
9
6
|
}
|
|
10
|
-
|
|
7
|
+
type PendingApiHookProps = BaseApiHookProps & {
|
|
8
|
+
isLoading: true;
|
|
9
|
+
result: null;
|
|
10
|
+
error: null;
|
|
11
|
+
};
|
|
12
|
+
type DoneApiHookProps<R> = BaseApiHookProps & {
|
|
13
|
+
isLoading: false;
|
|
14
|
+
result: R;
|
|
15
|
+
error: null;
|
|
16
|
+
};
|
|
17
|
+
type ErrorApiHookProps = BaseApiHookProps & {
|
|
18
|
+
isLoading: false;
|
|
19
|
+
result: null;
|
|
20
|
+
error: Error;
|
|
21
|
+
};
|
|
22
|
+
export type ApiHookProps<TData> = ErrorApiHookProps | PendingApiHookProps | DoneApiHookProps<TData>;
|
|
23
|
+
interface Callbacks<TData> {
|
|
24
|
+
onSuccess?: (data: TData) => void;
|
|
25
|
+
onError?: (e: Error) => void;
|
|
26
|
+
}
|
|
27
|
+
export declare const useApi: <TResponse extends {
|
|
28
|
+
data: unknown;
|
|
29
|
+
} | {
|
|
30
|
+
data: unknown;
|
|
31
|
+
}[], TResult = TResponse extends {
|
|
32
|
+
data: infer TData;
|
|
33
|
+
}[] ? TData[] : TResponse extends {
|
|
34
|
+
data: infer TData_1;
|
|
35
|
+
} ? TData_1 : never>(apiCall?: ((params?: ApiParams, id?: string) => Promise<TResponse>) | undefined, params?: ApiParams, callbacks?: Callbacks<TResult> | undefined) => ApiHookProps<TResult>;
|
|
36
|
+
export {};
|