feat(integration): ✨ Add webhooks
This commit is contained in:
@ -2,6 +2,7 @@ import { sendRequest } from 'utils'
|
||||
import { stringify } from 'qs'
|
||||
import useSWR from 'swr'
|
||||
import { fetcher } from './utils'
|
||||
import { Table, Variable, VariableForTest, WebhookResponse } from 'models'
|
||||
|
||||
export const getGoogleSheetsConsentScreenUrl = (
|
||||
redirectUrl: string,
|
||||
@ -64,3 +65,68 @@ export const useSheets = ({
|
||||
mutate,
|
||||
}
|
||||
}
|
||||
|
||||
export const executeWebhook = (
|
||||
typebotId: string,
|
||||
webhookId: string,
|
||||
variables: Table<Variable>
|
||||
) =>
|
||||
sendRequest<WebhookResponse>({
|
||||
url: `/api/typebots/${typebotId}/webhooks/${webhookId}/execute`,
|
||||
method: 'POST',
|
||||
body: {
|
||||
variables,
|
||||
},
|
||||
})
|
||||
|
||||
export const convertVariableForTestToVariables = (
|
||||
variablesForTest: Table<VariableForTest> | undefined,
|
||||
variables: Table<Variable>
|
||||
): Table<Variable> => {
|
||||
if (!variablesForTest) return { byId: {}, allIds: [] }
|
||||
return {
|
||||
byId: {
|
||||
...variables.byId,
|
||||
...variablesForTest.allIds.reduce((obj, id) => {
|
||||
const variableForTest = variablesForTest.byId[id]
|
||||
if (!variableForTest.variableId) return {}
|
||||
const variable = variables.byId[variableForTest.variableId ?? '']
|
||||
return {
|
||||
...obj,
|
||||
[variableForTest.variableId]: {
|
||||
...variable,
|
||||
value: variableForTest.value,
|
||||
},
|
||||
}
|
||||
}, {}),
|
||||
},
|
||||
allIds: variables.allIds,
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const getDeepKeys = (obj: any): string[] => {
|
||||
let keys: string[] = []
|
||||
for (const key in obj) {
|
||||
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
|
||||
const subkeys = getDeepKeys(obj[key])
|
||||
keys = keys.concat(
|
||||
subkeys.map(function (subkey) {
|
||||
return key + '.' + subkey
|
||||
})
|
||||
)
|
||||
} else if (Array.isArray(obj[key])) {
|
||||
for (let i = 0; i < obj[key].length; i++) {
|
||||
const subkeys = getDeepKeys(obj[key][i])
|
||||
keys = keys.concat(
|
||||
subkeys.map(function (subkey) {
|
||||
return key + '[' + i + ']' + '.' + subkey
|
||||
})
|
||||
)
|
||||
}
|
||||
} else {
|
||||
keys.push(key)
|
||||
}
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
Reference in New Issue
Block a user