import {z} from 'zod' export type Currency = 'UNK' | 'USD' | 'GBP' | 'CAD' | 'EUR' | 'AUD' | 'NZD' | 'SEK' | 'ILS' | 'CHF' | 'HKD' | 'JPY' | 'SGD' | 'KRW' | 'NOK' | 'DKK' | 'PLN' | 'RON' | 'HUF' | 'CZK' | 'BRL' | 'MXN' | 'ARS' | 'CLP' | 'COP' | 'INR' | 'TRY'; export type SSIOOrderLineType = 'BUDGET' | 'PERPETUALS'; export type SSIOInsertionOrderCreate = {accepted_terms_id: string, accepted_terms_time?: number | undefined, agency_link?: string | undefined, billing_contact_email: string, billing_contact_firstname: string, billing_contact_lastname: string, billto_billing_address_id: string, billto_business_address_id: string, billto_company_id: string, budget_amount?: number | undefined, currency_info: Currency, end_date?: string | undefined, estimated_monthly_spend?: number | undefined, media_contact_email: string, media_contact_firstname: string, media_contact_lastname: string, order_line_type: SSIOOrderLineType, order_name: string, pmp_id: string, po_number: string, start_date: string, user_email?: string | undefined}; export type CreateApiAdAccountsAdAccountIdSsioInsertionOrdersArgs = {ad_account_id: string, body: SSIOInsertionOrderCreate}; export type CreateApiAdAccountsAdAccountIdSsioInsertionOrdersOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const sSIOInsertionOrder = z.object({pin_order_id: 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 }; /** * Create insertion order through SSIO. * * POST /ad_accounts/{ad_account_id}/ssio/insertion_orders */ export const createApiAdAccountsAdAccountIdSsioInsertionOrders = async (args: CreateApiAdAccountsAdAccountIdSsioInsertionOrdersArgs, options?: CreateApiAdAccountsAdAccountIdSsioInsertionOrdersOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ad_accounts/${encodeURIComponent(String(args.ad_account_id))}/ssio/insertion_orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(sSIOInsertionOrder, await res.json()) };