import {z} from 'zod' export type GetApiV1BusinessesClientIdUsersArgs = {offset?: number | undefined, limit?: number | undefined, client_id: number}; export type GetApiV1BusinessesClientIdUsersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentType = z.enum(["bills", "cards", "external_expenses", "reimbursements"]); export const user = z.object({id: z.number().int().optional(), first_name: z.string().optional(), last_name: z.string().optional(), name: z.string().optional(), email: z.string().optional(), role: z.string().optional(), role_id: z.string().optional(), is_deleted: z.boolean().optional(), created: z.string().optional(), updated: z.string().optional(), payment_types: z.array(paymentType).optional(), out_of_office_mode: z.boolean().optional(), out_of_office_start_date: z.string().nullable().optional(), out_of_office_end_date: z.string().nullable().optional(), delegate_approver: z.number().int().nullable().optional(), expense_delegate: z.array(z.number().int()).nullable().optional()}); export const userListResponse = z.object({count: z.number().int().optional(), next: z.string().optional(), previous: z.string().optional(), results: z.array(user).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 }; /** * List users * * GET /v1/businesses/{client_id}/users */ export const getApiV1BusinessesClientIdUsers = async (args: GetApiV1BusinessesClientIdUsersArgs, options?: GetApiV1BusinessesClientIdUsersOptions) => { const {baseUrl = "https://public.letsweel.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/businesses/${encodeURIComponent(String(args.client_id))}/users${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(userListResponse, await res.json()) };