import {z} from 'zod' export type GetApiOpportunitiesIdArgs = {id: string}; export type GetApiOpportunitiesIdOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const opportunityNameTypeDto = z.object({viewType: z.literal("opportunityName"), name: z.string()}); export const opportunityDescriptionTypeDto = z.object({viewType: z.literal("opportunityDescription"), description: z.string().nullable()}); export const opportunityStageValueDto = z.object({id: z.string().min(1), name: z.string(), order: z.number(), isOpen: z.boolean()}); export const opportunityStageTypeDto = z.object({viewType: z.literal("opportunityStage"), stage: opportunityStageValueDto}); export const opportunityCompanyValueDto = z.object({id: z.string().min(1), name: z.string()}); export const opportunityCompanyTypeDto = z.object({viewType: z.literal("opportunityCompany"), company: opportunityCompanyValueDto}); export const dateISO = z.string(); export const userBadgeDto = z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}); export const opportunityOwnerTypeDto = z.object({viewType: z.literal("opportunityOwner"), owner: userBadgeDto.nullable()}); export const opportunityCloseDateValueDto = z.object({target: dateISO.nullable()}); export const opportunityCloseDateTypeDto = z.object({viewType: z.literal("opportunityCloseDate"), closeDate: opportunityCloseDateValueDto}); export const opportunityCreatedAtTypeDto = z.object({viewType: z.literal("opportunityCreatedAt"), createdAt: dateISO}); export const opportunityClientTeamTypeDto = z.object({viewType: z.literal("opportunityClientTeam"), clientTeam: z.array(z.object({id: z.string().min(1), firstName: z.string(), lastName: z.string(), middleName: z.string().nullable(), secondLastName: z.string().nullable(), avatarUrl: z.string().nullable(), candidateId: z.string().min(1), companyId: z.string().min(1), companyName: z.string(), companyLogoUrl: z.string().nullable(), jobTitle: z.string().nullable(), department: z.string().nullable(), emails: z.array(z.object({email: z.string(), purpose: z.enum(["personal", "work"]).nullable(), isPrimary: z.boolean()})), phoneNumbers: z.array(z.object({phoneNumber: z.string(), purpose: z.enum(["personal", "work"]).nullable(), isPrimary: z.boolean()}))}))}); export const opportunityNextTaskDueTypeDto = z.object({viewType: z.literal("opportunityNextTaskDue"), nextTaskDue: z.object({id: z.string().min(1), content: z.string(), dueDate: dateISO.nullable()}).nullable()}); export const dmCurrency = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNH", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLE", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XCG", "XDR", "XOF", "XPD", "XPF", "XPT", "YER", "ZAR", "ZMW", "ZWG", "ZWL"]); export const monetaryAmountDto = z.object({currency: dmCurrency, amount: z.number()}); export const dateRangeDto = z.object({startDate: dateISO, endDate: dateISO.nullable()}); export const sharedCustomAttributeTypeDto = z.union([z.object({viewType: z.literal("customText"), text: z.string().nullable()}), z.object({viewType: z.literal("customNumber"), number: z.number().nullable()}), z.object({viewType: z.literal("customCheckbox"), checked: z.boolean().nullable()}), z.object({viewType: z.literal("customDate"), date: dateISO.nullable()}), z.object({viewType: z.literal("customTimestamp"), timestamp: dateISO.nullable()}), z.object({viewType: z.literal("customRating"), rating: z.number().nullable()}), z.object({viewType: z.literal("customSelect"), select: z.string().nullable()}), z.object({viewType: z.literal("customMultiSelect"), multiSelect: z.array(z.string())}), z.object({viewType: z.literal("customCurrency"), monetaryAmount: monetaryAmountDto.nullable()}), z.object({viewType: z.literal("customDateRange"), dateRange: dateRangeDto.nullable()}), z.object({viewType: z.literal("customDuration"), duration: z.number().nullable()}), z.object({viewType: z.literal("customUrl"), url: z.string().nullable()}), z.object({viewType: z.literal("customEntity"), type: z.literal("user"), id: z.string().nullable(), entity: userBadgeDto.nullable().optional()}), z.object({viewType: z.literal("customMultiEntity"), type: z.literal("user"), ids: z.array(z.string()), entities: z.array(userBadgeDto).optional()})]); export const customAttributeItemDto = z.object({attributeDefinitionId: z.string().min(1), label: z.string(), aiFillOngoing: z.boolean(), aiOverridden: z.boolean(), isFilledByAI: z.boolean(), value: sharedCustomAttributeTypeDto}); export const opportunityDto = z.object({id: z.string().min(1), name: opportunityNameTypeDto, description: opportunityDescriptionTypeDto, stage: opportunityStageTypeDto, company: opportunityCompanyTypeDto, owner: opportunityOwnerTypeDto, closeDate: opportunityCloseDateTypeDto, createdAt: opportunityCreatedAtTypeDto, clientTeam: opportunityClientTeamTypeDto, nextTaskDue: opportunityNextTaskDueTypeDto, customAttributes: z.array(customAttributeItemDto)}); 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 }; /** * Retrieve an opportunity * * GET /opportunities/{id} */ export const getApiOpportunitiesId = async (args: GetApiOpportunitiesIdArgs, options: GetApiOpportunitiesIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/opportunities/${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(opportunityDto, await res.json()) };