farseer-cli 1.0.9 → 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 (81) hide show
  1. package/dist/commands/app.js +344 -316
  2. package/dist/commands/apps.js +83 -103
  3. package/dist/commands/checkout.js +156 -134
  4. package/dist/commands/config.js +126 -124
  5. package/dist/commands/diff.js +194 -169
  6. package/dist/commands/dimensions.js +122 -137
  7. package/dist/commands/files.js +77 -88
  8. package/dist/commands/install.js +77 -74
  9. package/dist/commands/list.js +74 -89
  10. package/dist/commands/login.js +101 -126
  11. package/dist/commands/logout.js +42 -57
  12. package/dist/commands/mcp-server.js +24 -39
  13. package/dist/commands/model.js +176 -184
  14. package/dist/commands/pull.js +276 -248
  15. package/dist/commands/push.js +222 -200
  16. package/dist/commands/run.js +322 -274
  17. package/dist/commands/status.js +170 -147
  18. package/dist/commands/variables.js +192 -168
  19. package/dist/commands/whoami.js +45 -61
  20. package/dist/index.js +115 -117
  21. package/dist/mcp/index.js +17 -33
  22. package/dist/mcp/prompts/workflows.js +146 -191
  23. package/dist/mcp/resources/documentation.js +149 -158
  24. package/dist/mcp/server.js +34 -50
  25. package/dist/mcp/tools/appTools.js +278 -307
  26. package/dist/mcp/tools/authTools.js +123 -130
  27. package/dist/mcp/tools/dataTools.js +271 -275
  28. package/dist/mcp/tools/modelTools.js +233 -265
  29. package/dist/mcp/tools/runTools.js +205 -199
  30. package/dist/mcp/tools/syncTools.js +315 -341
  31. package/dist/mcp/utils/helpers.js +124 -117
  32. package/dist/services/appSyncService.js +438 -379
  33. package/dist/services/configService.js +144 -154
  34. package/dist/services/farseerApi.js +401 -401
  35. package/dist/services/farseerFactory.js +274 -208
  36. package/dist/services/farseerService.js +488 -455
  37. package/dist/services/syncService.js +732 -657
  38. package/dist/utils/constants.js +20 -45
  39. package/dist/utils/helpers.js +366 -333
  40. package/dist/utils/logger.js +43 -74
  41. package/package.json +8 -1
  42. package/dist/commands/app.js.map +0 -7
  43. package/dist/commands/apps.js.map +0 -7
  44. package/dist/commands/checkout.js.map +0 -7
  45. package/dist/commands/config.js.map +0 -7
  46. package/dist/commands/diff.js.map +0 -7
  47. package/dist/commands/dimensions.js.map +0 -7
  48. package/dist/commands/files.js.map +0 -7
  49. package/dist/commands/install.js.map +0 -7
  50. package/dist/commands/list.js.map +0 -7
  51. package/dist/commands/login.js.map +0 -7
  52. package/dist/commands/logout.js.map +0 -7
  53. package/dist/commands/mcp-server.js.map +0 -7
  54. package/dist/commands/model.js.map +0 -7
  55. package/dist/commands/pull.js.map +0 -7
  56. package/dist/commands/push.js.map +0 -7
  57. package/dist/commands/run.js.map +0 -7
  58. package/dist/commands/status.js.map +0 -7
  59. package/dist/commands/variables.js.map +0 -7
  60. package/dist/commands/whoami.js.map +0 -7
  61. package/dist/index.js.map +0 -7
  62. package/dist/mcp/index.js.map +0 -7
  63. package/dist/mcp/prompts/workflows.js.map +0 -7
  64. package/dist/mcp/resources/documentation.js.map +0 -7
  65. package/dist/mcp/server.js.map +0 -7
  66. package/dist/mcp/tools/appTools.js.map +0 -7
  67. package/dist/mcp/tools/authTools.js.map +0 -7
  68. package/dist/mcp/tools/dataTools.js.map +0 -7
  69. package/dist/mcp/tools/modelTools.js.map +0 -7
  70. package/dist/mcp/tools/runTools.js.map +0 -7
  71. package/dist/mcp/tools/syncTools.js.map +0 -7
  72. package/dist/mcp/utils/helpers.js.map +0 -7
  73. package/dist/services/appSyncService.js.map +0 -7
  74. package/dist/services/configService.js.map +0 -7
  75. package/dist/services/farseerApi.js.map +0 -7
  76. package/dist/services/farseerFactory.js.map +0 -7
  77. package/dist/services/farseerService.js.map +0 -7
  78. package/dist/services/syncService.js.map +0 -7
  79. package/dist/utils/constants.js.map +0 -7
  80. package/dist/utils/helpers.js.map +0 -7
  81. package/dist/utils/logger.js.map +0 -7
@@ -1,231 +1,297 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
- var farseerFactory_exports = {};
29
- __export(farseerFactory_exports, {
30
- DEFAULT_SCRIPT_EXTENSIONS: () => DEFAULT_SCRIPT_EXTENSIONS,
31
- ensureFreshToken: () => ensureFreshToken,
32
- getAvailableAuth: () => getAvailableAuth,
33
- getFarseerClient: () => getFarseerClient,
34
- getFarseerClientWithFallback: () => getFarseerClientWithFallback,
35
- getNativeFarseerClient: () => getNativeFarseerClient,
36
- isAuthError: () => isAuthError,
37
- showLoginPrompt: () => showLoginPrompt
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;
38
25
  });
39
- module.exports = __toCommonJS(farseerFactory_exports);
40
- var farseer = __toESM(require("farseer-client"));
41
- var import_farseerService = require("./farseerService");
42
- var import_farseerApi = require("./farseerApi");
43
- var import_configService = require("./configService");
44
- var import_farseerService2 = require("./farseerService");
45
- var import_logger = require("../utils/logger");
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
+ */
46
61
  function isAuthError(error) {
47
- if (error instanceof Error) {
48
- const msg = error.message.toLowerCase();
49
- return msg.includes("401") || msg.includes("403") || msg.includes("unauthorized") || msg.includes("forbidden");
50
- }
51
- return false;
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;
52
67
  }
68
+ /**
69
+ * Show a consistent "please login" message
70
+ */
53
71
  function showLoginPrompt(tenant) {
54
- import_logger.logger.error("Authentication required.");
55
- if (tenant) {
56
- import_logger.logger.dim(`No valid credentials found for tenant "${tenant}".`);
57
- }
58
- import_logger.logger.info('Run "farseer login" to authenticate via browser.');
59
- import_logger.logger.dim('Or use "farseer config set <tenant> --api-key <key>" for API key auth.');
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.');
60
78
  }
61
- const DEFAULT_SCRIPT_EXTENSIONS = [".ts", ".js", ".mjs", ".cjs"];
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
+ */
62
88
  async function getFarseerClient(organisation, tenant) {
63
- const auth = (0, import_configService.getUserAuth)();
64
- if (auth) {
65
- let accessToken = auth.accessToken;
66
- if (!(0, import_configService.isUserAuthValid)()) {
67
- import_logger.logger.dim("Token expired, refreshing...");
68
- const refreshed = await (0, import_farseerService2.refreshAccessToken)(auth.refreshToken, auth.realm || "master");
69
- if (!refreshed) {
70
- import_logger.logger.warning("Could not refresh token. Please login again with: farseer login");
71
- } else {
72
- (0, import_configService.setUserAuth)({
73
- accessToken: refreshed.accessToken,
74
- refreshToken: refreshed.refreshToken,
75
- expiresAt: new Date(Date.now() + refreshed.expiresIn * 1e3).toISOString(),
76
- realm: auth.realm
77
- });
78
- accessToken = refreshed.accessToken;
79
- import_logger.logger.dim("Token refreshed");
80
- }
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
+ }
81
119
  }
82
- if ((0, import_configService.isUserAuthValid)()) {
83
- return {
84
- client: new import_farseerApi.FarseerApi(accessToken, organisation, tenant),
85
- authType: "jwt"
86
- };
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
+ };
87
127
  }
88
- }
89
- const credential = (0, import_configService.getCredential)(tenant);
90
- if (credential) {
91
- return {
92
- client: new import_farseerService.FarseerService(credential),
93
- authType: "apiKey"
94
- };
95
- }
96
- return null;
128
+ return null;
97
129
  }
130
+ /**
131
+ * Check what authentication methods are available
132
+ */
98
133
  function getAvailableAuth(tenant) {
99
- const credential = (0, import_configService.getCredential)(tenant);
100
- const auth = (0, import_configService.getUserAuth)();
101
- return {
102
- hasApiKey: !!credential,
103
- hasJwt: !!auth,
104
- jwtValid: (0, import_configService.isUserAuthValid)()
105
- };
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
+ };
106
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
+ */
107
154
  async function getFarseerClientWithFallback(organisation, tenant) {
108
- const auth = (0, import_configService.getUserAuth)();
109
- const credential = (0, import_configService.getCredential)(tenant);
110
- if (auth) {
111
- let accessToken = auth.accessToken;
112
- if (!(0, import_configService.isUserAuthValid)()) {
113
- import_logger.logger.dim("Token expired, refreshing...");
114
- const refreshed = await (0, import_farseerService2.refreshAccessToken)(auth.refreshToken, auth.realm || "master");
115
- if (refreshed) {
116
- (0, import_configService.setUserAuth)({
117
- accessToken: refreshed.accessToken,
118
- refreshToken: refreshed.refreshToken,
119
- expiresAt: new Date(Date.now() + refreshed.expiresIn * 1e3).toISOString(),
120
- realm: auth.realm
121
- });
122
- accessToken = refreshed.accessToken;
123
- import_logger.logger.dim("Token refreshed");
124
- }
125
- }
126
- if ((0, import_configService.isUserAuthValid)()) {
127
- const jwtClient = new import_farseerApi.FarseerApi(accessToken, organisation, tenant);
128
- try {
129
- const connected = await jwtClient.testConnection();
130
- if (connected) {
131
- return { client: jwtClient, authType: "jwt" };
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
+ }
132
188
  }
133
- } catch (error) {
134
- import_logger.logger.dim("JWT auth failed for this tenant, trying API key...");
135
- }
136
189
  }
137
- }
138
- if (credential) {
139
- const apiKeyClient = new import_farseerService.FarseerService(credential);
140
- try {
141
- const connected = await apiKeyClient.testConnection();
142
- if (connected) {
143
- return { client: apiKeyClient, authType: "apiKey" };
144
- }
145
- } catch {
146
- import_logger.logger.dim("API key auth failed.");
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
+ }
147
203
  }
148
- }
149
- return null;
204
+ // 3. Nothing worked
205
+ return null;
150
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
+ */
151
213
  async function ensureFreshToken(client) {
152
- const auth = (0, import_configService.getUserAuth)();
153
- if (!auth) {
154
- return false;
155
- }
156
- const expiresAt = new Date(auth.expiresAt);
157
- const now = /* @__PURE__ */ new Date();
158
- const bufferMs = 30 * 1e3;
159
- if (expiresAt.getTime() - now.getTime() > bufferMs) {
160
- return false;
161
- }
162
- import_logger.logger.dim("Refreshing token before operation...");
163
- const refreshed = await (0, import_farseerService2.refreshAccessToken)(auth.refreshToken, auth.realm || "master");
164
- if (!refreshed) {
165
- import_logger.logger.warning("Could not refresh token.");
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
+ }
166
245
  return false;
167
- }
168
- (0, import_configService.setUserAuth)({
169
- accessToken: refreshed.accessToken,
170
- refreshToken: refreshed.refreshToken,
171
- expiresAt: new Date(Date.now() + refreshed.expiresIn * 1e3).toISOString(),
172
- realm: auth.realm
173
- });
174
- if (client.updateAccessToken) {
175
- client.updateAccessToken(refreshed.accessToken);
176
- import_logger.logger.dim("Token refreshed");
177
- return true;
178
- }
179
- return false;
180
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
+ */
181
257
  async function getNativeFarseerClient(organisation, tenant) {
182
- const basePath = (0, import_configService.generateBasePath)(organisation);
183
- const auth = (0, import_configService.getUserAuth)();
184
- if (auth) {
185
- let accessToken = auth.accessToken;
186
- if (!(0, import_configService.isUserAuthValid)()) {
187
- const refreshed = await (0, import_farseerService2.refreshAccessToken)(auth.refreshToken, auth.realm || "master");
188
- if (refreshed) {
189
- (0, import_configService.setUserAuth)({
190
- accessToken: refreshed.accessToken,
191
- refreshToken: refreshed.refreshToken,
192
- expiresAt: new Date(Date.now() + refreshed.expiresIn * 1e3).toISOString(),
193
- realm: auth.realm
194
- });
195
- accessToken = refreshed.accessToken;
196
- }
197
- }
198
- if ((0, import_configService.isUserAuthValid)()) {
199
- return new farseer.FarseerClient({
200
- basePath,
201
- headers: {
202
- "X-TENANT-ID": tenant,
203
- "Authorization": `Bearer ${accessToken}`
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
+ });
204
283
  }
205
- });
206
284
  }
207
- }
208
- const credential = (0, import_configService.getCredential)(tenant);
209
- if (credential) {
210
- return new farseer.FarseerClient({
211
- basePath: credential.basePath,
212
- headers: {
213
- "X-TENANT-ID": credential.tenantId,
214
- "X-API-KEY": credential.apiKey
215
- }
216
- });
217
- }
218
- return null;
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;
219
297
  }
220
- // Annotate the CommonJS export names for ESM import in node:
221
- 0 && (module.exports = {
222
- DEFAULT_SCRIPT_EXTENSIONS,
223
- ensureFreshToken,
224
- getAvailableAuth,
225
- getFarseerClient,
226
- getFarseerClientWithFallback,
227
- getNativeFarseerClient,
228
- isAuthError,
229
- showLoginPrompt
230
- });
231
- //# sourceMappingURL=farseerFactory.js.map