import {z} from 'zod' export type UpdateBadgeRewardDto = {name?: string | undefined, image?: string | undefined, provider?: 'Vault' | undefined, type?: ('native' | 'erc20' | 'erc721' | 'erc1155' | 'points') | undefined, status?: ('open' | 'pending' | 'sent') | undefined, vaultId?: string | undefined, contract?: string | undefined, chainId?: string | undefined, amount?: Array | undefined, description?: string | undefined, endDate?: string | undefined, maxSpend?: number | undefined, transactionHash?: string | undefined}; export type PatchApiCommunityIdBadgeBadgeIdRewardRewardIdArgs = {id: string, badgeId: string, rewardId: string, body: UpdateBadgeRewardDto}; export type PatchApiCommunityIdBadgeBadgeIdRewardRewardIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const badgeRewardDto = z.object({id: z.string(), image: z.string(), provider: z.literal("Vault"), providerImage: z.string(), name: z.string(), type: z.enum(["native", "erc20", "erc721", "erc1155", "points"]), status: z.enum(["open", "pending", "sent"]), vaultId: z.string().optional(), contract: z.string().optional(), chainId: z.string().optional(), amount: z.array(z.number()), description: z.string(), endDate: z.string(), maxSpend: z.number(), transactionHash: z.string().optional(), deletedAt: z.string().optional(), sentAt: 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 }; /** * Update badge reward * * PATCH /community/{id}/badge/{badgeId}/reward/{rewardId} */ export const patchApiCommunityIdBadgeBadgeIdRewardRewardId = async (args: PatchApiCommunityIdBadgeBadgeIdRewardRewardIdArgs, options?: PatchApiCommunityIdBadgeBadgeIdRewardRewardIdOptions) => { const {baseUrl = "https://api.openpage.fun/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-Api-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/community/${encodeURIComponent(String(args.id))}/badge/${encodeURIComponent(String(args.badgeId))}/reward/${encodeURIComponent(String(args.rewardId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(badgeRewardDto, await res.json()) };