import {z} from 'zod' export type PinterestLibIntegerFormatType = string; export type LabelStatus = 'ACTIVE' | 'ARCHIVED'; export type LabelUpdateItem = {id: PinterestLibIntegerFormatType, status?: LabelStatus | undefined, value?: string | undefined}; export type LabelUpdateRequest = {labels: Array}; export type PatchApiAdAccountsAdAccountIdLabelsArgs = {ad_account_id: string, body: LabelUpdateRequest}; export type PatchApiAdAccountsAdAccountIdLabelsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const pinterestLibIntegerFormatType = z.string(); export const nullableLabelType = z.enum(["BRAND", "CUSTOM", "null"]).nullable(); export const nullableLabelStatus = z.enum(["ACTIVE", "ARCHIVED", "null"]).nullable(); export const labelErrorData = z.object({id: pinterestLibIntegerFormatType.optional(), label_type: nullableLabelType.optional(), status: nullableLabelStatus.optional(), value: z.string().max(100).optional()}); export const labelError = z.object({data: labelErrorData.optional(), error_messages: z.array(z.string()).optional()}); export const label = z.object({id: pinterestLibIntegerFormatType, label_type: nullableLabelType, status: nullableLabelStatus.optional(), value: z.string().max(100)}); export const labelsResponse = z.object({errors: z.array(labelError).optional(), labels: z.array(label).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 }; /** * Update labels * * PATCH /ad_accounts/{ad_account_id}/labels */ export const patchApiAdAccountsAdAccountIdLabels = async (args: PatchApiAdAccountsAdAccountIdLabelsArgs, options?: PatchApiAdAccountsAdAccountIdLabelsOptions) => { 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))}/labels${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(labelsResponse, await res.json()) };