import {z} from 'zod' export type GetApiGoalsArgs = {archived?: string | undefined, workspace_id?: number | undefined}; export type GetApiGoalsOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const modelsGoalCadence = z.object({achieved: z.boolean().optional(), cadence_end: z.string().optional(), cadence_start: z.string().optional(), goal_cadence_id: z.number().int().optional(), goal_parameter_id: z.number().int().optional(), streak: z.number().int().optional(), tracked_value: z.number().int().optional()}); export const modelsGoalProjectType = z.object({project_id: z.number().int().optional(), task_id: z.number().int().optional()}); export const modelsGoalType = z.object({billable: z.boolean().optional(), projects: z.array(modelsGoalProjectType).optional()}); export const modelsPlainGoal = z.object({archived: z.boolean().optional(), archived_at: z.string().optional(), cadence_period: z.string().optional(), comparing: z.string().optional(), current_cadence: modelsGoalCadence.optional(), description: z.string().optional(), end: z.string().optional(), goal_id: z.number().int().optional(), goal_parameter_id: z.number().int().optional(), goal_type: modelsGoalType.optional(), icon: z.number().int().optional(), icon_color: z.string().optional(), name: z.string().optional(), start: z.string().optional(), target_value: z.number().int().optional(), type_name: z.string().optional(), user_id: z.number().int().optional(), workspace_id: z.number().int().optional()}); export const getApiGoalsResponse = z.array(modelsPlainGoal); 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 }; /** * Get a list of goals * * GET /goals */ export const getApiGoals = async (args: GetApiGoalsArgs, options: GetApiGoalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.archived != null) query.append("archived", String(args.archived)) if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiGoalsResponse, await res.json()) };