import {z} from 'zod' export type GoalCreate = {name: string, target_amount: number | string, current_amount?: (number | string) | undefined, currency?: string | undefined, target_date?: (string | null) | undefined, tracking_type?: string | undefined, account_id?: (string | null) | undefined, asset_id?: (string | null) | undefined, asset_group_id?: (string | null) | undefined, icon?: (string | null) | undefined, color?: (string | null) | undefined, metadata_json: unknown}; export type CreateApiApiGoalsArgs = {'X-Workspace-Id'?: (string | null) | undefined, body: GoalCreate}; export type CreateApiApiGoalsOptions = {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 }; /** * Create Goal * * POST /api/goals */ export const createApiApiGoals = async (args: CreateApiApiGoalsArgs, options: CreateApiApiGoalsOptions) => { 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${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(goalRead, await res.json()) };