import {z} from 'zod' export type BluenteInternalUserModelUserProfileBirthDate = {day?: string | undefined, month?: string | undefined, year?: string | undefined}; export type BluenteInternalUserModelUserProficiency = {listening?: string | undefined, reading?: string | undefined, speaking?: string | undefined, writing?: string | undefined}; export type BluenteInternalUserModelUserCoursePreferences = {finance?: Array | undefined, general?: Array | undefined, learning?: Array | undefined}; export type BluenteInternalUserModelUserProfileNotificationRecommendation = {days?: Array | undefined, recommendation: boolean}; export type BluenteInternalUserModelUserProfileRequest = {avatar: string, birthDate?: BluenteInternalUserModelUserProfileBirthDate | undefined, chineseProficiency?: BluenteInternalUserModelUserProficiency | undefined, coursePreferences?: BluenteInternalUserModelUserCoursePreferences | undefined, learningReason?: Array | undefined, marketing?: boolean | undefined, notification?: boolean | undefined, notificationRecommendation: BluenteInternalUserModelUserProfileNotificationRecommendation, reason?: string | undefined}; export type CreateApiV2UserProfileArgs = {body: BluenteInternalUserModelUserProfileRequest}; export type CreateApiV2UserProfileOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const bluenteInternalUserModelUserProficiency = z.object({listening: z.string().optional(), reading: z.string().optional(), speaking: z.string().optional(), writing: z.string().optional()}); export const bluenteInternalUserModelUserCoursePreferences = z.object({finance: z.array(z.string()).optional(), general: z.array(z.string()).optional(), learning: z.array(z.string()).optional()}); export const bluenteInternalUserModelUserProfileNotificationRecommendation = z.object({days: z.array(z.string()).optional(), recommendation: z.boolean()}); export const createApiV2UserProfileResponse = z.object({code: z.number().int().optional(), data: z.object({avatar: z.string(), birthDate: z.string().optional(), chineseProficiency: bluenteInternalUserModelUserProficiency.optional(), coursePreferences: bluenteInternalUserModelUserCoursePreferences.optional(), learningReason: z.array(z.string()).optional(), marketing: z.boolean().optional(), notification: z.boolean().optional(), notificationRecommendation: bluenteInternalUserModelUserProfileNotificationRecommendation.optional(), reason: z.string().optional(), uuid: z.string().optional()}).optional(), message: z.string().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 }; /** * Creation of user profile * * POST /v2/user/profile */ export const createApiV2UserProfile = async (args: CreateApiV2UserProfileArgs, options: CreateApiV2UserProfileOptions) => { const {baseUrl, 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(/\/+$/, '')}/v2/user/profile${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(createApiV2UserProfileResponse, await res.json()) };