import { getCredentialTypes , getCredentialsNewName , getAllCredentials , deleteCredential , getCredentialData , createNewCredential , updateCredential , oAuth2CredentialAuthorize , oAuth1CredentialAuthorize , testCredential , } from '@/api/credentials' ; import Vue from 'vue' ; import { ActionContext , Module } from 'vuex' ; import { ICredentialMap , ICredentialsResponse , ICredentialsState , ICredentialTypeMap , IRootState , } from '../Interface' ; import { ICredentialType , ICredentialsDecrypted , NodeCredentialTestResult , INodeTypeDescription , } from 'n8n-workflow' ; import { getAppNameFromCredType } from '@/components/helpers' ; const DEFAULT_CREDENTIAL_NAME = 'Unnamed credential' ; const DEFAULT_CREDENTIAL_POSTFIX = 'account' ; const TYPES_WITH_DEFAULT_NAME = [ 'httpBasicAuth' , 'oAuth2Api' , 'httpDigestAuth' , 'oAuth1Api' ] ; const module = { namespaced : true , state : { credentialTypes : { } , credentials : { } , } , mutations : { setCredentialTypes : ( state , credentialTypes ) => { state . credentialTypes = credentialTypes . reduce ( ( accu , cred ) => { accu [ cred . name ] = cred ; return accu ; } , { } ) ; } , setCredentials : ( state , credentials ) => { state . credentials = credentials . reduce ( ( accu , cred ) => { if ( cred . id ) { accu [ cred . id ] = cred ; } return accu ; } , { } ) ; } , upsertCredential ( state , credential ) { if ( credential . id ) { Vue . set ( state . credentials , credential . id , credential ) ; } } , deleteCredential ( state , id ) { Vue . delete ( state . credentials , id ) ; } , enableOAuthCredential ( state , credential ) { } , } , getters : { allCredentialTypes ( state ) { return Object . values ( state . credentialTypes ) . sort ( ( a , b ) => a . displayName . localeCompare ( b . displayName ) ) ; } , allCredentials ( state ) { return Object . values ( state . credentials ) . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ; } , allCredentialsByType ( state , getters ) { const credentials = getters . allCredentials as ICredentialsResponse [ ] ; const types = getters . allCredentialTypes as ICredentialType [ ] ; return types . reduce ( ( accu , type ) => { accu [ type . name ] = credentials . filter ( ( cred ) => cred . type === type . name ) ; return accu ; } , { } ) ; } , getCredentialTypeByName : ( state ) => { return ( type ) => state . credentialTypes [ type ] ; } , getCredentialById : ( state ) => { return ( id ) => state . credentials [ id ] ; } , getCredentialByIdAndType : ( state ) => { return ( id , type ) => { const credential = state . credentials [ id ] ; return ! credential || credential . type !== type ? undefined : credential ; } ; } , getCredentialsByType : ( state , getters ) => { return ( credentialType ) : ICredentialsResponse [ ] => { return getters . allCredentialsByType [ credentialType ] || [ ] ; } ; } , getNodesWithAccess ( state , getters , rootState , rootGetters ) { return ( credentialTypeName ) => { const nodeTypes = rootGetters . allNodeTypes ; return nodeTypes . filter ( ( nodeType ) => { if ( ! nodeType . credentials ) { return false ; } for ( const credentialTypeDescription of nodeType . credentials ) { if ( credentialTypeDescription . name === credentialTypeName ) { return true ; } } return false ; } ) ; } ; } , } , actions : { fetchCredentialTypes : async ( context ) => { const credentialTypes = await getCredentialTypes ( context . rootGetters . getRestApiContext ) ; context . commit ( 'setCredentialTypes' , credentialTypes ) ; } , fetchAllCredentials : async ( context ) => { const credentials = await getAllCredentials ( context . rootGetters . getRestApiContext ) ; context . commit ( 'setCredentials' , credentials ) ; } , getCredentialData : async ( context , { id } ) => { return await getCredentialData ( context . rootGetters . getRestApiContext , id ) ; } , createNewCredential : async ( context , data ) => { const credential = await createNewCredential ( context . rootGetters . getRestApiContext , data ) ; context . commit ( 'upsertCredential' , credential ) ; return credential ; } , updateCredential : async ( context , params ) => { const { id , data } = params ; const credential = await updateCredential ( context . rootGetters . getRestApiContext , id , data ) ; context . commit ( 'upsertCredential' , credential ) ; return credential ; } , deleteCredential : async ( context , { id } ) => { const deleted = await deleteCredential ( context . rootGetters . getRestApiContext , id ) ; if ( deleted ) { context . commit ( 'deleteCredential' , id ) ; } } , oAuth2Authorize : async ( context , data ) => { return oAuth2CredentialAuthorize ( context . rootGetters . getRestApiContext , data ) ; } , oAuth1Authorize : async ( context , data ) => { return oAuth1CredentialAuthorize ( context . rootGetters . getRestApiContext , data ) ; } , testCredential : async ( context , data ) : Promise < NodeCredentialTestResult > => { return testCredential ( context . rootGetters . getRestApiContext , { credentials : data } ) ; } , getNewCredentialName : async ( context , params ) => { try { const { credentialTypeName } = params ; let newName = DEFAULT_CREDENTIAL_NAME ; if ( ! TYPES_WITH_DEFAULT_NAME . includes ( credentialTypeName ) ) { const { displayName } = context . getters . getCredentialTypeByName ( credentialTypeName ) ; newName = getAppNameFromCredType ( displayName ) ; newName = newName . length > 0 ? `${ newName } ${ DEFAULT_CREDENTIAL_POSTFIX }` : DEFAULT_CREDENTIAL_NAME ; } const res = await getCredentialsNewName ( context . rootGetters . getRestApiContext , newName ) ; return res . name ; } catch ( e ) { return DEFAULT_CREDENTIAL_NAME ; } } , } , } ; export default module ;