import {z} from 'zod' export type CancelMilestoneRequest = {effective_date: string}; export type UpdateApiContractsContractIdMilestonesMilestoneRefCancellationArgs = {contract_id: string, milestone_ref: string, body: CancelMilestoneRequest}; export type UpdateApiContractsContractIdMilestonesMilestoneRefCancellationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const contractMilestoneStatus = z.enum(["OPEN", "DUE_THIS_PERIOD", "OVERDUE", "COMPLETED", "CANCELLED"]); export const externalReference = z.object({type: z.string(), id: z.string(), url: z.string().optional()}); export const contractMilestoneCancellation = z.object({effective_date: z.string(), credit_memo_id: z.string().nullable().optional()}); export const contractMilestone = z.object({id: z.string(), key: z.string(), name: z.string(), sequence: z.number().int(), percentage: z.string().nullable().optional(), allocated_amount: roundedMonetaryAmount.nullable().optional(), amount: roundedMonetaryAmount.nullable().optional(), expected_date: z.string().nullable().optional(), status: contractMilestoneStatus, completed_date: z.string().nullable().optional(), memo: z.string().nullable().optional(), external_references: z.array(externalReference), cancellation: contractMilestoneCancellation.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 }; /** * Permanently cancels a pending milestone * * PUT /contracts/{contract_id}/milestones/{milestone_ref}/cancellation */ export const updateApiContractsContractIdMilestonesMilestoneRefCancellation = async (args: UpdateApiContractsContractIdMilestonesMilestoneRefCancellationArgs, options?: UpdateApiContractsContractIdMilestonesMilestoneRefCancellationOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/contracts/${encodeURIComponent(String(args.contract_id))}/milestones/${encodeURIComponent(String(args.milestone_ref))}/cancellation${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(contractMilestone, await res.json()) };