@trackunit/iris-app-runtime-core 0.3.22 → 0.3.23

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.
Files changed (2) hide show
  1. package/index.js +313 -0
  2. package/package.json +5 -4
package/index.js ADDED
@@ -0,0 +1,313 @@
1
+ import { CustomFieldType } from '@trackunit/iris-app-runtime-core-api';
2
+ export * from '@trackunit/iris-app-runtime-core-api';
3
+ import { connectToParent } from 'penpal';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ function __awaiter(thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ }
29
+
30
+ /**
31
+ * Setup using the subscribedMethods to subscribe to events from the host.
32
+ *
33
+ * @param subscribedMethods the methods to subscribe to
34
+ * @returns { Connection<HostConnectorApi> } the connection to the host
35
+ */
36
+ const setupHostConnector = (subscribedMethods) => {
37
+ var _a;
38
+ const methods = Object.assign({ onGlobalSelectionChanged: () => { }, onTokenChanged: () => { }, onAssetSortingStateChanged: () => { } }, subscribedMethods);
39
+ const connection = connectToParent({ methods });
40
+ (_a = connection.promise) === null || _a === void 0 ? void 0 : _a.catch(err => {
41
+ // TODO consider how to handle this catch
42
+ // eslint-disable-next-line no-console
43
+ console.log(err);
44
+ });
45
+ return connection;
46
+ };
47
+ const hostConnector = setupHostConnector({});
48
+ /**
49
+ * Gets the host connector.
50
+ *
51
+ * @returns { Promise<HostConnectorApi> } the connection to the host
52
+ */
53
+ const getHostConnector = () => {
54
+ return hostConnector.promise;
55
+ };
56
+
57
+ const AssetRuntime = {
58
+ getAssetInfo: () => __awaiter(void 0, void 0, void 0, function* () {
59
+ const api = yield getHostConnector();
60
+ return api.getAssetInfo();
61
+ }),
62
+ };
63
+
64
+ const AssetSortingRuntime = {
65
+ getAssetSortingState: () => __awaiter(void 0, void 0, void 0, function* () {
66
+ const api = yield getHostConnector();
67
+ return api.getAssetSortingState();
68
+ }),
69
+ setAssetSortingState: (...args) => __awaiter(void 0, void 0, void 0, function* () {
70
+ const api = yield getHostConnector();
71
+ return api.setAssetSortingState(...args);
72
+ }),
73
+ };
74
+
75
+ const CurrentUserRuntime = {
76
+ getCurrentUserContext: () => __awaiter(void 0, void 0, void 0, function* () {
77
+ const api = yield getHostConnector();
78
+ return api.getCurrentUserContext();
79
+ }),
80
+ getCurrentUserRole: (userIds) => __awaiter(void 0, void 0, void 0, function* () {
81
+ const api = yield getHostConnector();
82
+ return api.getCurrentUserRole(userIds);
83
+ }),
84
+ };
85
+
86
+ /**
87
+ * Get the value of a custom field from a raw value.
88
+ *
89
+ * @param customFieldDefinition the definition of the custom field
90
+ * @param newValue the new value to set
91
+ * @returns { CustomFieldValue } an updated CustomFieldValue
92
+ */
93
+ const getCustomFieldValueFromRawValue = (customFieldDefinition, newValue) => {
94
+ if (customFieldDefinition.type === CustomFieldType.BOOLEAN) {
95
+ return {
96
+ booleanValue: newValue ? true : false,
97
+ type: CustomFieldType.BOOLEAN,
98
+ };
99
+ }
100
+ else if (customFieldDefinition.type === CustomFieldType.DATE) {
101
+ return {
102
+ dateValue: getStringValue(newValue),
103
+ type: CustomFieldType.DATE,
104
+ };
105
+ }
106
+ else if (customFieldDefinition.type === CustomFieldType.DROPDOWN) {
107
+ const stringArrayValue = [];
108
+ if (Array.isArray(newValue)) {
109
+ newValue.forEach(item => {
110
+ if (typeof item === "string") {
111
+ stringArrayValue.push(item);
112
+ }
113
+ else {
114
+ stringArrayValue.push(item === null || item === void 0 ? void 0 : item.value);
115
+ }
116
+ });
117
+ }
118
+ else {
119
+ if (typeof newValue === "string") {
120
+ stringArrayValue.push(newValue);
121
+ }
122
+ else if (typeof newValue === "object") {
123
+ stringArrayValue.push(newValue === null || newValue === void 0 ? void 0 : newValue.value);
124
+ }
125
+ }
126
+ return {
127
+ stringArrayValue: stringArrayValue,
128
+ type: CustomFieldType.DROPDOWN,
129
+ };
130
+ }
131
+ else if (customFieldDefinition.type === CustomFieldType.EMAIL) {
132
+ return {
133
+ stringValue: getStringValue(newValue),
134
+ type: CustomFieldType.EMAIL,
135
+ };
136
+ }
137
+ else if (customFieldDefinition.type === CustomFieldType.NUMBER) {
138
+ let value = customFieldDefinition.isInteger
139
+ ? parseInt(newValue)
140
+ : parseFloat(newValue);
141
+ if (isNaN(value)) {
142
+ value = null;
143
+ }
144
+ return {
145
+ numberValue: value,
146
+ type: CustomFieldType.NUMBER,
147
+ };
148
+ }
149
+ else if (customFieldDefinition.type === CustomFieldType.PHONE_NUMBER) {
150
+ return {
151
+ stringValue: getStringValue(newValue),
152
+ type: CustomFieldType.PHONE_NUMBER,
153
+ };
154
+ }
155
+ else if (customFieldDefinition.type === CustomFieldType.STRING) {
156
+ return {
157
+ stringValue: getStringValue(newValue),
158
+ type: CustomFieldType.STRING,
159
+ };
160
+ }
161
+ else if (customFieldDefinition.type === CustomFieldType.WEB_ADDRESS) {
162
+ return {
163
+ stringValue: getStringValue(newValue),
164
+ type: CustomFieldType.WEB_ADDRESS,
165
+ };
166
+ }
167
+ throw new Error("Unsupported custom field type");
168
+ };
169
+ const getStringValue = (value) => {
170
+ const stringValue = value;
171
+ if (!isNaN(new Date(stringValue).getDate())) {
172
+ return new Date(stringValue).toISOString();
173
+ }
174
+ else if (stringValue === null || stringValue === undefined) {
175
+ return null;
176
+ }
177
+ else if (stringValue.trim().length === 0) {
178
+ return null;
179
+ }
180
+ else {
181
+ return stringValue.trim();
182
+ }
183
+ };
184
+ const CustomFieldRuntime = {
185
+ getCustomFieldsFor: (entity) => __awaiter(void 0, void 0, void 0, function* () {
186
+ const api = yield getHostConnector();
187
+ return api.getCustomFieldsFor(entity);
188
+ }),
189
+ setCustomFieldsFor: (entity, values) => __awaiter(void 0, void 0, void 0, function* () {
190
+ const api = yield getHostConnector();
191
+ return api.setCustomFieldsFor(entity, values);
192
+ }),
193
+ setCustomFieldsFromFormData: (entity, formData, originalDefinitions) => __awaiter(void 0, void 0, void 0, function* () {
194
+ const api = yield getHostConnector();
195
+ const values = [];
196
+ Object.keys(formData).forEach(key => {
197
+ var _a;
198
+ const found = (_a = originalDefinitions.find(originalDefinition => originalDefinition.definition.key === key)) === null || _a === void 0 ? void 0 : _a.definition;
199
+ if (found) {
200
+ values.push({
201
+ definitionKey: key,
202
+ value: getCustomFieldValueFromRawValue(found, formData[key]),
203
+ });
204
+ }
205
+ else {
206
+ throw new Error("Unsupported custom field type: " + key);
207
+ }
208
+ });
209
+ return api.setCustomFieldsFor(entity, values);
210
+ }),
211
+ };
212
+
213
+ const DeveloperSettingsRuntime = {
214
+ getDeveloperSettingsContext: () => __awaiter(void 0, void 0, void 0, function* () {
215
+ const api = yield getHostConnector();
216
+ return api.getDeveloperSettingsContext();
217
+ }),
218
+ };
219
+
220
+ const EnvironmentRuntime = {
221
+ getEnvironmentContext: () => __awaiter(void 0, void 0, void 0, function* () {
222
+ const api = yield getHostConnector();
223
+ return api.getEnvironmentContext();
224
+ }),
225
+ };
226
+
227
+ const GlobalSelectionRuntime = {
228
+ getGlobalSelection: () => __awaiter(void 0, void 0, void 0, function* () {
229
+ const api = yield getHostConnector();
230
+ return api.getGlobalSelectionContext();
231
+ }),
232
+ };
233
+
234
+ const NavigationRuntime = {
235
+ setDeepLink: (props) => __awaiter(void 0, void 0, void 0, function* () {
236
+ const api = yield getHostConnector();
237
+ return api.setDeepLink(props);
238
+ }),
239
+ gotoAssetHome: (assetId, options) => __awaiter(void 0, void 0, void 0, function* () {
240
+ const api = yield getHostConnector();
241
+ return api.gotoAssetHome(assetId, options);
242
+ }),
243
+ };
244
+
245
+ const ParamsRuntime = {
246
+ getAppName: () => __awaiter(void 0, void 0, void 0, function* () {
247
+ const api = yield getHostConnector();
248
+ return api.getAppName();
249
+ }),
250
+ getOrgName: () => __awaiter(void 0, void 0, void 0, function* () {
251
+ const api = yield getHostConnector();
252
+ return api.getOrgName();
253
+ }),
254
+ getExtensionName: () => __awaiter(void 0, void 0, void 0, function* () {
255
+ const api = yield getHostConnector();
256
+ return api.getExtensionName();
257
+ }),
258
+ };
259
+
260
+ const RestRuntime = {
261
+ apiHost: "https://API_HOST",
262
+ request(path, method, requestParams, body, bodyType, secureByDefault) {
263
+ return __awaiter(this, void 0, void 0, function* () {
264
+ const api = yield getHostConnector();
265
+ return api.requestTrackunitRestApi(path, method, requestParams, body, bodyType, secureByDefault);
266
+ });
267
+ },
268
+ };
269
+
270
+ const SiteRuntime = {
271
+ getSiteInfo: () => __awaiter(void 0, void 0, void 0, function* () {
272
+ const api = yield getHostConnector();
273
+ return api.getSiteInfo();
274
+ }),
275
+ };
276
+
277
+ const ToastRuntime = {
278
+ addToast(toast) {
279
+ var _a, _b;
280
+ return __awaiter(this, void 0, void 0, function* () {
281
+ const api = yield getHostConnector();
282
+ return api
283
+ .addToast(Object.assign(Object.assign({}, toast), { primaryAction: (_a = toast.primaryAction) === null || _a === void 0 ? void 0 : _a.label, secondaryAction: (_b = toast.secondaryAction) === null || _b === void 0 ? void 0 : _b.label }))
284
+ .then(res => {
285
+ var _a, _b, _c, _d;
286
+ switch (res) {
287
+ case "primaryAction":
288
+ (_b = (_a = toast.primaryAction) === null || _a === void 0 ? void 0 : _a.onClick) === null || _b === void 0 ? void 0 : _b.call(_a);
289
+ break;
290
+ case "secondaryAction":
291
+ (_d = (_c = toast.secondaryAction) === null || _c === void 0 ? void 0 : _c.onClick) === null || _d === void 0 ? void 0 : _d.call(_c);
292
+ break;
293
+ }
294
+ });
295
+ });
296
+ },
297
+ };
298
+
299
+ const TokenRuntime = {
300
+ getTokenContext: () => __awaiter(void 0, void 0, void 0, function* () {
301
+ const api = yield getHostConnector();
302
+ return api.getTokenContext();
303
+ }),
304
+ };
305
+
306
+ const UserSubscriptionRuntime = {
307
+ getUserSubscriptionContext: () => __awaiter(void 0, void 0, void 0, function* () {
308
+ const api = yield getHostConnector();
309
+ return api.getUserSubscriptionContext();
310
+ }),
311
+ };
312
+
313
+ export { AssetRuntime, AssetSortingRuntime, CurrentUserRuntime, CustomFieldRuntime, DeveloperSettingsRuntime, EnvironmentRuntime, GlobalSelectionRuntime, NavigationRuntime, ParamsRuntime, RestRuntime, SiteRuntime, ToastRuntime, TokenRuntime, UserSubscriptionRuntime, getCustomFieldValueFromRawValue, getHostConnector, setupHostConnector };
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@trackunit/iris-app-runtime-core",
3
- "version": "0.3.22",
3
+ "version": "0.3.23",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
+ "module": "./index.js",
6
7
  "main": "./index.cjs",
7
- "type": "commonjs",
8
+ "type": "module",
8
9
  "types": "./src/index.d.ts",
9
10
  "dependencies": {
10
- "@trackunit/iris-app-runtime-core-api": "0.3.20",
11
- "@trackunit/react-core-contexts-api": "0.2.19"
11
+ "@trackunit/iris-app-runtime-core-api": "0.3.21",
12
+ "@trackunit/react-core-contexts-api": "0.2.20"
12
13
  },
13
14
  "peerDependencies": {}
14
15
  }