nextemos 1.0.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/README.md +20 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +10 -0
- package/dist/useFetch.d.ts +21 -0
- package/dist/useFetch.js +79 -0
- package/dist/useLocalStorage.d.ts +9 -0
- package/dist/useLocalStorage.js +48 -0
- package/package.json +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Introduction
|
|
2
|
+
TODO: Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
|
|
3
|
+
|
|
4
|
+
# Getting Started
|
|
5
|
+
TODO: Guide users through getting your code up and running on their own system. In this section you can talk about:
|
|
6
|
+
1. Installation process
|
|
7
|
+
2. Software dependencies
|
|
8
|
+
3. Latest releases
|
|
9
|
+
4. API references
|
|
10
|
+
|
|
11
|
+
# Build and Test
|
|
12
|
+
TODO: Describe and show how to build your code and run the tests.
|
|
13
|
+
|
|
14
|
+
# Contribute
|
|
15
|
+
TODO: Explain how other users and developers can contribute to make your code better.
|
|
16
|
+
|
|
17
|
+
If you want to learn more about creating good readme files then refer the following [guidelines](https://docs.microsoft.com/en-us/azure/devops/repos/git/create-a-readme?view=azure-devops). You can also seek inspiration from the below readme files:
|
|
18
|
+
- [ASP.NET Core](https://github.com/aspnet/Home)
|
|
19
|
+
- [Visual Studio Code](https://github.com/Microsoft/vscode)
|
|
20
|
+
- [Chakra Core](https://github.com/Microsoft/ChakraCore)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useLocalStorage = exports.useFetch = void 0;
|
|
7
|
+
var useFetch_1 = require("./useFetch");
|
|
8
|
+
Object.defineProperty(exports, "useFetch", { enumerable: true, get: function () { return __importDefault(useFetch_1).default; } });
|
|
9
|
+
var useLocalStorage_1 = require("./useLocalStorage");
|
|
10
|
+
Object.defineProperty(exports, "useLocalStorage", { enumerable: true, get: function () { return __importDefault(useLocalStorage_1).default; } });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for the fetch props.
|
|
3
|
+
*/
|
|
4
|
+
interface IFetchProps {
|
|
5
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
6
|
+
url: string;
|
|
7
|
+
requestOptions?: any;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Custom hook for making HTTP requests using the fetch API.
|
|
11
|
+
*
|
|
12
|
+
* @param {IFetchProps} param0 - The fetch props.
|
|
13
|
+
* @returns {Object} - An object containing the response, loading state, error, and refetch function.
|
|
14
|
+
*/
|
|
15
|
+
declare const useFetch: ({ method, url, requestOptions }: IFetchProps) => {
|
|
16
|
+
response: null;
|
|
17
|
+
loading: boolean;
|
|
18
|
+
error: string | null;
|
|
19
|
+
refetch: () => void;
|
|
20
|
+
};
|
|
21
|
+
export default useFetch;
|
package/dist/useFetch.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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
|
+
const react_1 = require("react");
|
|
13
|
+
/**
|
|
14
|
+
* Custom hook for making HTTP requests using the fetch API.
|
|
15
|
+
*
|
|
16
|
+
* @param {IFetchProps} param0 - The fetch props.
|
|
17
|
+
* @returns {Object} - An object containing the response, loading state, error, and refetch function.
|
|
18
|
+
*/
|
|
19
|
+
const useFetch = ({ method = "GET", url, requestOptions = {} }) => {
|
|
20
|
+
// State for storing the response data.
|
|
21
|
+
const [response, setResponse] = (0, react_1.useState)(null);
|
|
22
|
+
// State for tracking the loading state.
|
|
23
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
24
|
+
// State for storing the error message.
|
|
25
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
26
|
+
// State for triggering a refetch.
|
|
27
|
+
const [reload, setReload] = (0, react_1.useState)(0);
|
|
28
|
+
/**
|
|
29
|
+
* Function for triggering a refetch.
|
|
30
|
+
*/
|
|
31
|
+
const refetch = () => setReload((prev) => prev + 1);
|
|
32
|
+
(0, react_1.useEffect)(() => {
|
|
33
|
+
// Create an AbortController instance to cancel the fetch request if needed.
|
|
34
|
+
const controller = new AbortController();
|
|
35
|
+
const signal = controller.signal;
|
|
36
|
+
/**
|
|
37
|
+
* Function for fetching the data.
|
|
38
|
+
*/
|
|
39
|
+
const fetchData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
setLoading(true);
|
|
41
|
+
try {
|
|
42
|
+
// Make the fetch request with the provided method, URL, and options.
|
|
43
|
+
const res = yield fetch(url, Object.assign(Object.assign({ method: method.toUpperCase() }, requestOptions), { signal }));
|
|
44
|
+
// Check if the response is not OK and throw an error.
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
throw new Error(`Error: ${res.statusText}`);
|
|
47
|
+
}
|
|
48
|
+
// Parse the response data as JSON.
|
|
49
|
+
const data = yield res.json();
|
|
50
|
+
// Update the response state with the fetched data.
|
|
51
|
+
setResponse(data);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
// Handle any errors that occur during the fetch request.
|
|
55
|
+
if (error instanceof Error) {
|
|
56
|
+
setError(error.message);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
setError('An unknown error occurred');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
setLoading(false);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
// Call the fetchData function.
|
|
67
|
+
fetchData();
|
|
68
|
+
// Cleanup function to abort the fetch request if the component unmounts or the dependencies change.
|
|
69
|
+
return () => controller.abort();
|
|
70
|
+
}, [url, method, requestOptions, reload]);
|
|
71
|
+
// Return the response, loading state, error, and refetch function.
|
|
72
|
+
return {
|
|
73
|
+
response,
|
|
74
|
+
loading,
|
|
75
|
+
error,
|
|
76
|
+
refetch
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
exports.default = useFetch;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom hook that synchronizes state with localStorage.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} key - The key to store the value under in localStorage.
|
|
5
|
+
* @param {any} initialValue - The initial value to use if the key does not exist in localStorage.
|
|
6
|
+
* @returns {[any, function]} - The current value stored in localStorage and a function to update it.
|
|
7
|
+
*/
|
|
8
|
+
declare const useLocalStorage: (key: string, initialValue: any) => any[];
|
|
9
|
+
export default useLocalStorage;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook that synchronizes state with localStorage.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} key - The key to store the value under in localStorage.
|
|
8
|
+
* @param {any} initialValue - The initial value to use if the key does not exist in localStorage.
|
|
9
|
+
* @returns {[any, function]} - The current value stored in localStorage and a function to update it.
|
|
10
|
+
*/
|
|
11
|
+
const useLocalStorage = (key, initialValue) => {
|
|
12
|
+
// Initialize the state with a function to read from localStorage or use the initial value
|
|
13
|
+
const [storedValue, setStoredValue] = (0, react_1.useState)(() => {
|
|
14
|
+
try {
|
|
15
|
+
// Attempt to get the item from localStorage using the key
|
|
16
|
+
const item = window.localStorage.getItem(key);
|
|
17
|
+
// If the item exists, parse it from JSON; otherwise, return the initial value
|
|
18
|
+
return item ? JSON.parse(item) : initialValue;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
// Log any errors and return the initial value
|
|
22
|
+
console.error(error);
|
|
23
|
+
return initialValue;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Function to update the state and localStorage.
|
|
28
|
+
*
|
|
29
|
+
* @param {any} value - The new value to store, or a function that returns the new value.
|
|
30
|
+
*/
|
|
31
|
+
const setValue = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
// Determine the new value to store, supporting functional updates
|
|
34
|
+
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
35
|
+
// Update the state with the new value
|
|
36
|
+
setStoredValue(valueToStore);
|
|
37
|
+
// Store the new value in localStorage as a JSON string
|
|
38
|
+
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
// Log any errors that occur during the update
|
|
42
|
+
console.error(error);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
// Return the current value and the function to update it
|
|
46
|
+
return [storedValue, setValue];
|
|
47
|
+
};
|
|
48
|
+
exports.default = useLocalStorage;
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nextemos",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "For helpers and hooks used in NextJS projects",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc"
|
|
17
|
+
},
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/react": "^18.3.3",
|
|
22
|
+
"typescript": "^5.4.5"
|
|
23
|
+
}
|
|
24
|
+
}
|