99 lines
2.4 KiB
TypeScript
99 lines
2.4 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { useAuthStore } from './auth.store'
|
|
|
|
interface Host {
|
|
id: number
|
|
name: string
|
|
hostname: string
|
|
port: number
|
|
protocol: 'ssh' | 'rdp'
|
|
groupId: number | null
|
|
credentialId: number | null
|
|
tags: string[]
|
|
notes: string | null
|
|
color: string | null
|
|
lastConnectedAt: string | null
|
|
group: { id: number; name: string } | null
|
|
}
|
|
|
|
interface HostGroup {
|
|
id: number
|
|
name: string
|
|
parentId: number | null
|
|
children: HostGroup[]
|
|
hosts: Host[]
|
|
}
|
|
|
|
export const useConnectionStore = defineStore('connections', {
|
|
state: () => ({
|
|
hosts: [] as Host[],
|
|
groups: [] as HostGroup[],
|
|
search: '',
|
|
loading: false,
|
|
}),
|
|
actions: {
|
|
headers() {
|
|
const auth = useAuthStore()
|
|
return { Authorization: `Bearer ${auth.token}` }
|
|
},
|
|
async fetchHosts() {
|
|
this.loading = true
|
|
try {
|
|
this.hosts = await $fetch('/api/hosts', { headers: this.headers() })
|
|
} finally {
|
|
this.loading = false
|
|
}
|
|
},
|
|
async fetchTree() {
|
|
this.groups = await $fetch('/api/groups/tree', { headers: this.headers() })
|
|
},
|
|
async createHost(data: Partial<Host>) {
|
|
const host = await $fetch<Host>('/api/hosts', {
|
|
method: 'POST',
|
|
body: data,
|
|
headers: this.headers(),
|
|
})
|
|
await this.fetchHosts()
|
|
return host
|
|
},
|
|
async updateHost(id: number, data: Partial<Host>) {
|
|
await $fetch(`/api/hosts/${id}`, {
|
|
method: 'PUT',
|
|
body: data,
|
|
headers: this.headers(),
|
|
})
|
|
await this.fetchHosts()
|
|
},
|
|
async deleteHost(id: number) {
|
|
await $fetch(`/api/hosts/${id}`, {
|
|
method: 'DELETE',
|
|
headers: this.headers(),
|
|
})
|
|
await this.fetchHosts()
|
|
},
|
|
async createGroup(data: { name: string; parentId?: number }) {
|
|
await $fetch('/api/groups', {
|
|
method: 'POST',
|
|
body: data,
|
|
headers: this.headers(),
|
|
})
|
|
await this.fetchTree()
|
|
},
|
|
async updateGroup(id: number, data: { name?: string; parentId?: number }) {
|
|
await $fetch(`/api/groups/${id}`, {
|
|
method: 'PUT',
|
|
body: data,
|
|
headers: this.headers(),
|
|
})
|
|
await this.fetchTree()
|
|
},
|
|
async deleteGroup(id: number) {
|
|
await $fetch(`/api/groups/${id}`, {
|
|
method: 'DELETE',
|
|
headers: this.headers(),
|
|
})
|
|
await this.fetchTree()
|
|
},
|
|
},
|
|
})
|