import {z} from 'zod' export type BreakdownTypeEnum = 'standard' | 'department_row'; export type Budget = {id: number, entity_name: string, prior_start_date: string, prior_end_date: string, department_name: string, tags: Array<{[key: string]: unknown}>, is_deleted: boolean, deleted_at: string | null, name: string, description?: (string | null) | undefined, cadence?: (string | null) | undefined, start_date: string, end_date?: (string | null) | undefined, periods?: number | undefined, breakdown_type?: BreakdownTypeEnum | undefined, tag_ids?: Array | undefined, tag_group_ids?: Array | undefined, currency?: string | undefined, created_at: string, last_modified_at: string, customer: number, entity?: (number | null) | undefined, department?: (number | null) | undefined}; export type UpdateApiCoaApiBudgetsIdArgs = {id: number, body: Budget}; export type UpdateApiCoaApiBudgetsIdOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const breakdownTypeEnum = z.enum(["standard", "department_row"]); export const budget = z.object({id: z.number().int(), entity_name: z.string(), prior_start_date: z.string(), prior_end_date: z.string(), department_name: z.string(), tags: z.array(z.record(z.string(), z.unknown())), is_deleted: z.boolean(), deleted_at: z.string().nullable(), name: z.string(), description: z.string().nullable().optional(), cadence: z.string().nullable().optional(), start_date: z.string(), end_date: z.string().nullable().optional(), periods: z.number().int().gte(0).lte(32767).optional(), breakdown_type: breakdownTypeEnum.optional(), tag_ids: z.array(z.number().int().gte(-9223372036854776000).lte(9223372036854776000)).optional(), tag_group_ids: z.array(z.number().int().gte(-9223372036854776000).lte(9223372036854776000)).optional(), currency: z.string().max(3).optional(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), entity: z.number().int().nullable().optional(), department: 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 }; /** * Update Budget * * PUT /coa/api/budgets/{id} */ export const updateApiCoaApiBudgetsId = async (args: UpdateApiCoaApiBudgetsIdArgs, options?: UpdateApiCoaApiBudgetsIdOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/budgets/${encodeURIComponent(String(args.id))}${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(budget, await res.json()) };