import {z} from 'zod' export type GetApiApiV2TasksArgs = {modifiedSince?: string | undefined}; export type GetApiApiV2TasksOptions = {baseUrl?: string | undefined, mms?: string | undefined, fetch?: typeof fetch | undefined}; export const taskDTO = z.object({id: z.string().optional(), name: z.string().min(1), type: z.enum(["GENERAL", "ANAMNESIS", "COORDINATION_FITNESS_TEST", "HEALTH_FITNESS_TEST", "PWC_FITNESS_TEST", "MAX_FORCE_FITNESS_TEST", "MOBILITY_SCREEN_FITNESS_TEST", "BLOOD_PRESSURE_FITNESS_TEST", "POLAR_FITNESS_TEST", "INBODY_FITNESS_TEST", "JAWON_FITNESS_TEST"]).optional(), dueDate: z.string(), authorId: z.string().min(1), targetId: z.string().min(1), completerId: z.string().optional(), completed: z.boolean().optional()}); export const getApiApiV2TasksResponse = z.array(taskDTO); 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 }; /** * Retrieve all tasks * * GET /api/v2/tasks */ export const getApiApiV2Tasks = async (args: GetApiApiV2TasksArgs, options?: GetApiApiV2TasksOptions) => { const {baseUrl = "https://mms.api.ed.ts.egym.coffee", fetch: fetchFn = fetch, mms} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.modifiedSince != null) query.append("modifiedSince", String(args.modifiedSince)) if (mms !== undefined) headers.set("x-api-key", mms) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/tasks${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(getApiApiV2TasksResponse, await res.json()) };