farseer-cli 1.0.8 → 1.0.10
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/dist/commands/app.js +377 -0
- package/dist/commands/apps.js +91 -0
- package/dist/commands/checkout.js +164 -0
- package/dist/commands/config.js +141 -0
- package/dist/commands/diff.js +210 -0
- package/dist/commands/dimensions.js +130 -0
- package/dist/commands/files.js +88 -0
- package/dist/commands/install.js +82 -0
- package/dist/commands/list.js +77 -0
- package/dist/commands/login.js +109 -0
- package/dist/commands/logout.js +44 -0
- package/dist/commands/mcp-server.js +26 -0
- package/dist/commands/model.js +181 -0
- package/dist/commands/pull.js +286 -0
- package/dist/commands/push.js +235 -0
- package/dist/commands/run.js +336 -0
- package/dist/commands/status.js +178 -0
- package/dist/commands/variables.js +200 -0
- package/dist/commands/whoami.js +47 -0
- package/dist/index.js +163 -0
- package/dist/mcp/index.js +19 -0
- package/dist/mcp/prompts/workflows.js +403 -0
- package/dist/mcp/resources/documentation.js +158 -0
- package/dist/mcp/server.js +36 -0
- package/dist/mcp/tools/appTools.js +348 -0
- package/dist/mcp/tools/authTools.js +152 -0
- package/dist/mcp/tools/dataTools.js +358 -0
- package/dist/mcp/tools/modelTools.js +293 -0
- package/dist/mcp/tools/runTools.js +239 -0
- package/dist/mcp/tools/syncTools.js +383 -0
- package/dist/mcp/utils/helpers.js +142 -0
- package/dist/services/appSyncService.js +458 -0
- package/dist/services/configService.js +186 -0
- package/dist/services/farseerApi.js +426 -0
- package/dist/services/farseerFactory.js +297 -0
- package/dist/services/farseerService.js +647 -0
- package/dist/services/syncService.js +764 -0
- package/dist/utils/constants.js +21 -0
- package/dist/utils/helpers.js +446 -0
- package/dist/utils/logger.js +45 -0
- package/package.json +8 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.loadConfig = loadConfig;
|
|
37
|
+
exports.saveConfig = saveConfig;
|
|
38
|
+
exports.getCredential = getCredential;
|
|
39
|
+
exports.setCredential = setCredential;
|
|
40
|
+
exports.removeCredential = removeCredential;
|
|
41
|
+
exports.listCredentials = listCredentials;
|
|
42
|
+
exports.getUserAuth = getUserAuth;
|
|
43
|
+
exports.setUserAuth = setUserAuth;
|
|
44
|
+
exports.clearUserAuth = clearUserAuth;
|
|
45
|
+
exports.isUserAuthValid = isUserAuthValid;
|
|
46
|
+
exports.generateBasePath = generateBasePath;
|
|
47
|
+
exports.getCurrentCheckout = getCurrentCheckout;
|
|
48
|
+
exports.getCurrentTenant = getCurrentTenant;
|
|
49
|
+
exports.getCurrentOrganisation = getCurrentOrganisation;
|
|
50
|
+
exports.setCurrentCheckout = setCurrentCheckout;
|
|
51
|
+
exports.setCurrentTenant = setCurrentTenant;
|
|
52
|
+
exports.clearCurrentTenant = clearCurrentTenant;
|
|
53
|
+
exports.saveTenantOrgMapping = saveTenantOrgMapping;
|
|
54
|
+
exports.getTenantOrg = getTenantOrg;
|
|
55
|
+
const fs = __importStar(require("fs"));
|
|
56
|
+
const path = __importStar(require("path"));
|
|
57
|
+
const os = __importStar(require("os"));
|
|
58
|
+
const CONFIG_DIR = path.join(os.homedir(), '.farseer');
|
|
59
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
60
|
+
function ensureConfigDir() {
|
|
61
|
+
if (!fs.existsSync(CONFIG_DIR)) {
|
|
62
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function getDefaultConfig() {
|
|
66
|
+
return {
|
|
67
|
+
credentials: {},
|
|
68
|
+
userAuth: undefined,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function loadConfig() {
|
|
72
|
+
ensureConfigDir();
|
|
73
|
+
if (!fs.existsSync(CONFIG_FILE)) {
|
|
74
|
+
return getDefaultConfig();
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
|
|
78
|
+
return JSON.parse(content);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return getDefaultConfig();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function saveConfig(config) {
|
|
85
|
+
ensureConfigDir();
|
|
86
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf-8');
|
|
87
|
+
}
|
|
88
|
+
function getCredential(tenant) {
|
|
89
|
+
const config = loadConfig();
|
|
90
|
+
return config.credentials[tenant];
|
|
91
|
+
}
|
|
92
|
+
function setCredential(tenant, credential) {
|
|
93
|
+
const config = loadConfig();
|
|
94
|
+
config.credentials[tenant] = credential;
|
|
95
|
+
saveConfig(config);
|
|
96
|
+
}
|
|
97
|
+
function removeCredential(tenant) {
|
|
98
|
+
const config = loadConfig();
|
|
99
|
+
if (config.credentials[tenant]) {
|
|
100
|
+
delete config.credentials[tenant];
|
|
101
|
+
saveConfig(config);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
function listCredentials() {
|
|
107
|
+
const config = loadConfig();
|
|
108
|
+
return config.credentials;
|
|
109
|
+
}
|
|
110
|
+
function getUserAuth() {
|
|
111
|
+
const config = loadConfig();
|
|
112
|
+
return config.userAuth;
|
|
113
|
+
}
|
|
114
|
+
function setUserAuth(auth) {
|
|
115
|
+
const config = loadConfig();
|
|
116
|
+
config.userAuth = auth;
|
|
117
|
+
saveConfig(config);
|
|
118
|
+
}
|
|
119
|
+
function clearUserAuth() {
|
|
120
|
+
const config = loadConfig();
|
|
121
|
+
config.userAuth = undefined;
|
|
122
|
+
saveConfig(config);
|
|
123
|
+
}
|
|
124
|
+
function isUserAuthValid() {
|
|
125
|
+
const auth = getUserAuth();
|
|
126
|
+
if (!auth)
|
|
127
|
+
return false;
|
|
128
|
+
const expiresAt = new Date(auth.expiresAt);
|
|
129
|
+
// Add 10 second buffer to proactively refresh before actual expiry
|
|
130
|
+
// Note: Keycloak tokens typically last 60 seconds, so buffer must be small
|
|
131
|
+
const bufferMs = 10 * 1000;
|
|
132
|
+
return expiresAt.getTime() > Date.now() + bufferMs;
|
|
133
|
+
}
|
|
134
|
+
function generateBasePath(organisation) {
|
|
135
|
+
return `https://${organisation}.farseer.io/api/v3`;
|
|
136
|
+
}
|
|
137
|
+
// Current checkout management
|
|
138
|
+
function getCurrentCheckout() {
|
|
139
|
+
const config = loadConfig();
|
|
140
|
+
// Support both new and old format
|
|
141
|
+
if (config.currentCheckout) {
|
|
142
|
+
return config.currentCheckout;
|
|
143
|
+
}
|
|
144
|
+
// Fallback to old format (tenant only, assume same as org)
|
|
145
|
+
if (config.currentTenant) {
|
|
146
|
+
return { organisation: config.currentTenant, tenant: config.currentTenant };
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
function getCurrentTenant() {
|
|
151
|
+
const checkout = getCurrentCheckout();
|
|
152
|
+
return checkout?.tenant;
|
|
153
|
+
}
|
|
154
|
+
function getCurrentOrganisation() {
|
|
155
|
+
const checkout = getCurrentCheckout();
|
|
156
|
+
return checkout?.organisation;
|
|
157
|
+
}
|
|
158
|
+
function setCurrentCheckout(organisation, tenant) {
|
|
159
|
+
const config = loadConfig();
|
|
160
|
+
config.currentCheckout = { organisation, tenant };
|
|
161
|
+
config.currentTenant = tenant; // Keep for backward compatibility
|
|
162
|
+
saveConfig(config);
|
|
163
|
+
}
|
|
164
|
+
function setCurrentTenant(tenant) {
|
|
165
|
+
// Assume organisation = tenant when only tenant is specified
|
|
166
|
+
setCurrentCheckout(tenant, tenant);
|
|
167
|
+
}
|
|
168
|
+
function clearCurrentTenant() {
|
|
169
|
+
const config = loadConfig();
|
|
170
|
+
config.currentTenant = undefined;
|
|
171
|
+
config.currentCheckout = undefined;
|
|
172
|
+
saveConfig(config);
|
|
173
|
+
}
|
|
174
|
+
// Tenant-Organisation mapping
|
|
175
|
+
function saveTenantOrgMapping(tenant, organisation) {
|
|
176
|
+
const config = loadConfig();
|
|
177
|
+
if (!config.tenantOrgMapping) {
|
|
178
|
+
config.tenantOrgMapping = {};
|
|
179
|
+
}
|
|
180
|
+
config.tenantOrgMapping[tenant] = organisation;
|
|
181
|
+
saveConfig(config);
|
|
182
|
+
}
|
|
183
|
+
function getTenantOrg(tenant) {
|
|
184
|
+
const config = loadConfig();
|
|
185
|
+
return config.tenantOrgMapping?.[tenant];
|
|
186
|
+
}
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* FarseerApi - Direct HTTP API client using JWT Bearer token authentication
|
|
4
|
+
*
|
|
5
|
+
* This is an alternative to FarseerService that doesn't use farseer-client library.
|
|
6
|
+
* Use this when authenticating via browser login (Keycloak JWT token) instead of API key.
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FarseerApi = void 0;
|
|
13
|
+
const axios_1 = __importDefault(require("axios"));
|
|
14
|
+
const configService_1 = require("./configService");
|
|
15
|
+
const farseerService_1 = require("./farseerService");
|
|
16
|
+
class FarseerApi {
|
|
17
|
+
constructor(accessToken, tenant, tenantId) {
|
|
18
|
+
this.isRefreshing = false;
|
|
19
|
+
this.refreshSubscribers = [];
|
|
20
|
+
this.tenant = tenant;
|
|
21
|
+
this.tenantId = tenantId || tenant;
|
|
22
|
+
// Try to get basePath from existing config, or generate from tenant name
|
|
23
|
+
const credential = (0, configService_1.getCredential)(tenant);
|
|
24
|
+
const basePath = credential?.basePath || `https://${tenant}.farseer.io/api/v3`;
|
|
25
|
+
this.client = axios_1.default.create({
|
|
26
|
+
baseURL: basePath,
|
|
27
|
+
headers: {
|
|
28
|
+
Authorization: `Bearer ${accessToken}`,
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
'x-api-version': '3.3.0',
|
|
31
|
+
'X-TENANT-ID': this.tenantId,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
// Add response interceptor for automatic token refresh on 401
|
|
35
|
+
this.client.interceptors.response.use((response) => response, async (error) => {
|
|
36
|
+
const originalRequest = error.config;
|
|
37
|
+
// Only handle 401 errors and avoid infinite loops
|
|
38
|
+
if (error.response?.status === 401 && !originalRequest._retry) {
|
|
39
|
+
originalRequest._retry = true;
|
|
40
|
+
// If already refreshing, wait for the new token
|
|
41
|
+
if (this.isRefreshing) {
|
|
42
|
+
return new Promise((resolve) => {
|
|
43
|
+
this.refreshSubscribers.push((token) => {
|
|
44
|
+
originalRequest.headers['Authorization'] = `Bearer ${token}`;
|
|
45
|
+
resolve(this.client(originalRequest));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
this.isRefreshing = true;
|
|
50
|
+
try {
|
|
51
|
+
const auth = (0, configService_1.getUserAuth)();
|
|
52
|
+
if (!auth?.refreshToken) {
|
|
53
|
+
throw new Error('No refresh token available');
|
|
54
|
+
}
|
|
55
|
+
const refreshed = await (0, farseerService_1.refreshAccessToken)(auth.refreshToken, auth.realm || 'master');
|
|
56
|
+
if (!refreshed) {
|
|
57
|
+
throw new Error('Token refresh failed');
|
|
58
|
+
}
|
|
59
|
+
// Save the new token
|
|
60
|
+
(0, configService_1.setUserAuth)({
|
|
61
|
+
accessToken: refreshed.accessToken,
|
|
62
|
+
refreshToken: refreshed.refreshToken,
|
|
63
|
+
expiresAt: new Date(Date.now() + refreshed.expiresIn * 1000).toISOString(),
|
|
64
|
+
realm: auth.realm,
|
|
65
|
+
});
|
|
66
|
+
// Update this client's token
|
|
67
|
+
this.updateAccessToken(refreshed.accessToken);
|
|
68
|
+
// Notify all waiting requests
|
|
69
|
+
this.refreshSubscribers.forEach((callback) => callback(refreshed.accessToken));
|
|
70
|
+
this.refreshSubscribers = [];
|
|
71
|
+
// Retry the original request
|
|
72
|
+
originalRequest.headers['Authorization'] = `Bearer ${refreshed.accessToken}`;
|
|
73
|
+
return this.client(originalRequest);
|
|
74
|
+
}
|
|
75
|
+
catch (refreshError) {
|
|
76
|
+
// Token refresh failed - let the error propagate
|
|
77
|
+
this.refreshSubscribers = [];
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
this.isRefreshing = false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return Promise.reject(error);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Update the access token (used when token is refreshed)
|
|
89
|
+
*/
|
|
90
|
+
updateAccessToken(newToken) {
|
|
91
|
+
this.client.defaults.headers['Authorization'] = `Bearer ${newToken}`;
|
|
92
|
+
}
|
|
93
|
+
async testConnection() {
|
|
94
|
+
try {
|
|
95
|
+
await this.getItemByPath(['Files']);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async getFilesFolder() {
|
|
103
|
+
try {
|
|
104
|
+
const item = await this.getItemByPath(['Files']);
|
|
105
|
+
return {
|
|
106
|
+
id: item.id,
|
|
107
|
+
name: item.name,
|
|
108
|
+
type: item.type,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async listFolderItems(folderId) {
|
|
116
|
+
// Body is array: [{ id: null }] for root, [{ id: folderId }] for specific folder
|
|
117
|
+
const response = await this.client.post('/folders/items', [{ id: folderId }]);
|
|
118
|
+
return response.data[0] || { items: [] };
|
|
119
|
+
}
|
|
120
|
+
async findFolderByName(parentId, name) {
|
|
121
|
+
const content = await this.listFolderItems(parentId);
|
|
122
|
+
const folder = content.items?.find((item) => item.name.toLowerCase() === name.toLowerCase() && item.type.toLowerCase() === 'folder');
|
|
123
|
+
return folder || null;
|
|
124
|
+
}
|
|
125
|
+
async getItemByPath(path) {
|
|
126
|
+
let currentId = null;
|
|
127
|
+
for (const segment of path) {
|
|
128
|
+
const folder = await this.findFolderByName(currentId, segment);
|
|
129
|
+
if (!folder) {
|
|
130
|
+
throw new Error(`Folder not found: ${segment}`);
|
|
131
|
+
}
|
|
132
|
+
currentId = folder.id;
|
|
133
|
+
}
|
|
134
|
+
if (currentId === null) {
|
|
135
|
+
throw new Error('Path is empty');
|
|
136
|
+
}
|
|
137
|
+
// Return a minimal FolderItem with the found id
|
|
138
|
+
return { id: currentId, name: path[path.length - 1], type: 'folder' };
|
|
139
|
+
}
|
|
140
|
+
async listFiles(folderPath = ['Files']) {
|
|
141
|
+
const files = [];
|
|
142
|
+
try {
|
|
143
|
+
const folder = await this.getItemByPath(folderPath);
|
|
144
|
+
const folderContent = await this.listFolderItems(folder.id);
|
|
145
|
+
for (const item of folderContent.items || []) {
|
|
146
|
+
const itemPath = [...folderPath.slice(1), item.name].join('/');
|
|
147
|
+
const itemType = item.type.toLowerCase();
|
|
148
|
+
if (itemType === 'folder') {
|
|
149
|
+
const subFiles = await this.listFiles([...folderPath, item.name]);
|
|
150
|
+
files.push(...subFiles);
|
|
151
|
+
}
|
|
152
|
+
else if ((itemType === 'farseer-file' || itemType === 'farseer_file') && item.reference) {
|
|
153
|
+
files.push({
|
|
154
|
+
name: item.name,
|
|
155
|
+
path: itemPath,
|
|
156
|
+
reference: item.reference,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
163
|
+
const status = error.response?.status;
|
|
164
|
+
const data = error.response?.data;
|
|
165
|
+
const message = typeof data === 'string' ? data : data?.message || JSON.stringify(data);
|
|
166
|
+
console.error(`API Error: ${status} "${message}" (path: ${folderPath.join('/')})`);
|
|
167
|
+
if (data?.code === 'Forbidden') {
|
|
168
|
+
const errorMessage = data?.message || 'Access denied';
|
|
169
|
+
throw new Error(`Access denied: ${errorMessage}\nYou may need to use --tenant-id to specify a different tenant ID.`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
return files;
|
|
175
|
+
}
|
|
176
|
+
async getFileContent(reference) {
|
|
177
|
+
const response = await this.client.get(`/farseerFiles/${reference}`, {
|
|
178
|
+
responseType: 'text',
|
|
179
|
+
});
|
|
180
|
+
return response.data;
|
|
181
|
+
}
|
|
182
|
+
async getFileContentAsBuffer(reference) {
|
|
183
|
+
const response = await this.client.get(`/farseerFiles/${reference}`, {
|
|
184
|
+
responseType: 'arraybuffer',
|
|
185
|
+
});
|
|
186
|
+
return Buffer.from(response.data);
|
|
187
|
+
}
|
|
188
|
+
async createFile(content, fileName, folderPath = ['Files']) {
|
|
189
|
+
await this.ensureFolderPath(folderPath);
|
|
190
|
+
const folder = await this.getItemByPath(folderPath);
|
|
191
|
+
const formData = new FormData();
|
|
192
|
+
const blob = content instanceof Buffer ? new Blob([content]) : new Blob([content]);
|
|
193
|
+
formData.append('file', blob, fileName);
|
|
194
|
+
formData.append('category', 'GENERAL');
|
|
195
|
+
formData.append('folderId', folder.id.toString());
|
|
196
|
+
await this.client.post('/farseerFiles', formData, {
|
|
197
|
+
headers: {
|
|
198
|
+
'Content-Type': 'multipart/form-data',
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
async updateFile(reference, content, fileName) {
|
|
203
|
+
const formData = new FormData();
|
|
204
|
+
const blob = content instanceof Buffer ? new Blob([content]) : new Blob([content]);
|
|
205
|
+
formData.append('file', blob, fileName);
|
|
206
|
+
await this.client.put(`/farseerFiles/${reference}`, formData, {
|
|
207
|
+
headers: {
|
|
208
|
+
'Content-Type': 'multipart/form-data',
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async deleteFile(reference) {
|
|
213
|
+
await this.client.delete(`/farseerFiles/${reference}`);
|
|
214
|
+
}
|
|
215
|
+
async ensureFolderPath(folderPath) {
|
|
216
|
+
const currentPath = [];
|
|
217
|
+
for (const segment of folderPath) {
|
|
218
|
+
currentPath.push(segment);
|
|
219
|
+
try {
|
|
220
|
+
await this.getItemByPath(currentPath);
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
if (currentPath.length > 1) {
|
|
224
|
+
const parentPath = currentPath.slice(0, -1);
|
|
225
|
+
const parent = await this.getItemByPath(parentPath);
|
|
226
|
+
await this.client.post('/folders', {
|
|
227
|
+
parentId: parent.id,
|
|
228
|
+
name: segment,
|
|
229
|
+
allowedTypes: [],
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
async getFileByPath(filePath) {
|
|
236
|
+
const pathParts = ['Files', ...filePath.split('/')];
|
|
237
|
+
const fileName = pathParts.pop();
|
|
238
|
+
const folderPath = pathParts;
|
|
239
|
+
try {
|
|
240
|
+
const folder = await this.getItemByPath(folderPath);
|
|
241
|
+
const folderContent = await this.listFolderItems(folder.id);
|
|
242
|
+
for (const item of folderContent.items || []) {
|
|
243
|
+
const itemType = item.type.toLowerCase();
|
|
244
|
+
if (item.name === fileName &&
|
|
245
|
+
(itemType === 'farseer-file' || itemType === 'farseer_file') &&
|
|
246
|
+
item.reference) {
|
|
247
|
+
return {
|
|
248
|
+
name: item.name,
|
|
249
|
+
path: filePath,
|
|
250
|
+
reference: item.reference,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
async getFileMetadata(reference) {
|
|
261
|
+
try {
|
|
262
|
+
const response = await this.client.get(`/farseerFiles/${reference}/metadata`);
|
|
263
|
+
const data = response.data;
|
|
264
|
+
return {
|
|
265
|
+
uploadTime: data.uploadTime,
|
|
266
|
+
uploader: data.uploader
|
|
267
|
+
? {
|
|
268
|
+
id: data.uploader.id,
|
|
269
|
+
email: data.uploader.email || '',
|
|
270
|
+
firstName: data.uploader.firstName || '',
|
|
271
|
+
lastName: data.uploader.lastName || '',
|
|
272
|
+
}
|
|
273
|
+
: undefined,
|
|
274
|
+
size: data.size,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
// Metadata fetch failed - non-critical, continue without it
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
isTextFile(filename) {
|
|
283
|
+
const ext = filename.toLowerCase().split('.').pop() || '';
|
|
284
|
+
return ['ts', 'js', 'mjs', 'cjs', 'json', 'txt', 'md', 'csv', 'xml', 'html', 'css', 'yaml', 'yml'].includes(ext);
|
|
285
|
+
}
|
|
286
|
+
isBinaryFile(filename) {
|
|
287
|
+
return !this.isTextFile(filename);
|
|
288
|
+
}
|
|
289
|
+
// ==================== Apps (Remote Jobs) API ====================
|
|
290
|
+
/**
|
|
291
|
+
* Get the Apps folder ID
|
|
292
|
+
*/
|
|
293
|
+
async getAppsFolderId() {
|
|
294
|
+
const content = await this.listFolderItems(null);
|
|
295
|
+
const appsFolder = content.items?.find((item) => item.name.toLowerCase() === 'apps' && item.type.toLowerCase() === 'folder');
|
|
296
|
+
return appsFolder?.id || null;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* List all apps (Remote Jobs) on the tenant, including those in subfolders
|
|
300
|
+
*/
|
|
301
|
+
async listApps() {
|
|
302
|
+
const appsFolderId = await this.getAppsFolderId();
|
|
303
|
+
if (!appsFolderId) {
|
|
304
|
+
return [];
|
|
305
|
+
}
|
|
306
|
+
return this.listAppsRecursive(appsFolderId);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Recursively list all apps in a folder and its subfolders
|
|
310
|
+
*/
|
|
311
|
+
async listAppsRecursive(folderId) {
|
|
312
|
+
const apps = [];
|
|
313
|
+
const content = await this.listFolderItems(folderId);
|
|
314
|
+
for (const item of content.items || []) {
|
|
315
|
+
const itemType = item.type.toLowerCase();
|
|
316
|
+
if (itemType === 'remote_job') {
|
|
317
|
+
// Found an app
|
|
318
|
+
apps.push({
|
|
319
|
+
id: item.id,
|
|
320
|
+
name: item.name,
|
|
321
|
+
type: item.type,
|
|
322
|
+
reference: item.reference || '',
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
else if (itemType === 'folder') {
|
|
326
|
+
// Recurse into subfolder
|
|
327
|
+
const subApps = await this.listAppsRecursive(item.id);
|
|
328
|
+
apps.push(...subApps);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return apps;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Get app details by reference ID
|
|
335
|
+
*/
|
|
336
|
+
async getApp(referenceId) {
|
|
337
|
+
try {
|
|
338
|
+
const response = await this.client.get(`/remoteJobs/${referenceId}`);
|
|
339
|
+
return response.data;
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Get app by name
|
|
347
|
+
*/
|
|
348
|
+
async getAppByName(name) {
|
|
349
|
+
const apps = await this.listApps();
|
|
350
|
+
const app = apps.find((a) => a.name.toLowerCase() === name.toLowerCase());
|
|
351
|
+
if (!app) {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
return this.getApp(app.reference);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Create a new app
|
|
358
|
+
*/
|
|
359
|
+
async createApp(name) {
|
|
360
|
+
const appsFolderId = await this.getAppsFolderId();
|
|
361
|
+
if (!appsFolderId) {
|
|
362
|
+
throw new Error('Apps folder not found');
|
|
363
|
+
}
|
|
364
|
+
const response = await this.client.post('/remoteJobs', {
|
|
365
|
+
folderId: appsFolderId,
|
|
366
|
+
name,
|
|
367
|
+
});
|
|
368
|
+
return response.data;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Update an app
|
|
372
|
+
*/
|
|
373
|
+
async updateApp(referenceId, update) {
|
|
374
|
+
await this.client.put(`/remoteJobs/${referenceId}`, {
|
|
375
|
+
...update,
|
|
376
|
+
schedule: null,
|
|
377
|
+
runnerName: null,
|
|
378
|
+
predefinedActions: [],
|
|
379
|
+
});
|
|
380
|
+
// API returns 204 No Content, so fetch the updated app
|
|
381
|
+
const app = await this.getApp(referenceId);
|
|
382
|
+
if (!app) {
|
|
383
|
+
throw new Error('Failed to fetch updated app');
|
|
384
|
+
}
|
|
385
|
+
return app;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Delete an app
|
|
389
|
+
*/
|
|
390
|
+
async deleteApp(referenceId) {
|
|
391
|
+
await this.client.delete(`/remoteJobs/${referenceId}`);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Run an app with arguments
|
|
395
|
+
*/
|
|
396
|
+
async runApp(referenceId, args = {}) {
|
|
397
|
+
const response = await this.client.post(`/remoteJobs/${referenceId}/run`, {
|
|
398
|
+
arguments: args,
|
|
399
|
+
});
|
|
400
|
+
return response.data;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Get script files available for apps (searches all .ts/.js files in Files folder)
|
|
404
|
+
* Returns full path relative to Files/ folder (e.g., "Scripts/test.ts", "Scripts/subfolder/helper.ts")
|
|
405
|
+
*/
|
|
406
|
+
async getScriptFiles() {
|
|
407
|
+
// listFiles already searches recursively, so just filter for .ts/.js files
|
|
408
|
+
const allFiles = await this.listFiles(['Files']);
|
|
409
|
+
return allFiles
|
|
410
|
+
.filter((f) => f.name.endsWith('.ts') || f.name.endsWith('.js'))
|
|
411
|
+
.map((f) => ({
|
|
412
|
+
id: f.reference,
|
|
413
|
+
name: f.path, // Use full path to support scripts in subfolders
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
// ==================== Model Export API ====================
|
|
417
|
+
/**
|
|
418
|
+
* Export the entire model structure - all dimension tables, variables, and configurations.
|
|
419
|
+
* Essential for understanding a tenant's data model.
|
|
420
|
+
*/
|
|
421
|
+
async exportModel() {
|
|
422
|
+
const response = await this.client.post('/model/export');
|
|
423
|
+
return response.data;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
exports.FarseerApi = FarseerApi;
|