2
0

(chat) Improve chat API compatibility with preview mode

This commit is contained in:
Baptiste Arnaud
2023-01-16 12:13:21 +01:00
parent dbe5c3cdb1
commit 7311988901
55 changed files with 4133 additions and 465 deletions

View File

@ -1,28 +1,31 @@
import { InitialChatReply, SendMessageInput, StartParams } from 'models'
import { getViewerUrl, sendRequest } from 'utils'
import { InitialChatReply } from '@/types'
import { SendMessageInput, StartParams } from 'models'
import { getViewerUrl, isEmpty, sendRequest } from 'utils'
export async function getInitialChatReplyQuery({
typebotId,
typebot,
isPreview,
apiHost,
prefilledVariables,
startGroupId,
resultId,
}: StartParams & {
apiHost?: string
}) {
if (!typebotId)
if (!typebot)
throw new Error('Typebot ID is required to get initial messages')
const response = await sendRequest<InitialChatReply>({
return sendRequest<InitialChatReply>({
method: 'POST',
url: `${apiHost ?? getViewerUrl()}/api/v1/sendMessage`,
url: `${isEmpty(apiHost) ? getViewerUrl() : apiHost}/api/v1/sendMessage`,
body: {
startParams: {
isPreview,
typebotId,
typebot,
prefilledVariables,
startGroupId,
resultId,
},
} satisfies SendMessageInput,
})
return response.data
}

View File

@ -1,5 +1,5 @@
import { ChatReply, SendMessageInput } from 'models'
import { getViewerUrl, sendRequest } from 'utils'
import { getViewerUrl, isEmpty, sendRequest } from 'utils'
export async function sendMessageQuery({
apiHost,
@ -7,7 +7,7 @@ export async function sendMessageQuery({
}: SendMessageInput & { apiHost?: string }) {
const response = await sendRequest<ChatReply>({
method: 'POST',
url: `${apiHost ?? getViewerUrl()}/api/v1/sendMessage`,
url: `${isEmpty(apiHost) ? getViewerUrl() : apiHost}/api/v1/sendMessage`,
body,
})