Repository: n8n-io/n8n
File: packages/cli/src/modules/external-secrets.ee/secrets-providers-connections.service.ee.ts (TypeScript)

Code before:
	toPublicConnectionListItem(
		connection: SecretsProviderConnection,
	): SecretsProvidersResponses.ConnectionListItem {
		const secretNames = this.externalSecretsManager.getSecretNames(connection.providerKey);
		const connectionInstance = this.externalSecretsManager.getProvider(connection.providerKey);

		return {
			id: String(connection.id),
			name: connection.providerKey,
			type: connection.type as SecretsProviderType,
			secretsCount: secretNames.length,
			state: connectionInstance?.state ?? 'initializing',
			projects: connection.projectAccess.map((access) => ({
				id: access.project.id,
				name: access.project.name,
			})),
			createdAt: connection.createdAt.toISOString(),
			updatedAt: connection.updatedAt.toISOString(),
		};
	}

	toPublicConnection(connection: SecretsProviderConnection): SecretsProvidersResponses.Connection {
		const decryptedSettings = this.decryptConnectionSettings(connection.encryptedSettings);
		const properties = this.externalSecretsManager.getProviderProperties(connection.type);
		const redactedSettings = this.redactionService.redact(decryptedSettings, properties);
		const secretNames = this.externalSecretsManager.getSecretNames(connection.providerKey);
		const connectionInstance = this.externalSecretsManager.getProvider(connection.providerKey);

		return {
			id: String(connection.id),
			name: connection.providerKey,
			type: connection.type as SecretsProviderType,
			secretsCount: secretNames.length,
			state: connectionInstance?.state ?? 'initializing',
			secrets: secretNames.map((name) => ({ name })),
			projects: connection.projectAccess.map((access) => ({
				id: access.project.id,
				name: access.project.name,
			})),
			settings: redactedSettings,
			createdAt: connection.createdAt.toISOString(),
			updatedAt: connection.updatedAt.toISOString(),
		};
	}

	async testConnection(
		providerKey: string,
		userId: string,
	): Promise<TestSecretProviderConnectionResponse> {
		const connection = await this.getConnection(providerKey);
		const decryptedSettings = this.decryptConnectionSettings(connection.encryptedSettings);

Review comment (suggestion):
Wanted to get away from the "old" methods where there is an assumption that we are working with a fixed set of global providers. That's why I added a dedicated method just to get the "type" properties.

Code after:
	toPublicConnectionListItem(
		connection: SecretsProviderConnection,
	): SecretsProvidersResponses.ConnectionListItem {
		const secretNames = this.externalSecretsManager.getSecretNames(connection.providerKey);
		const connectionInstance = this.externalSecretsManager.getProvider(connection.providerKey);

		return {
			id: String(connection.id),
			name: connection.providerKey,
			type: connection.type as SecretsProviderType,
			secretsCount: secretNames.length,
			// Provider may not be registered yet in multi-main setups.
			// When that's the case the default state is 'initializing'.
			state: connectionInstance?.state ?? 'initializing',
			projects: connection.projectAccess.map((access) => ({
				id: access.project.id,
				name: access.project.name,
			})),
			createdAt: connection.createdAt.toISOString(),
			updatedAt: connection.updatedAt.toISOString(),
		};
	}

	toPublicConnection(connection: SecretsProviderConnection): SecretsProvidersResponses.Connection {
		const decryptedSettings = this.decryptConnectionSettings(connection.encryptedSettings);
		const properties = this.externalSecretsManager.getProviderProperties(connection.type);
		const redactedSettings = this.redactionService.redact(decryptedSettings, properties);
		const secretNames = this.externalSecretsManager.getSecretNames(connection.providerKey);
		const connectionInstance = this.externalSecretsManager.getProvider(connection.providerKey);

		return {
			id: String(connection.id),
			name: connection.providerKey,
			type: connection.type as SecretsProviderType,
			secretsCount: secretNames.length,
			// Provider may not be registered yet in multi-main setups.
			// When that's the case the default state is 'initializing'.
			state: connectionInstance?.state ?? 'initializing',
			secrets: secretNames.map((name) => ({ name })),
			projects: connection.projectAccess.map((access) => ({
				id: access.project.id,
				name: access.project.name,
			})),
			settings: redactedSettings,
			createdAt: connection.createdAt.toISOString(),
			updatedAt: connection.updatedAt.toISOString(),
		};
	}

	async testConnection(
		providerKey: string,