@react-pakistan/util-functions 1.22.18 → 1.22.19

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/hooks/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './use-change';
2
+ export * from './use-fetch';
2
3
  export * from './use-force-render';
3
4
  export * from './use-ip-geo-location';
4
5
  export * from './use-is-mobile';
package/hooks/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./use-change"), exports);
18
+ __exportStar(require("./use-fetch"), exports);
18
19
  __exportStar(require("./use-force-render"), exports);
19
20
  __exportStar(require("./use-ip-geo-location"), exports);
20
21
  __exportStar(require("./use-is-mobile"), exports);
@@ -0,0 +1,15 @@
1
+ import { API_METHODS } from '../constants/api-methods';
2
+ interface Params {
3
+ url: string;
4
+ method: API_METHODS;
5
+ }
6
+ interface Return {
7
+ data: object | null;
8
+ loading: boolean | null;
9
+ error: Error | null;
10
+ fetchData: (b?: string, cb?: (d: object) => void, params?: {
11
+ id: string;
12
+ }) => void;
13
+ }
14
+ export declare const useFetch: ({ url, method, }: Params) => Return;
15
+ export {};
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.useFetch = void 0;
13
+ const react_1 = require("react");
14
+ const api_methods_1 = require("../constants/api-methods");
15
+ const useFetch = ({ url, method, }) => {
16
+ const [data, setData] = (0, react_1.useState)(null);
17
+ const [loading, setLoading] = (0, react_1.useState)(null);
18
+ const [error, setError] = (0, react_1.useState)(null);
19
+ const fetchData = (0, react_1.useCallback)((body, cb, params) => {
20
+ const fetchUrl = (params === null || params === void 0 ? void 0 : params.id) ? `${url}/${params.id}` : url;
21
+ (() => __awaiter(void 0, void 0, void 0, function* () {
22
+ try {
23
+ setLoading(true);
24
+ setData(null);
25
+ setError(null);
26
+ const response = yield fetch(fetchUrl, Object.assign({ method }, (method !== api_methods_1.API_METHODS.GET && { body })));
27
+ const json = yield response.json();
28
+ if (response.status === 200) {
29
+ setLoading(false);
30
+ setData(json.data);
31
+ cb === null || cb === void 0 ? void 0 : cb(json.data);
32
+ }
33
+ }
34
+ catch (err) {
35
+ setLoading(false);
36
+ setError(new Error(err));
37
+ }
38
+ }))();
39
+ }, [method, url]);
40
+ return {
41
+ data,
42
+ loading,
43
+ error,
44
+ fetchData,
45
+ };
46
+ };
47
+ exports.useFetch = useFetch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pakistan/util-functions",
3
- "version": "1.22.18",
3
+ "version": "1.22.19",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {