import {z} from 'zod' export type GetApiApiV1WebhookTestArgs = {destination: string, version: 'webhook/v1', kind: 'News/v1' | 'Signals/v1' | 'Earnings/v1' | 'Ratings/v1' | 'Dividends/v1' | 'IPOs/v1' | 'Guidance/v1' | 'Splits/v1' | 'OptionActivity/v1' | 'Conference/v1' | 'Economics/v1' | 'Offerings/v1' | 'MA/v1' | 'Retail/v1' | 'FDA/v1' | 'WIIMs/v1' | 'SECInsiderTransaction/v1' | 'GovernmentTrade/v1', token?: string | undefined}; export type GetApiApiV1WebhookTestOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1WebhookTestResponse = z.object({status: z.string().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 Webhook Delivery * * GET /api/v1/webhook/test */ export const getApiApiV1WebhookTest = async (args: GetApiApiV1WebhookTestArgs, options?: GetApiApiV1WebhookTestOptions) => { const {baseUrl = "https://api.benzinga.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.destination != null) query.append("destination", String(args.destination)) if (args.version != null) query.append("version", String(args.version)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.token != null) query.append("token", String(args.token)) if (ApiKeyAuth !== undefined) headers.set("Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhook/test${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1WebhookTestResponse, await res.json()) };