import {z} from 'zod' export type ExperienceLeaderboardPrizeDto = {name: string, description: string, imageUrl: string}; export type ExperienceLeaderboardDto = {badgeId: string, metric: string, direction: 'ASC' | 'DESC', totalPlays?: number | undefined, prizes?: Array | undefined}; export type CreateExperienceDto = {name: string, type: 'web' | 'mml' | 'unity' | 'instant', description: string, imageUrl: string, bannerUrl: string, codeUrl: string, startDate: string, endDate: string, leaderboard?: ExperienceLeaderboardDto | undefined}; export type CreateApiCommunityIdExperienceArgs = {id: string, body: CreateExperienceDto}; export type CreateApiCommunityIdExperienceOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const experienceLeaderboardPrizeDto = z.object({name: z.string(), description: z.string(), imageUrl: z.string()}); export const experienceLeaderboardDto = z.object({badgeId: z.string(), metric: z.string(), direction: z.enum(["ASC", "DESC"]), totalPlays: z.number().optional(), prizes: z.array(experienceLeaderboardPrizeDto).optional()}); export const experienceDto = z.object({id: z.string(), name: z.string(), description: z.string(), public: z.boolean(), secret: z.string(), imageUrl: z.string(), bannerUrl: z.string(), experienceUrl: z.string(), serverId: z.string(), serverStatus: z.enum(["active", "inactive"]), codeUrl: z.string(), startDate: z.string(), endDate: z.string(), type: z.enum(["web", "mml", "unity", "instant"]), communityId: z.string(), createdAt: z.string(), updatedAt: z.string(), leaderboard: experienceLeaderboardDto.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 }; /** * Create experience * * POST /community/{id}/experience */ export const createApiCommunityIdExperience = async (args: CreateApiCommunityIdExperienceArgs, options?: CreateApiCommunityIdExperienceOptions) => { 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))}/experience${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(experienceDto, await res.json()) };