import {z} from 'zod' export type UpdateApiNetworksWebhooksWebhookIdArgs = {webhookId: number, body: {webhook_types: Array<'advertiser_created' | 'advertiser_signed_up' | 'advertiser_updated' | 'affiliate_approved_for_offer' | 'affiliate_created' | 'affiliate_signed_up' | 'affiliate_signup_verdict' | 'affiliate_updated' | 'offer_created' | 'offer_updated' | 'traffic_optimized'>, http_method: 'get' | 'post', webhook_url: string, description: string, status: 'active' | 'inactive'}}; export type UpdateApiNetworksWebhooksWebhookIdOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({network_webhook_id: z.number().int().optional(), network_id: z.number().int().optional(), webhook_types: z.array(z.enum(["advertiser_created", "advertiser_signed_up", "advertiser_updated", "affiliate_approved_for_offer", "affiliate_created", "affiliate_signed_up", "affiliate_signup_verdict", "affiliate_updated", "offer_created", "offer_updated", "traffic_optimized"])).optional(), http_method: z.enum(["get", "post"]).optional(), webhook_url: z.string().optional(), description: z.string().optional(), status: z.enum(["active", "inactive"]).optional(), time_created: z.number().int().optional(), time_saved: z.number().int().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 }; /** * Update Webhook * * PUT /networks/webhooks/{webhookId} */ export const updateApiNetworksWebhooksWebhookId = async (args: UpdateApiNetworksWebhooksWebhookIdArgs, options?: UpdateApiNetworksWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.eflow.team/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/networks/webhooks/${encodeURIComponent(String(args.webhookId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(webhook, await res.json()) };