import {z} from 'zod' export type PinterestLibIntegerFormatType = string; export type GetApiIntegrationsIdArgs = {id: PinterestLibIntegerFormatType}; export type GetApiIntegrationsIdOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const pinterestLibIntegerFormatType = z.string(); export const integrationRecord = z.object({additional_id_1: z.string().nullable().optional(), connected_advertiser_id: z.string().nullable().optional(), connected_lba_id: z.string().nullable().optional(), connected_merchant_id: z.string().nullable().optional(), connected_tag_id: z.string().nullable().optional(), connected_user_id: z.string().optional(), created_time: z.number().int().optional(), external_business_id: z.string().nullable().optional(), id: pinterestLibIntegerFormatType, partner_access_token: z.string().nullable().optional(), partner_access_token_expiry: z.number().int().nullable().optional(), partner_metadata: z.string().nullable().optional(), partner_primary_email: z.string().nullable().optional(), partner_refresh_token: z.string().nullable().optional(), partner_refresh_token_expiry: z.number().int().nullable().optional(), scopes: z.string().nullable().optional(), updated_time: 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 }; /** * Get integration metadata * * GET /integrations/{id} */ export const getApiIntegrationsId = async (args: GetApiIntegrationsIdArgs, options?: GetApiIntegrationsIdOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/integrations/${encodeURIComponent(String(args.id))}${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(integrationRecord, await res.json()) };