appwrite-utils-cli 1.6.3 → 1.6.4

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 (55) hide show
  1. package/CONFIG_TODO.md +1189 -0
  2. package/SERVICE_IMPLEMENTATION_REPORT.md +462 -0
  3. package/dist/cli/commands/configCommands.js +7 -1
  4. package/dist/collections/attributes.js +102 -30
  5. package/dist/config/ConfigManager.d.ts +445 -0
  6. package/dist/config/ConfigManager.js +625 -0
  7. package/dist/config/index.d.ts +8 -0
  8. package/dist/config/index.js +7 -0
  9. package/dist/config/services/ConfigDiscoveryService.d.ts +126 -0
  10. package/dist/config/services/ConfigDiscoveryService.js +374 -0
  11. package/dist/config/services/ConfigLoaderService.d.ts +105 -0
  12. package/dist/config/services/ConfigLoaderService.js +410 -0
  13. package/dist/config/services/ConfigMergeService.d.ts +208 -0
  14. package/dist/config/services/ConfigMergeService.js +307 -0
  15. package/dist/config/services/ConfigValidationService.d.ts +214 -0
  16. package/dist/config/services/ConfigValidationService.js +310 -0
  17. package/dist/config/services/SessionAuthService.d.ts +225 -0
  18. package/dist/config/services/SessionAuthService.js +456 -0
  19. package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +1 -0
  20. package/dist/config/services/__tests__/ConfigMergeService.test.js +271 -0
  21. package/dist/config/services/index.d.ts +13 -0
  22. package/dist/config/services/index.js +10 -0
  23. package/dist/interactiveCLI.js +8 -6
  24. package/dist/main.js +2 -2
  25. package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +1 -1
  26. package/dist/shared/operationQueue.js +1 -1
  27. package/dist/utils/ClientFactory.d.ts +87 -0
  28. package/dist/utils/ClientFactory.js +164 -0
  29. package/dist/utils/getClientFromConfig.js +4 -3
  30. package/dist/utils/helperFunctions.d.ts +1 -0
  31. package/dist/utils/helperFunctions.js +21 -5
  32. package/dist/utils/yamlConverter.d.ts +2 -2
  33. package/dist/utils/yamlConverter.js +2 -2
  34. package/dist/utilsController.d.ts +18 -15
  35. package/dist/utilsController.js +83 -131
  36. package/package.json +1 -1
  37. package/src/cli/commands/configCommands.ts +8 -1
  38. package/src/collections/attributes.ts +118 -31
  39. package/src/config/ConfigManager.ts +808 -0
  40. package/src/config/index.ts +10 -0
  41. package/src/config/services/ConfigDiscoveryService.ts +463 -0
  42. package/src/config/services/ConfigLoaderService.ts +560 -0
  43. package/src/config/services/ConfigMergeService.ts +386 -0
  44. package/src/config/services/ConfigValidationService.ts +394 -0
  45. package/src/config/services/SessionAuthService.ts +565 -0
  46. package/src/config/services/__tests__/ConfigMergeService.test.ts +351 -0
  47. package/src/config/services/index.ts +29 -0
  48. package/src/interactiveCLI.ts +9 -7
  49. package/src/main.ts +2 -2
  50. package/src/shared/operationQueue.ts +1 -1
  51. package/src/utils/ClientFactory.ts +186 -0
  52. package/src/utils/getClientFromConfig.ts +4 -3
  53. package/src/utils/helperFunctions.ts +27 -7
  54. package/src/utils/yamlConverter.ts +4 -4
  55. package/src/utilsController.ts +99 -187
@@ -0,0 +1,386 @@
1
+ import type { AppwriteConfig } from "appwrite-utils";
2
+ import { merge, cloneDeep, isPlainObject } from "es-toolkit";
3
+ import type { SessionAuthInfo } from "./SessionAuthService.js";
4
+
5
+ /**
6
+ * Configuration override options that can be applied from CLI arguments or environment
7
+ */
8
+ export interface ConfigOverrides {
9
+ /** Appwrite endpoint URL (e.g., https://cloud.appwrite.io/v1) */
10
+ appwriteEndpoint?: string;
11
+ /** Appwrite project ID */
12
+ appwriteProject?: string;
13
+ /** API key for authentication */
14
+ appwriteKey?: string;
15
+ /** Session cookie for authentication */
16
+ sessionCookie?: string;
17
+ /** Explicitly set authentication method */
18
+ authMethod?: "session" | "apikey" | "auto";
19
+ }
20
+
21
+ /**
22
+ * Service responsible for merging configuration from multiple sources with proper priority handling.
23
+ *
24
+ * @description
25
+ * Handles configuration merging with the following priority order (highest to lowest):
26
+ * 1. CLI arguments/overrides (--endpoint, --apiKey, etc.)
27
+ * 2. Session cookie from ~/.appwrite/prefs.json
28
+ * 3. Config file values (YAML/TS/JSON)
29
+ * 4. Environment variables (APPWRITE_ENDPOINT, APPWRITE_PROJECT, etc.)
30
+ *
31
+ * The service ensures proper deep merging of nested objects while preserving array order
32
+ * and preventing undefined/null values from overwriting existing values.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const mergeService = new ConfigMergeService();
37
+ *
38
+ * // Merge session into config
39
+ * const configWithSession = mergeService.mergeSession(baseConfig, sessionInfo);
40
+ *
41
+ * // Apply CLI overrides
42
+ * const finalConfig = mergeService.applyOverrides(configWithSession, {
43
+ * appwriteEndpoint: 'https://custom-endpoint.com/v1',
44
+ * appwriteKey: 'custom-api-key'
45
+ * });
46
+ * ```
47
+ */
48
+ export class ConfigMergeService {
49
+ /**
50
+ * Merges session authentication information into an existing configuration.
51
+ *
52
+ * @description
53
+ * Adds session authentication details to the configuration, setting:
54
+ * - sessionCookie: The authentication cookie from the session
55
+ * - authMethod: Set to "session" to indicate session-based auth
56
+ *
57
+ * The session endpoint and projectId are used for validation but not merged
58
+ * since they should match the config's values.
59
+ *
60
+ * @param config - Base configuration to merge session into
61
+ * @param session - Session authentication information
62
+ * @returns New configuration object with session merged (input config is not mutated)
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const configWithSession = mergeService.mergeSession(config, {
67
+ * projectId: "my-project",
68
+ * endpoint: "https://cloud.appwrite.io/v1",
69
+ * sessionCookie: "eyJhbGc...",
70
+ * email: "user@example.com"
71
+ * });
72
+ * ```
73
+ */
74
+ public mergeSession(
75
+ config: AppwriteConfig,
76
+ session: SessionAuthInfo
77
+ ): AppwriteConfig {
78
+ // Clone config to avoid mutation
79
+ const merged = cloneDeep(config);
80
+
81
+ // Add session authentication (map 'cookie' to 'sessionCookie')
82
+ merged.sessionCookie = session.cookie;
83
+ merged.authMethod = "session";
84
+
85
+ // Add session metadata if available
86
+ if (session.email || session.expiresAt) {
87
+ merged.sessionMetadata = {
88
+ email: session.email,
89
+ expiresAt: session.expiresAt,
90
+ };
91
+ }
92
+
93
+ return merged;
94
+ }
95
+
96
+ /**
97
+ * Applies configuration overrides from CLI arguments or programmatic sources.
98
+ *
99
+ * @description
100
+ * Applies high-priority overrides that take precedence over all other config sources.
101
+ * Only applies values that are explicitly set (not undefined or null).
102
+ *
103
+ * This method is typically used to apply CLI arguments like:
104
+ * - --endpoint
105
+ * - --apiKey
106
+ * - --project
107
+ *
108
+ * @param config - Base configuration to apply overrides to
109
+ * @param overrides - Override values to apply
110
+ * @returns New configuration object with overrides applied (input config is not mutated)
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * const overriddenConfig = mergeService.applyOverrides(config, {
115
+ * appwriteEndpoint: 'https://custom.appwrite.io/v1',
116
+ * appwriteKey: 'my-api-key',
117
+ * authMethod: 'apikey'
118
+ * });
119
+ * ```
120
+ */
121
+ public applyOverrides(
122
+ config: AppwriteConfig,
123
+ overrides: ConfigOverrides
124
+ ): AppwriteConfig {
125
+ // Clone config to avoid mutation
126
+ const merged = cloneDeep(config);
127
+
128
+ // Apply each override only if it has a value (not undefined/null)
129
+ if (overrides.appwriteEndpoint !== undefined && overrides.appwriteEndpoint !== null) {
130
+ merged.appwriteEndpoint = overrides.appwriteEndpoint;
131
+ }
132
+
133
+ if (overrides.appwriteProject !== undefined && overrides.appwriteProject !== null) {
134
+ merged.appwriteProject = overrides.appwriteProject;
135
+ }
136
+
137
+ if (overrides.appwriteKey !== undefined && overrides.appwriteKey !== null) {
138
+ merged.appwriteKey = overrides.appwriteKey;
139
+ }
140
+
141
+ if (overrides.sessionCookie !== undefined && overrides.sessionCookie !== null) {
142
+ merged.sessionCookie = overrides.sessionCookie;
143
+ }
144
+
145
+ if (overrides.authMethod !== undefined && overrides.authMethod !== null) {
146
+ merged.authMethod = overrides.authMethod;
147
+ }
148
+
149
+ return merged;
150
+ }
151
+
152
+ /**
153
+ * Merges environment variables into the configuration.
154
+ *
155
+ * @description
156
+ * Reads environment variables and merges them into the config with the lowest priority.
157
+ * Only applies environment variables that are set and have non-empty values.
158
+ *
159
+ * Supported environment variables:
160
+ * - APPWRITE_ENDPOINT: Appwrite API endpoint URL
161
+ * - APPWRITE_PROJECT: Appwrite project ID
162
+ * - APPWRITE_API_KEY: API key for authentication
163
+ * - APPWRITE_SESSION_COOKIE: Session cookie for authentication
164
+ *
165
+ * Environment variables are only applied if the corresponding config value is not already set.
166
+ *
167
+ * @param config - Base configuration to merge environment variables into
168
+ * @returns New configuration object with environment variables merged (input config is not mutated)
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * // With env vars: APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
173
+ * const configWithEnv = mergeService.mergeEnvironmentVariables(config);
174
+ * // configWithEnv.appwriteEndpoint will be set from env var if not already set
175
+ * ```
176
+ */
177
+ public mergeEnvironmentVariables(config: AppwriteConfig): AppwriteConfig {
178
+ // Clone config to avoid mutation
179
+ const merged = cloneDeep(config);
180
+
181
+ // Read environment variables
182
+ const envEndpoint = process.env.APPWRITE_ENDPOINT;
183
+ const envProject = process.env.APPWRITE_PROJECT;
184
+ const envApiKey = process.env.APPWRITE_API_KEY;
185
+ const envSessionCookie = process.env.APPWRITE_SESSION_COOKIE;
186
+
187
+ // Apply environment variables only if not already set in config
188
+ // Environment variables have the lowest priority
189
+ if (envEndpoint && !merged.appwriteEndpoint) {
190
+ merged.appwriteEndpoint = envEndpoint;
191
+ }
192
+
193
+ if (envProject && !merged.appwriteProject) {
194
+ merged.appwriteProject = envProject;
195
+ }
196
+
197
+ if (envApiKey && !merged.appwriteKey) {
198
+ merged.appwriteKey = envApiKey;
199
+ }
200
+
201
+ if (envSessionCookie && !merged.sessionCookie) {
202
+ merged.sessionCookie = envSessionCookie;
203
+ }
204
+
205
+ return merged;
206
+ }
207
+
208
+ /**
209
+ * Performs a deep merge of multiple partial configuration sources into a complete configuration.
210
+ *
211
+ * @description
212
+ * Merges multiple configuration sources using a deep merge strategy that:
213
+ * - Recursively merges nested objects
214
+ * - Preserves array order (arrays are replaced, not merged)
215
+ * - Skips undefined and null values (they don't overwrite existing values)
216
+ * - Later sources in the array take precedence over earlier ones
217
+ *
218
+ * This method is useful when combining:
219
+ * - Base defaults with file-based config
220
+ * - Multiple configuration files
221
+ * - Config fragments from different sources
222
+ *
223
+ * @param sources - Array of partial configurations to merge (order matters: later sources override earlier ones)
224
+ * @returns Complete merged configuration object
225
+ *
226
+ * @throws {Error} If no valid configuration can be produced from the sources
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * const merged = mergeService.mergeSources([
231
+ * envConfig, // Lowest priority
232
+ * fileConfig, // Medium priority
233
+ * sessionConfig, // Higher priority
234
+ * overrideConfig // Highest priority
235
+ * ]);
236
+ * ```
237
+ */
238
+ public mergeSources(sources: Array<Partial<AppwriteConfig>>): AppwriteConfig {
239
+ // Filter out undefined/null sources
240
+ const validSources = sources.filter(
241
+ (source) => source !== undefined && source !== null
242
+ );
243
+
244
+ if (validSources.length === 0) {
245
+ throw new Error("No valid configuration sources provided for merging");
246
+ }
247
+
248
+ // Start with an empty base object
249
+ let merged = {} as AppwriteConfig;
250
+
251
+ // Merge each source in order (later sources override earlier ones)
252
+ for (const source of validSources) {
253
+ merged = this.deepMergeConfigs(merged, source);
254
+ }
255
+
256
+ // Validate that we have the minimum required fields
257
+ if (!merged.appwriteEndpoint || !merged.appwriteProject) {
258
+ throw new Error(
259
+ "Merged configuration is missing required fields: appwriteEndpoint and/or appwriteProject"
260
+ );
261
+ }
262
+
263
+ return merged;
264
+ }
265
+
266
+ /**
267
+ * Internal helper for deep merging two configuration objects.
268
+ *
269
+ * @description
270
+ * Performs a deep merge of two objects with special handling for:
271
+ * - Plain objects: Recursively merged
272
+ * - Arrays: Target array is replaced by source array (not merged)
273
+ * - Undefined/null values in source: Skipped (don't overwrite target)
274
+ * - Primitive values: Source overwrites target
275
+ *
276
+ * This is used internally by mergeSources but can also be used directly
277
+ * for specific merge operations.
278
+ *
279
+ * @param target - Base configuration object
280
+ * @param source - Configuration to merge into target
281
+ * @returns New deeply merged configuration object
282
+ *
283
+ * @private
284
+ */
285
+ private deepMergeConfigs(
286
+ target: Partial<AppwriteConfig>,
287
+ source: Partial<AppwriteConfig>
288
+ ): AppwriteConfig {
289
+ // Clone target to avoid mutation
290
+ const result = cloneDeep(target);
291
+
292
+ // Iterate through source properties
293
+ for (const key in source) {
294
+ if (!Object.prototype.hasOwnProperty.call(source, key)) {
295
+ continue;
296
+ }
297
+
298
+ const sourceValue = source[key as keyof AppwriteConfig];
299
+ const targetValue = result[key as keyof AppwriteConfig];
300
+
301
+ // Skip undefined and null values from source
302
+ if (sourceValue === undefined || sourceValue === null) {
303
+ continue;
304
+ }
305
+
306
+ // Handle arrays: Replace target array with source array (don't merge)
307
+ if (Array.isArray(sourceValue)) {
308
+ (result as any)[key] = cloneDeep(sourceValue);
309
+ continue;
310
+ }
311
+
312
+ // Handle plain objects: Recursively merge
313
+ if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
314
+ (result as any)[key] = this.deepMergeConfigs(
315
+ targetValue as Partial<AppwriteConfig>,
316
+ sourceValue as Partial<AppwriteConfig>
317
+ );
318
+ continue;
319
+ }
320
+
321
+ // For all other cases (primitives, etc.), source overwrites target
322
+ (result as any)[key] = cloneDeep(sourceValue);
323
+ }
324
+
325
+ return result as AppwriteConfig;
326
+ }
327
+
328
+ /**
329
+ * Creates a configuration by merging all sources in the correct priority order.
330
+ *
331
+ * @description
332
+ * Convenience method that applies the full configuration merge pipeline:
333
+ * 1. Start with base config (from file)
334
+ * 2. Merge environment variables (lowest priority)
335
+ * 3. Merge session information (if provided)
336
+ * 4. Apply CLI/programmatic overrides (highest priority)
337
+ *
338
+ * This is the recommended way to build a final configuration from all sources.
339
+ *
340
+ * @param baseConfig - Configuration loaded from file (YAML/TS/JSON)
341
+ * @param options - Optional merge options
342
+ * @param options.session - Session authentication to merge
343
+ * @param options.overrides - CLI/programmatic overrides to apply
344
+ * @param options.includeEnv - Whether to include environment variables (default: true)
345
+ * @returns Final merged configuration with all sources applied in priority order
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * const finalConfig = mergeService.mergeAllSources(fileConfig, {
350
+ * session: sessionInfo,
351
+ * overrides: cliOverrides,
352
+ * includeEnv: true
353
+ * });
354
+ * ```
355
+ */
356
+ public mergeAllSources(
357
+ baseConfig: AppwriteConfig,
358
+ options: {
359
+ session?: SessionAuthInfo;
360
+ overrides?: ConfigOverrides;
361
+ includeEnv?: boolean;
362
+ } = {}
363
+ ): AppwriteConfig {
364
+ const { session, overrides, includeEnv = true } = options;
365
+
366
+ // Start with base config
367
+ let config = cloneDeep(baseConfig);
368
+
369
+ // Step 1: Merge environment variables (lowest priority)
370
+ if (includeEnv) {
371
+ config = this.mergeEnvironmentVariables(config);
372
+ }
373
+
374
+ // Step 2: Merge session (medium-high priority)
375
+ if (session) {
376
+ config = this.mergeSession(config, session);
377
+ }
378
+
379
+ // Step 3: Apply overrides (highest priority)
380
+ if (overrides) {
381
+ config = this.applyOverrides(config, overrides);
382
+ }
383
+
384
+ return config;
385
+ }
386
+ }