import {z} from 'zod' export type GetApiV1OrgOrgIdBudgetBudgetIdArgs = {orgId: string, budgetId: string, date?: string | undefined}; export type GetApiV1OrgOrgIdBudgetBudgetIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const shareAccess = z.object({access: z.enum(["NONE", "LIMITED_READ", "LIMITED_WRITE", "STANDARD_READ", "STANDARD_WRITE", "COMPENSATION_READ", "FULL_READ", "COMP_PLANNING_PARTICIPANT", "COMPENSATION_WRITE", "WRITE", "COMPENSATION_OWNER", "OWNER"]), userId: z.string().optional(), groupId: z.string().optional(), everyone: z.boolean().optional(), fields: z.string().optional()}); export const money = z.object({amount: z.number(), currency: z.string(), places: z.number().int().optional()}); export const attribution = z.object({principalUserId: z.string().optional(), agentUserIds: z.array(z.string()).optional(), eventId: z.string().optional(), aiChatId: z.string().optional(), aiToolUseId: z.string().optional(), channel: z.enum(["WEB", "MOBILE", "SLACK", "TEAMS", "MCP"]).optional()}); export const budget = z.object({id: z.string(), orgId: z.string(), slug: z.string(), label: z.string().optional(), ownerPersonIds: z.array(z.string()).optional(), groupIds: z.array(z.string()).optional(), color: z.string().optional(), startDate: z.string(), endDate: z.string(), sensitive: z.enum(["GLOBAL", "ORG", "SENSITIVE", "PERSONAL", "MANAGER", "HIGH", "PRIVATE"]), shareAccess: z.array(shareAccess), costExpr: z.string().optional(), costBasis: z.enum(["ANNUALIZED", "PERIOD_TOTAL"]).optional(), status: z.enum(["DRAFT", "ACTIVE", "ARCHIVED"]), usage: money.optional(), effectiveCostBasis: z.enum(["ANNUALIZED", "PERIOD_TOTAL"]).optional(), amount: money.optional(), baselinedAt: z.string().optional(), createDate: z.string(), deleteDate: z.string().optional(), createId: z.string().optional(), createBehalfId: z.string().optional(), createAttribution: attribution.optional(), createAt: z.string().optional(), updateId: z.string().optional(), updateBehalfId: z.string().optional(), updateAttribution: attribution.optional(), updateAt: z.string().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 a budget by id or slug * * GET /v1/org/{orgId}/budget/{budgetId} */ export const getApiV1OrgOrgIdBudgetBudgetId = async (args: GetApiV1OrgOrgIdBudgetBudgetIdArgs, options?: GetApiV1OrgOrgIdBudgetBudgetIdOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/org/${encodeURIComponent(String(args.orgId))}/budget/${encodeURIComponent(String(args.budgetId))}${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(budget, await res.json()) };