import {z} from 'zod' export type IntegrationLogClientError = {cause?: string | undefined, column_number?: number | undefined, file_name?: string | undefined, line_number?: number | undefined, message?: string | undefined, message_detail?: string | undefined, name?: string | undefined, number?: number | undefined, stack_trace?: string | undefined}; export type IntegrationLogEventType = 'APP' | 'API'; export type IntegrationLogLevel = 'INFO' | 'WARN' | 'ERROR'; export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'; export type IntegrationLogClientRequest = {host: string, method: HttpMethod, path: string, request_headers?: {[key: string]: string} | undefined, response_headers?: {[key: string]: string} | undefined, response_status_code?: number | undefined}; export type IntegrationLog = {advertiser_id?: (string | null) | undefined, app_version_number?: string | undefined, client_timestamp: number, error?: IntegrationLogClientError | undefined, event_type: IntegrationLogEventType, external_business_id?: (string | null) | undefined, feed_profile_id?: (string | null) | undefined, log_level: IntegrationLogLevel, merchant_id?: (string | null) | undefined, message?: string | undefined, platform_version_number?: string | undefined, request?: IntegrationLogClientRequest | undefined, tag_id?: (string | null) | undefined}; export type IntegrationLogsRequestCreate = {logs: Array}; export type CreateApiIntegrationsLogsArgs = {body: IntegrationLogsRequestCreate}; export type CreateApiIntegrationsLogsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const integrationLogsSuccessResponse = z.object({message: 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 }; /** * Receives batched logs from integration applications. * * POST /integrations/logs */ export const createApiIntegrationsLogs = async (args: CreateApiIntegrationsLogsArgs, options?: CreateApiIntegrationsLogsOptions) => { 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(/\/+$/, '')}/integrations/logs${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(integrationLogsSuccessResponse, await res.json()) };