import {z} from 'zod' export type Deal = {id?: string | undefined, integration?: string | undefined, srcId?: string | undefined, ownerId?: (string | null) | undefined, ownerName?: (string | null) | undefined, stage?: (string | null) | undefined, dealName?: (string | null) | undefined, description?: (string | null) | undefined, prospectId?: (string | null) | undefined, prospectName?: (string | null) | undefined, expectedRevenue?: (number | null) | undefined, currency?: (string | null) | undefined, created?: (string | null) | undefined, closed?: (string | null) | undefined, associatedContactIds?: (Array | null) | undefined, associatedCompanyIds?: (Array | null) | undefined, numAssociatedContacts?: (number | null) | undefined}; export type CreateApiDealsArgs = {body: Deal}; export type CreateApiDealsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const deal = z.object({id: z.string().optional(), integration: z.string().optional(), srcId: z.string().optional(), ownerId: z.string().nullable().optional(), ownerName: z.string().nullable().optional(), stage: z.string().nullable().optional(), dealName: z.string().nullable().optional(), description: z.string().nullable().optional(), prospectId: z.string().nullable().optional(), prospectName: z.string().nullable().optional(), expectedRevenue: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), created: z.string().nullable().optional(), closed: z.string().nullable().optional(), associatedContactIds: z.array(z.string()).nullable().optional(), associatedCompanyIds: z.array(z.string()).nullable().optional(), numAssociatedContacts: z.number().int().nullable().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 }; /** * Creates a Deal resource. * * POST /deals */ export const createApiDeals = async (args: CreateApiDealsArgs, options?: CreateApiDealsOptions) => { const {baseUrl = "https://example.myintranetapps.com/api", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/deals${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(deal, await res.json()) };