import {z} from 'zod' export type V1TrainingLevel = 'Beginner' | 'Middle' | 'Expert'; export type T9n = {De?: (string | null) | undefined, En?: (string | null) | undefined, Es?: (string | null) | undefined, Fr?: (string | null) | undefined, It?: (string | null) | undefined, Nl?: (string | null) | undefined, Pt?: (string | null) | undefined} | null; export type V1Training = {id?: number | undefined, type?: ('External' | 'Internal') | undefined, state?: ('Ready' | 'Archived') | undefined, title: string, subtitle: string, description: string, provider?: ({id?: number | undefined, name?: string | undefined, websiteHref?: string | undefined}) | undefined, level: V1TrainingLevel, isCertifying: boolean, isMandatory: boolean, category?: ({id?: number | undefined, name?: string | undefined, t9n?: ({name?: T9n | undefined}) | undefined}) | undefined}; export type CreateApiTalentTrainingApiV1TrainingsArgs = {body: V1Training}; export type CreateApiTalentTrainingApiV1TrainingsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v1TrainingLevel = z.enum(["Beginner", "Middle", "Expert"]); export const t9n = z.object({De: z.string().nullable().optional(), En: z.string().nullable().optional(), Es: z.string().nullable().optional(), Fr: z.string().nullable().optional(), It: z.string().nullable().optional(), Nl: z.string().nullable().optional(), Pt: z.string().nullable().optional()}).nullable(); export const v1Training = z.object({id: z.number().int().optional(), type: z.enum(["External", "Internal"]).optional(), state: z.enum(["Ready", "Archived"]).optional(), title: z.string(), subtitle: z.string(), description: z.string(), provider: z.object({id: z.number().int().optional(), name: z.string().optional(), websiteHref: z.string().optional()}).optional(), level: v1TrainingLevel, isCertifying: z.boolean(), isMandatory: z.boolean(), category: z.object({id: z.number().int().optional(), name: z.string().optional(), t9n: z.object({name: t9n.optional()}).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 }; /** * Create a training * * POST /talent-training/api/v1/trainings */ export const createApiTalentTrainingApiV1Trainings = async (args: CreateApiTalentTrainingApiV1TrainingsArgs, options?: CreateApiTalentTrainingApiV1TrainingsOptions) => { 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/v1/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(v1Training, await res.json()) };