2022-02-07 18:06:37 +01:00
|
|
|
import { Credentials } from 'models'
|
2022-05-13 15:22:44 -07:00
|
|
|
import { stringify } from 'qs'
|
2022-01-18 18:25:18 +01:00
|
|
|
import useSWR from 'swr'
|
2022-02-07 18:06:37 +01:00
|
|
|
import { sendRequest } from 'utils'
|
2022-05-13 15:22:44 -07:00
|
|
|
import { fetcher } from './utils'
|
2022-01-18 18:25:18 +01:00
|
|
|
|
|
|
|
export const useCredentials = ({
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId,
|
2022-01-18 18:25:18 +01:00
|
|
|
onError,
|
|
|
|
}: {
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId?: string
|
2022-04-11 12:59:00 -05:00
|
|
|
onError?: (error: Error) => void
|
2022-01-18 18:25:18 +01:00
|
|
|
}) => {
|
|
|
|
const { data, error, mutate } = useSWR<{ credentials: Credentials[] }, Error>(
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId ? `/api/credentials?${stringify({ workspaceId })}` : null,
|
2022-01-18 18:25:18 +01:00
|
|
|
fetcher
|
|
|
|
)
|
2022-04-11 12:59:00 -05:00
|
|
|
if (error && onError) onError(error)
|
2022-01-18 18:25:18 +01:00
|
|
|
return {
|
2022-02-19 10:58:56 +01:00
|
|
|
credentials: data?.credentials ?? [],
|
2022-01-18 18:25:18 +01:00
|
|
|
isLoading: !error && !data,
|
|
|
|
mutate,
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 18:06:37 +01:00
|
|
|
|
|
|
|
export const createCredentials = async (
|
2022-05-14 16:52:05 -07:00
|
|
|
credentials: Omit<Credentials, 'id' | 'iv' | 'createdAt'>
|
2022-02-07 18:06:37 +01:00
|
|
|
) =>
|
|
|
|
sendRequest<{
|
|
|
|
credentials: Credentials
|
|
|
|
}>({
|
2022-05-13 15:22:44 -07:00
|
|
|
url: `/api/credentials?${stringify({
|
|
|
|
workspaceId: credentials.workspaceId,
|
|
|
|
})}`,
|
2022-02-07 18:06:37 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: credentials,
|
|
|
|
})
|
2022-02-19 10:58:56 +01:00
|
|
|
|
|
|
|
export const deleteCredentials = async (
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId: string,
|
2022-02-19 10:58:56 +01:00
|
|
|
credentialsId: string
|
|
|
|
) =>
|
|
|
|
sendRequest<{
|
|
|
|
credentials: Credentials
|
|
|
|
}>({
|
2022-05-13 15:22:44 -07:00
|
|
|
url: `/api/credentials/${credentialsId}?${stringify({ workspaceId })}`,
|
2022-02-19 10:58:56 +01:00
|
|
|
method: 'DELETE',
|
|
|
|
})
|