@izara_frontend/remote-endpoint 1.0.2 → 1.0.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.
- package/package.json +1 -1
- package/src/remoteEndpoint.js +40 -18
package/package.json
CHANGED
package/src/remoteEndpoint.js
CHANGED
|
@@ -3,12 +3,13 @@ import {
|
|
|
3
3
|
} from "@izaraFrontends/store";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
+
* Creates a GET endpoint configuration for fetching an object based on its type.
|
|
7
|
+
* Retrieves the base URL from the store and constructs a query endpoint with POST method.
|
|
6
8
|
*
|
|
7
|
-
* @param {object} objType
|
|
8
|
-
* @param {string}
|
|
9
|
-
* @param {string} objType.objectType
|
|
10
|
-
*
|
|
11
|
-
* @returns {object} query endpoint object
|
|
9
|
+
* @param {object} objType - The object type configuration.
|
|
10
|
+
* @param {string} objType.serviceTag - The service tag identifier.
|
|
11
|
+
* @param {string} objType.objectType - The object type identifier.
|
|
12
|
+
* @returns {object|null} Query endpoint object with name, type, and query configuration, or null if invalid.
|
|
12
13
|
*/
|
|
13
14
|
export function createGetEndpoint(objType) {
|
|
14
15
|
if (!validateObjType(objType)) {
|
|
@@ -48,12 +49,16 @@ export function createGetEndpoint(objType) {
|
|
|
48
49
|
|
|
49
50
|
|
|
50
51
|
/**
|
|
52
|
+
* Creates a custom query endpoint with configurable URL path, method, and cache key.
|
|
53
|
+
* Retrieves the base URL from the store based on provided locations array.
|
|
51
54
|
*
|
|
52
|
-
* @param {
|
|
53
|
-
* @param {string}
|
|
54
|
-
* @param {string}
|
|
55
|
-
*
|
|
56
|
-
* @
|
|
55
|
+
* @param {string[]} baseUrlLocations - Array of strings representing the path to the base URL in the store slice.
|
|
56
|
+
* @param {string} endpointName - The name of the endpoint.
|
|
57
|
+
* @param {string} pathParams - The path parameters to append to the base URL.
|
|
58
|
+
* @param {string} method - The HTTP method (e.g., 'GET', 'POST', 'PUT', 'DELETE').
|
|
59
|
+
* @param {object} options - Optional configuration.
|
|
60
|
+
* @param {object} options.customCacheKey - Custom cache key configuration for query caching.
|
|
61
|
+
* @returns {object|null} Query endpoint object with name, type, and query configuration, or null if base URL not found.
|
|
57
62
|
*/
|
|
58
63
|
export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathParams, method, { customCacheKey = {} } = {}) {
|
|
59
64
|
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations);
|
|
@@ -80,12 +85,14 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
|
|
|
80
85
|
}
|
|
81
86
|
|
|
82
87
|
/**
|
|
88
|
+
* Creates a mutation endpoint for modifying data with configurable URL path and method.
|
|
89
|
+
* Retrieves the base URL from the store based on provided locations array.
|
|
83
90
|
*
|
|
84
|
-
* @param {
|
|
85
|
-
* @param {string}
|
|
86
|
-
* @param {string}
|
|
87
|
-
*
|
|
88
|
-
* @returns {object}
|
|
91
|
+
* @param {string[]} baseUrlLocations - Array of strings representing the path to the base URL in the store slice.
|
|
92
|
+
* @param {string} endpointName - The name of the endpoint.
|
|
93
|
+
* @param {string} pathParams - The path parameters to append to the base URL.
|
|
94
|
+
* @param {string} method - The HTTP method (e.g., 'POST', 'PUT', 'PATCH', 'DELETE').
|
|
95
|
+
* @returns {object|null} Mutation endpoint object with name, type, and query configuration, or null if base URL not found.
|
|
89
96
|
*/
|
|
90
97
|
export function createMutationEndpoint(baseUrlLocations = [], endpointName, pathParams, method) {
|
|
91
98
|
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations)
|
|
@@ -110,6 +117,15 @@ export function createMutationEndpoint(baseUrlLocations = [], endpointName, path
|
|
|
110
117
|
}
|
|
111
118
|
}
|
|
112
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Validates the given object type to ensure it contains valid serviceTag and objectType properties.
|
|
122
|
+
* Both properties must be non-empty strings after trimming.
|
|
123
|
+
*
|
|
124
|
+
* @param {object} objType - The object to validate.
|
|
125
|
+
* @param {string} objType.serviceTag - The service tag string to validate.
|
|
126
|
+
* @param {string} objType.objectType - The object type string to validate.
|
|
127
|
+
* @returns {boolean} True if the object type is valid, otherwise false.
|
|
128
|
+
*/
|
|
113
129
|
function validateObjType(objType) {
|
|
114
130
|
if (objType === null || objType === undefined) {
|
|
115
131
|
return false;
|
|
@@ -141,21 +157,27 @@ function validateObjType(objType) {
|
|
|
141
157
|
}
|
|
142
158
|
}
|
|
143
159
|
|
|
144
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Retrieves the base URL from the store slice based on the provided location path.
|
|
162
|
+
* Traverses the store's rootConfigSlice following the path specified in baseUrlLocations array.
|
|
163
|
+
*
|
|
164
|
+
* @param {string[]} baseUrlLocations - An array of strings representing the path to the base URL in the store slice.
|
|
165
|
+
* @returns {string|null} The base URL if found, otherwise null.
|
|
166
|
+
*/
|
|
145
167
|
function getBaseUrlFromStoreSlice(baseUrlLocations = []) {
|
|
146
168
|
const storeData = store.getState();
|
|
147
169
|
|
|
148
170
|
let baseUrl = null;
|
|
149
171
|
let currentSlice = storeData?.rootConfigSlice
|
|
150
172
|
|
|
151
|
-
for (
|
|
173
|
+
for (let urlLocationIdx = 0; urlLocationIdx < baseUrlLocations.length; urlLocationIdx++) {
|
|
152
174
|
const baseUrlLocation = baseUrlLocations[urlLocationIdx];
|
|
153
175
|
|
|
154
176
|
if (currentSlice && typeof (currentSlice) === 'object') {
|
|
155
177
|
if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx !== baseUrlLocations.length - 1) {
|
|
156
178
|
currentSlice = currentSlice[baseUrlLocation];
|
|
157
179
|
} else if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx === baseUrlLocations.length - 1) {
|
|
158
|
-
if (typeof
|
|
180
|
+
if (typeof currentSlice[baseUrlLocation] === 'string') {
|
|
159
181
|
baseUrl = currentSlice[baseUrlLocation];
|
|
160
182
|
}
|
|
161
183
|
}
|