import {z} from 'zod' export type GetApiNetworksCreativesCreativeIdArgs = {creativeId: number}; export type GetApiNetworksCreativesCreativeIdOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const creative = z.object({network_offer_creative_id: z.number().int().optional(), network_id: z.number().int().optional(), network_offer_id: z.number().int().optional(), name: z.string().optional(), creative_type: z.enum(["image", "html", "thumbnail", "email", "archive", "video", "text", "link"]).optional(), is_private: z.boolean().optional(), creative_status: z.enum(["active", "paused", "deleted"]).optional(), resource_url: z.string().optional(), html_code: z.string().optional(), width: z.number().int().optional(), height: z.number().int().optional(), email_from: z.string().optional(), email_subject: z.string().optional(), time_created: z.number().int().optional(), time_saved: z.number().int().optional(), additional_offer_ids: z.array(z.number().int()).nullable().optional(), is_apply_specific_affiliates: z.boolean().optional(), email_subject_lines: z.string().optional(), email_from_lines: z.string().optional(), relationship: z.object({offer: z.object({network_offer_id: z.number().int().optional(), network_id: z.number().int().optional(), network_advertiser_id: z.number().int().optional(), network_offer_group_id: z.number().int().optional(), name: z.string().optional(), offer_status: z.string().optional(), network_tracking_domain_id: z.number().int().optional(), visibility: z.string().optional(), currency_id: z.string().optional()}).optional(), resource_asset: z.object({network_asset_id: z.number().int().optional(), content_type: z.string().optional(), filename: z.string().optional(), url: z.string().optional(), file_size: z.number().int().optional(), image_width: z.number().int().optional(), image_height: z.number().int().optional()}).optional()}).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 }; /** * Get Creative by ID * * GET /networks/creatives/{creativeId} */ export const getApiNetworksCreativesCreativeId = async (args: GetApiNetworksCreativesCreativeIdArgs, options?: GetApiNetworksCreativesCreativeIdOptions) => { const {baseUrl = "https://api.eflow.team/v1", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-Eflow-Api-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/networks/creatives/${encodeURIComponent(String(args.creativeId))}${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(creative, await res.json()) };