import {z} from 'zod' export type HabitInUpdate = {id: number, name?: string | undefined, description?: string | undefined, type?: string | undefined, unit?: string | undefined, goalDefaultValue?: number | undefined, goalValueIncrements?: number | undefined, interfaceKey?: string | undefined, hasDailyRecords?: boolean | undefined, hidden?: boolean | undefined, icon?: string | undefined, iconGoal?: string | undefined, contentsCategoryId?: number | undefined}; export type BodyHabitsAdminUpdateApiV5AdminHabitsPut = {habit: HabitInUpdate}; export type UpdateApiApiV5AdminHabitsArgs = {body: BodyHabitsAdminUpdateApiV5AdminHabitsPut}; export type UpdateApiApiV5AdminHabitsOptions = {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 habitInResponse = z.object({habit: habit}); 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 }; /** * Update Habit * * PUT /api/v5/admin/habits/ */ export const updateApiApiV5AdminHabits = async (args: UpdateApiApiV5AdminHabitsArgs, options?: UpdateApiApiV5AdminHabitsOptions) => { 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/admin/habits/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(habitInResponse, await res.json()) };