import {z} from 'zod' export type GetApiApiV3LeavesArgs = {paging: string, leaveAccountId?: Array | undefined, date: string, 'leavePeriod.ownerId'?: Array | undefined, 'leavePeriod.owner.departmentId'?: Array | undefined, 'leavePeriod.owner.legalEntityId'?: Array | undefined}; export type GetApiApiV3LeavesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const leaveAccount = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const leavePeriodLog = z.object({id: z.number().int().optional(), date: z.string().optional(), comment: z.string().optional(), status: z.union([z.literal(0), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]).optional()}); export const leavePeriod = z.object({id: z.number().optional(), ownerId: z.number().optional(), isConfirmed: z.boolean().optional(), confirmationDate: z.string().nullable().optional(), attachmentId: z.string().nullable().optional(), leaves: z.array(z.object({})).optional(), logs: z.array(leavePeriodLog).optional()}); export const leave = z.object({id: z.string(), date: z.string(), isAm: z.boolean(), leaveAccountId: z.number().int(), leaveAccount: leaveAccount.optional(), leavePeriodId: z.number().int(), leavePeriod: leavePeriod.optional(), value: z.string().nullable().optional(), creationDate: z.string().optional(), isActive: z.boolean().optional(), cancellationDate: z.string().nullable().optional(), cancellationUserId: z.number().int().nullable().optional(), comment: z.string().optional()}); export const getApiApiV3LeavesResponse = z.object({data: z.object({items: z.array(leave).optional()}).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 leaves * * GET /api/v3/leaves */ export const getApiApiV3Leaves = async (args: GetApiApiV3LeavesArgs, options?: GetApiApiV3LeavesOptions) => { const {baseUrl = "https://example.ilucca.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.paging != null) query.append("paging", String(args.paging)) if (args.leaveAccountId != null) query.append("leaveAccountId", args.leaveAccountId.join(',')) if (args.date != null) query.append("date", String(args.date)) if (args['leavePeriod.ownerId'] != null) query.append("leavePeriod.ownerId", args['leavePeriod.ownerId'].join(',')) if (args['leavePeriod.owner.departmentId'] != null) query.append("leavePeriod.owner.departmentId", args['leavePeriod.owner.departmentId'].join(',')) if (args['leavePeriod.owner.legalEntityId'] != null) query.append("leavePeriod.owner.legalEntityId", args['leavePeriod.owner.legalEntityId'].join(',')) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/leaves${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(getApiApiV3LeavesResponse, await res.json()) };