@opengeoweb/api 9.8.0 → 9.10.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/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
- setResult(Array.isArray(response) ? response.map(function (d) {
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeoweb/api",
3
- "version": "9.8.0",
3
+ "version": "9.10.0",
4
4
  "description": "GeoWeb API library for the opengeoweb project",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -12,12 +12,12 @@ export interface Credentials {
12
12
  keep_session_alive_at?: number;
13
13
  has_connection_issue?: boolean;
14
14
  }
15
- export type ApiUrls = {
15
+ export interface ApiUrls {
16
16
  baseURL?: string;
17
17
  appURL?: string;
18
18
  authTokenURL?: string;
19
19
  authClientId?: string;
20
- };
20
+ }
21
21
  export interface ApiModule {
22
22
  auth?: Credentials;
23
23
  onSetAuth?: (cred: Credentials) => void;
@@ -1,12 +1,36 @@
1
1
  import { ConfigType } from '@opengeoweb/shared';
2
- export type ApiParams = string | ConfigType | {
3
- [key: string]: string | undefined;
4
- } | null;
5
- export interface ApiHookProps {
6
- isLoading: boolean;
7
- error: Error;
8
- result: any;
2
+ export type ApiParams = string | ConfigType | Record<string, string | undefined> | null;
3
+ interface BaseApiHookProps {
9
4
  fetchApiData?: (params: ApiParams) => Promise<void>;
10
5
  clearResults?: () => void;
11
6
  }
12
- export declare const useApi: (apiCall?: ((params?: ApiParams, id?: string) => Promise<any>) | undefined, params?: ApiParams) => ApiHookProps;
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 {};