import {z} from 'zod' export type GetApiV1ApplicationsApplicationIdAchievementProgressIntegrationIdArgs = {applicationId: number, integrationId: string, pageSize?: number | undefined, skip?: number | undefined, achievementId?: number | undefined, title?: string | undefined}; export type GetApiV1ApplicationsApplicationIdAchievementProgressIntegrationIdOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignReference = z.object({id: z.number().int(), applicationId: z.number().int()}); export const achievementProgressWithDefinition = z.object({status: z.enum(["inprogress", "completed", "expired", "not_started"]), progress: z.number(), startDate: z.string().optional(), completionDate: z.string().optional(), endDate: z.string().optional(), achievementId: z.number().int(), name: z.string().min(1).max(1000), title: z.string(), description: z.string(), campaignId: z.number().int().optional(), campaignIds: z.array(z.number().int()), referencedByCampaigns: z.array(campaignReference), target: z.number().optional(), achievementRecurrencePolicy: z.enum(["no_recurrence", "on_expiration", "on_completion"]), achievementActivationPolicy: z.enum(["user_action", "fixed_schedule"]), achievementFixedStartDate: z.string().optional(), achievementEndDate: z.string().optional(), achievementAllowRollbackAfterCompletion: z.boolean().optional()}); export const getApiV1ApplicationsApplicationIdAchievementProgressIntegrationIdResponse = z.object({hasMore: z.boolean(), data: z.array(achievementProgressWithDefinition)}); 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 customer achievements * * GET /v1/applications/{applicationId}/achievement_progress/{integrationId} * * Also accepts auth: management_key */ export const getApiV1ApplicationsApplicationIdAchievementProgressIntegrationId = async (args: GetApiV1ApplicationsApplicationIdAchievementProgressIntegrationIdArgs, options?: GetApiV1ApplicationsApplicationIdAchievementProgressIntegrationIdOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.achievementId != null) query.append("achievementId", String(args.achievementId)) if (args.title != null) query.append("title", String(args.title)) if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/applications/${encodeURIComponent(String(args.applicationId))}/achievement_progress/${encodeURIComponent(String(args.integrationId))}${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(getApiV1ApplicationsApplicationIdAchievementProgressIntegrationIdResponse, await res.json()) };