nextemos 2.3.1 → 2.3.3

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.
@@ -43,9 +43,12 @@ const fetchRequest = () => {
43
43
  const response = yield fetch(apiURL.toString(), Object.assign(Object.assign({}, options), { method }));
44
44
  // Headers nesnesini Object'e dönüştürme
45
45
  const headers = {};
46
- response.headers.forEach((value, name) => {
47
- headers[name] = value;
48
- });
46
+ if (response.headers) {
47
+ response.headers.forEach((value, name) => {
48
+ headers[name] = value;
49
+ });
50
+ }
51
+ // response data nesnesini JSON'a dönüştürme.
49
52
  const responseData = yield response.json().catch(() => ({}));
50
53
  if (!response.ok) {
51
54
  const errorMessage = (responseData === null || responseData === void 0 ? void 0 : responseData.message) || 'Bir şeyler yanlış gitti!';
@@ -10,12 +10,15 @@ interface IFetchProps {
10
10
  * Custom hook for making HTTP requests using the fetch API.
11
11
  *
12
12
  * @param {IFetchProps} param0 - The fetch props.
13
- * @returns {Object} - An object containing the response, loading state, error, and refetch function.
13
+ * @returns {Object} - An object containing the response, headers, loading state, error, and refetch function.
14
14
  */
15
15
  declare const useFetch: <T>({ method, url, requestOptions }: IFetchProps) => {
16
16
  response: T | null;
17
17
  loading: boolean;
18
18
  error: string | null;
19
+ headers: {
20
+ [key: string]: string;
21
+ };
19
22
  refetch: () => void;
20
23
  };
21
24
  export default useFetch;
@@ -14,7 +14,7 @@ const react_1 = require("react");
14
14
  * Custom hook for making HTTP requests using the fetch API.
15
15
  *
16
16
  * @param {IFetchProps} param0 - The fetch props.
17
- * @returns {Object} - An object containing the response, loading state, error, and refetch function.
17
+ * @returns {Object} - An object containing the response, headers, loading state, error, and refetch function.
18
18
  */
19
19
  const useFetch = ({ method = "GET", url, requestOptions = {} }) => {
20
20
  // State for storing the response data.
@@ -23,6 +23,8 @@ const useFetch = ({ method = "GET", url, requestOptions = {} }) => {
23
23
  const [loading, setLoading] = (0, react_1.useState)(true);
24
24
  // State for storing the error message.
25
25
  const [error, setError] = (0, react_1.useState)(null);
26
+ // State for storing the response headers.
27
+ const [headers, setHeader] = (0, react_1.useState)({});
26
28
  // State for triggering a refetch.
27
29
  const [reload, setReload] = (0, react_1.useState)(0);
28
30
  /**
@@ -45,6 +47,12 @@ const useFetch = ({ method = "GET", url, requestOptions = {} }) => {
45
47
  if (!res.ok) {
46
48
  throw new Error(`Error: ${res.statusText}`);
47
49
  }
50
+ // Parse the response headers as JSON
51
+ const _headers = {};
52
+ res.headers.forEach((value, name) => {
53
+ _headers[name] = value;
54
+ });
55
+ setHeader(_headers);
48
56
  // Parse the response data as JSON.
49
57
  const data = yield res.json();
50
58
  // Update the response state with the fetched data.
@@ -73,6 +81,7 @@ const useFetch = ({ method = "GET", url, requestOptions = {} }) => {
73
81
  response,
74
82
  loading,
75
83
  error,
84
+ headers,
76
85
  refetch
77
86
  };
78
87
  };
package/dist/test.js CHANGED
@@ -1,8 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hooks_1 = require("./hooks");
3
4
  const services_1 = require("./services");
4
5
  function test() {
5
6
  const res = services_1.Service.Blog.GetBlogCategoryTreeRequest({ categoryId: 11 });
7
+ const resp = (0, hooks_1.useFetch)({
8
+ url: 'https://api.example.com/blog/categories',
9
+ method: 'GET',
10
+ requestOptions: {
11
+ headers: {
12
+ 'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
13
+ },
14
+ },
15
+ });
6
16
  return;
7
17
  }
8
18
  exports.default = test;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextemos",
3
- "version": "2.3.1",
3
+ "version": "2.3.3",
4
4
  "description": "For helpers and hooks used in NextJS projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",