import {z} from 'zod' export type GoalUpdate = {name?: (string | null) | undefined, target_amount?: (number | string | null) | undefined, current_amount?: (number | string | null) | undefined, currency?: (string | null) | undefined, target_date?: (string | null) | undefined, tracking_type?: (string | null) | undefined, account_id?: (string | null) | undefined, asset_id?: (string | null) | undefined, asset_group_id?: (string | null) | undefined, status?: (string | null) | undefined, icon?: (string | null) | undefined, color?: (string | null) | undefined, position?: (number | null) | undefined, metadata_json: unknown}; export type PatchApiApiGoalsGoalIdArgs = {goal_id: string, 'X-Workspace-Id'?: (string | null) | undefined, body: GoalUpdate}; export type PatchApiApiGoalsGoalIdOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const goalRead = z.object({id: z.string(), user_id: z.string(), name: z.string(), target_amount: z.string(), current_amount: z.string(), currency: z.string(), target_amount_primary: z.string().nullable().optional(), current_amount_primary: z.string().nullable().optional(), target_date: z.string().nullable().optional(), tracking_type: z.string(), account_id: z.string().nullable().optional(), asset_id: z.string().nullable().optional(), asset_group_id: z.string().nullable().optional(), status: z.string(), icon: z.string().nullable().optional(), color: z.string().nullable().optional(), position: z.number().int(), metadata_json: z.unknown(), created_at: z.string(), updated_at: z.string(), percentage: z.number().optional(), monthly_contribution: z.number().nullable().optional(), on_track: z.string().nullable().optional(), account_name: z.string().nullable().optional(), asset_name: z.string().nullable().optional(), asset_group_name: z.string().nullable().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 }; /** * Update Goal * * PATCH /api/goals/{goal_id} */ export const patchApiApiGoalsGoalId = async (args: PatchApiApiGoalsGoalIdArgs, options: PatchApiApiGoalsGoalIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Workspace-Id'] != null) headers.set("X-Workspace-Id", String(args['X-Workspace-Id'])) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/goals/${encodeURIComponent(String(args.goal_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(goalRead, await res.json()) };