2
0
Files
bot/apps/builder/services/credentials.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

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