@izara_frontend/remote-endpoint 1.0.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.
package/README.md ADDED
File without changes
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export {
2
+ createGetEndpoint,
3
+ createQueryEndpoint,
4
+ createMutationEndpoint,
5
+ } from './src/remoteEndpoint.js'
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@izara_frontend/remote-endpoint",
3
+ "version": "1.0.1",
4
+ "description": "create standard frontend query endpoint",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [],
11
+ "author": "Sven Mason <thebarbariansven@gmail.com>",
12
+ "license": "AGPL-3.0-or-later",
13
+ "dependencies": {}
14
+ }
@@ -0,0 +1,179 @@
1
+ import {
2
+ store
3
+ } from "@izaraFrontends/store";
4
+
5
+ /**
6
+ *
7
+ * @param {object} objType
8
+ * @param {string} ojType.serviceTag
9
+ * @param {string} objType.objectType
10
+ *
11
+ * @returns {object} query endpoint object
12
+ */
13
+ export function createGetEndpoint(objType) {
14
+ if (!validateObjType(objType)) {
15
+ return null;
16
+ }
17
+
18
+ const objTypeConcat = `${objType.serviceTag}_${objType.objectType}`;
19
+ const storeData = store.getState();
20
+
21
+ const getEndpoints = storeData?.rootConfigSlice?.rootConfig?.endpoints?.getEndpoints ?? {};
22
+
23
+ const baseUrl = getEndpoints[objTypeConcat];
24
+
25
+ if (baseUrl && typeof (baseUrl) === 'string') {
26
+ return {
27
+ name: `get_${objType.serviceTag}_${objType.objectType}`,
28
+ type: "query",
29
+ query: {
30
+ query: ({ identifiers, headers }) => {
31
+
32
+ console.log("CreateEndpoint", identifiers, headers, baseUrl)
33
+ return {
34
+ url: `https://${baseUrl}.execute-api.us-east-2.amazonaws.com/${objType.serviceTag}/${objType.objectType}/get`,
35
+ method: "POST",
36
+ body: { identifiers },
37
+ headers: headers
38
+ }
39
+ },
40
+ serializeQueryArgs: ({ queryArgs }) => {
41
+ return {
42
+ ...queryArgs?.identifiers,
43
+ };
44
+ }
45
+ }
46
+ }
47
+ } else {
48
+ return null;
49
+ }
50
+ }
51
+
52
+
53
+ /**
54
+ *
55
+ * @param {object} objType
56
+ * @param {string} ojType.serviceTag
57
+ * @param {string} objType.objectType
58
+ *
59
+ * @returns {object} query endpoint object
60
+ */
61
+ 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
+ }
83
+
84
+ if (baseUrl) {
85
+ return {
86
+ name: `${endpointName}`,
87
+ type: "query",
88
+ query: {
89
+ query: ({ body, headers }) => ({
90
+ url: `https://${baseUrlLocations}.execute-api.us-east-2.amazonaws.com/${pathParams}`,
91
+ method: method,
92
+ body: { ...body },
93
+ headers: {
94
+ ...headers
95
+ }
96
+ }),
97
+ ...customCacheKey
98
+ }
99
+ }
100
+ } else {
101
+ return null;
102
+ }
103
+ }
104
+
105
+ /**
106
+ *
107
+ * @param {object} objType
108
+ * @param {string} ojType.serviceTag
109
+ * @param {string} objType.objectType
110
+ *
111
+ * @returns {object} mutation endpoint object
112
+ */
113
+ export function createMutationEndpoint(baseUrlLocations = [], endpointName, pathParams, method) {
114
+ const storeData = store.getState();
115
+
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
+ }
128
+ }
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
+ }
147
+ }
148
+ }
149
+
150
+ function validateObjType(objType) {
151
+ if (objType === null || objType === undefined) {
152
+ return false;
153
+ }
154
+
155
+ const serviceTagValue = objType.serviceTag
156
+ const objectTypeValue = objType.objectType
157
+
158
+ let isValidServiceTag = false
159
+ if (typeof serviceTagValue === "string") {
160
+ const trimmedServiceTag = serviceTagValue.trim()
161
+ if (trimmedServiceTag !== "") {
162
+ isValidServiceTag = true
163
+ }
164
+ }
165
+
166
+ let isValidObjectType = false
167
+ if (typeof objectTypeValue === "string") {
168
+ const trimmedObjectType = objectTypeValue.trim()
169
+ if (trimmedObjectType !== "") {
170
+ isValidObjectType = true
171
+ }
172
+ }
173
+
174
+ if (isValidServiceTag === false || isValidObjectType === false) {
175
+ return false;
176
+ } else {
177
+ return true;
178
+ }
179
+ }