import {z} from 'zod' export type CreateApiCompaniesCompanyIdWebhookTestArgs = {companyId: string, body: {type?: ('alert' | 'data') | undefined, webhook?: ({url: string, token: string, auth_header?: string | undefined}) | undefined}}; export type CreateApiCompaniesCompanyIdWebhookTestOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookAlertEvent = z.object({event: z.object({earliest_timestamp: z.number().int().optional(), test: z.boolean().optional()}).and(z.record(z.string(), z.record(z.string(), z.object({employees: z.number().int().optional(), third_party: z.number().int().optional(), externals: z.number().int().optional(), clients: z.number().int().optional(), apps: z.number().int().optional(), supply_chain: z.number().int().optional()})))).optional(), sourcetype: z.literal("hudsonrock:alert").optional()}); export const webhookDataEvent = z.object({event: z.object({category: z.enum(["employee", "third_party", "client", "app", "external"]).optional(), stealer: z.object({}).optional(), test: z.boolean().optional()}).optional(), sourcetype: z.literal("hudsonrock:data").optional()}); export const createApiCompaniesCompanyIdWebhookTestResponse = z.object({delivered: z.boolean().optional(), status_code: z.number().int().nullable().optional(), response_time_ms: z.number().int().nullable().optional(), response_body: z.string().nullable().optional(), error: z.string().nullable().optional(), type: z.enum(["alert", "data"]).optional(), payload: z.union([webhookAlertEvent, webhookDataEvent]).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Test Company Webhook * * POST /companies/{companyId}/webhook/test */ export const createApiCompaniesCompanyIdWebhookTest = async (args: CreateApiCompaniesCompanyIdWebhookTestArgs, options?: CreateApiCompaniesCompanyIdWebhookTestOptions) => { const {baseUrl = "https://api.hudsonrock.com/json/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companyId))}/webhook/test${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCompaniesCompanyIdWebhookTestResponse, await res.json()) };