import {z} from 'zod' export type IndividualHabitInCreate = {name: string, description?: string | undefined, type?: string | undefined, unit: string, price?: number | undefined, startValue?: number | undefined, goalDefaultValue: number, goalValueIncrements: number, repetitionDays: Array, startDate: string, endDate?: string | undefined, reminderTime?: string | undefined, goalValue: number, icon: string}; export type BodyUserHabitsCreateIndividualApiV5HabitsUserHabitsIndividualPost = {user_habit: IndividualHabitInCreate}; export type CreateApiApiV5HabitsUserHabitsIndividualArgs = {body: BodyUserHabitsCreateIndividualApiV5HabitsUserHabitsIndividualPost}; export type CreateApiApiV5HabitsUserHabitsIndividualOptions = {baseUrl?: string | undefined, RWAPIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const habit = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), name: z.string(), description: z.string().optional(), type: z.string(), unit: z.string(), goalDefaultValue: z.number().int().optional(), goalValueIncrements: z.number().int().optional(), interfaceKey: z.string().optional(), hasDailyRecords: z.boolean(), hidden: z.boolean(), individual: z.boolean().optional(), iconId: z.number().int().optional(), iconGoalId: z.number().int().optional(), iconUrl: z.string().optional(), iconGoalUrl: z.string().optional(), contentsCategoryId: z.number().int().optional()}); export const userHabit = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), userId: z.number().int(), habitId: z.number().int(), price: z.number().optional(), reminderTime: z.string().optional()}); export const habitGoal = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), userHabitId: z.number().int(), date: z.string(), goalValue: z.number().int().optional(), currentValue: z.number().int().optional(), achievedAt: z.string().optional()}); export const userHabitPeriod = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), userHabitId: z.number().int(), startDate: z.string(), endDate: z.string().optional(), repetitionDays: z.array(z.number().int()), startValue: z.number().int().optional()}); export const habitWithUserHabitInResponse = z.object({habit: habit, userHabit: userHabit, habitGoal: habitGoal, period: userHabitPeriod}); 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 Individual User Habit * * POST /api/v5/habits/user-habits/individual */ export const createApiApiV5HabitsUserHabitsIndividual = async (args: CreateApiApiV5HabitsUserHabitsIndividualArgs, options?: CreateApiApiV5HabitsUserHabitsIndividualOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch, RWAPIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (RWAPIKeyHeader !== undefined) headers.set("Authorization", RWAPIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/habits/user-habits/individual${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(habitWithUserHabitInResponse, await res.json()) };