import {z} from 'zod' export type BudgetMemberUpsertRequestDto = {limit?: number | undefined, recurringLimit?: number | undefined, role?: ('MEMBER' | 'OWNER' | 'OBSERVER') | undefined, shareBudgetFunds?: boolean | undefined}; export type UpdateApiV3SpendBudgetsBudgetIdMembersUserIdArgs = {apiToken?: (string | null) | undefined, budgetId: string, userId: string, body: BudgetMemberUpsertRequestDto}; export type UpdateApiV3SpendBudgetsBudgetIdMembersUserIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const budgetMemberResponseDto = z.object({userId: z.string().optional(), userUuid: z.string().optional(), spent: z.number().optional(), limit: z.number().optional(), recurringLimit: z.number().optional(), recurring: z.boolean().optional(), budgetRole: z.enum(["MEMBER", "OWNER", "OBSERVER"]).optional(), name: z.string().optional(), retired: z.boolean().optional(), shareBudgetFunds: z.boolean().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 }; /** * Add or update a budget member * * PUT /v3/spend/budgets/{budgetId}/members/{userId} */ export const updateApiV3SpendBudgetsBudgetIdMembersUserId = async (args: UpdateApiV3SpendBudgetsBudgetIdMembersUserIdArgs, options?: UpdateApiV3SpendBudgetsBudgetIdMembersUserIdOptions) => { const {baseUrl = "https://gateway.stage.bill.com/connect", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.apiToken != null) headers.set("apiToken", String(args.apiToken)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/spend/budgets/${encodeURIComponent(String(args.budgetId))}/members/${encodeURIComponent(String(args.userId))}${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(budgetMemberResponseDto, await res.json()) };