import {z} from 'zod' export type ModelsGoalCadence = {achieved?: boolean | undefined, cadence_end?: string | undefined, cadence_start?: string | undefined, goal_cadence_id?: number | undefined, goal_parameter_id?: number | undefined, streak?: number | undefined, tracked_value?: number | undefined}; export type ModelsGoalProjectType = {project_id?: number | undefined, task_id?: number | undefined}; export type ModelsGoalType = {billable?: boolean | undefined, projects?: Array | undefined}; export type ModelsPlainGoal = {archived?: boolean | undefined, archived_at?: string | undefined, cadence_period?: string | undefined, comparing?: string | undefined, current_cadence?: ModelsGoalCadence | undefined, description?: string | undefined, end?: string | undefined, goal_id?: number | undefined, goal_parameter_id?: number | undefined, goal_type?: ModelsGoalType | undefined, icon?: number | undefined, icon_color?: string | undefined, name?: string | undefined, start?: string | undefined, target_value?: number | undefined, type_name?: string | undefined, user_id?: number | undefined, workspace_id?: number | undefined}; export type CreateApiGoalsArgs = {body: ModelsPlainGoal}; export type CreateApiGoalsOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; 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 goalsParamsCreate = z.object({cadence_period: z.string(), comparing: z.string(), description: z.string().optional(), end: z.string().optional(), goal_type: modelsGoalType, icon: z.number().int().optional(), icon_color: z.string().optional(), name: z.string(), start: z.string(), target_value: z.number().int(), workspace_id: z.number().int()}); 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 a Goal * * POST /goals */ export const createApiGoals = async (args: CreateApiGoalsArgs, options: CreateApiGoalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(goalsParamsCreate, await res.json()) };