import {z} from 'zod' export type GetApiApiV2ProfileShelvesArgs = {address: string}; export type GetApiApiV2ProfileShelvesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const profileShelfItemIdentifierResponse = z.object({chain: z.string(), contract_address: z.string(), token_id: z.string()}); export const profileShelfItemMetadataResponse = z.object({description: z.string().nullable().optional()}); export const profileShelfResponse = z.object({account_address: z.string().nullable().optional(), description: z.string().nullable().optional(), display_order: z.number().int(), icon: z.string().nullable().optional(), id: z.string(), items: z.array(profileShelfItemIdentifierResponse), shelf_item_metadata: z.record(z.string(), profileShelfItemMetadataResponse), single_item_shelf_side: z.string().nullable().optional(), title: z.string(), view: z.string().nullable().optional()}); export const getApiApiV2ProfileShelvesResponse = z.array(profileShelfResponse); 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 profile shelves * * GET /api/v2/profile/shelves * * Also accepts auth: ApiKeyAuth + WalletAuth */ export const getApiApiV2ProfileShelves = async (args: GetApiApiV2ProfileShelvesArgs, options?: GetApiApiV2ProfileShelvesOptions) => { const {baseUrl = "https://api.opensea.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/profile/shelves${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(getApiApiV2ProfileShelvesResponse, await res.json()) };