@halix/action-sdk 1.0.19 → 1.0.21
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/lib/cjs/content.js +257 -0
- package/lib/cjs/data-crud.js +297 -0
- package/lib/cjs/index.js +53 -678
- package/lib/cjs/lists.js +175 -0
- package/lib/cjs/sdk-general.js +92 -0
- package/lib/cjs/types/content.d.ts +119 -0
- package/lib/cjs/types/content.d.ts.map +1 -0
- package/lib/cjs/types/data-crud.d.ts +156 -0
- package/lib/cjs/types/data-crud.d.ts.map +1 -0
- package/lib/cjs/types/index.d.ts +8 -578
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/lists.d.ts +212 -0
- package/lib/cjs/types/lists.d.ts.map +1 -0
- package/lib/cjs/types/sdk-general.d.ts +263 -0
- package/lib/cjs/types/sdk-general.d.ts.map +1 -0
- package/lib/cjs/types/utilities.d.ts +73 -0
- package/lib/cjs/types/utilities.d.ts.map +1 -0
- package/lib/cjs/utilities.js +131 -0
- package/lib/esm/content.js +228 -0
- package/lib/esm/content.js.map +1 -0
- package/lib/esm/data-crud.js +264 -0
- package/lib/esm/data-crud.js.map +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/index.mjs +26 -673
- package/lib/esm/lists.js +157 -0
- package/lib/esm/lists.js.map +1 -0
- package/lib/esm/sdk-general.js +138 -0
- package/lib/esm/sdk-general.js.map +1 -0
- package/lib/esm/types/content.d.ts +118 -0
- package/lib/esm/types/data-crud.d.ts +155 -0
- package/lib/esm/types/index.d.ts +8 -578
- package/lib/esm/types/lists.d.ts +211 -0
- package/lib/esm/types/sdk-general.d.ts +262 -0
- package/lib/esm/types/utilities.d.ts +72 -0
- package/lib/esm/utilities.js +126 -0
- package/lib/esm/utilities.js.map +1 -0
- package/package.json +12 -1
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Halix SDK License v1.0
|
|
3
|
+
// Copyright (c) 2025 halix.io LLC.
|
|
4
|
+
//
|
|
5
|
+
// This source code is licensed for use **only** within applications
|
|
6
|
+
// running on the Halix platform, in accordance with Halix SDK guidelines.
|
|
7
|
+
//
|
|
8
|
+
// Unauthorized use outside the Halix platform is prohibited.
|
|
9
|
+
// Full license terms available in the LICENSE file.
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
20
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.getOrCreateResource = getOrCreateResource;
|
|
24
|
+
exports.getOrCreateResourceAsObservable = getOrCreateResourceAsObservable;
|
|
25
|
+
exports.saveResource = saveResource;
|
|
26
|
+
exports.saveResourceAsObservable = saveResourceAsObservable;
|
|
27
|
+
exports.sendFileContents = sendFileContents;
|
|
28
|
+
exports.sendFileContentsAsObservable = sendFileContentsAsObservable;
|
|
29
|
+
exports.createOrUpdateResource = createOrUpdateResource;
|
|
30
|
+
exports.createOrUpdateResourceAsObservable = createOrUpdateResourceAsObservable;
|
|
31
|
+
/**
|
|
32
|
+
* @module @halix/action-sdk/content
|
|
33
|
+
* @description Content resource management functions for the Halix Platform action SDK. This module
|
|
34
|
+
* handles file uploads, content resources, and file storage operations.
|
|
35
|
+
*
|
|
36
|
+
* Key features:
|
|
37
|
+
* - Retrieve content resources (images, documents, etc.)
|
|
38
|
+
* - Upload/save content resources (images, documents, etc.)
|
|
39
|
+
*/
|
|
40
|
+
const axios_1 = __importDefault(require("axios"));
|
|
41
|
+
const rxjs_1 = require("rxjs");
|
|
42
|
+
const sdk_general_1 = require("./sdk-general");
|
|
43
|
+
// ================================================================================
|
|
44
|
+
// CONTENT RESOURCE FUNCTIONS
|
|
45
|
+
// ================================================================================
|
|
46
|
+
/**
|
|
47
|
+
* getOrCreateResource retrieves an existing content resource by its key, or creates a new one
|
|
48
|
+
* if the key is not provided. If a resource key is provided, it attempts to fetch the existing
|
|
49
|
+
* resource from the server. If no key is provided, it creates a new resource with the specified
|
|
50
|
+
* properties.
|
|
51
|
+
*
|
|
52
|
+
* @param resourceKey - Optional key of the existing resource to retrieve
|
|
53
|
+
* @param fileToUpload - Optional file or blob to upload
|
|
54
|
+
* @param publicFlag - Whether the resource should be public
|
|
55
|
+
* @param resourceType - The type of resource
|
|
56
|
+
* @param tags - Array of tags for the resource
|
|
57
|
+
*
|
|
58
|
+
* @returns Promise resolving to a ContentResource
|
|
59
|
+
*/
|
|
60
|
+
function getOrCreateResource(resourceKey, fileToUpload, publicFlag, resourceType, tags) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
if (!sdk_general_1.userContext) {
|
|
63
|
+
throw new Error("userContext is required but not available; check that the initialize function has been called");
|
|
64
|
+
}
|
|
65
|
+
if (resourceKey) {
|
|
66
|
+
let url = `${sdk_general_1.serviceAddress}/sandboxes/${sdk_general_1.sandboxKey}/contentResource/${resourceKey}`;
|
|
67
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
68
|
+
console.log("Sending GET request to " + url + " with token " + authToken);
|
|
69
|
+
let response = yield axios_1.default.get(url, {
|
|
70
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
71
|
+
});
|
|
72
|
+
let resource = response.data;
|
|
73
|
+
if (fileToUpload) {
|
|
74
|
+
resource.contentType = fileToUpload.type;
|
|
75
|
+
// Null out the name and extension; the server will set these if they are blank
|
|
76
|
+
resource.name = null;
|
|
77
|
+
resource.extension = null;
|
|
78
|
+
}
|
|
79
|
+
return resource;
|
|
80
|
+
}
|
|
81
|
+
let newResource = {
|
|
82
|
+
isPublic: publicFlag,
|
|
83
|
+
resourceType: resourceType,
|
|
84
|
+
tags: tags,
|
|
85
|
+
organizationKey: sdk_general_1.userContext.orgKey,
|
|
86
|
+
sandboxKey: sdk_general_1.sandboxKey,
|
|
87
|
+
userKey: sdk_general_1.userContext.user.objKey
|
|
88
|
+
};
|
|
89
|
+
if (fileToUpload) {
|
|
90
|
+
newResource.contentType = fileToUpload.type;
|
|
91
|
+
// Null out the name and extension; the server will set these if they are blank
|
|
92
|
+
newResource.name = null;
|
|
93
|
+
newResource.extension = null;
|
|
94
|
+
}
|
|
95
|
+
return newResource;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* getOrCreateResourceAsObservable retrieves an existing content resource by its key, or creates a new one
|
|
100
|
+
* if the key is not provided. If a resource key is provided, it attempts to fetch the existing
|
|
101
|
+
* resource from the server. If no key is provided, it creates a new resource with the specified
|
|
102
|
+
* properties.
|
|
103
|
+
*
|
|
104
|
+
* @param resourceKey - Optional key of the existing resource to retrieve
|
|
105
|
+
* @param fileToUpload - Optional file or blob to upload
|
|
106
|
+
* @param publicFlag - Whether the resource should be public
|
|
107
|
+
* @param resourceType - The type of resource
|
|
108
|
+
* @param tags - Array of tags for the resource
|
|
109
|
+
*
|
|
110
|
+
* @returns Observable resolving to a ContentResource
|
|
111
|
+
*/
|
|
112
|
+
function getOrCreateResourceAsObservable(resourceKey, fileToUpload, publicFlag, resourceType, tags) {
|
|
113
|
+
return (0, rxjs_1.from)(getOrCreateResource(resourceKey, fileToUpload, publicFlag, resourceType, tags));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* saveResource saves a content resource to the server. The resource is saved with appropriate
|
|
117
|
+
* ownership parameters based on the current context (solution builder vs regular organization view).
|
|
118
|
+
*
|
|
119
|
+
* @param resource - The ContentResource to save
|
|
120
|
+
*
|
|
121
|
+
* @returns Promise resolving to the saved ContentResource
|
|
122
|
+
*/
|
|
123
|
+
function saveResource(resource) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
if (!sdk_general_1.userContext) {
|
|
126
|
+
throw new Error("userContext is required but not available; check that the initialize function has been called");
|
|
127
|
+
}
|
|
128
|
+
let params = {};
|
|
129
|
+
if (sdk_general_1.userContext.orgProxy.objType === "Solution") {
|
|
130
|
+
// When in the solution builder view, content is owned by the solution. The solution key is the org proxy key in
|
|
131
|
+
// the builder view.
|
|
132
|
+
params.solutionKey = sdk_general_1.userContext.orgProxyKey;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
params.organizationKey = sdk_general_1.userContext.orgKey;
|
|
136
|
+
params.userKey = sdk_general_1.userContext.user.objKey;
|
|
137
|
+
}
|
|
138
|
+
let url = `${sdk_general_1.serviceAddress}/sandboxes/${sdk_general_1.sandboxKey}/contentResource`;
|
|
139
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
140
|
+
console.log("Sending POST request to " + url + " with token " + authToken);
|
|
141
|
+
let response = yield axios_1.default.post(url, JSON.stringify(resource), {
|
|
142
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
143
|
+
params: params,
|
|
144
|
+
});
|
|
145
|
+
return response.data;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* saveResourceAsObservable saves a content resource to the server. The resource is saved with appropriate
|
|
150
|
+
* ownership parameters based on the current context (solution builder vs regular organization view).
|
|
151
|
+
*
|
|
152
|
+
* @param resource - The ContentResource to save
|
|
153
|
+
*
|
|
154
|
+
* @returns Observable resolving to the saved ContentResource
|
|
155
|
+
*/
|
|
156
|
+
function saveResourceAsObservable(resource) {
|
|
157
|
+
return (0, rxjs_1.from)(saveResource(resource));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* sendFileContents uploads file contents to the server for a specific resource. The file is uploaded
|
|
161
|
+
* via FormData with the appropriate scope and public flag settings.
|
|
162
|
+
*
|
|
163
|
+
* @param resourceKey - The key of the resource to upload file contents for
|
|
164
|
+
* @param fileToUpload - The file or blob to upload
|
|
165
|
+
* @param publicFlag - Whether the file should be public
|
|
166
|
+
*
|
|
167
|
+
* @returns Promise resolving to true if upload was successful
|
|
168
|
+
*/
|
|
169
|
+
function sendFileContents(resourceKey, fileToUpload, publicFlag) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
if (!sdk_general_1.userContext) {
|
|
172
|
+
throw new Error("userContext is required but not available; check that the initialize function has been called");
|
|
173
|
+
}
|
|
174
|
+
let url = `${sdk_general_1.serviceAddress}/filecontent/${sdk_general_1.sandboxKey}/${resourceKey}`;
|
|
175
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
176
|
+
console.log("Sending file upload request to " + url + " with token " + authToken);
|
|
177
|
+
let formData = new FormData();
|
|
178
|
+
formData.append("fileUpload", fileToUpload);
|
|
179
|
+
formData.append("scopeKeyPath", sdk_general_1.userContext.orgProxyKey);
|
|
180
|
+
formData.append("public", String(publicFlag));
|
|
181
|
+
let response = yield axios_1.default.post(url, formData, {
|
|
182
|
+
headers: {
|
|
183
|
+
"Authorization": `Bearer ${authToken}`,
|
|
184
|
+
"Content-Type": "multipart/form-data"
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
return response.status === 204;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* sendFileContentsAsObservable uploads file contents to the server for a specific resource. The file is uploaded
|
|
192
|
+
* via FormData with the appropriate scope and public flag settings.
|
|
193
|
+
*
|
|
194
|
+
* @param resourceKey - The key of the resource to upload file contents for
|
|
195
|
+
* @param fileToUpload - The file or blob to upload
|
|
196
|
+
* @param publicFlag - Whether the file should be public
|
|
197
|
+
*
|
|
198
|
+
* @returns Observable resolving to true if upload was successful
|
|
199
|
+
*/
|
|
200
|
+
function sendFileContentsAsObservable(resourceKey, fileToUpload, publicFlag) {
|
|
201
|
+
return (0, rxjs_1.from)(sendFileContents(resourceKey, fileToUpload, publicFlag));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* createOrUpdateResource creates a new content resource or updates an existing one, then uploads
|
|
205
|
+
* the file contents to that resource. If a resourceKey is provided, it updates the existing resource;
|
|
206
|
+
* otherwise, it creates a new resource and uploads the file to the newly created resource.
|
|
207
|
+
*
|
|
208
|
+
* @param resourceKey - Optional key of the existing resource to update; if not provided, a new resource is created
|
|
209
|
+
* @param fileToUpload - The file or blob to upload
|
|
210
|
+
* @param publicFlag - Whether the resource should be public
|
|
211
|
+
* @param resourceType - The type of resource
|
|
212
|
+
* @param tags - Array of tags for the resource
|
|
213
|
+
*
|
|
214
|
+
* @returns Promise resolving to the ContentResource with uploaded file
|
|
215
|
+
*/
|
|
216
|
+
function createOrUpdateResource(resourceKey, fileToUpload, publicFlag, resourceType, tags) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
if (!sdk_general_1.userContext) {
|
|
219
|
+
throw new Error("userContext is required but not available; check that the initialize function has been called");
|
|
220
|
+
}
|
|
221
|
+
// Get or create the resource
|
|
222
|
+
let resource = yield getOrCreateResource(resourceKey, fileToUpload, publicFlag, resourceType, tags);
|
|
223
|
+
// Save the resource to get the objKey if it's new
|
|
224
|
+
let savedResource = yield saveResource(resource);
|
|
225
|
+
// Upload the file contents
|
|
226
|
+
if (!savedResource.objKey) {
|
|
227
|
+
throw new Error("Resource was saved but no objKey was returned");
|
|
228
|
+
}
|
|
229
|
+
let uploadSuccess = yield sendFileContents(savedResource.objKey, fileToUpload, publicFlag);
|
|
230
|
+
if (!uploadSuccess) {
|
|
231
|
+
throw new Error("Failed to upload file contents");
|
|
232
|
+
}
|
|
233
|
+
// Get the updated resource with file metadata
|
|
234
|
+
let updatedResource = yield getOrCreateResource(savedResource.objKey, fileToUpload, publicFlag, resourceType, []);
|
|
235
|
+
// Set the name if it's not set and we have a File with a name
|
|
236
|
+
if (updatedResource && !updatedResource.name && fileToUpload instanceof File) {
|
|
237
|
+
updatedResource.name = fileToUpload.name;
|
|
238
|
+
}
|
|
239
|
+
return updatedResource;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* createOrUpdateResourceAsObservable creates a new content resource or updates an existing one, then uploads
|
|
244
|
+
* the file contents to that resource. If a resourceKey is provided, it updates the existing resource;
|
|
245
|
+
* otherwise, it creates a new resource and uploads the file to the newly created resource.
|
|
246
|
+
*
|
|
247
|
+
* @param resourceKey - Optional key of the existing resource to update; if not provided, a new resource is created
|
|
248
|
+
* @param fileToUpload - The file or blob to upload
|
|
249
|
+
* @param publicFlag - Whether the resource should be public
|
|
250
|
+
* @param resourceType - The type of resource
|
|
251
|
+
* @param tags - Array of tags for the resource
|
|
252
|
+
*
|
|
253
|
+
* @returns Observable resolving to the ContentResource with uploaded file
|
|
254
|
+
*/
|
|
255
|
+
function createOrUpdateResourceAsObservable(resourceKey, fileToUpload, publicFlag, resourceType, tags) {
|
|
256
|
+
return (0, rxjs_1.from)(createOrUpdateResource(resourceKey, fileToUpload, publicFlag, resourceType, tags));
|
|
257
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Halix SDK License v1.0
|
|
3
|
+
// Copyright (c) 2025 halix.io LLC.
|
|
4
|
+
//
|
|
5
|
+
// This source code is licensed for use **only** within applications
|
|
6
|
+
// running on the Halix platform, in accordance with Halix SDK guidelines.
|
|
7
|
+
//
|
|
8
|
+
// Unauthorized use outside the Halix platform is prohibited.
|
|
9
|
+
// Full license terms available in the LICENSE file.
|
|
10
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
11
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
14
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
15
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
16
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
20
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.getObject = getObject;
|
|
24
|
+
exports.getObjectAsObservable = getObjectAsObservable;
|
|
25
|
+
exports.getRelatedObjects = getRelatedObjects;
|
|
26
|
+
exports.getRelatedObjectsAsObservable = getRelatedObjectsAsObservable;
|
|
27
|
+
exports.saveRelatedObject = saveRelatedObject;
|
|
28
|
+
exports.saveRelatedObjectAsObservable = saveRelatedObjectAsObservable;
|
|
29
|
+
exports.deleteRelatedObject = deleteRelatedObject;
|
|
30
|
+
exports.deleteRelatedObjectAsObservable = deleteRelatedObjectAsObservable;
|
|
31
|
+
exports.deleteRelatedObjects = deleteRelatedObjects;
|
|
32
|
+
exports.deleteRelatedObjectsAsObservable = deleteRelatedObjectsAsObservable;
|
|
33
|
+
/**
|
|
34
|
+
* @module @halix/action-sdk/data-crud
|
|
35
|
+
* @description Data CRUD operations for the Halix Platform action SDK. This module provides functions
|
|
36
|
+
* for creating, reading, updating, and deleting data objects through the Halix data service API.
|
|
37
|
+
* Note: for list-like displays, the `lists` module is preferred.
|
|
38
|
+
*
|
|
39
|
+
* Key features:
|
|
40
|
+
* - Retrieve one object a time
|
|
41
|
+
* - Retrieve all objects related to a parent object
|
|
42
|
+
* - Save a single object
|
|
43
|
+
* - Delete a single or multiple objects
|
|
44
|
+
*/
|
|
45
|
+
const axios_1 = __importDefault(require("axios"));
|
|
46
|
+
const rxjs_1 = require("rxjs");
|
|
47
|
+
const sdk_general_1 = require("./sdk-general");
|
|
48
|
+
// ================================================================================
|
|
49
|
+
// DATA RETRIEVAL FUNCTIONS
|
|
50
|
+
// ================================================================================
|
|
51
|
+
/**
|
|
52
|
+
* getObject retrieves a single object from the database by its data element ID and key.
|
|
53
|
+
*
|
|
54
|
+
* @param dataElementId - The ID of the data element
|
|
55
|
+
* @param key - The key of the object
|
|
56
|
+
* @param fetchedRelationships - Optional array of relationships to fetch; if provided, the returned
|
|
57
|
+
* object will include the specified related objects as nested objects
|
|
58
|
+
* @returns Promise resolving to the object data
|
|
59
|
+
*/
|
|
60
|
+
function getObject(dataElementId, key, fetchedRelationships) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
let params;
|
|
63
|
+
if (fetchedRelationships) {
|
|
64
|
+
let p = {};
|
|
65
|
+
if (fetchedRelationships) {
|
|
66
|
+
p.fetchedRelationships = fetchedRelationships.join(",");
|
|
67
|
+
}
|
|
68
|
+
params = new URLSearchParams(p);
|
|
69
|
+
}
|
|
70
|
+
let url = `${sdk_general_1.serviceAddress}/schema/sandboxes/${sdk_general_1.sandboxKey}/${dataElementId}/${key}`;
|
|
71
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
72
|
+
console.log("Sending GET request to " + url + " with token " + authToken);
|
|
73
|
+
let response = yield axios_1.default.get(url, {
|
|
74
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
75
|
+
params: params,
|
|
76
|
+
});
|
|
77
|
+
return response.data;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* getObjectAsObservable retrieves a single object from the database by its data element ID and key.
|
|
82
|
+
*
|
|
83
|
+
* @param dataElementId - The ID of the data element
|
|
84
|
+
* @param key - The key of the object
|
|
85
|
+
* @param fetchedRelationships - Optional array of relationships to fetch; if provided, the returned
|
|
86
|
+
* object will include the specified related objects as nested objects
|
|
87
|
+
*
|
|
88
|
+
* @returns Observable resolving to the object data
|
|
89
|
+
*/
|
|
90
|
+
function getObjectAsObservable(dataElementId, key, fetchedRelationships) {
|
|
91
|
+
return (0, rxjs_1.from)(getObject(dataElementId, key, fetchedRelationships));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* getRelatedObjects retrieves an array of objects from the the database. The objects returned are
|
|
95
|
+
* related to a parent through a defined relationship in the schema. In a typical setup, action's
|
|
96
|
+
* auth token must have scope access to the parent object in order to access all of its related
|
|
97
|
+
* objects.
|
|
98
|
+
*
|
|
99
|
+
* It is common to use getRelatedObjects to retrieve all objects belonging to the current user proxy
|
|
100
|
+
* or organization proxy. For example, in a user context where the current user proxy element is
|
|
101
|
+
* "customer," an action might want to retrieve all "purchase" objects related to the current
|
|
102
|
+
* customer. Similarly, in an organization context where the current organization proxy is
|
|
103
|
+
* "business," an action might want to retrieve all "employee" objects related to the current
|
|
104
|
+
* business.
|
|
105
|
+
*
|
|
106
|
+
* @param parentElementId - The ID of the parent element
|
|
107
|
+
* @param parentKey - The key of the parent object
|
|
108
|
+
* @param elementId - The ID of the element
|
|
109
|
+
* @param filter - Optional filter criteria for the query; if not provided, all related objects will
|
|
110
|
+
* be returned
|
|
111
|
+
* @param fetchedRelationships - Optional array of relationships to fetch; if provided, the returned
|
|
112
|
+
* objects will include the specified related objects as nested objects
|
|
113
|
+
*
|
|
114
|
+
* @returns Promise resolving to an array of objects
|
|
115
|
+
*/
|
|
116
|
+
function getRelatedObjects(parentElementId, parentKey, elementId, filter, fetchedRelationships) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
let params;
|
|
119
|
+
if (filter || fetchedRelationships) {
|
|
120
|
+
let p = {};
|
|
121
|
+
if (filter) {
|
|
122
|
+
p.filter = filter;
|
|
123
|
+
}
|
|
124
|
+
if (fetchedRelationships) {
|
|
125
|
+
p.fetchedRelationships = fetchedRelationships.join(",");
|
|
126
|
+
}
|
|
127
|
+
params = new URLSearchParams(p);
|
|
128
|
+
}
|
|
129
|
+
let url = `${sdk_general_1.serviceAddress}/schema/sandboxes/${sdk_general_1.sandboxKey}/${parentElementId}/${parentKey}/${elementId}`;
|
|
130
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
131
|
+
console.log("Sending GET request to " + url + " with token " + authToken);
|
|
132
|
+
let response = yield axios_1.default.get(url, {
|
|
133
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
134
|
+
params: params,
|
|
135
|
+
});
|
|
136
|
+
return response.data;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* getRelatedObjectsAsObservable retrieves an array of objects from the the database. The objects
|
|
141
|
+
* returned are related to a parent through a defined relationship in the schema. In a typical
|
|
142
|
+
* setup, action's auth token must have scope access to the parent object in order to access all of
|
|
143
|
+
* its related objects.
|
|
144
|
+
*
|
|
145
|
+
* It is common to use getRelatedObjects to retrieve all objects belonging to the current user proxy
|
|
146
|
+
* or organization proxy. For example, in a user context where the current user proxy element is
|
|
147
|
+
* "customer," an action might want to retrieve all "purchase" objects related to the current
|
|
148
|
+
* customer. Similarly, in an organization context where the current organization proxy is
|
|
149
|
+
* "business," an action might want to retrieve all "employee" objects related to the current
|
|
150
|
+
* business.
|
|
151
|
+
*
|
|
152
|
+
* @param parentElementId - The ID of the parent element
|
|
153
|
+
* @param parentKey - The key of the parent element
|
|
154
|
+
* @param elementId - The ID of the element
|
|
155
|
+
* @param filter - Optional filter criteria for the query; if not provided, all related objects will
|
|
156
|
+
* be returned
|
|
157
|
+
* @param fetchedRelationships - Optional array of relationships to fetch; if provided, the returned
|
|
158
|
+
* objects will include the specified related objects as nested objects
|
|
159
|
+
*
|
|
160
|
+
* @returns Observable resolving to an array of objects
|
|
161
|
+
*/
|
|
162
|
+
function getRelatedObjectsAsObservable(parentElementId, parentKey, elementId, filter, fetchedRelationships) {
|
|
163
|
+
return (0, rxjs_1.from)(getRelatedObjects(parentElementId, parentKey, elementId, filter, fetchedRelationships));
|
|
164
|
+
}
|
|
165
|
+
// ================================================================================
|
|
166
|
+
// DATA SAVE FUNCTIONS
|
|
167
|
+
// ================================================================================
|
|
168
|
+
/**
|
|
169
|
+
* saveRelatedObject saves a related object to the database. The objectToSave is saved, and its
|
|
170
|
+
* relationship to the parent object is established based on the relationship specified in the
|
|
171
|
+
* schema. The objectToSave must have a relationship to the parent object and the user must have
|
|
172
|
+
* scope access to the parent object.
|
|
173
|
+
*
|
|
174
|
+
* @param parentElementId - The ID of the parent element
|
|
175
|
+
* @param parentKey - The key of the parent object
|
|
176
|
+
* @param elementId - The element ID of the object to save
|
|
177
|
+
* @param objectToSave - The object data to save (as a JSON string)
|
|
178
|
+
* @param opts - Optional save options
|
|
179
|
+
*
|
|
180
|
+
* @returns Promise resolving to saved object, including any updates made to the object during the
|
|
181
|
+
* save operation (such as assigning an objKey if the object is new), or the assignment of
|
|
182
|
+
* calculated values
|
|
183
|
+
*/
|
|
184
|
+
function saveRelatedObject(parentElementId, parentKey, elementId, objectToSave, opts) {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
let url = `${sdk_general_1.serviceAddress}/schema/sandboxes/${sdk_general_1.sandboxKey}/${parentElementId}/${parentKey}/${elementId}`;
|
|
187
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.bypassValidation) === false) {
|
|
188
|
+
url += "?bypassValidation=false";
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
url += "?bypassValidation=true";
|
|
192
|
+
}
|
|
193
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
194
|
+
console.log("Sending POST request to " + url + " with token " + authToken);
|
|
195
|
+
let response = yield axios_1.default.post(url, objectToSave, {
|
|
196
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
197
|
+
});
|
|
198
|
+
return response.data;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* saveRelatedObjectAsObservable saves a related object to the database. The objectToSave is saved,
|
|
203
|
+
* and its relationship to the parent object is established based on the relationship specified in
|
|
204
|
+
* the schema. The objectToSave must have a relationship to the parent object and the user must have
|
|
205
|
+
* scope access to the parent object.
|
|
206
|
+
*
|
|
207
|
+
* @param parentElementId - The ID of the parent element
|
|
208
|
+
* @param parentKey - The key of the parent object
|
|
209
|
+
* @param elementId - The element ID of the object to save
|
|
210
|
+
* @param objectToSave - The object data to save (as a JSON string)
|
|
211
|
+
* @param opts - Optional save options
|
|
212
|
+
*
|
|
213
|
+
* @returns Observable resolving to saved object, including any updates made to the object during
|
|
214
|
+
* the save operation (such as assigning an objKey if the object is new), or the assignment of
|
|
215
|
+
* calculated values
|
|
216
|
+
*/
|
|
217
|
+
function saveRelatedObjectAsObservable(parentElementId, parentKey, elementId, objectToSave, opts) {
|
|
218
|
+
return (0, rxjs_1.from)(saveRelatedObject(parentElementId, parentKey, elementId, objectToSave, opts));
|
|
219
|
+
}
|
|
220
|
+
// ================================================================================
|
|
221
|
+
// DATA DELETE FUNCTIONS
|
|
222
|
+
// ================================================================================
|
|
223
|
+
/**
|
|
224
|
+
* deleteRelatedObject deletes a single object related to a specific parent.
|
|
225
|
+
*
|
|
226
|
+
* @param parentElementId - The ID of the parent element
|
|
227
|
+
* @param parentKey - The key of the parent object
|
|
228
|
+
* @param childElementId - The ID of the child element to delete
|
|
229
|
+
* @param childKey - The key of the child object to delete
|
|
230
|
+
*
|
|
231
|
+
* @returns Promise resolving to true if deletion was successful
|
|
232
|
+
*/
|
|
233
|
+
function deleteRelatedObject(parentElementId, parentKey, childElementId, childKey) {
|
|
234
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
235
|
+
if (!sdk_general_1.userContext) {
|
|
236
|
+
throw new Error("userContext is required but not available; check that the initialize function has been called");
|
|
237
|
+
}
|
|
238
|
+
let url = `${sdk_general_1.serviceAddress}/schema/sandboxes/${sdk_general_1.sandboxKey}/${parentElementId}/${parentKey}/${childElementId}/${childKey}`;
|
|
239
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
240
|
+
console.log("Sending DELETE request to " + url + " with token " + authToken);
|
|
241
|
+
let response = yield axios_1.default.delete(url, {
|
|
242
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
243
|
+
});
|
|
244
|
+
return response.status === 204;
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* deleteRelatedObjectAsObservable deletes a single object related to a specific parent.
|
|
249
|
+
*
|
|
250
|
+
* @param parentElementId - The ID of the parent element
|
|
251
|
+
* @param parentKey - The key of the parent object
|
|
252
|
+
* @param childElementId - The ID of the child element to delete
|
|
253
|
+
* @param childKey - The key of the child object to delete
|
|
254
|
+
*
|
|
255
|
+
* @returns Observable resolving to true if deletion was successful
|
|
256
|
+
*/
|
|
257
|
+
function deleteRelatedObjectAsObservable(parentElementId, parentKey, childElementId, childKey) {
|
|
258
|
+
return (0, rxjs_1.from)(deleteRelatedObject(parentElementId, parentKey, childElementId, childKey));
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* deleteRelatedObjects deletes multiple objects related to a specific parent.
|
|
262
|
+
*
|
|
263
|
+
* @param parentElementId - The ID of the parent element
|
|
264
|
+
* @param parentKey - The key of the parent object
|
|
265
|
+
* @param childElementId - The ID of the child element to delete
|
|
266
|
+
* @param childKeys - Array of keys of the child objects to delete
|
|
267
|
+
*
|
|
268
|
+
* @returns Promise resolving to true if deletion was successful
|
|
269
|
+
*/
|
|
270
|
+
function deleteRelatedObjects(parentElementId, parentKey, childElementId, childKeys) {
|
|
271
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
272
|
+
if (!sdk_general_1.userContext) {
|
|
273
|
+
throw new Error("userContext is required but not available; check that the initialize function has been called");
|
|
274
|
+
}
|
|
275
|
+
let url = `${sdk_general_1.serviceAddress}/schema/sandboxes/${sdk_general_1.sandboxKey}/${parentElementId}/${parentKey}/${childElementId}`;
|
|
276
|
+
let authToken = yield (0, rxjs_1.lastValueFrom)((0, sdk_general_1.getAuthToken)());
|
|
277
|
+
console.log("Sending DELETE request to " + url + " with token " + authToken);
|
|
278
|
+
let response = yield axios_1.default.delete(url, {
|
|
279
|
+
headers: { "Authorization": `Bearer ${authToken}` },
|
|
280
|
+
params: { keys: childKeys.join(",") },
|
|
281
|
+
});
|
|
282
|
+
return response.status === 204;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* deleteRelatedObjectsAsObservable deletes multiple objects related to a specific parent.
|
|
287
|
+
*
|
|
288
|
+
* @param parentElementId - The ID of the parent element
|
|
289
|
+
* @param parentKey - The key of the parent object
|
|
290
|
+
* @param childElementId - The ID of the child element to delete
|
|
291
|
+
* @param childKeys - Array of keys of the child objects to delete
|
|
292
|
+
*
|
|
293
|
+
* @returns Observable resolving to true if deletion was successful
|
|
294
|
+
*/
|
|
295
|
+
function deleteRelatedObjectsAsObservable(parentElementId, parentKey, childElementId, childKeys) {
|
|
296
|
+
return (0, rxjs_1.from)(deleteRelatedObjects(parentElementId, parentKey, childElementId, childKeys));
|
|
297
|
+
}
|