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.
Files changed (41) hide show
  1. package/dist/commands/app.js +377 -0
  2. package/dist/commands/apps.js +91 -0
  3. package/dist/commands/checkout.js +164 -0
  4. package/dist/commands/config.js +141 -0
  5. package/dist/commands/diff.js +210 -0
  6. package/dist/commands/dimensions.js +130 -0
  7. package/dist/commands/files.js +88 -0
  8. package/dist/commands/install.js +82 -0
  9. package/dist/commands/list.js +77 -0
  10. package/dist/commands/login.js +109 -0
  11. package/dist/commands/logout.js +44 -0
  12. package/dist/commands/mcp-server.js +26 -0
  13. package/dist/commands/model.js +181 -0
  14. package/dist/commands/pull.js +286 -0
  15. package/dist/commands/push.js +235 -0
  16. package/dist/commands/run.js +336 -0
  17. package/dist/commands/status.js +178 -0
  18. package/dist/commands/variables.js +200 -0
  19. package/dist/commands/whoami.js +47 -0
  20. package/dist/index.js +163 -0
  21. package/dist/mcp/index.js +19 -0
  22. package/dist/mcp/prompts/workflows.js +403 -0
  23. package/dist/mcp/resources/documentation.js +158 -0
  24. package/dist/mcp/server.js +36 -0
  25. package/dist/mcp/tools/appTools.js +348 -0
  26. package/dist/mcp/tools/authTools.js +152 -0
  27. package/dist/mcp/tools/dataTools.js +358 -0
  28. package/dist/mcp/tools/modelTools.js +293 -0
  29. package/dist/mcp/tools/runTools.js +239 -0
  30. package/dist/mcp/tools/syncTools.js +383 -0
  31. package/dist/mcp/utils/helpers.js +142 -0
  32. package/dist/services/appSyncService.js +458 -0
  33. package/dist/services/configService.js +186 -0
  34. package/dist/services/farseerApi.js +426 -0
  35. package/dist/services/farseerFactory.js +297 -0
  36. package/dist/services/farseerService.js +647 -0
  37. package/dist/services/syncService.js +764 -0
  38. package/dist/utils/constants.js +21 -0
  39. package/dist/utils/helpers.js +446 -0
  40. package/dist/utils/logger.js +45 -0
  41. package/package.json +8 -1
@@ -0,0 +1,297 @@
1
+ "use strict";
2
+ /**
3
+ * Factory for creating Farseer API client based on available authentication
4
+ *
5
+ * Priority:
6
+ * 1. JWT token (from browser login) - try first
7
+ * 2. API key (from config) - fallback if JWT fails or unavailable
8
+ * 3. Prompt user to login - if nothing works
9
+ */
10
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ var desc = Object.getOwnPropertyDescriptor(m, k);
13
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
14
+ desc = { enumerable: true, get: function() { return m[k]; } };
15
+ }
16
+ Object.defineProperty(o, k2, desc);
17
+ }) : (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ o[k2] = m[k];
20
+ }));
21
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
22
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
23
+ }) : function(o, v) {
24
+ o["default"] = v;
25
+ });
26
+ var __importStar = (this && this.__importStar) || (function () {
27
+ var ownKeys = function(o) {
28
+ ownKeys = Object.getOwnPropertyNames || function (o) {
29
+ var ar = [];
30
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
31
+ return ar;
32
+ };
33
+ return ownKeys(o);
34
+ };
35
+ return function (mod) {
36
+ if (mod && mod.__esModule) return mod;
37
+ var result = {};
38
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
39
+ __setModuleDefault(result, mod);
40
+ return result;
41
+ };
42
+ })();
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.DEFAULT_SCRIPT_EXTENSIONS = void 0;
45
+ exports.isAuthError = isAuthError;
46
+ exports.showLoginPrompt = showLoginPrompt;
47
+ exports.getFarseerClient = getFarseerClient;
48
+ exports.getAvailableAuth = getAvailableAuth;
49
+ exports.getFarseerClientWithFallback = getFarseerClientWithFallback;
50
+ exports.ensureFreshToken = ensureFreshToken;
51
+ exports.getNativeFarseerClient = getNativeFarseerClient;
52
+ const farseer = __importStar(require("farseer-client"));
53
+ const farseerService_1 = require("./farseerService");
54
+ const farseerApi_1 = require("./farseerApi");
55
+ const configService_1 = require("./configService");
56
+ const farseerService_2 = require("./farseerService");
57
+ const logger_1 = require("../utils/logger");
58
+ /**
59
+ * Check if an error is an authentication error (401/403)
60
+ */
61
+ function isAuthError(error) {
62
+ if (error instanceof Error) {
63
+ const msg = error.message.toLowerCase();
64
+ return msg.includes('401') || msg.includes('403') || msg.includes('unauthorized') || msg.includes('forbidden');
65
+ }
66
+ return false;
67
+ }
68
+ /**
69
+ * Show a consistent "please login" message
70
+ */
71
+ function showLoginPrompt(tenant) {
72
+ logger_1.logger.error('Authentication required.');
73
+ if (tenant) {
74
+ logger_1.logger.dim(`No valid credentials found for tenant "${tenant}".`);
75
+ }
76
+ logger_1.logger.info('Run "farseer login" to authenticate via browser.');
77
+ logger_1.logger.dim('Or use "farseer config set <tenant> --api-key <key>" for API key auth.');
78
+ }
79
+ // Default file extensions to sync (scripts only)
80
+ exports.DEFAULT_SCRIPT_EXTENSIONS = ['.ts', '.js', '.mjs', '.cjs'];
81
+ /**
82
+ * Get a Farseer client for the given organisation and tenant.
83
+ * Tries JWT token first (supports apps), then falls back to API key.
84
+ *
85
+ * @param organisation - The organisation hostname (e.g., "jgl" for jgl.farseer.io)
86
+ * @param tenant - Tenant name for X-TENANT-ID header (e.g., "tt-hotels-hr-dev")
87
+ */
88
+ async function getFarseerClient(organisation, tenant) {
89
+ // 1. Try JWT token from browser login first (supports apps sync)
90
+ const auth = (0, configService_1.getUserAuth)();
91
+ if (auth) {
92
+ // Check if token needs refresh
93
+ let accessToken = auth.accessToken;
94
+ if (!(0, configService_1.isUserAuthValid)()) {
95
+ logger_1.logger.dim('Token expired, refreshing...');
96
+ const refreshed = await (0, farseerService_2.refreshAccessToken)(auth.refreshToken, auth.realm || 'master');
97
+ if (!refreshed) {
98
+ logger_1.logger.warning('Could not refresh token. Please login again with: farseer login');
99
+ // Fall through to API key
100
+ }
101
+ else {
102
+ // Save refreshed token
103
+ (0, configService_1.setUserAuth)({
104
+ accessToken: refreshed.accessToken,
105
+ refreshToken: refreshed.refreshToken,
106
+ expiresAt: new Date(Date.now() + refreshed.expiresIn * 1000).toISOString(),
107
+ realm: auth.realm,
108
+ });
109
+ accessToken = refreshed.accessToken;
110
+ logger_1.logger.dim('Token refreshed');
111
+ }
112
+ }
113
+ if ((0, configService_1.isUserAuthValid)()) {
114
+ return {
115
+ client: new farseerApi_1.FarseerApi(accessToken, organisation, tenant),
116
+ authType: 'jwt',
117
+ };
118
+ }
119
+ }
120
+ // 2. Fall back to API key config
121
+ const credential = (0, configService_1.getCredential)(tenant);
122
+ if (credential) {
123
+ return {
124
+ client: new farseerService_1.FarseerService(credential),
125
+ authType: 'apiKey',
126
+ };
127
+ }
128
+ return null;
129
+ }
130
+ /**
131
+ * Check what authentication methods are available
132
+ */
133
+ function getAvailableAuth(tenant) {
134
+ const credential = (0, configService_1.getCredential)(tenant);
135
+ const auth = (0, configService_1.getUserAuth)();
136
+ return {
137
+ hasApiKey: !!credential,
138
+ hasJwt: !!auth,
139
+ jwtValid: (0, configService_1.isUserAuthValid)(),
140
+ };
141
+ }
142
+ /**
143
+ * Get a Farseer client with automatic fallback and connection testing.
144
+ * This is the recommended way to get a client for commands.
145
+ *
146
+ * Flow:
147
+ * 1. Try JWT if available and valid
148
+ * 2. If JWT fails or unavailable, try API key
149
+ * 3. If nothing works, return null (caller should show login prompt)
150
+ *
151
+ * @param organisation - The organisation hostname (e.g., "jgl" for jgl.farseer.io)
152
+ * @param tenant - Tenant name for X-TENANT-ID header (e.g., "tt-hotels-hr-dev")
153
+ */
154
+ async function getFarseerClientWithFallback(organisation, tenant) {
155
+ const auth = (0, configService_1.getUserAuth)();
156
+ const credential = (0, configService_1.getCredential)(tenant);
157
+ // 1. Try JWT token first
158
+ if (auth) {
159
+ let accessToken = auth.accessToken;
160
+ // Refresh token if needed
161
+ if (!(0, configService_1.isUserAuthValid)()) {
162
+ logger_1.logger.dim('Token expired, refreshing...');
163
+ const refreshed = await (0, farseerService_2.refreshAccessToken)(auth.refreshToken, auth.realm || 'master');
164
+ if (refreshed) {
165
+ (0, configService_1.setUserAuth)({
166
+ accessToken: refreshed.accessToken,
167
+ refreshToken: refreshed.refreshToken,
168
+ expiresAt: new Date(Date.now() + refreshed.expiresIn * 1000).toISOString(),
169
+ realm: auth.realm,
170
+ });
171
+ accessToken = refreshed.accessToken;
172
+ logger_1.logger.dim('Token refreshed');
173
+ }
174
+ }
175
+ if ((0, configService_1.isUserAuthValid)()) {
176
+ const jwtClient = new farseerApi_1.FarseerApi(accessToken, organisation, tenant);
177
+ // Test JWT connection
178
+ try {
179
+ const connected = await jwtClient.testConnection();
180
+ if (connected) {
181
+ return { client: jwtClient, authType: 'jwt' };
182
+ }
183
+ }
184
+ catch (error) {
185
+ // JWT failed, will try API key
186
+ logger_1.logger.dim('JWT auth failed for this tenant, trying API key...');
187
+ }
188
+ }
189
+ }
190
+ // 2. Try API key
191
+ if (credential) {
192
+ const apiKeyClient = new farseerService_1.FarseerService(credential);
193
+ try {
194
+ const connected = await apiKeyClient.testConnection();
195
+ if (connected) {
196
+ return { client: apiKeyClient, authType: 'apiKey' };
197
+ }
198
+ }
199
+ catch {
200
+ // API key also failed
201
+ logger_1.logger.dim('API key auth failed.');
202
+ }
203
+ }
204
+ // 3. Nothing worked
205
+ return null;
206
+ }
207
+ /**
208
+ * Ensure the token is fresh and update the client if needed.
209
+ * Call this before long-running operations to avoid token expiration.
210
+ *
211
+ * @returns true if token was refreshed, false if no refresh needed or API key auth
212
+ */
213
+ async function ensureFreshToken(client) {
214
+ const auth = (0, configService_1.getUserAuth)();
215
+ if (!auth) {
216
+ return false; // No JWT auth, probably using API key
217
+ }
218
+ // Check if token needs refresh (give 30 second buffer)
219
+ const expiresAt = new Date(auth.expiresAt);
220
+ const now = new Date();
221
+ const bufferMs = 30 * 1000; // 30 seconds
222
+ if (expiresAt.getTime() - now.getTime() > bufferMs) {
223
+ return false; // Token is still fresh enough
224
+ }
225
+ // Refresh the token
226
+ logger_1.logger.dim('Refreshing token before operation...');
227
+ const refreshed = await (0, farseerService_2.refreshAccessToken)(auth.refreshToken, auth.realm || 'master');
228
+ if (!refreshed) {
229
+ logger_1.logger.warning('Could not refresh token.');
230
+ return false;
231
+ }
232
+ // Save the new token
233
+ (0, configService_1.setUserAuth)({
234
+ accessToken: refreshed.accessToken,
235
+ refreshToken: refreshed.refreshToken,
236
+ expiresAt: new Date(Date.now() + refreshed.expiresIn * 1000).toISOString(),
237
+ realm: auth.realm,
238
+ });
239
+ // Update the client's token if it supports it
240
+ if (client.updateAccessToken) {
241
+ client.updateAccessToken(refreshed.accessToken);
242
+ logger_1.logger.dim('Token refreshed');
243
+ return true;
244
+ }
245
+ return false;
246
+ }
247
+ /**
248
+ * Get a native farseer-client FarseerClient instance.
249
+ * This provides access to the full farseer-client API (export, import jobs, file creation, etc.)
250
+ * that isn't available through the simplified IFarseerClient interface.
251
+ *
252
+ * Tries JWT first, then API key. Handles token refresh automatically.
253
+ *
254
+ * @param organisation - The organisation hostname (e.g., "jgl" for jgl.farseer.io)
255
+ * @param tenant - Tenant name for X-TENANT-ID header
256
+ */
257
+ async function getNativeFarseerClient(organisation, tenant) {
258
+ const basePath = (0, configService_1.generateBasePath)(organisation);
259
+ // 1. Try JWT token first
260
+ const auth = (0, configService_1.getUserAuth)();
261
+ if (auth) {
262
+ let accessToken = auth.accessToken;
263
+ if (!(0, configService_1.isUserAuthValid)()) {
264
+ const refreshed = await (0, farseerService_2.refreshAccessToken)(auth.refreshToken, auth.realm || 'master');
265
+ if (refreshed) {
266
+ (0, configService_1.setUserAuth)({
267
+ accessToken: refreshed.accessToken,
268
+ refreshToken: refreshed.refreshToken,
269
+ expiresAt: new Date(Date.now() + refreshed.expiresIn * 1000).toISOString(),
270
+ realm: auth.realm,
271
+ });
272
+ accessToken = refreshed.accessToken;
273
+ }
274
+ }
275
+ if ((0, configService_1.isUserAuthValid)()) {
276
+ return new farseer.FarseerClient({
277
+ basePath,
278
+ headers: {
279
+ 'X-TENANT-ID': tenant,
280
+ Authorization: `Bearer ${accessToken}`,
281
+ },
282
+ });
283
+ }
284
+ }
285
+ // 2. Try API key
286
+ const credential = (0, configService_1.getCredential)(tenant);
287
+ if (credential) {
288
+ return new farseer.FarseerClient({
289
+ basePath: credential.basePath,
290
+ headers: {
291
+ 'X-TENANT-ID': credential.tenantId,
292
+ 'X-API-KEY': credential.apiKey,
293
+ },
294
+ });
295
+ }
296
+ return null;
297
+ }