import {z} from 'zod' export type GetApiPlacementsIdArgs = {id: string}; export type GetApiPlacementsIdOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dateISO = z.string(); export const placementUserBadgeDto = z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}); export const dmSupportedCurrency = z.enum(["EUR", "GBP", "USD", "ZAR", "AUD", "CHF", "SAR", "AED", "EGP", "HUF", "RON", "BGN", "CZK", "PLN", "NOK", "SEK", "DKK", "CAD", "HKD", "JPY", "THB", "CNY", "INR", "SGD", "MYR", "QAR", "PHP", "LKR", "KWD", "PKR", "MOP", "IDR", "VND", "KRW", "MXN", "BRL", "ARS", "NZD", "TWD", "ILS", "TRY", "RUB", "MDL", "MUR", "OMR"]); export const placementMonetaryAmountDto = z.object({amount: z.number(), currency: dmSupportedCurrency}); export const placementFeeSplitDto = z.object({user: placementUserBadgeDto, percentage: z.number().gte(0).lte(1)}); export const placementFeeDto = z.object({id: z.string(), net: placementMonetaryAmountDto.nullable(), splits: z.array(placementFeeSplitDto), details: z.record(z.string(), z.unknown()).nullable().optional()}); export const placementExtensionOfDto = z.object({id: z.string()}); 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 customAttributeTypeDto = 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: z.object({amount: z.number(), currency: dmCurrency}).nullable()}), z.object({viewType: z.literal("customDateRange"), dateRange: z.object({startDate: dateISO, endDate: dateISO.nullable()}).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: z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}).nullable().optional()}), z.object({viewType: z.literal("customMultiEntity"), type: z.literal("user"), ids: z.array(z.string()), entities: z.array(z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()})).optional()})]); export const placementAttributeValueDto = z.object({id: z.string(), attributeDefinitionId: z.string(), value: customAttributeTypeDto}); export const placementDto = z.object({id: z.string(), templateId: z.string(), type: z.enum(["CONTINGENT", "INTERIM", "RETAINED"]), placedByUser: placementUserBadgeDto, placedAt: dateISO, startDate: dateISO.nullable(), endDate: dateISO.nullable(), fee: placementFeeDto.nullable(), extensionOf: placementExtensionOfDto.nullable(), applicationId: z.string(), attributeValues: z.array(placementAttributeValueDto)}); 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 a placement * * GET /placements/{id} */ export const getApiPlacementsId = async (args: GetApiPlacementsIdArgs, options: GetApiPlacementsIdOptions) => { 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(/\/+$/, '')}/placements/${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(placementDto, await res.json()) };