import {z} from 'zod' export type V2RealizedTraining = {id: unknown, ownerId: number, training?: ({id?: number | undefined, title?: string | undefined, categoryId?: number | undefined, providerUrl?: string | undefined, level?: ('Beginner' | 'Middle' | 'Expert') | undefined}) | undefined, trainingDemandId?: number | undefined, sessionId?: number | undefined, sessionStartsOn: string, sessionEndsOn: string, hasAttended?: boolean | undefined, durationInHours: number}; export type CreateApiTalentTrainingApiV2RealizedTrainingsArgs = {body: V2RealizedTraining}; export type CreateApiTalentTrainingApiV2RealizedTrainingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2RealizedTraining = z.object({id: z.unknown(), ownerId: z.number().int().gte(1), training: z.object({id: z.number().int().optional(), title: z.string().optional(), categoryId: z.number().int().optional(), providerUrl: z.string().optional(), level: z.enum(["Beginner", "Middle", "Expert"]).optional()}).optional(), trainingDemandId: z.number().int().gte(1).optional(), sessionId: z.number().int().gte(1).optional(), sessionStartsOn: z.string(), sessionEndsOn: z.string(), hasAttended: z.boolean().optional(), durationInHours: z.number().multipleOf(0.01)}); 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 realized-training (v2) * * POST /talent-training/api/v2/realized-trainings */ export const createApiTalentTrainingApiV2RealizedTrainings = async (args: CreateApiTalentTrainingApiV2RealizedTrainingsArgs, options?: CreateApiTalentTrainingApiV2RealizedTrainingsOptions) => { const {baseUrl = "https://example.ilucca.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/talent-training/api/v2/realized-trainings${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(v2RealizedTraining, await res.json()) };