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

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-02-18 14:57:10 +01:00
import { CustomDomain } from 'db'
import { Credentials } from 'models'
2022-05-13 15:22:44 -07:00
import { stringify } from 'qs'
2022-02-18 14:57:10 +01:00
import useSWR from 'swr'
import { sendRequest } from 'utils'
2022-05-13 15:22:44 -07:00
import { fetcher } from './utils'
2022-02-18 14:57:10 +01:00
export const useCustomDomains = ({
2022-05-13 15:22:44 -07:00
workspaceId,
2022-02-18 14:57:10 +01:00
onError,
}: {
2022-05-13 15:22:44 -07:00
workspaceId?: string
2022-02-18 14:57:10 +01:00
onError: (error: Error) => void
}) => {
const { data, error, mutate } = useSWR<
2022-05-14 16:52:05 -07:00
{ customDomains: Omit<CustomDomain, 'createdAt'>[] },
2022-02-18 14:57:10 +01:00
Error
2022-05-13 15:22:44 -07:00
>(
workspaceId ? `/api/customDomains?${stringify({ workspaceId })}` : null,
fetcher
)
2022-02-18 14:57:10 +01:00
if (error) onError(error)
return {
customDomains: data?.customDomains,
isLoading: !error && !data,
mutate,
}
}
export const createCustomDomain = async (
2022-05-13 15:22:44 -07:00
workspaceId: string,
2022-05-14 16:52:05 -07:00
customDomain: Omit<CustomDomain, 'createdAt' | 'workspaceId'>
2022-02-18 14:57:10 +01:00
) =>
sendRequest<{
credentials: Credentials
}>({
2022-05-13 15:22:44 -07:00
url: `/api/customDomains?${stringify({ workspaceId })}`,
2022-02-18 14:57:10 +01:00
method: 'POST',
body: customDomain,
})
export const deleteCustomDomain = async (
2022-05-13 15:22:44 -07:00
workspaceId: string,
2022-02-18 14:57:10 +01:00
customDomain: string
) =>
sendRequest<{
credentials: Credentials
}>({
2022-05-13 15:22:44 -07:00
url: `/api/customDomains/${customDomain}?${stringify({ workspaceId })}`,
2022-02-18 14:57:10 +01:00
method: 'DELETE',
})