import {z} from 'zod' export type GetApiV1OrgOrgIdGoalGoalIdTargetGoalTargetIdArgs = {orgId: string, goalId: string, goalTargetId: string}; export type GetApiV1OrgOrgIdGoalGoalIdTargetGoalTargetIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const translations = z.object({values: z.record(z.string(), z.string())}); export const goalTargetValue = z.object({targetValueTypeId: z.string().optional(), value: z.union([z.string(), z.number(), z.boolean()])}); export const attribution = z.object({principalUserId: z.string().optional(), agentUserIds: z.array(z.string()).optional(), eventId: z.string().optional(), aiChatId: z.string().optional(), aiToolUseId: z.string().optional(), channel: z.enum(["WEB", "MOBILE", "SLACK", "TEAMS", "MCP"]).optional()}); export const goalTarget = z.object({id: z.string(), orgId: z.string(), goalId: z.string(), label: z.string().optional(), labelTr: translations.optional(), slug: z.string().optional(), startValue: z.union([z.string(), z.number(), z.boolean()]).optional(), targetValues: z.array(goalTargetValue).optional(), direction: z.enum(["MINIMIZE", "MAXIMIZE", "TARGET"]).optional(), type: z.enum(["MILESTONE", "MONEY", "PERCENT", "INTEGER", "DECIMAL"]), valueExpr: z.string().optional(), startDate: z.string(), endDate: z.string(), createId: z.string().optional(), createBehalfId: z.string().optional(), createAttribution: attribution.optional(), createAt: z.string().optional(), updateId: z.string().optional(), updateBehalfId: z.string().optional(), updateAttribution: attribution.optional(), updateAt: z.string().optional(), deleteId: z.string().optional(), deleteBehalfId: z.string().optional(), deleteAttribution: attribution.optional(), deleteAt: z.string().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 }; /** * Get a goal target by id * * GET /v1/org/{orgId}/goal/{goalId}/target/{goalTargetId} */ export const getApiV1OrgOrgIdGoalGoalIdTargetGoalTargetId = async (args: GetApiV1OrgOrgIdGoalGoalIdTargetGoalTargetIdArgs, options?: GetApiV1OrgOrgIdGoalGoalIdTargetGoalTargetIdOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/org/${encodeURIComponent(String(args.orgId))}/goal/${encodeURIComponent(String(args.goalId))}/target/${encodeURIComponent(String(args.goalTargetId))}${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(goalTarget, await res.json()) };