import {z} from 'zod' export type GetApiV3BillApprovalsPoliciesArgs = {sessionId?: (string | null) | undefined, devKey?: (string | null) | undefined}; export type GetApiV3BillApprovalsPoliciesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ruleValueDto = z.object({value: z.string().optional(), displayName: z.string().optional()}); export const billApprovalPolicyRuleResponseDto = z.object({key: z.enum(["BILL_AMOUNT", "BILL_LINE_ITEM_AMOUNT", "ACCOUNTING_CLASS_ID", "CHART_OF_ACCOUNT_ID", "CUSTOMER_ID", "DEPARTMENT_ID", "EMPLOYEE_ID", "JOB_ID", "LOCATION_ID", "VENDOR_ID", "UNDEFINED"]).optional(), op: z.enum(["lt", "gte", "in", "nin", "UNDEFINED"]).optional(), values: z.array(ruleValueDto).optional()}); export const approverUserDto = z.object({userId: z.string().optional(), fullName: z.string().optional()}); export const groupUsers = z.object({id: z.string().optional(), fullName: z.string().optional()}); export const approverGroupDto = z.object({groupName: z.string().optional(), groupId: z.string().optional(), users: z.array(groupUsers).optional()}); export const billApprovalPolicyResponseDto = z.object({id: z.string().optional(), policyName: z.string().optional(), status: z.string().optional(), rules: z.array(billApprovalPolicyRuleResponseDto).optional(), approvers: z.object({users: z.array(approverUserDto).optional(), groups: z.array(approverGroupDto).optional()}).optional()}); export const getApiV3BillApprovalsPoliciesResponse = z.array(billApprovalPolicyResponseDto); 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 list of bill approval policies * * GET /v3/bill-approvals/policies */ export const getApiV3BillApprovalsPolicies = async (args: GetApiV3BillApprovalsPoliciesArgs, options?: GetApiV3BillApprovalsPoliciesOptions) => { const {baseUrl = "https://gateway.stage.bill.com/connect", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sessionId != null) headers.set("sessionId", String(args.sessionId)) if (args.devKey != null) headers.set("devKey", String(args.devKey)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/bill-approvals/policies${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(getApiV3BillApprovalsPoliciesResponse, await res.json()) };