import {z} from 'zod' export type TaskDTO = {id?: string | undefined, name: string, type?: ('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') | undefined, dueDate: string, authorId: string, targetId: string, completerId?: string | undefined, completed?: boolean | undefined}; export type CreateApiApiV2TasksArgs = {body: TaskDTO}; export type CreateApiApiV2TasksOptions = {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 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 }; /** * Create a task * * POST /api/v2/tasks */ export const createApiApiV2Tasks = async (args: CreateApiApiV2TasksArgs, options?: CreateApiApiV2TasksOptions) => { const {baseUrl = "https://mms.api.ed.ts.egym.coffee", fetch: fetchFn = fetch, mms} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(taskDTO, await res.json()) };