import {z} from 'zod' export type GetApiV0ProtoArgs = {page?: number | undefined, perPage?: number | undefined, sort?: string | undefined, order?: ('asc' | 'desc') | undefined, god?: ('light' | 'death' | 'nature' | 'war' | 'magic' | 'deception') | undefined, rarity?: ('common' | 'rare' | 'epic' | 'legendary' | 'mythic') | undefined, type?: ('creature' | 'spell' | 'weapon') | undefined, tribe?: ('nether' | 'aether' | 'atlantean' | 'viking' | 'olympian' | 'anubian' | 'amazon') | undefined, mana?: string | undefined, attack?: string | undefined, health?: string | undefined}; export type GetApiV0ProtoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const prototypeCard = z.object({id: z.number(), name: z.string().min(1), effect: z.string().min(1), god: z.string().min(1), rarity: z.string().min(1), tribe: z.object({String: z.string().min(1), Valid: z.boolean()}), mana: z.number(), attack: z.object({Int64: z.number(), Valid: z.boolean()}), health: z.object({Int64: z.number(), Valid: z.boolean()}), type: z.string().min(1), set: z.string().min(1), collectable: z.boolean(), live: z.string().min(1), art_id: z.string().min(1), lib_id: z.string().min(1)}); export const prototypeCardList = z.object({total: z.number(), page: z.number(), perPage: z.number(), records: z.array(prototypeCard)}); 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 list of protos * * GET /v0/proto */ export const getApiV0Proto = async (args: GetApiV0ProtoArgs, options?: GetApiV0ProtoOptions) => { const {baseUrl = "https://api.godsunchained.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.god != null) query.append("god", String(args.god)) if (args.rarity != null) query.append("rarity", String(args.rarity)) if (args.type != null) query.append("type", String(args.type)) if (args.tribe != null) query.append("tribe", String(args.tribe)) if (args.mana != null) query.append("mana", String(args.mana)) if (args.attack != null) query.append("attack", String(args.attack)) if (args.health != null) query.append("health", String(args.health)) const url = `${baseUrl.replace(/\/+$/, '')}/v0/proto${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(prototypeCardList, await res.json()) };