import {z} from 'zod' export type GetApiApiGoalsArgs = {status?: (string | null) | undefined, 'X-Workspace-Id'?: (string | null) | undefined}; export type GetApiApiGoalsOptions = {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 getApiApiGoalsResponse = z.array(goalRead); 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 }; /** * List Goals * * GET /api/goals */ export const getApiApiGoals = async (args: GetApiApiGoalsArgs, options: GetApiApiGoalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiGoalsResponse, await res.json()) };