appwrite-utils-cli 1.7.2 → 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.
@@ -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.2",
4
+ "version": "1.7.3",
5
5
  "main": "src/main.ts",
6
6
  "type": "module",
7
7
  "repository": {
@@ -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