import {z} from 'zod' export type CreateApiApiSubscriptionsIdTestArgs = {id: string, mode?: ('direct' | 'kafka' | 'full') | undefined}; export type CreateApiApiSubscriptionsIdTestOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const testSubscriptionResponse = z.object({subscriptionId: z.string(), subscriptionName: z.string().optional(), url: z.string(), mode: z.enum(["direct", "kafka", "full"]), flowCheck: z.object({success: z.boolean().optional(), statusCode: z.number().int().optional(), durationMs: z.number().int().optional()}).optional(), testPayload: z.object({entityType: z.string().optional(), signalType: z.string().optional(), data: z.object({}).optional()}).optional(), isSuccess: z.boolean(), timestamp: z.string()}); 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 Subscription * * POST /api/subscriptions/{id}/test */ export const createApiApiSubscriptionsIdTest = async (args: CreateApiApiSubscriptionsIdTestArgs, options?: CreateApiApiSubscriptionsIdTestOptions) => { const {baseUrl = "https://api.lusha.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.mode != null) query.append("mode", String(args.mode)) if (ApiKeyAuth !== undefined) headers.set("api_key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/subscriptions/${encodeURIComponent(String(args.id))}/test${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(testSubscriptionResponse, await res.json()) };