nextemos 2.1.0 → 2.1.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.
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface APIResponse<T = any> {
|
|
2
|
+
data?: T;
|
|
3
|
+
error?: string;
|
|
4
|
+
status: number;
|
|
5
|
+
}
|
|
6
|
+
type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
|
|
7
|
+
interface RequestOptions extends RequestInit {
|
|
8
|
+
url: string;
|
|
9
|
+
method: HTTPMethod;
|
|
10
|
+
params?: Record<string, any>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a HTTP client for making requests.
|
|
14
|
+
* @returns {object} HTTP client with methods for making requests.
|
|
15
|
+
*/
|
|
16
|
+
declare const fetchRequest: () => {
|
|
17
|
+
get: <T>(url: string, options?: Omit<RequestOptions, 'method'>) => Promise<APIResponse<T>>;
|
|
18
|
+
post: <T_1>(url: string, data: any, options?: Omit<RequestOptions, 'method'>) => Promise<APIResponse<T_1>>;
|
|
19
|
+
put: <T_2>(url: string, data: any, options?: Omit<RequestOptions, 'method'>) => Promise<APIResponse<T_2>>;
|
|
20
|
+
delete: <T_3>(url: string, options?: Omit<RequestOptions, 'method'>) => Promise<APIResponse<T_3>>;
|
|
21
|
+
};
|
|
22
|
+
export default fetchRequest;
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
/**
|
|
24
|
+
* Creates a HTTP client for making requests.
|
|
25
|
+
* @returns {object} HTTP client with methods for making requests.
|
|
26
|
+
*/
|
|
27
|
+
const fetchRequest = () => {
|
|
28
|
+
/**
|
|
29
|
+
* Makes an HTTP request using fetch.
|
|
30
|
+
* @template T
|
|
31
|
+
* @param {RequestOptions} options - Request options.
|
|
32
|
+
* @returns {Promise<APIResponse<T>>} Response from the API.
|
|
33
|
+
*/
|
|
34
|
+
const request = (_a) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
var { url, method, params } = _a, requestOptions = __rest(_a, ["url", "method", "params"]);
|
|
36
|
+
const apiURL = new URL(url); // Assuming url already contains apiUrl and apiVersion
|
|
37
|
+
if (params) {
|
|
38
|
+
Object.keys(params).forEach(key => apiURL.searchParams.append(key, params[key]));
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const response = yield fetch(apiURL.toString(), Object.assign(Object.assign({}, requestOptions), { method }));
|
|
42
|
+
const responseData = yield response.json().catch(() => ({}));
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
const errorMessage = (responseData === null || responseData === void 0 ? void 0 : responseData.message) || 'Something went wrong!';
|
|
45
|
+
console.error(`Error: ${response.status} - ${errorMessage}`);
|
|
46
|
+
return { status: response.status, error: errorMessage };
|
|
47
|
+
}
|
|
48
|
+
return { status: response.status, data: responseData };
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error('HTTP request failed:', error);
|
|
52
|
+
return { status: 500, error: 'Internal Server Error' };
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
/**
|
|
56
|
+
* Makes a GET request.
|
|
57
|
+
* @template T
|
|
58
|
+
* @param {string} url - Endpoint URL.
|
|
59
|
+
* @param {RequestOptions} [options] - Request options.
|
|
60
|
+
* @returns {Promise<APIResponse<T>>} Response from the API.
|
|
61
|
+
*/
|
|
62
|
+
const get = (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
return yield request(Object.assign(Object.assign({}, options), { method: 'GET', url }));
|
|
64
|
+
});
|
|
65
|
+
/**
|
|
66
|
+
* Makes a POST request.
|
|
67
|
+
* @template T
|
|
68
|
+
* @param {string} url - Endpoint URL.
|
|
69
|
+
* @param {any} data - Request payload.
|
|
70
|
+
* @param {RequestOptions} [options] - Request options.
|
|
71
|
+
* @returns {Promise<APIResponse<T>>} Response from the API.
|
|
72
|
+
*/
|
|
73
|
+
const post = (url, data, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
|
+
return yield request(Object.assign(Object.assign({}, options), { method: 'POST', url, body: data }));
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* Makes a PUT request.
|
|
78
|
+
* @template T
|
|
79
|
+
* @param {string} url - Endpoint URL.
|
|
80
|
+
* @param {any} data - Request payload.
|
|
81
|
+
* @param {RequestOptions} [options] - Request options.
|
|
82
|
+
* @returns {Promise<APIResponse<T>>} Response from the API.
|
|
83
|
+
*/
|
|
84
|
+
const put = (url, data, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
|
+
return yield request(Object.assign(Object.assign({}, options), { method: 'PUT', url, body: data }));
|
|
86
|
+
});
|
|
87
|
+
/**
|
|
88
|
+
* Makes a DELETE request.
|
|
89
|
+
* @template T
|
|
90
|
+
* @param {string} url - Endpoint URL.
|
|
91
|
+
* @param {RequestOptions} [options] - Request options.
|
|
92
|
+
* @returns {Promise<APIResponse<T>>} Response from the API.
|
|
93
|
+
*/
|
|
94
|
+
const remove = (url, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
|
+
return yield request(Object.assign(Object.assign({}, options), { method: 'DELETE', url }));
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
get,
|
|
99
|
+
post,
|
|
100
|
+
put,
|
|
101
|
+
delete: remove,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
exports.default = fetchRequest;
|