import {z} from 'zod' export type GetApiV0UserAddressInventoryArgs = {page?: number | undefined, perPage?: number | undefined, sort?: string | undefined, order?: ('asc' | 'desc') | undefined, rarity?: ('common' | 'rare' | 'epic' | 'legendary' | 'mythic') | undefined, quality?: ('plain' | 'shadow' | 'gold' | 'diamond') | undefined, god?: ('light' | 'death' | 'nature' | 'war' | 'magic' | 'deception') | undefined, type?: ('creature' | 'spell' | 'weapon') | undefined, tribe?: ('nether' | 'aether' | 'atlantean' | 'viking' | 'olympian' | 'anubian' | 'amazon') | undefined, purity?: string | undefined, mana?: string | undefined, health?: string | undefined, attack?: string | undefined, proto?: number | undefined}; export type GetApiV0UserAddressInventoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userInventoryItem = z.object({name: z.string().min(1), mana: z.number(), health: z.object({Int64: z.number(), Valid: z.boolean()}), attack: z.object({Int64: z.number(), Valid: z.boolean()}), tribe: z.object({String: z.string().min(1), Valid: z.boolean()}), rarity: z.string().min(1), type: z.string().min(1), god: z.string().min(1), effect: z.string().min(1), user: z.string().min(1), id: z.object({Int64: z.number(), Valid: z.boolean()}), proto: z.number(), purity: z.number(), opened: z.boolean(), set: z.object({String: z.string().min(1), Valid: z.boolean()})}); export const userInventoryItemList = z.object({total: z.number(), page: z.number(), perPage: z.number(), records: z.array(userInventoryItem)}); 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 inventory * * GET /v0/user/{address}/inventory */ export const getApiV0UserAddressInventory = async (args: GetApiV0UserAddressInventoryArgs, options?: GetApiV0UserAddressInventoryOptions) => { 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.rarity != null) query.append("rarity", String(args.rarity)) if (args.quality != null) query.append("quality", String(args.quality)) if (args.god != null) query.append("god", String(args.god)) if (args.type != null) query.append("type", String(args.type)) if (args.tribe != null) query.append("tribe", String(args.tribe)) if (args.purity != null) query.append("purity", String(args.purity)) if (args.mana != null) query.append("mana", String(args.mana)) if (args.health != null) query.append("health", String(args.health)) if (args.attack != null) query.append("attack", String(args.attack)) if (args.proto != null) query.append("proto", String(args.proto)) const url = `${baseUrl.replace(/\/+$/, '')}/v0/user/${encodeURIComponent(String(args.address))}/inventory${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(userInventoryItemList, await res.json()) };