:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, connect, kea, key, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { publicWebhooksHostOrigin } from 'lib/utils/apiHost'
import { tryJsonParse } from 'lib/utils/json'
import type { HogFunctionConfigurationType } from '../../../../types'
import { HogFunctionConfigurationLogicProps, hogFunctionConfigurationLogic } from '../hogFunctionConfigurationLogic'
export type HogFunctionSourceWebhookTestForm = {
method: string
headers: string
query: string
body: string
mock_request: boolean
}
export type HogFunctionSourceWebhookTestResult = {
status: number
body: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionSourceWebhookTestLogicValues {
configuration: HogFunctionConfigurationType // hogFunctionConfigurationLogic
templateId: string | undefined // hogFunctionConfigurationLogic
exampleCurlRequest: string
expanded: boolean
isTestInvocationSubmitting: boolean
isTestInvocationValid: boolean
showTestInvocationErrors: boolean
testInvocation: HogFunctionSourceWebhookTestForm
testInvocationAllErrors: Record<string, any>
testInvocationChanged: boolean
testInvocationErrors: DeepPartialMap<HogFunctionSourceWebhookTestForm, ValidationErrorType>
testInvocationHasErrors: boolean
testInvocationManualErrors: Record<string, any>
testInvocationTouched: boolean
testInvocationTouches: Record<string, boolean>
testInvocationValidationErrors: DeepPartialMap<HogFunctionSourceWebhookTestForm, ValidationErrorType>
testResult: HogFunctionSourceWebhookTestResult | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionSourceWebhookTestLogicActions {
resetTestInvocation: (values?: HogFunctionSourceWebhookTestForm) => {
values?: HogFunctionSourceWebhookTestForm
}
setTestInvocationManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setTestInvocationValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setTestInvocationValues: (values: DeepPartial<HogFunctionSourceWebhookTestForm>) => {
values: DeepPartial<HogFunctionSourceWebhookTestForm>
}
setTestResult: (result: HogFunctionSourceWebhookTestResult | null) => {
result: HogFunctionSourceWebhookTestResult | null
}
submitTestInvocation: () => {
value: boolean
}
submitTestInvocationFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitTestInvocationRequest: (testInvocation: HogFunctionSourceWebhookTestForm) => {
testInvocation: HogFunctionSourceWebhookTestForm
}
submitTestInvocationSuccess: (testInvocation: HogFunctionSourceWebhookTestForm) => {
testInvocation: HogFunctionSourceWebhookTestForm
}
toggleExpanded: (expanded?: boolean) => {
expanded: boolean | undefined
}
touchTestInvocationField: (key: string) => {
key: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogFunctionSourceWebhookTestLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
exampleCurlRequest: (testInvocation: HogFunctionSourceWebhookTestForm, arg: any) => string
}
}
export type hogFunctionSourceWebhookTestLogicType = MakeLogicType<
hogFunctionSourceWebhookTestLogicValues,
hogFunctionSourceWebhookTestLogicActions,
HogFunctionConfigurationLogicProps,
hogFunctionSourceWebhookTestLogicMeta
>
export const hogFunctionSourceWebhookTestLogic = kea<hogFunctionSourceWebhookTestLogicType>([
props({} as HogFunctionConfigurationLogicProps),
key(({ id, templateId }: HogFunctionConfigurationLogicProps) => {
return id ?? templateId ?? 'new'
}),
path((id) => ['scenes', 'pipeline', 'hogfunctions', 'hogFunctionSourceWebhookTestLogic', id]),
connect((props: HogFunctionConfigurationLogicProps) => ({
values: [hogFunctionConfigurationLogic(props), ['configuration', 'templateId']],
})),
actions({
setTestResult: (result: HogFunctionSourceWebhookTestResult | null) => ({ result }),
toggleExpanded: (expanded?: boolean) => ({ expanded }),
}),
reducers({
expanded: [
false as boolean,
{
toggleExpanded: (state, { expanded }) => (expanded === undefined ? !state : expanded),
},
],
testResult: [
null as HogFunctionSourceWebhookTestResult | null,
{
setTestResult: (_, { result }) => result,
},
],
}),
forms(({ props, actions }) => ({
testInvocation: {
defaults: {
mock_request: true,
method: 'POST',
headers: `{
"Content-Type": "application/json"
}`,
query: '',
body: `{
"event": "my example event",
"distinct_id": "webhook-test-123"
}`,
} as HogFunctionSourceWebhookTestForm,
alwaysShowErrors: true,
errors: ({ headers, body }) => {
return {
headers: !headers ? 'Required' : tryJsonParse(headers) ? undefined : 'Invalid JSON',
body: !body ? 'Required' : tryJsonParse(body) ? undefined : 'Invalid JSON',
}
},
submit: async (data) => {
actions.setTestResult(null)
const fetchOptions: RequestInit = {
method: data.method,
headers: tryJsonParse(data.headers),
body: data.method == 'GET' ? undefined : data.body,
credentials: 'omit',
}
const response = await fetch(
`${publicWebhooksHostOrigin()}/public/webhooks/${props.id ?? 'unknown'}${data.query ? `?${data.query}` : ''}`,
fetchOptions
)
actions.setTestResult({
status: response.status,
body: await response.text(),
})
},
},
})),
selectors({
exampleCurlRequest: [
(s) => [s.testInvocation, (_, props) => props],
(testInvocation: HogFunctionSourceWebhookTestForm, props) => {
const headersJson = tryJsonParse(testInvocation.headers)
const headers = headersJson
? Object.entries(headersJson)
.map(([key, value]) => `-H "${key}: ${value}"`)
.join(' ')
: ''
return `curl -X ${testInvocation.method} ${headers} \\
-d '${testInvocation.body}' \\
${publicWebhooksHostOrigin()}/public/webhooks/${props.id ?? 'unknown'}${testInvocation.query ? `?${testInvocation.query}` : ''}`
},
],
}),
])