@izara_frontend/remote-endpoint 1.0.1 → 1.0.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_frontend/remote-endpoint",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "create standard frontend query endpoint",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -15,12 +15,9 @@ export function createGetEndpoint(objType) {
15
15
  return null;
16
16
  }
17
17
 
18
- const objTypeConcat = `${objType.serviceTag}_${objType.objectType}`;
19
- const storeData = store.getState();
20
-
21
- const getEndpoints = storeData?.rootConfigSlice?.rootConfig?.endpoints?.getEndpoints ?? {};
18
+ const baseUrlLocations = ['rootConfig', 'endpoints', 'getEndpoints', `${objType.serviceTag}_${objType.objectType}`];
19
+ const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations)
22
20
 
23
- const baseUrl = getEndpoints[objTypeConcat];
24
21
 
25
22
  if (baseUrl && typeof (baseUrl) === 'string') {
26
23
  return {
@@ -59,27 +56,7 @@ export function createGetEndpoint(objType) {
59
56
  * @returns {object} query endpoint object
60
57
  */
61
58
  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
- }
59
+ const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations);
83
60
 
84
61
  if (baseUrl) {
85
62
  return {
@@ -87,7 +64,7 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
87
64
  type: "query",
88
65
  query: {
89
66
  query: ({ body, headers }) => ({
90
- url: `https://${baseUrlLocations}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
67
+ url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
91
68
  method: method,
92
69
  body: { ...body },
93
70
  headers: {
@@ -111,39 +88,25 @@ export function createQueryEndpoint(baseUrlLocations = [], endpointName, pathPar
111
88
  * @returns {object} mutation endpoint object
112
89
  */
113
90
  export function createMutationEndpoint(baseUrlLocations = [], endpointName, pathParams, method) {
114
- const storeData = store.getState();
91
+ const baseUrl = getBaseUrlFromStoreSlice(baseUrlLocations)
115
92
 
116
- let baseUrl = null;
117
- let currentSlice = storeData?.rootConfigSlice
118
- for (let urlLocationIdx = 0; urlLocationIdx > baseUrlLocations.length; urlLocationIdx++) {
119
- const baseUrlLocation = baseUrlLocations[urlLocationIdx];
120
-
121
- if (currentSlice && typeof (currentSlice) === 'object') {
122
- if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx !== baseUrlLocations.length - 1) {
123
- currentSlice = currentSlice[baseUrlLocation];
124
- } else if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx === baseUrlLocations.length - 1) {
125
- if (typeof (currentSlice[baseUrlLocation] === 'string')) {
126
- baseUrl = currentSlice[baseUrlLocation];
127
- }
93
+ if (baseUrl) {
94
+ return {
95
+ name: `${endpointName}`,
96
+ type: "mutation",
97
+ query: {
98
+ query: (body, headers = {},) => ({
99
+ url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
100
+ method: method,
101
+ body: { ...body },
102
+ headers: {
103
+ ...headers
104
+ }
105
+ }),
128
106
  }
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
107
  }
108
+ } else {
109
+ return null;
147
110
  }
148
111
  }
149
112
 
@@ -176,4 +139,29 @@ function validateObjType(objType) {
176
139
  } else {
177
140
  return true;
178
141
  }
142
+ }
143
+
144
+
145
+ function getBaseUrlFromStoreSlice(baseUrlLocations = []) {
146
+ const storeData = store.getState();
147
+
148
+ let baseUrl = null;
149
+ let currentSlice = storeData?.rootConfigSlice
150
+
151
+ for (const urlLocationIdx = 0; urlLocationIdx < baseUrlLocations.length; urlLocationIdx++) {
152
+ const baseUrlLocation = baseUrlLocations[urlLocationIdx];
153
+
154
+ if (currentSlice && typeof (currentSlice) === 'object') {
155
+ if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx !== baseUrlLocations.length - 1) {
156
+ currentSlice = currentSlice[baseUrlLocation];
157
+ } else if (currentSlice.hasOwnProperty(baseUrlLocation) && urlLocationIdx === baseUrlLocations.length - 1) {
158
+ if (typeof (currentSlice[baseUrlLocation] === 'string')) {
159
+ baseUrl = currentSlice[baseUrlLocation];
160
+ }
161
+ }
162
+ } else {
163
+ break;
164
+ }
165
+ }
166
+ return baseUrl;
179
167
  }