import {z} from 'zod' export type CreateApiSiteListsUploadArgs = {body: BodyInit}; export type CreateApiSiteListsUploadOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const siteListFull = z.object({id: z.number().int().optional(), version: z.number().int().optional(), status: z.boolean().optional(), organization_id: z.number().int().optional(), name: z.string().max(64).optional(), restriction: z.enum(["INCLUDE", "EXCLUDE"]).optional(), filename: z.string().max(255).nullable().optional(), content_updated_on: z.string().optional(), sites_count: z.number().int().nullable().optional(), sites_count_domain: z.number().int().nullable().optional(), sites_count_app: z.number().int().nullable().optional(), sites_count_primary_seller_id: z.number().int().nullable().optional(), created_on: z.string().optional(), updated_on: z.string().optional()}); export const singleMetadata = z.object({status: z.string()}); export const createApiSiteListsUploadResponse = z.object({data: siteListFull.optional(), meta: singleMetadata.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 }; /** * Upload New Site Lists * * POST /site_lists/upload */ export const createApiSiteListsUpload = async (args: CreateApiSiteListsUploadArgs, options?: CreateApiSiteListsUploadOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/site_lists/upload${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiSiteListsUploadResponse, await res.json()) };