import {z} from 'zod' export type GetApiApiV3TimeentriesArgs = {paging: string, ownerId?: Array | undefined, startsAt?: string | undefined, 'axisSections.id'?: string | undefined, 'axisSections.code'?: Array | undefined, modifiedAt?: string | undefined, archivedAt?: string | undefined, orderBy?: string | undefined}; export type GetApiApiV3TimeentriesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timeEntry = z.object({}); export const getApiApiV3TimeentriesResponse = z.object({data: z.object({count: z.number().int().optional(), items: z.array(timeEntry).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 multiple time-entries * * GET /api/v3/timeentries */ export const getApiApiV3Timeentries = async (args: GetApiApiV3TimeentriesArgs, options?: GetApiApiV3TimeentriesOptions) => { 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.ownerId != null) query.append("ownerId", args.ownerId.join(',')) if (args.startsAt != null) query.append("startsAt", String(args.startsAt)) if (args['axisSections.id'] != null) query.append("axisSections.id", String(args['axisSections.id'])) for (const item of args['axisSections.code'] ?? []) query.append("axisSections.code", String(item)) if (args.modifiedAt != null) query.append("modifiedAt", String(args.modifiedAt)) if (args.archivedAt != null) query.append("archivedAt", String(args.archivedAt)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/timeentries${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(getApiApiV3TimeentriesResponse, await res.json()) };