|
| 1 | +/** |
| 2 | + * User Credentials API |
| 3 | + * |
| 4 | + * This module provides type definitions, interfaces, and utility functions |
| 5 | + * for managing user service credentials. It includes types for credential |
| 6 | + * groups, service definitions, and helper functions for credential management. |
| 7 | + * |
| 8 | + * @module userCredentials |
| 9 | + */ |
| 10 | + |
| 11 | +import type { components } from "@/api"; |
| 12 | +import { useToolsServiceCredentialsDefinitionsStore } from "@/stores/toolsServiceCredentialsDefinitionsStore"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Just an alias for a string that represents a unique key for a service credentials identifier. |
| 16 | + * The key is a combination of the service name and version, formatted as "name-version". |
| 17 | + */ |
| 18 | +type ServiceCredentialsIdentifierKey = string; |
| 19 | + |
| 20 | +/** Type for credential field types. */ |
| 21 | +export type CredentialType = "variable" | "secret"; |
| 22 | +/** Service credential group response from API. */ |
| 23 | +export type ServiceCredentialGroupResponse = components["schemas"]["ServiceCredentialGroupResponse"]; |
| 24 | +/** Payload for creating source credentials. */ |
| 25 | +export type CreateSourceCredentialsPayload = components["schemas"]["CreateSourceCredentialsPayload"]; |
| 26 | +/** User service credentials response from API. */ |
| 27 | +export type UserServiceCredentialsResponse = components["schemas"]["UserServiceCredentialsResponse"]; |
| 28 | +/** Service credential payload for API requests. */ |
| 29 | +export type ServiceCredentialPayload = components["schemas"]["ServiceCredentialPayload"]; |
| 30 | +/** Service credential group payload for API requests. */ |
| 31 | +export type ServiceCredentialGroupPayload = components["schemas"]["ServiceCredentialGroupPayload"]; |
| 32 | +/** User service credentials with definition response from API. */ |
| 33 | +export type UserServiceCredentialsWithDefinitionResponse = |
| 34 | + components["schemas"]["UserServiceCredentialsWithDefinitionResponse"]; |
| 35 | +/** Payload for selecting current credential group. */ |
| 36 | +export type SelectCurrentGroupPayload = components["schemas"]["SelectCurrentGroupPayload"]; |
| 37 | +/** Service parameter definition from API. */ |
| 38 | +export type ServiceParameterDefinition = components["schemas"]["ServiceParameterDefinition"]; |
| 39 | +/** Service credentials definition from API. */ |
| 40 | +export type ServiceCredentialsDefinition = components["schemas"]["ServiceCredentialsDefinition"]; |
| 41 | + |
| 42 | +/** |
| 43 | + * Service credentials identifier interface. |
| 44 | + * @interface ServiceCredentialsIdentifier |
| 45 | + */ |
| 46 | +export interface ServiceCredentialsIdentifier { |
| 47 | + /** Service name. */ |
| 48 | + name: string; |
| 49 | + /** Service version. */ |
| 50 | + version: string; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Represents the definition of credentials for a particular source. |
| 55 | + * A source can be an entity using a service that uses credentials, for example, a tool. |
| 56 | + * A source may accept multiple services, each with its own credentials. |
| 57 | + * |
| 58 | + * The `services` map is indexed by the service name and version using the `getKeyFromCredentialsIdentifier` function. |
| 59 | + * @interface SourceCredentialsDefinition |
| 60 | + */ |
| 61 | +export interface SourceCredentialsDefinition { |
| 62 | + /** Type of the source (e.g., "tool"). */ |
| 63 | + sourceType: string; |
| 64 | + /** Unique identifier for the source. */ |
| 65 | + sourceId: string; |
| 66 | + /** Map of services indexed by service identifier key. */ |
| 67 | + services: Map<ServiceCredentialsIdentifierKey, ServiceCredentialsDefinition>; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Service credentials context interface. |
| 72 | + * @interface ServiceCredentialsContext |
| 73 | + * @todo Replace with proper API schema model when available. |
| 74 | + */ |
| 75 | +export interface ServiceCredentialsContext { |
| 76 | + /** User credentials ID or null if not set. */ |
| 77 | + user_credentials_id: string | null; |
| 78 | + /** Service name. */ |
| 79 | + name: string; |
| 80 | + /** Service version. */ |
| 81 | + version: string; |
| 82 | + /** Selected credential group information. */ |
| 83 | + selected_group: { |
| 84 | + /** Group ID or null if not selected. */ |
| 85 | + id: string | null; |
| 86 | + /** Group name. */ |
| 87 | + name: string; |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Generates a unique key from service credentials identifier |
| 93 | + * @param {ServiceCredentialsIdentifier} credentialsIdentifier - Service credentials identifier |
| 94 | + * @returns {ServiceCredentialsIdentifierKey} Unique key in format "name-version" |
| 95 | + */ |
| 96 | +export function getKeyFromCredentialsIdentifier( |
| 97 | + credentialsIdentifier: ServiceCredentialsIdentifier, |
| 98 | +): ServiceCredentialsIdentifierKey { |
| 99 | + return `${credentialsIdentifier.name}-${credentialsIdentifier.version}`; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Transforms tool information into source credentials definition |
| 104 | + * @param {string} toolId - The id of the tool |
| 105 | + * @param {string} toolVersion - The version of the tool |
| 106 | + * @returns {SourceCredentialsDefinition} Source credentials definition for the tool |
| 107 | + */ |
| 108 | +export function transformToSourceCredentials(toolId: string, toolVersion: string): SourceCredentialsDefinition { |
| 109 | + const { getToolServiceCredentialsDefinitionsFor } = useToolsServiceCredentialsDefinitionsStore(); |
| 110 | + |
| 111 | + const toolCredentialsDefinitions = getToolServiceCredentialsDefinitionsFor(toolId, toolVersion); |
| 112 | + |
| 113 | + const services = new Map( |
| 114 | + toolCredentialsDefinitions.map((service) => [getKeyFromCredentialsIdentifier(service), service]), |
| 115 | + ); |
| 116 | + |
| 117 | + return { |
| 118 | + sourceType: "tool", |
| 119 | + sourceId: toolId, |
| 120 | + services, |
| 121 | + }; |
| 122 | +} |
0 commit comments