import {z} from 'zod' export type GetApiV1IncomesByownersArgs = {ownerIds?: Array | undefined}; export type GetApiV1IncomesByownersOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const adaptekApiCommonConstantsIncomeIncomeType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(8), z.literal(9), z.literal(10), z.literal(11), z.literal(12), z.literal(13), z.literal(14), z.literal(15), z.literal(16), z.literal(18), z.literal(19), z.literal(20), z.literal(21), z.literal(22), z.literal(23), z.literal(24), z.literal(26), z.literal(27), z.literal(28), z.literal(29), z.literal(30), z.literal(31), z.literal(32), z.literal(33), z.literal(34), z.literal(35), z.literal(36)]); export const adaptekApiApplicationIncomesIncomeOwnerDto = z.object({id: z.number().int().optional(), incomeID: z.number().int().optional(), personID: z.number().int().optional(), isDeleted: z.boolean().optional(), isAgeOwner: z.boolean().optional()}); export const adaptekApiApplicationIncomesIncomeDto = z.object({id: z.number().int().optional(), incomeTypeID: adaptekApiCommonConstantsIncomeIncomeType.optional(), description: z.string().nullable().optional(), startAge: z.number().int().optional(), endAge: z.number().int().optional(), monthlyIncome: z.number().optional(), personModified: z.number().int().nullable().optional(), userModified: z.number().int().nullable().optional(), dateModified: z.string().optional(), annualGrowthPercentage: z.number().optional(), beneficiaryPercentage: z.number().optional(), dateCreated: z.string().optional(), personCreated: z.number().int().nullable().optional(), userCreated: z.number().int().nullable().optional(), isDeleted: z.boolean().optional(), incomeOwners: z.array(adaptekApiApplicationIncomesIncomeOwnerDto).nullable().optional()}); export const getApiV1IncomesByownersResponse = z.array(adaptekApiApplicationIncomesIncomeDto); 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 }; /** * Returns a list of Incomes by Owners * * GET /v1/Incomes/byowners */ export const getApiV1IncomesByowners = async (args: GetApiV1IncomesByownersArgs, options: GetApiV1IncomesByownersOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.ownerIds ?? []) query.append("ownerIds", String(item)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Incomes/byowners${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(getApiV1IncomesByownersResponse, await res.json()) };