@izara_frontend/remote-endpoint 1.0.2 → 1.0.4
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 +47 -22
package/package.json
CHANGED
package/src/remoteEndpoint.js
CHANGED
|
@@ -3,14 +3,15 @@ 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
|
-
export function createGetEndpoint(objType) {
|
|
14
|
+
export function createGetEndpoint(objType, { customCacheKey = {} } = {}) {
|
|
14
15
|
if (!validateObjType(objType)) {
|
|
15
16
|
return null;
|
|
16
17
|
}
|
|
@@ -34,11 +35,13 @@ export function createGetEndpoint(objType) {
|
|
|
34
35
|
headers: headers
|
|
35
36
|
}
|
|
36
37
|
},
|
|
38
|
+
keepUnusedDataFor: 3600,
|
|
37
39
|
serializeQueryArgs: ({ queryArgs }) => {
|
|
38
40
|
return {
|
|
39
41
|
...queryArgs?.identifiers,
|
|
40
42
|
};
|
|
41
|
-
}
|
|
43
|
+
},
|
|
44
|
+
...customCacheKey
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
47
|
} else {
|
|
@@ -48,12 +51,16 @@ export function createGetEndpoint(objType) {
|
|
|
48
51
|
|
|
49
52
|
|
|
50
53
|
/**
|
|
54
|
+
* Creates a custom query endpoint with configurable URL path, method, and cache key.
|
|
55
|
+
* Retrieves the base URL from the store based on provided locations array.
|
|
51
56
|
*
|
|
52
|
-
* @param {
|
|
53
|
-
* @param {string}
|
|
54
|
-
* @param {string}
|
|
55
|
-
*
|
|
56
|
-
* @
|
|
57
|
+
* @param {string[]} baseUrlLocations - Array of strings representing the path to the base URL in the store slice.
|
|
58
|
+
* @param {string} endpointName - The name of the endpoint.
|
|
59
|
+
* @param {string} pathParams - The path parameters to append to the base URL.
|
|
60
|
+
* @param {string} method - The HTTP method (e.g., 'GET', 'POST', 'PUT', 'DELETE').
|
|
61
|
+
* @param {object} options - Optional configuration.
|
|
62
|
+
* @param {object} options.customCacheKey - Custom cache key configuration for query caching.
|
|
63
|
+
* @returns {object|null} Query endpoint object with name, type, and query configuration, or null if base URL not found.
|
|
57
64
|
*/
|
|
58
65
|
export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathParams, method, { customCacheKey = {} } = {}) {
|
|
59
66
|
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations);
|
|
@@ -63,7 +70,7 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
|
|
|
63
70
|
name: `${endpointName}`,
|
|
64
71
|
type: "query",
|
|
65
72
|
query: {
|
|
66
|
-
query: ({ body, headers }) => ({
|
|
73
|
+
query: ({ body, headers = {} }) => ({
|
|
67
74
|
url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
|
|
68
75
|
method: method,
|
|
69
76
|
body: { ...body },
|
|
@@ -71,6 +78,7 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
|
|
|
71
78
|
...headers
|
|
72
79
|
}
|
|
73
80
|
}),
|
|
81
|
+
keepUnusedDataFor: 3600,
|
|
74
82
|
...customCacheKey
|
|
75
83
|
}
|
|
76
84
|
}
|
|
@@ -80,12 +88,14 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
|
|
|
80
88
|
}
|
|
81
89
|
|
|
82
90
|
/**
|
|
91
|
+
* Creates a mutation endpoint for modifying data with configurable URL path and method.
|
|
92
|
+
* Retrieves the base URL from the store based on provided locations array.
|
|
83
93
|
*
|
|
84
|
-
* @param {
|
|
85
|
-
* @param {string}
|
|
86
|
-
* @param {string}
|
|
87
|
-
*
|
|
88
|
-
* @returns {object}
|
|
94
|
+
* @param {string[]} baseUrlLocations - Array of strings representing the path to the base URL in the store slice.
|
|
95
|
+
* @param {string} endpointName - The name of the endpoint.
|
|
96
|
+
* @param {string} pathParams - The path parameters to append to the base URL.
|
|
97
|
+
* @param {string} method - The HTTP method (e.g., 'POST', 'PUT', 'PATCH', 'DELETE').
|
|
98
|
+
* @returns {object|null} Mutation endpoint object with name, type, and query configuration, or null if base URL not found.
|
|
89
99
|
*/
|
|
90
100
|
export function createMutationEndpoint(baseUrlLocations = [], endpointName, pathParams, method) {
|
|
91
101
|
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations)
|
|
@@ -95,7 +105,7 @@ export function createMutationEndpoint(baseUrlLocations = [], endpointName, path
|
|
|
95
105
|
name: `${endpointName}`,
|
|
96
106
|
type: "mutation",
|
|
97
107
|
query: {
|
|
98
|
-
query: (body, headers = {}
|
|
108
|
+
query: ({ body, headers = {} }) => ({
|
|
99
109
|
url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
|
|
100
110
|
method: method,
|
|
101
111
|
body: { ...body },
|
|
@@ -110,6 +120,15 @@ export function createMutationEndpoint(baseUrlLocations = [], endpointName, path
|
|
|
110
120
|
}
|
|
111
121
|
}
|
|
112
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Validates the given object type to ensure it contains valid serviceTag and objectType properties.
|
|
125
|
+
* Both properties must be non-empty strings after trimming.
|
|
126
|
+
*
|
|
127
|
+
* @param {object} objType - The object to validate.
|
|
128
|
+
* @param {string} objType.serviceTag - The service tag string to validate.
|
|
129
|
+
* @param {string} objType.objectType - The object type string to validate.
|
|
130
|
+
* @returns {boolean} True if the object type is valid, otherwise false.
|
|
131
|
+
*/
|
|
113
132
|
function validateObjType(objType) {
|
|
114
133
|
if (objType === null || objType === undefined) {
|
|
115
134
|
return false;
|
|
@@ -141,21 +160,27 @@ function validateObjType(objType) {
|
|
|
141
160
|
}
|
|
142
161
|
}
|
|
143
162
|
|
|
144
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Retrieves the base URL from the store slice based on the provided location path.
|
|
165
|
+
* Traverses the store's rootConfigSlice following the path specified in baseUrlLocations array.
|
|
166
|
+
*
|
|
167
|
+
* @param {string[]} baseUrlLocations - An array of strings representing the path to the base URL in the store slice.
|
|
168
|
+
* @returns {string|null} The base URL if found, otherwise null.
|
|
169
|
+
*/
|
|
145
170
|
function getBaseUrlFromStoreSlice(baseUrlLocations = []) {
|
|
146
171
|
const storeData = store.getState();
|
|
147
172
|
|
|
148
173
|
let baseUrl = null;
|
|
149
174
|
let currentSlice = storeData?.rootConfigSlice
|
|
150
175
|
|
|
151
|
-
for (
|
|
176
|
+
for (let urlLocationIdx = 0; urlLocationIdx < baseUrlLocations.length; urlLocationIdx++) {
|
|
152
177
|
const baseUrlLocation = baseUrlLocations[urlLocationIdx];
|
|
153
178
|
|
|
154
179
|
if (currentSlice && typeof (currentSlice) === 'object') {
|
|
155
180
|
if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx !== baseUrlLocations.length - 1) {
|
|
156
181
|
currentSlice = currentSlice[baseUrlLocation];
|
|
157
182
|
} else if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx === baseUrlLocations.length - 1) {
|
|
158
|
-
if (typeof
|
|
183
|
+
if (typeof currentSlice[baseUrlLocation] === 'string') {
|
|
159
184
|
baseUrl = currentSlice[baseUrlLocation];
|
|
160
185
|
}
|
|
161
186
|
}
|