farseer-cli 1.10.3 → 1.11.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 +5 -0
- package/dist/commands/app.js +41 -42
- package/dist/commands/app.js.map +2 -2
- package/dist/commands/apps.js +3 -3
- package/dist/commands/apps.js.map +2 -2
- package/dist/commands/cells.js +3 -3
- package/dist/commands/cells.js.map +2 -2
- package/dist/commands/config.js +23 -15
- package/dist/commands/config.js.map +2 -2
- package/dist/commands/dashboards.js +3 -3
- package/dist/commands/dashboards.js.map +2 -2
- package/dist/commands/dimensions.js +3 -3
- package/dist/commands/dimensions.js.map +2 -2
- package/dist/commands/files.js +5 -4
- package/dist/commands/files.js.map +2 -2
- package/dist/commands/formula.js +3 -3
- package/dist/commands/formula.js.map +2 -2
- package/dist/commands/list.js +5 -4
- package/dist/commands/list.js.map +2 -2
- package/dist/commands/login.js +6 -5
- package/dist/commands/login.js.map +2 -2
- package/dist/commands/model.js +5 -4
- package/dist/commands/model.js.map +2 -2
- package/dist/commands/push.js +5 -7
- package/dist/commands/push.js.map +2 -2
- package/dist/commands/run.js +13 -15
- package/dist/commands/run.js.map +2 -2
- package/dist/commands/sheets.js +3 -3
- package/dist/commands/sheets.js.map +2 -2
- package/dist/commands/variables.js +5 -4
- package/dist/commands/variables.js.map +2 -2
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/index.js.map +2 -2
- package/dist/mcp/tools/appTools.js +38 -23
- package/dist/mcp/tools/appTools.js.map +2 -2
- package/dist/mcp/tools/dataTools.js +5 -2
- package/dist/mcp/tools/dataTools.js.map +2 -2
- package/dist/mcp/tools/modelTools.js +45 -48
- package/dist/mcp/tools/modelTools.js.map +2 -2
- package/dist/mcp/tools/runTools.js +12 -7
- package/dist/mcp/tools/runTools.js.map +2 -2
- package/dist/mcp/utils/helpers.js +41 -0
- package/dist/mcp/utils/helpers.js.map +2 -2
- package/dist/mcp/utils/session.js +2 -2
- package/dist/mcp/utils/session.js.map +2 -2
- package/dist/services/appSyncService.js.map +1 -1
- package/dist/services/apps.js +149 -0
- package/dist/services/apps.js.map +7 -0
- package/dist/services/authErrors.js +77 -0
- package/dist/services/authErrors.js.map +7 -0
- package/dist/services/configService.js +7 -4
- package/dist/services/configService.js.map +2 -2
- package/dist/services/credentials.js +87 -0
- package/dist/services/credentials.js.map +7 -0
- package/dist/services/farseerService.js +247 -123
- package/dist/services/farseerService.js.map +2 -2
- package/dist/services/httpError.js +35 -0
- package/dist/services/httpError.js.map +7 -0
- package/dist/services/modelExport.js +63 -0
- package/dist/services/modelExport.js.map +7 -0
- package/dist/services/organisation.js +88 -0
- package/dist/services/organisation.js.map +7 -0
- package/dist/services/preflight.js +10 -13
- package/dist/services/preflight.js.map +2 -2
- package/dist/services/syncService.js +2 -2
- package/dist/services/syncService.js.map +2 -2
- package/dist/utils/appResolver.js +63 -0
- package/dist/utils/appResolver.js.map +7 -0
- package/dist/utils/constants.js +2 -5
- package/dist/utils/constants.js.map +2 -2
- package/dist/utils/fileLogger.js +24 -1
- package/dist/utils/fileLogger.js.map +2 -2
- package/dist/utils/folderMatch.js +49 -0
- package/dist/utils/folderMatch.js.map +7 -0
- package/dist/utils/helpers.js +98 -1
- package/dist/utils/helpers.js.map +3 -3
- package/dist/utils/syncScope.js +44 -0
- package/dist/utils/syncScope.js.map +7 -0
- package/docs/api-reference.md +13 -1
- package/package.json +1 -1
- package/dist/services/farseerApi.js +0 -716
- package/dist/services/farseerApi.js.map +0 -7
- package/dist/services/farseerFactory.js +0 -276
- package/dist/services/farseerFactory.js.map +0 -7
|
@@ -1,716 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var farseerApi_exports = {};
|
|
30
|
-
__export(farseerApi_exports, {
|
|
31
|
-
AppDeleteRefusedError: () => AppDeleteRefusedError,
|
|
32
|
-
FarseerApi: () => FarseerApi,
|
|
33
|
-
REMOTE_JOB_ITEM_TYPE: () => REMOTE_JOB_ITEM_TYPE,
|
|
34
|
-
applySourceFolderChange: () => applySourceFolderChange,
|
|
35
|
-
findScriptInFolder: () => findScriptInFolder,
|
|
36
|
-
loadDimensionTableViaEntitiesApi: () => loadDimensionTableViaEntitiesApi,
|
|
37
|
-
mergeAppUpdate: () => mergeAppUpdate,
|
|
38
|
-
remoteJobReverseReferences: () => remoteJobReverseReferences,
|
|
39
|
-
resolveAppItemNodeId: () => resolveAppItemNodeId
|
|
40
|
-
});
|
|
41
|
-
module.exports = __toCommonJS(farseerApi_exports);
|
|
42
|
-
var import_axios = __toESM(require("axios"));
|
|
43
|
-
var import_chalk = __toESM(require("chalk"));
|
|
44
|
-
var farseer = __toESM(require("farseer-client"));
|
|
45
|
-
var import_configService = require("./configService");
|
|
46
|
-
var import_farseerService = require("./farseerService");
|
|
47
|
-
var import_errorFormatter = require("../utils/errorFormatter");
|
|
48
|
-
var import_logger = require("../utils/logger");
|
|
49
|
-
function mergeAppUpdate(current, changes) {
|
|
50
|
-
return {
|
|
51
|
-
name: changes.name ?? current.name,
|
|
52
|
-
description: changes.description ?? current.description ?? "",
|
|
53
|
-
expectedArguments: changes.expectedArguments ?? current.expectedArguments ?? [],
|
|
54
|
-
mainScriptFileId: changes.mainScriptFileId ?? current.mainScriptFileId ?? null,
|
|
55
|
-
sourceFolderId: changes.sourceFolderId ?? current.sourceFolderId ?? null,
|
|
56
|
-
schedule: changes.schedule ?? current.schedule ?? null,
|
|
57
|
-
runnerName: changes.runnerName ?? current.runnerName ?? null,
|
|
58
|
-
runnerImage: changes.runnerImage ?? current.runnerImage ?? null,
|
|
59
|
-
predefinedActions: changes.predefinedActions ?? current.predefinedActions ?? []
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
function findScriptInFolder(folderScripts, name) {
|
|
63
|
-
return folderScripts.find((s) => s.name === name);
|
|
64
|
-
}
|
|
65
|
-
async function applySourceFolderChange(client, app, changes, options, caller) {
|
|
66
|
-
const cli = caller === "cli";
|
|
67
|
-
const hint = (text) => cli ? import_chalk.default.dim(text) : text;
|
|
68
|
-
let folderScripts = app.scriptFiles ?? [];
|
|
69
|
-
let sourceFolderId;
|
|
70
|
-
if (options.sourceFolder) {
|
|
71
|
-
let resolved;
|
|
72
|
-
try {
|
|
73
|
-
resolved = await client.resolveSourceFolder(options.sourceFolder);
|
|
74
|
-
} catch {
|
|
75
|
-
return [
|
|
76
|
-
`Source folder not found under Files/: ${options.sourceFolder}`,
|
|
77
|
-
hint('Use a path relative to Files/, e.g. "Scripts/MyApp".')
|
|
78
|
-
].join("\n");
|
|
79
|
-
}
|
|
80
|
-
sourceFolderId = resolved.id;
|
|
81
|
-
folderScripts = resolved.scripts;
|
|
82
|
-
if (!options.entrypoint && app.mainScriptFileId) {
|
|
83
|
-
const stillThere = folderScripts.some((s) => s.id === app.mainScriptFileId);
|
|
84
|
-
if (!stillThere) {
|
|
85
|
-
const files = folderScripts.map((s) => s.name).join(", ") || "(none)";
|
|
86
|
-
return [
|
|
87
|
-
"The app's current entrypoint is not in the new source folder.",
|
|
88
|
-
hint(
|
|
89
|
-
cli ? "Pass --entrypoint <script> (a file in the new folder) in the same command." : 'Pass "entrypoint" (a file in the new folder) in the same call.'
|
|
90
|
-
),
|
|
91
|
-
hint(`Files in this folder: ${files}`)
|
|
92
|
-
].join("\n");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
let mainScriptFileId;
|
|
97
|
-
if (options.entrypoint) {
|
|
98
|
-
const match = findScriptInFolder(folderScripts, options.entrypoint);
|
|
99
|
-
if (!match) {
|
|
100
|
-
const files = folderScripts.map((s) => s.name).join(", ") || "(none)";
|
|
101
|
-
const lines = [
|
|
102
|
-
`Entrypoint not found in the source folder: ${options.entrypoint}`,
|
|
103
|
-
hint(`Files in this folder: ${files}`)
|
|
104
|
-
];
|
|
105
|
-
if (!options.sourceFolder) {
|
|
106
|
-
lines.push(
|
|
107
|
-
hint(
|
|
108
|
-
cli ? "To use a file from another folder, set it first with --source-folder <path>." : "To use a file from another folder, pass sourceFolder first."
|
|
109
|
-
)
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
return lines.join("\n");
|
|
113
|
-
}
|
|
114
|
-
mainScriptFileId = match.id;
|
|
115
|
-
}
|
|
116
|
-
if (sourceFolderId !== void 0) {
|
|
117
|
-
changes.sourceFolderId = sourceFolderId;
|
|
118
|
-
}
|
|
119
|
-
if (mainScriptFileId !== void 0) {
|
|
120
|
-
changes.mainScriptFileId = mainScriptFileId;
|
|
121
|
-
}
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
const REMOTE_JOB_ITEM_TYPE = farseer.FolderItemType.RemoteJob;
|
|
125
|
-
function remoteJobReverseReferences(remoteJobIds) {
|
|
126
|
-
return remoteJobIds.map((reference) => ({ reference, type: REMOTE_JOB_ITEM_TYPE }));
|
|
127
|
-
}
|
|
128
|
-
class AppDeleteRefusedError extends Error {
|
|
129
|
-
}
|
|
130
|
-
function resolveAppItemNodeId(remoteJobId, items) {
|
|
131
|
-
const matches = items.filter((item2) => item2.reference === remoteJobId);
|
|
132
|
-
if (matches.length === 0) {
|
|
133
|
-
throw new AppDeleteRefusedError(`No app found for Remote Job id ${remoteJobId} \u2014 nothing was deleted.`);
|
|
134
|
-
}
|
|
135
|
-
if (matches.length > 1) {
|
|
136
|
-
throw new AppDeleteRefusedError(
|
|
137
|
-
`Remote Job id ${remoteJobId} resolved to ${matches.length} folder items \u2014 refusing to delete.`
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
const item = matches[0];
|
|
141
|
-
if (item.type !== REMOTE_JOB_ITEM_TYPE) {
|
|
142
|
-
throw new AppDeleteRefusedError(
|
|
143
|
-
`Refusing to delete: Remote Job id ${remoteJobId} resolved to a ${item.type} ("${item.name}"), not an app.`
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
return item.id;
|
|
147
|
-
}
|
|
148
|
-
class FarseerApi {
|
|
149
|
-
// basePath is passed in rather than worked out here. It used to come from
|
|
150
|
-
// `getCredential(...)?.basePath` keyed on the organisation, so a stored API
|
|
151
|
-
// key named after the organisation silently decided where a browser-login
|
|
152
|
-
// request went — and the SDK client for the same pair never consulted it.
|
|
153
|
-
// The one decision now lives in ResolvedAuth (services/farseerFactory).
|
|
154
|
-
constructor(accessToken, basePath, tenantId) {
|
|
155
|
-
this.isRefreshing = false;
|
|
156
|
-
this.refreshSubscribers = [];
|
|
157
|
-
this.tenantId = tenantId;
|
|
158
|
-
this.accessToken = accessToken;
|
|
159
|
-
this.basePath = basePath;
|
|
160
|
-
this.client = import_axios.default.create({
|
|
161
|
-
baseURL: basePath,
|
|
162
|
-
headers: {
|
|
163
|
-
Authorization: `Bearer ${accessToken}`,
|
|
164
|
-
"Content-Type": "application/json",
|
|
165
|
-
"x-api-version": "3.3.0",
|
|
166
|
-
"X-TENANT-ID": this.tenantId
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
this.client.interceptors.response.use(
|
|
170
|
-
(response) => response,
|
|
171
|
-
async (error) => {
|
|
172
|
-
const originalRequest = error.config;
|
|
173
|
-
if (error.response?.status === 401 && !originalRequest._retry) {
|
|
174
|
-
originalRequest._retry = true;
|
|
175
|
-
if (this.isRefreshing) {
|
|
176
|
-
return new Promise((resolve) => {
|
|
177
|
-
this.refreshSubscribers.push((token) => {
|
|
178
|
-
originalRequest.headers["Authorization"] = `Bearer ${token}`;
|
|
179
|
-
resolve(this.client(originalRequest));
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
this.isRefreshing = true;
|
|
184
|
-
try {
|
|
185
|
-
const auth = (0, import_configService.getUserAuth)();
|
|
186
|
-
if (!auth?.refreshToken) {
|
|
187
|
-
throw new Error("No refresh token available");
|
|
188
|
-
}
|
|
189
|
-
const refreshed = await (0, import_farseerService.refreshAccessToken)(auth.refreshToken, auth.realm || "master");
|
|
190
|
-
if (!refreshed) {
|
|
191
|
-
throw new Error("Token refresh failed");
|
|
192
|
-
}
|
|
193
|
-
(0, import_configService.setUserAuth)({
|
|
194
|
-
accessToken: refreshed.accessToken,
|
|
195
|
-
refreshToken: refreshed.refreshToken,
|
|
196
|
-
expiresAt: new Date(Date.now() + refreshed.expiresIn * 1e3).toISOString(),
|
|
197
|
-
realm: auth.realm
|
|
198
|
-
});
|
|
199
|
-
this.updateAccessToken(refreshed.accessToken);
|
|
200
|
-
this.refreshSubscribers.forEach((callback) => callback(refreshed.accessToken));
|
|
201
|
-
this.refreshSubscribers = [];
|
|
202
|
-
originalRequest.headers["Authorization"] = `Bearer ${refreshed.accessToken}`;
|
|
203
|
-
return this.client(originalRequest);
|
|
204
|
-
} catch (refreshError) {
|
|
205
|
-
this.refreshSubscribers = [];
|
|
206
|
-
throw error;
|
|
207
|
-
} finally {
|
|
208
|
-
this.isRefreshing = false;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return Promise.reject(error);
|
|
212
|
-
}
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Update the access token (used when token is refreshed)
|
|
217
|
-
*/
|
|
218
|
-
updateAccessToken(newToken) {
|
|
219
|
-
this.accessToken = newToken;
|
|
220
|
-
this.client.defaults.headers["Authorization"] = `Bearer ${newToken}`;
|
|
221
|
-
}
|
|
222
|
-
async testConnection() {
|
|
223
|
-
try {
|
|
224
|
-
await this.getItemByPath(["Files"]);
|
|
225
|
-
return true;
|
|
226
|
-
} catch {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
async getOrganizationInfo() {
|
|
231
|
-
const response = await this.client.get("/organization");
|
|
232
|
-
return response.data;
|
|
233
|
-
}
|
|
234
|
-
async getFilesFolder() {
|
|
235
|
-
try {
|
|
236
|
-
const item = await this.getItemByPath(["Files"]);
|
|
237
|
-
return {
|
|
238
|
-
id: item.id,
|
|
239
|
-
name: item.name,
|
|
240
|
-
type: item.type
|
|
241
|
-
};
|
|
242
|
-
} catch {
|
|
243
|
-
return null;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
async listFolderItems(folderId) {
|
|
247
|
-
const response = await this.client.post("/folders/items", [{ id: folderId }]);
|
|
248
|
-
return response.data[0] || { items: [] };
|
|
249
|
-
}
|
|
250
|
-
async findFolderByName(parentId, name) {
|
|
251
|
-
const content = await this.listFolderItems(parentId);
|
|
252
|
-
const folder = content.items?.find(
|
|
253
|
-
(item) => item.name.toLowerCase() === name.toLowerCase() && item.type.toLowerCase() === "folder"
|
|
254
|
-
);
|
|
255
|
-
return folder || null;
|
|
256
|
-
}
|
|
257
|
-
async getItemByPath(path) {
|
|
258
|
-
let currentId = null;
|
|
259
|
-
for (const segment of path) {
|
|
260
|
-
const folder = await this.findFolderByName(currentId, segment);
|
|
261
|
-
if (!folder) {
|
|
262
|
-
throw new Error(`Folder not found: ${segment}`);
|
|
263
|
-
}
|
|
264
|
-
currentId = folder.id;
|
|
265
|
-
}
|
|
266
|
-
if (currentId === null) {
|
|
267
|
-
throw new Error("Path is empty");
|
|
268
|
-
}
|
|
269
|
-
return { id: currentId, name: path[path.length - 1], type: "folder" };
|
|
270
|
-
}
|
|
271
|
-
async listFiles(folderPath = ["Files"]) {
|
|
272
|
-
const files = [];
|
|
273
|
-
try {
|
|
274
|
-
const folder = await this.getItemByPath(folderPath);
|
|
275
|
-
const folderContent = await this.listFolderItems(folder.id);
|
|
276
|
-
for (const item of folderContent.items || []) {
|
|
277
|
-
const itemPath = [...folderPath.slice(1), item.name].join("/");
|
|
278
|
-
const itemType = item.type.toLowerCase();
|
|
279
|
-
if (itemType === "folder") {
|
|
280
|
-
const subFiles = await this.listFiles([...folderPath, item.name]);
|
|
281
|
-
files.push(...subFiles);
|
|
282
|
-
} else if ((itemType === "farseer-file" || itemType === "farseer_file") && item.reference) {
|
|
283
|
-
files.push({
|
|
284
|
-
name: item.name,
|
|
285
|
-
path: itemPath,
|
|
286
|
-
reference: item.reference
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
} catch (error) {
|
|
291
|
-
if (import_axios.default.isAxiosError(error) && error.response?.data?.code === "Forbidden") {
|
|
292
|
-
const errorMessage = error.response.data?.message || "Access denied";
|
|
293
|
-
throw new Error(
|
|
294
|
-
`Access denied: ${errorMessage}
|
|
295
|
-
You may need to use --tenant-id to specify a different tenant ID.`
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
throw await (0, import_errorFormatter.toError)(error, `Failed to list files in ${folderPath.join("/") || "root"}`);
|
|
299
|
-
}
|
|
300
|
-
return files;
|
|
301
|
-
}
|
|
302
|
-
async getFileContent(reference) {
|
|
303
|
-
const response = await this.client.get(`/farseerFiles/${reference}`, {
|
|
304
|
-
responseType: "text"
|
|
305
|
-
});
|
|
306
|
-
return response.data;
|
|
307
|
-
}
|
|
308
|
-
async getFileContentAsBuffer(reference) {
|
|
309
|
-
const response = await this.client.get(`/farseerFiles/${reference}`, {
|
|
310
|
-
responseType: "arraybuffer"
|
|
311
|
-
});
|
|
312
|
-
return Buffer.from(response.data);
|
|
313
|
-
}
|
|
314
|
-
async createFile(content, fileName, folderPath = ["Files"]) {
|
|
315
|
-
await this.ensureFolderPath(folderPath);
|
|
316
|
-
const folder = await this.getItemByPath(folderPath);
|
|
317
|
-
const formData = new FormData();
|
|
318
|
-
const blob = new Blob([content], { type: "application/octet-stream" });
|
|
319
|
-
formData.append("file", blob, fileName);
|
|
320
|
-
formData.append("category", "GENERAL");
|
|
321
|
-
formData.append("folderId", folder.id.toString());
|
|
322
|
-
try {
|
|
323
|
-
await this.client.post("/farseerFiles", formData, {
|
|
324
|
-
headers: {
|
|
325
|
-
"Content-Type": "multipart/form-data"
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
} catch (error) {
|
|
329
|
-
throw await (0, import_errorFormatter.toError)(error, `Failed to create file "${fileName}"`);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
async updateFile(reference, content, fileName) {
|
|
333
|
-
const formData = new FormData();
|
|
334
|
-
const blob = new Blob([content], { type: "application/octet-stream" });
|
|
335
|
-
formData.append("file", blob, fileName);
|
|
336
|
-
try {
|
|
337
|
-
await this.client.put(`/farseerFiles/${reference}`, formData, {
|
|
338
|
-
headers: {
|
|
339
|
-
"Content-Type": "multipart/form-data"
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
} catch (error) {
|
|
343
|
-
throw await (0, import_errorFormatter.toError)(error, `Failed to update file "${fileName}" (ref: ${reference})`);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
async deleteFile(reference) {
|
|
347
|
-
try {
|
|
348
|
-
await this.client.delete(`/farseerFiles/${reference}`);
|
|
349
|
-
} catch (error) {
|
|
350
|
-
throw await (0, import_errorFormatter.toError)(error, `Failed to delete file (ref: ${reference})`);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
async ensureFolderPath(folderPath) {
|
|
354
|
-
const currentPath = [];
|
|
355
|
-
for (const segment of folderPath) {
|
|
356
|
-
currentPath.push(segment);
|
|
357
|
-
try {
|
|
358
|
-
await this.getItemByPath(currentPath);
|
|
359
|
-
} catch {
|
|
360
|
-
if (currentPath.length > 1) {
|
|
361
|
-
const parentPath = currentPath.slice(0, -1);
|
|
362
|
-
const parent = await this.getItemByPath(parentPath);
|
|
363
|
-
await this.client.post("/folders", {
|
|
364
|
-
parentId: parent.id,
|
|
365
|
-
name: segment,
|
|
366
|
-
allowedTypes: null
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
async getFileByPath(filePath) {
|
|
373
|
-
const pathParts = ["Files", ...filePath.split("/")];
|
|
374
|
-
const fileName = pathParts.pop();
|
|
375
|
-
const folderPath = pathParts;
|
|
376
|
-
try {
|
|
377
|
-
const folder = await this.getItemByPath(folderPath);
|
|
378
|
-
const folderContent = await this.listFolderItems(folder.id);
|
|
379
|
-
for (const item of folderContent.items || []) {
|
|
380
|
-
const itemType = item.type.toLowerCase();
|
|
381
|
-
if (item.name === fileName && (itemType === "farseer-file" || itemType === "farseer_file") && item.reference) {
|
|
382
|
-
return {
|
|
383
|
-
name: item.name,
|
|
384
|
-
path: filePath,
|
|
385
|
-
reference: item.reference
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
} catch {
|
|
390
|
-
return null;
|
|
391
|
-
}
|
|
392
|
-
return null;
|
|
393
|
-
}
|
|
394
|
-
async getFileMetadata(reference) {
|
|
395
|
-
try {
|
|
396
|
-
const response = await this.client.get(`/farseerFiles/${reference}/metadata`);
|
|
397
|
-
const data = response.data;
|
|
398
|
-
return {
|
|
399
|
-
uploadTime: data.uploadTime,
|
|
400
|
-
uploader: data.uploader ? {
|
|
401
|
-
id: data.uploader.id,
|
|
402
|
-
email: data.uploader.email || "",
|
|
403
|
-
firstName: data.uploader.firstName || "",
|
|
404
|
-
lastName: data.uploader.lastName || ""
|
|
405
|
-
} : void 0,
|
|
406
|
-
size: data.size
|
|
407
|
-
};
|
|
408
|
-
} catch (error) {
|
|
409
|
-
return null;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
isTextFile(filename) {
|
|
413
|
-
const ext = filename.toLowerCase().split(".").pop() || "";
|
|
414
|
-
return ["ts", "js", "mjs", "cjs", "json", "txt", "md", "csv", "xml", "html", "css", "yaml", "yml"].includes(
|
|
415
|
-
ext
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
isBinaryFile(filename) {
|
|
419
|
-
return !this.isTextFile(filename);
|
|
420
|
-
}
|
|
421
|
-
// ==================== Apps (Remote Jobs) API ====================
|
|
422
|
-
/**
|
|
423
|
-
* Get the Apps folder ID
|
|
424
|
-
*/
|
|
425
|
-
async getAppsFolderId() {
|
|
426
|
-
const content = await this.listFolderItems(null);
|
|
427
|
-
const appsFolder = content.items?.find(
|
|
428
|
-
(item) => item.name.toLowerCase() === "apps" && item.type.toLowerCase() === "folder"
|
|
429
|
-
);
|
|
430
|
-
return appsFolder?.id || null;
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* List all apps (Remote Jobs) on the tenant, including those in subfolders
|
|
434
|
-
*/
|
|
435
|
-
async listApps() {
|
|
436
|
-
const appsFolderId = await this.getAppsFolderId();
|
|
437
|
-
if (!appsFolderId) {
|
|
438
|
-
return [];
|
|
439
|
-
}
|
|
440
|
-
return this.listAppsRecursive(appsFolderId);
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Recursively list all apps in a folder and its subfolders
|
|
444
|
-
*/
|
|
445
|
-
async listAppsRecursive(folderId) {
|
|
446
|
-
const apps = [];
|
|
447
|
-
const content = await this.listFolderItems(folderId);
|
|
448
|
-
for (const item of content.items || []) {
|
|
449
|
-
const itemType = item.type.toLowerCase();
|
|
450
|
-
if (itemType === "remote_job") {
|
|
451
|
-
apps.push({
|
|
452
|
-
name: item.name,
|
|
453
|
-
type: item.type,
|
|
454
|
-
reference: item.reference || ""
|
|
455
|
-
});
|
|
456
|
-
} else if (itemType === "folder") {
|
|
457
|
-
const subApps = await this.listAppsRecursive(item.id);
|
|
458
|
-
apps.push(...subApps);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
return apps;
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* Get app details by reference ID
|
|
465
|
-
*/
|
|
466
|
-
async getApp(referenceId) {
|
|
467
|
-
try {
|
|
468
|
-
const response = await this.client.get(`/remoteJobs/${referenceId}`);
|
|
469
|
-
const app = response.data;
|
|
470
|
-
if ((!app.scriptFiles || app.scriptFiles.length === 0) && app.sourceFolderId) {
|
|
471
|
-
app.scriptFiles = await this.resolveAppScripts(app.sourceFolderId);
|
|
472
|
-
}
|
|
473
|
-
return app;
|
|
474
|
-
} catch {
|
|
475
|
-
return null;
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Resolve script files from an app's source folder
|
|
480
|
-
*/
|
|
481
|
-
async resolveAppScripts(sourceFolderId) {
|
|
482
|
-
try {
|
|
483
|
-
const content = await this.listFolderItems(sourceFolderId);
|
|
484
|
-
return (content.items || []).filter((item) => {
|
|
485
|
-
const t = item.type.toLowerCase();
|
|
486
|
-
return (t === "farseer-file" || t === "farseer_file") && item.reference;
|
|
487
|
-
}).map((item) => ({ id: item.reference, name: item.name }));
|
|
488
|
-
} catch {
|
|
489
|
-
return [];
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
async resolveSourceFolder(relativePath) {
|
|
493
|
-
const segments = relativePath.split("/").map((s) => s.trim()).filter(Boolean);
|
|
494
|
-
const folder = await this.getItemByPath(["Files", ...segments]);
|
|
495
|
-
const scripts = await this.resolveAppScripts(folder.id);
|
|
496
|
-
return { id: folder.id, scripts };
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Get app by name
|
|
500
|
-
*/
|
|
501
|
-
async getAppByName(name) {
|
|
502
|
-
const apps = await this.listApps();
|
|
503
|
-
const app = apps.find((a) => a.name.toLowerCase() === name.toLowerCase());
|
|
504
|
-
if (!app) {
|
|
505
|
-
return null;
|
|
506
|
-
}
|
|
507
|
-
return this.getApp(app.reference);
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* Create a new app
|
|
511
|
-
*/
|
|
512
|
-
async createApp(name) {
|
|
513
|
-
const appsFolderId = await this.getAppsFolderId();
|
|
514
|
-
if (!appsFolderId) {
|
|
515
|
-
throw new Error("Apps folder not found");
|
|
516
|
-
}
|
|
517
|
-
const response = await this.client.post("/remoteJobs", {
|
|
518
|
-
folderId: appsFolderId,
|
|
519
|
-
name
|
|
520
|
-
});
|
|
521
|
-
return response.data;
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* Update an app
|
|
525
|
-
*/
|
|
526
|
-
async updateApp(referenceId, update, currentApp) {
|
|
527
|
-
await this.client.put(`/remoteJobs/${referenceId}`, mergeAppUpdate(currentApp, update));
|
|
528
|
-
const app = await this.getApp(referenceId);
|
|
529
|
-
if (!app) {
|
|
530
|
-
throw new Error(`Failed to fetch updated app ${referenceId}`);
|
|
531
|
-
}
|
|
532
|
-
return app;
|
|
533
|
-
}
|
|
534
|
-
async resolveRemoteJobItems(remoteJobIds) {
|
|
535
|
-
if (remoteJobIds.length === 0) {
|
|
536
|
-
return [];
|
|
537
|
-
}
|
|
538
|
-
const response = await this.client.post(
|
|
539
|
-
"/folders/resolveItems",
|
|
540
|
-
remoteJobReverseReferences(remoteJobIds)
|
|
541
|
-
);
|
|
542
|
-
return response.data ?? [];
|
|
543
|
-
}
|
|
544
|
-
// Takes the Remote Job id and resolves the folder-item id itself, so no
|
|
545
|
-
// caller can hand this a raw node id. See `resolveAppItemNodeId`.
|
|
546
|
-
async deleteApp(remoteJobId) {
|
|
547
|
-
const items = await this.resolveRemoteJobItems([remoteJobId]);
|
|
548
|
-
const itemNodeId = resolveAppItemNodeId(remoteJobId, items);
|
|
549
|
-
await this.client.post("/folders/deleteItems", [{ id: itemNodeId }]);
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Run an app with arguments
|
|
553
|
-
*/
|
|
554
|
-
async runApp(referenceId, args = {}) {
|
|
555
|
-
const response = await this.client.post(`/remoteJobs/${referenceId}/run`, {
|
|
556
|
-
arguments: args
|
|
557
|
-
});
|
|
558
|
-
return response.data;
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Get script files available for apps (searches all .ts/.js files in Files folder)
|
|
562
|
-
* Returns full path relative to Files/ folder (e.g., "Scripts/test.ts", "Scripts/subfolder/helper.ts")
|
|
563
|
-
*/
|
|
564
|
-
async getScriptFiles() {
|
|
565
|
-
const allFiles = await this.listFiles(["Files"]);
|
|
566
|
-
return allFiles.filter((f) => f.name.endsWith(".ts") || f.name.endsWith(".js")).map((f) => ({
|
|
567
|
-
id: f.reference,
|
|
568
|
-
name: f.path
|
|
569
|
-
// Use full path to support scripts in subfolders
|
|
570
|
-
}));
|
|
571
|
-
}
|
|
572
|
-
// ==================== Model Export API ====================
|
|
573
|
-
/**
|
|
574
|
-
* Export the model structure. By default returns tables (without
|
|
575
|
-
* members) and variables — enough for `farseer variables list` /
|
|
576
|
-
* `farseer tables list` without dragging the full member matrix.
|
|
577
|
-
*
|
|
578
|
-
* Pass `types` to scope further. Pass `tableGids` / `variableGids`
|
|
579
|
-
* to limit member/variable expansion to specific objects (used by
|
|
580
|
-
* `dimension details <name>` so we hit one table's members instead
|
|
581
|
-
* of all of them).
|
|
582
|
-
*
|
|
583
|
-
* Why this matters: an unscoped `['DTAB','DMEM','VAR']` export
|
|
584
|
-
* pulls every member of every dimension over the wire. On larger
|
|
585
|
-
* tenants the JSON gets big enough to exceed Cloud Run / proxy
|
|
586
|
-
* response limits, so the client sees "socket hang up" before any
|
|
587
|
-
* data arrives. The server-side filter is honoured all the way down
|
|
588
|
-
* to rama (modelMapping.ts → modelService2.modelExport), so a
|
|
589
|
-
* scoped request is cheap.
|
|
590
|
-
*/
|
|
591
|
-
async exportModel(options = {}) {
|
|
592
|
-
const types = options.types ?? ["DTAB", "VAR"];
|
|
593
|
-
const requestBody = { types };
|
|
594
|
-
if (options.tableGids && options.tableGids.length > 0) {
|
|
595
|
-
requestBody.tableGids = options.tableGids;
|
|
596
|
-
}
|
|
597
|
-
if (options.variableGids && options.variableGids.length > 0) {
|
|
598
|
-
requestBody.variableGids = options.variableGids;
|
|
599
|
-
}
|
|
600
|
-
try {
|
|
601
|
-
const response = await this.client.post("/model/export", requestBody);
|
|
602
|
-
return response.data;
|
|
603
|
-
} catch (err) {
|
|
604
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
605
|
-
import_logger.logger.dim(`/model/export failed (${message}); falling back to initTagMap.`);
|
|
606
|
-
return this.buildModelFromTagMaps(options);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
/**
|
|
610
|
-
* Fetch one dimension table with its members through the
|
|
611
|
-
* entities API (search → getRelations → getMany). This stays
|
|
612
|
-
* scoped to a single table, unlike `/model/export` with
|
|
613
|
-
* `tableGids` filter which currently 500s for some tables on
|
|
614
|
-
* large tenants (rama-side scoping bug, tracked separately).
|
|
615
|
-
*/
|
|
616
|
-
async loadDimensionTableWithMembers(name) {
|
|
617
|
-
return loadDimensionTableViaEntitiesApi(this.makeNativeClient(), name);
|
|
618
|
-
}
|
|
619
|
-
makeNativeClient() {
|
|
620
|
-
return new farseer.FarseerClient({
|
|
621
|
-
basePath: this.basePath,
|
|
622
|
-
headers: {
|
|
623
|
-
"X-TENANT-ID": this.tenantId,
|
|
624
|
-
Authorization: `Bearer ${this.accessToken}`
|
|
625
|
-
}
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
async buildModelFromTagMaps(options = {}) {
|
|
629
|
-
const types = options.types ?? ["DTAB", "VAR"];
|
|
630
|
-
const wantTables = types.includes("DTAB");
|
|
631
|
-
const wantMembers = types.includes("DMEM");
|
|
632
|
-
const wantVariables = types.includes("VAR");
|
|
633
|
-
const nativeClient = this.makeNativeClient();
|
|
634
|
-
let tagMaps;
|
|
635
|
-
try {
|
|
636
|
-
tagMaps = await nativeClient.initTagMap();
|
|
637
|
-
} catch (err) {
|
|
638
|
-
if (err && typeof err.status === "number" && typeof err.url === "string") {
|
|
639
|
-
const text = typeof err.text === "function" ? await err.text().catch(() => "") : "";
|
|
640
|
-
throw new Error(`initTagMap failed: HTTP ${err.status} ${text}`.trim());
|
|
641
|
-
}
|
|
642
|
-
throw err;
|
|
643
|
-
}
|
|
644
|
-
const tables = [];
|
|
645
|
-
if (wantTables) {
|
|
646
|
-
const tableGidFilter = options.tableGids && options.tableGids.length > 0 ? new Set(options.tableGids) : null;
|
|
647
|
-
for (const [name, tag] of tagMaps.dimensionTableMapByName) {
|
|
648
|
-
if (tableGidFilter && !tableGidFilter.has(tag.id)) continue;
|
|
649
|
-
const members = wantMembers ? tagMaps.dimensionMemberMapsByTableId.get(tag.id) : void 0;
|
|
650
|
-
tables.push({
|
|
651
|
-
gid: tag.id,
|
|
652
|
-
name,
|
|
653
|
-
columns: tag.columns?.map((c) => ({
|
|
654
|
-
name: c.name,
|
|
655
|
-
type: c.foreignKey ? "FOREIGN_KEY" : c.primaryKey ? "PRIMARY_KEY" : String(c.type),
|
|
656
|
-
foreignKeyTableName: c.foreignKey ? tagMaps.mapById.get(c.refTableGid)?.name : void 0
|
|
657
|
-
})) ?? [],
|
|
658
|
-
rows: members ? Array.from(members.values()).map((m) => [m.name]) : []
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
const variables = [];
|
|
663
|
-
if (wantVariables) {
|
|
664
|
-
const variableGidFilter = options.variableGids && options.variableGids.length > 0 ? new Set(options.variableGids) : null;
|
|
665
|
-
for (const [name, tag] of tagMaps.variableMapByName) {
|
|
666
|
-
if (variableGidFilter && !variableGidFilter.has(tag.id)) continue;
|
|
667
|
-
const dimNames = (tag.dimensionTagIds ?? []).map((id) => tagMaps.mapById.get(id)?.name).filter((n) => Boolean(n));
|
|
668
|
-
variables.push({
|
|
669
|
-
gid: tag.id,
|
|
670
|
-
name,
|
|
671
|
-
tables: dimNames,
|
|
672
|
-
members: [],
|
|
673
|
-
rollupType: tag.rollupType ?? "SUM",
|
|
674
|
-
dataType: tag.dataType ?? "DECIMAL",
|
|
675
|
-
readonly: tag.readonly ?? false,
|
|
676
|
-
calculateOnDemand: tag.calculateOnDemand ?? false,
|
|
677
|
-
formula: tag.expression ?? null,
|
|
678
|
-
description: tag.description ?? null
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
return { tables, variables, dependencies: { tables: [], members: [], variables: [] } };
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
async function loadDimensionTableViaEntitiesApi(nativeClient, name) {
|
|
686
|
-
const searchResult = await nativeClient.entities.search({
|
|
687
|
-
name,
|
|
688
|
-
types: [farseer.EntityType.DimensionTable],
|
|
689
|
-
limit: 1
|
|
690
|
-
});
|
|
691
|
-
if (searchResult.length < 1) {
|
|
692
|
-
return null;
|
|
693
|
-
}
|
|
694
|
-
const loaded = await nativeClient.data.loadFarseerDimensionTable(name);
|
|
695
|
-
const objects = loaded.table.objects();
|
|
696
|
-
const columns = loaded.metadata.map((c) => ({
|
|
697
|
-
name: c.name,
|
|
698
|
-
type: c.type,
|
|
699
|
-
foreignKeyTableName: c.foreignKeyTableName
|
|
700
|
-
}));
|
|
701
|
-
const rows = objects.map((obj) => columns.map((col) => obj[col.name ?? ""]));
|
|
702
|
-
return { name, columns, rows };
|
|
703
|
-
}
|
|
704
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
705
|
-
0 && (module.exports = {
|
|
706
|
-
AppDeleteRefusedError,
|
|
707
|
-
FarseerApi,
|
|
708
|
-
REMOTE_JOB_ITEM_TYPE,
|
|
709
|
-
applySourceFolderChange,
|
|
710
|
-
findScriptInFolder,
|
|
711
|
-
loadDimensionTableViaEntitiesApi,
|
|
712
|
-
mergeAppUpdate,
|
|
713
|
-
remoteJobReverseReferences,
|
|
714
|
-
resolveAppItemNodeId
|
|
715
|
-
});
|
|
716
|
-
//# sourceMappingURL=farseerApi.js.map
|