import {z} from 'zod' export type GetApiApiV3CompletionsDashboardArgs = {user_id: number, page_size?: number | undefined, page_number?: number | undefined}; export type GetApiApiV3CompletionsDashboardOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardCompletionResponse = z.object({code: z.string().min(4).max(6), user_id: z.number().int(), name: z.string(), also_known_as: z.string().nullable().optional(), time: z.number(), screenshot: z.string(), video: z.string().nullable().optional(), completion: z.boolean(), rank: z.number().int().nullable().optional(), medal: z.enum(["Gold", "Silver", "Bronze", "null"]).nullable().optional(), map_name: z.string(), difficulty: z.enum(["Easy -", "Easy", "Easy +", "Medium -", "Medium", "Medium +", "Hard -", "Hard", "Hard +", "Very Hard -", "Very Hard", "Very Hard +", "Extreme -", "Extreme", "Extreme +", "Hell"]), message_id: z.number().int(), reason: z.string().nullable().optional(), status: z.enum(["Pending", "Verified", "Rejected"]), total_results: z.number().int().nullable().optional(), upvotes: z.number().int().optional(), id: z.number().int().nullable().optional()}); export const getApiApiV3CompletionsDashboardResponse = z.array(dashboardCompletionResponse); 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 User Dashboard Completions * * GET /api/v3/completions/dashboard */ export const getApiApiV3CompletionsDashboard = async (args: GetApiApiV3CompletionsDashboardArgs, options?: GetApiApiV3CompletionsDashboardOptions) => { const {baseUrl = "https://dev-api.genji.pk", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_number != null) query.append("page_number", String(args.page_number)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/completions/dashboard${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(getApiApiV3CompletionsDashboardResponse, await res.json()) };