appwrite-utils-cli 1.7.1 → 1.7.3

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.
@@ -149,9 +149,26 @@ export class ConfigManager {
149
149
  logger.debug(`Config discovered at: ${configPath}`, { prefix: "ConfigManager" });
150
150
  // 3. Load config from file
151
151
  let config = await this.loaderService.loadFromPath(configPath);
152
- // 4. Load session authentication
153
- const session = options.sessionOverride ||
154
- (await this.sessionService.findSession(config.appwriteEndpoint, config.appwriteProject));
152
+ // 4. Load session authentication (only if appropriate based on config)
153
+ // Determine if we should load session:
154
+ // - Load if authMethod is explicitly "session"
155
+ // - Load if authMethod is "auto" or undefined AND no API key exists
156
+ // - Skip if authMethod is "apikey" or if API key is present with "auto" mode
157
+ const hasApiKey = !!(config.appwriteKey && config.appwriteKey.trim().length > 0);
158
+ const authMethod = config.authMethod || "auto";
159
+ const shouldLoadSession = options.sessionOverride !== undefined ||
160
+ authMethod === "session" ||
161
+ (authMethod === "auto" && !hasApiKey);
162
+ logger.debug("Session loading decision", {
163
+ prefix: "ConfigManager",
164
+ hasApiKey,
165
+ authMethod,
166
+ shouldLoadSession,
167
+ });
168
+ const session = shouldLoadSession
169
+ ? options.sessionOverride ||
170
+ (await this.sessionService.findSession(config.appwriteEndpoint, config.appwriteProject))
171
+ : null;
155
172
  // 5. Merge session into config
156
173
  if (session) {
157
174
  logger.debug("Merging session authentication into config", { prefix: "ConfigManager" });
@@ -57,7 +57,11 @@ export class ConfigMergeService {
57
57
  const merged = cloneDeep(config);
58
58
  // Add session authentication (map 'cookie' to 'sessionCookie')
59
59
  merged.sessionCookie = session.cookie;
60
- merged.authMethod = "session";
60
+ // Only set authMethod to "session" if not explicitly set to "apikey"
61
+ // This allows API key authentication to take priority when both are available
62
+ if (merged.authMethod !== "apikey") {
63
+ merged.authMethod = "session";
64
+ }
61
65
  // Add session metadata if available
62
66
  if (session.email || session.expiresAt) {
63
67
  merged.sessionMetadata = {
@@ -60,32 +60,75 @@ export class ClientFactory {
60
60
  const client = new Client()
61
61
  .setEndpoint(config.appwriteEndpoint)
62
62
  .setProject(config.appwriteProject);
63
- // Apply authentication (priority already resolved by ConfigManager)
64
- if (config.sessionCookie) {
65
- // Session authentication (from ConfigManager's session loading)
66
- client.setSession(config.sessionCookie);
67
- logger.debug("Applied session authentication to client", {
63
+ // Apply authentication based on authMethod preference
64
+ const authMethod = config.authMethod || "auto";
65
+ logger.debug("Applying authentication", {
66
+ prefix: "ClientFactory",
67
+ authMethod,
68
+ hasApiKey: !!config.appwriteKey,
69
+ hasSession: !!config.sessionCookie,
70
+ });
71
+ if (authMethod === "apikey") {
72
+ // Explicit API key preference - use only API key
73
+ if (!config.appwriteKey || config.appwriteKey.trim().length === 0) {
74
+ const error = new Error("authMethod set to 'apikey' but no API key provided.\n\n" +
75
+ "Either:\n" +
76
+ " - Set appwriteKey in your config file\n" +
77
+ " - Provide --apiKey flag\n" +
78
+ " - Set APPWRITE_API_KEY environment variable");
79
+ logger.error("Failed to create client - API key required", { prefix: "ClientFactory" });
80
+ throw error;
81
+ }
82
+ client.setKey(config.appwriteKey);
83
+ logger.debug("Applied API key authentication (explicit preference)", {
68
84
  prefix: "ClientFactory",
69
- email: config.sessionMetadata?.email,
70
85
  });
71
86
  }
72
- else if (config.appwriteKey) {
73
- // API key authentication (from config file or overrides)
74
- client.setKey(config.appwriteKey);
75
- logger.debug("Applied API key authentication to client", {
87
+ else if (authMethod === "session") {
88
+ // Explicit session preference - use only session
89
+ if (!config.sessionCookie) {
90
+ const error = new Error("authMethod set to 'session' but no session cookie available.\n\n" +
91
+ "Either:\n" +
92
+ " - Run 'appwrite login' to create a session\n" +
93
+ " - Change authMethod to 'apikey' or 'auto'\n" +
94
+ " - Provide --sessionCookie flag");
95
+ logger.error("Failed to create client - session required", { prefix: "ClientFactory" });
96
+ throw error;
97
+ }
98
+ client.setSession(config.sessionCookie);
99
+ logger.debug("Applied session authentication (explicit preference)", {
76
100
  prefix: "ClientFactory",
101
+ email: config.sessionMetadata?.email,
77
102
  });
78
103
  }
79
104
  else {
80
- // No authentication available - this should have been caught by ConfigManager
81
- const error = new Error("No authentication method available in configuration.\n\n" +
82
- "This should have been resolved by ConfigManager during config loading.\n" +
83
- "Expected either:\n" +
84
- " - config.sessionCookie (from session authentication)\n" +
85
- " - config.appwriteKey (from config file or CLI overrides)\n\n" +
86
- "Suggestion: Ensure ConfigManager.loadConfig() was called before ClientFactory.createFromConfig().");
87
- logger.error("Failed to create client - no authentication", { prefix: "ClientFactory" });
88
- throw error;
105
+ // Auto mode: Prefer API key if present, fallback to session
106
+ if (config.appwriteKey && config.appwriteKey.trim().length > 0) {
107
+ client.setKey(config.appwriteKey);
108
+ logger.debug("Applied API key authentication (auto mode - preferred)", {
109
+ prefix: "ClientFactory",
110
+ });
111
+ }
112
+ else if (config.sessionCookie) {
113
+ client.setSession(config.sessionCookie);
114
+ logger.debug("Applied session authentication (auto mode - fallback)", {
115
+ prefix: "ClientFactory",
116
+ email: config.sessionMetadata?.email,
117
+ });
118
+ }
119
+ else {
120
+ // No authentication available
121
+ const error = new Error("No authentication method available in configuration.\n\n" +
122
+ "Expected either:\n" +
123
+ " - config.appwriteKey (from config file, CLI flags, or environment)\n" +
124
+ " - config.sessionCookie (from session authentication)\n\n" +
125
+ "Suggestion:\n" +
126
+ " - Add appwriteKey to your config file, OR\n" +
127
+ " - Run 'appwrite login' to create a session, OR\n" +
128
+ " - Provide --apiKey flag");
129
+ logger.error("Failed to create client - no authentication", { prefix: "ClientFactory" });
130
+ throw error;
131
+ }
89
132
  }
90
133
  // Create adapter with version detection
91
134
  // AdapterFactory uses internal caching, so repeated calls are fast
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "appwrite-utils-cli",
3
3
  "description": "Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.",
4
- "version": "1.7.1",
4
+ "version": "1.7.3",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -249,10 +249,30 @@ export class ConfigManager {
249
249
  // 3. Load config from file
250
250
  let config = await this.loaderService.loadFromPath(configPath);
251
251
 
252
- // 4. Load session authentication
253
- const session =
254
- options.sessionOverride ||
255
- (await this.sessionService.findSession(config.appwriteEndpoint, config.appwriteProject));
252
+ // 4. Load session authentication (only if appropriate based on config)
253
+ // Determine if we should load session:
254
+ // - Load if authMethod is explicitly "session"
255
+ // - Load if authMethod is "auto" or undefined AND no API key exists
256
+ // - Skip if authMethod is "apikey" or if API key is present with "auto" mode
257
+ const hasApiKey = !!(config.appwriteKey && config.appwriteKey.trim().length > 0);
258
+ const authMethod = config.authMethod || "auto";
259
+
260
+ const shouldLoadSession =
261
+ options.sessionOverride !== undefined ||
262
+ authMethod === "session" ||
263
+ (authMethod === "auto" && !hasApiKey);
264
+
265
+ logger.debug("Session loading decision", {
266
+ prefix: "ConfigManager",
267
+ hasApiKey,
268
+ authMethod,
269
+ shouldLoadSession,
270
+ });
271
+
272
+ const session = shouldLoadSession
273
+ ? options.sessionOverride ||
274
+ (await this.sessionService.findSession(config.appwriteEndpoint, config.appwriteProject))
275
+ : null;
256
276
 
257
277
  // 5. Merge session into config
258
278
  if (session) {
@@ -80,7 +80,12 @@ export class ConfigMergeService {
80
80
 
81
81
  // Add session authentication (map 'cookie' to 'sessionCookie')
82
82
  merged.sessionCookie = session.cookie;
83
- merged.authMethod = "session";
83
+
84
+ // Only set authMethod to "session" if not explicitly set to "apikey"
85
+ // This allows API key authentication to take priority when both are available
86
+ if (merged.authMethod !== "apikey") {
87
+ merged.authMethod = "session";
88
+ }
84
89
 
85
90
  // Add session metadata if available
86
91
  if (session.email || session.expiresAt) {
@@ -67,32 +67,81 @@ export class ClientFactory {
67
67
  .setEndpoint(config.appwriteEndpoint)
68
68
  .setProject(config.appwriteProject);
69
69
 
70
- // Apply authentication (priority already resolved by ConfigManager)
71
- if (config.sessionCookie) {
72
- // Session authentication (from ConfigManager's session loading)
73
- client.setSession(config.sessionCookie);
74
- logger.debug("Applied session authentication to client", {
70
+ // Apply authentication based on authMethod preference
71
+ const authMethod = config.authMethod || "auto";
72
+
73
+ logger.debug("Applying authentication", {
74
+ prefix: "ClientFactory",
75
+ authMethod,
76
+ hasApiKey: !!config.appwriteKey,
77
+ hasSession: !!config.sessionCookie,
78
+ });
79
+
80
+ if (authMethod === "apikey") {
81
+ // Explicit API key preference - use only API key
82
+ if (!config.appwriteKey || config.appwriteKey.trim().length === 0) {
83
+ const error = new Error(
84
+ "authMethod set to 'apikey' but no API key provided.\n\n" +
85
+ "Either:\n" +
86
+ " - Set appwriteKey in your config file\n" +
87
+ " - Provide --apiKey flag\n" +
88
+ " - Set APPWRITE_API_KEY environment variable"
89
+ );
90
+ logger.error("Failed to create client - API key required", { prefix: "ClientFactory" });
91
+ throw error;
92
+ }
93
+ client.setKey(config.appwriteKey);
94
+ logger.debug("Applied API key authentication (explicit preference)", {
75
95
  prefix: "ClientFactory",
76
- email: config.sessionMetadata?.email,
77
96
  });
78
- } else if (config.appwriteKey) {
79
- // API key authentication (from config file or overrides)
80
- client.setKey(config.appwriteKey);
81
- logger.debug("Applied API key authentication to client", {
97
+
98
+ } else if (authMethod === "session") {
99
+ // Explicit session preference - use only session
100
+ if (!config.sessionCookie) {
101
+ const error = new Error(
102
+ "authMethod set to 'session' but no session cookie available.\n\n" +
103
+ "Either:\n" +
104
+ " - Run 'appwrite login' to create a session\n" +
105
+ " - Change authMethod to 'apikey' or 'auto'\n" +
106
+ " - Provide --sessionCookie flag"
107
+ );
108
+ logger.error("Failed to create client - session required", { prefix: "ClientFactory" });
109
+ throw error;
110
+ }
111
+ client.setSession(config.sessionCookie);
112
+ logger.debug("Applied session authentication (explicit preference)", {
82
113
  prefix: "ClientFactory",
114
+ email: config.sessionMetadata?.email,
83
115
  });
116
+
84
117
  } else {
85
- // No authentication available - this should have been caught by ConfigManager
86
- const error = new Error(
87
- "No authentication method available in configuration.\n\n" +
88
- "This should have been resolved by ConfigManager during config loading.\n" +
89
- "Expected either:\n" +
90
- " - config.sessionCookie (from session authentication)\n" +
91
- " - config.appwriteKey (from config file or CLI overrides)\n\n" +
92
- "Suggestion: Ensure ConfigManager.loadConfig() was called before ClientFactory.createFromConfig()."
93
- );
94
- logger.error("Failed to create client - no authentication", { prefix: "ClientFactory" });
95
- throw error;
118
+ // Auto mode: Prefer API key if present, fallback to session
119
+ if (config.appwriteKey && config.appwriteKey.trim().length > 0) {
120
+ client.setKey(config.appwriteKey);
121
+ logger.debug("Applied API key authentication (auto mode - preferred)", {
122
+ prefix: "ClientFactory",
123
+ });
124
+ } else if (config.sessionCookie) {
125
+ client.setSession(config.sessionCookie);
126
+ logger.debug("Applied session authentication (auto mode - fallback)", {
127
+ prefix: "ClientFactory",
128
+ email: config.sessionMetadata?.email,
129
+ });
130
+ } else {
131
+ // No authentication available
132
+ const error = new Error(
133
+ "No authentication method available in configuration.\n\n" +
134
+ "Expected either:\n" +
135
+ " - config.appwriteKey (from config file, CLI flags, or environment)\n" +
136
+ " - config.sessionCookie (from session authentication)\n\n" +
137
+ "Suggestion:\n" +
138
+ " - Add appwriteKey to your config file, OR\n" +
139
+ " - Run 'appwrite login' to create a session, OR\n" +
140
+ " - Provide --apiKey flag"
141
+ );
142
+ logger.error("Failed to create client - no authentication", { prefix: "ClientFactory" });
143
+ throw error;
144
+ }
96
145
  }
97
146
 
98
147
  // Create adapter with version detection