@izara_frontend/remote-endpoint 1.0.1 → 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 +82 -72
package/package.json
CHANGED
package/src/remoteEndpoint.js
CHANGED
|
@@ -3,24 +3,22 @@ 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)) {
|
|
15
16
|
return null;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const getEndpoints = storeData?.rootConfigSlice?.rootConfig?.endpoints?.getEndpoints ?? {};
|
|
19
|
+
const baseUrlLocations = ['rootConfig', 'endpoints', 'getEndpoints', `${objType.serviceTag}_${objType.objectType}`];
|
|
20
|
+
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations)
|
|
22
21
|
|
|
23
|
-
const baseUrl = getEndpoints[objTypeConcat];
|
|
24
22
|
|
|
25
23
|
if (baseUrl && typeof (baseUrl) === 'string') {
|
|
26
24
|
return {
|
|
@@ -51,35 +49,19 @@ export function createGetEndpoint(objType) {
|
|
|
51
49
|
|
|
52
50
|
|
|
53
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.
|
|
54
54
|
*
|
|
55
|
-
* @param {
|
|
56
|
-
* @param {string}
|
|
57
|
-
* @param {string}
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
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.
|
|
60
62
|
*/
|
|
61
63
|
export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathParams, method, { customCacheKey = {} } = {}) {
|
|
62
|
-
|
|
63
|
-
const storeData = store.getState();
|
|
64
|
-
|
|
65
|
-
let baseUrl = null;
|
|
66
|
-
let currentSlice = storeData?.rootConfigSlice
|
|
67
|
-
|
|
68
|
-
for (let urlLocationIdx = 0; urlLocationIdx > baseUrlLocations.length; urlLocationIdx++) {
|
|
69
|
-
const baseUrlLocation = baseUrlLocations[urlLocationIdx];
|
|
70
|
-
|
|
71
|
-
if (currentSlice && typeof (currentSlice) === 'object') {
|
|
72
|
-
if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx !== baseUrlLocations.length - 1) {
|
|
73
|
-
currentSlice = currentSlice[baseUrlLocation];
|
|
74
|
-
} else if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx === baseUrlLocations.length - 1) {
|
|
75
|
-
if (typeof (currentSlice[baseUrlLocation] === 'string')) {
|
|
76
|
-
baseUrl = currentSlice[baseUrlLocation];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
64
|
+
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations);
|
|
83
65
|
|
|
84
66
|
if (baseUrl) {
|
|
85
67
|
return {
|
|
@@ -87,7 +69,7 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
|
|
|
87
69
|
type: "query",
|
|
88
70
|
query: {
|
|
89
71
|
query: ({ body, headers }) => ({
|
|
90
|
-
url: `https://${
|
|
72
|
+
url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
|
|
91
73
|
method: method,
|
|
92
74
|
body: { ...body },
|
|
93
75
|
headers: {
|
|
@@ -103,50 +85,47 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
|
|
|
103
85
|
}
|
|
104
86
|
|
|
105
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.
|
|
106
90
|
*
|
|
107
|
-
* @param {
|
|
108
|
-
* @param {string}
|
|
109
|
-
* @param {string}
|
|
110
|
-
*
|
|
111
|
-
* @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.
|
|
112
96
|
*/
|
|
113
97
|
export function createMutationEndpoint(baseUrlLocations = [], endpointName, pathParams, method) {
|
|
114
|
-
const
|
|
98
|
+
const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations)
|
|
115
99
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
100
|
+
if (baseUrl) {
|
|
101
|
+
return {
|
|
102
|
+
name: `${endpointName}`,
|
|
103
|
+
type: "mutation",
|
|
104
|
+
query: {
|
|
105
|
+
query: (body, headers = {},) => ({
|
|
106
|
+
url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
|
|
107
|
+
method: method,
|
|
108
|
+
body: { ...body },
|
|
109
|
+
headers: {
|
|
110
|
+
...headers
|
|
111
|
+
}
|
|
112
|
+
}),
|
|
128
113
|
}
|
|
129
|
-
} else {
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
name: `${endpointName}`,
|
|
136
|
-
type: "mutation",
|
|
137
|
-
query: {
|
|
138
|
-
query: (body, headers = {},) => ({
|
|
139
|
-
url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
|
|
140
|
-
method: method,
|
|
141
|
-
body: { ...body },
|
|
142
|
-
headers: {
|
|
143
|
-
...headers
|
|
144
|
-
}
|
|
145
|
-
}),
|
|
146
114
|
}
|
|
115
|
+
} else {
|
|
116
|
+
return null;
|
|
147
117
|
}
|
|
148
118
|
}
|
|
149
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
|
+
*/
|
|
150
129
|
function validateObjType(objType) {
|
|
151
130
|
if (objType === null || objType === undefined) {
|
|
152
131
|
return false;
|
|
@@ -176,4 +155,35 @@ function validateObjType(objType) {
|
|
|
176
155
|
} else {
|
|
177
156
|
return true;
|
|
178
157
|
}
|
|
158
|
+
}
|
|
159
|
+
|
|
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
|
+
*/
|
|
167
|
+
function getBaseUrlFromStoreSlice(baseUrlLocations = []) {
|
|
168
|
+
const storeData = store.getState();
|
|
169
|
+
|
|
170
|
+
let baseUrl = null;
|
|
171
|
+
let currentSlice = storeData?.rootConfigSlice
|
|
172
|
+
|
|
173
|
+
for (let urlLocationIdx = 0; urlLocationIdx < baseUrlLocations.length; urlLocationIdx++) {
|
|
174
|
+
const baseUrlLocation = baseUrlLocations[urlLocationIdx];
|
|
175
|
+
|
|
176
|
+
if (currentSlice && typeof (currentSlice) === 'object') {
|
|
177
|
+
if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx !== baseUrlLocations.length - 1) {
|
|
178
|
+
currentSlice = currentSlice[baseUrlLocation];
|
|
179
|
+
} else if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx === baseUrlLocations.length - 1) {
|
|
180
|
+
if (typeof currentSlice[baseUrlLocation] === 'string') {
|
|
181
|
+
baseUrl = currentSlice[baseUrlLocation];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return baseUrl;
|
|
179
189
|
}
|