import {z} from 'zod' export type GetApiCoaApiCostAllocationsArgs = {include_deleted?: boolean | undefined, last_modified_at__gte?: string | undefined, last_modified_at__lte?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiCoaApiCostAllocationsOptions = {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 paginatedCostAllocationList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(costAllocation)}); 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 }; /** * List Cost Allocation * * GET /coa/api/cost-allocations */ export const getApiCoaApiCostAllocations = async (args: GetApiCoaApiCostAllocationsArgs, options?: GetApiCoaApiCostAllocationsOptions) => { 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 (args.include_deleted != null) query.append("include_deleted", String(args.include_deleted)) if (args.last_modified_at__gte != null) query.append("last_modified_at__gte", String(args.last_modified_at__gte)) if (args.last_modified_at__lte != null) query.append("last_modified_at__lte", String(args.last_modified_at__lte)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/cost-allocations${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(paginatedCostAllocationList, await res.json()) };