import {z} from 'zod' export type GetApiCoaApiCostAllocationsIdArgs = {id: number}; export type GetApiCoaApiCostAllocationsIdOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const costAllocationLine = z.object({id: z.number().int(), allocation: z.number().int().optional(), account_name: z.string().nullable(), department_name: z.string(), tags: z.array(z.record(z.string(), z.unknown())), percentage: z.number().gt(-1000).lt(1000).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(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), account: z.number().int(), department: z.number().int().nullable().optional()}); export const costAllocation = z.object({id: z.number().int(), lines: z.array(costAllocationLine), line_count: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), name: z.string().max(250), number: z.string().max(120).nullable().optional(), created_at: z.string(), last_modified_at: z.string(), customer: z.number().int(), account: 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 }; /** * Get Cost Allocation * * GET /coa/api/cost-allocations/{id} */ export const getApiCoaApiCostAllocationsId = async (args: GetApiCoaApiCostAllocationsIdArgs, options?: GetApiCoaApiCostAllocationsIdOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/cost-allocations/${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(costAllocation, await res.json()) };