import {z} from 'zod' export type GetApiOffersArgs = {includeExpired?: boolean | undefined, nftId?: string | undefined, collection?: string | undefined, creator?: string | undefined}; export type GetApiOffersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const offerStruct = z.object({minimumPrincipalPerPeriod: z.string().optional(), periodDuration: z.number().int().optional(), creator: z.string().optional(), nftContractAddress: z.string().optional(), price: z.string().optional(), periodInterestRateBps: z.number().int().optional(), nftId: z.string().optional(), expiration: z.number().int().optional(), downPaymentAmount: z.string().optional(), collectionOfferLimit: z.string().optional()}); export const offer = z.object({offer: offerStruct.optional(), signature: z.string().optional(), createdAt: z.number().int().optional(), status: z.enum(["ACTIVE", "EXPIRED", "CANCELLED", "USED_TO_EXECUTE_LOAN", "COLLECTION_OFFER_LIMIT_REACHED"]).optional(), isCollectionOffer: z.boolean().optional(), howManyTimesUsed: z.number().int().optional()}); export const getApiOffersResponse = z.array(offer); 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 }; /** * Retrieve offers * * GET /offers */ export const getApiOffers = async (args: GetApiOffersArgs, options?: GetApiOffersOptions) => { const {baseUrl = "https://api.niftyapes.money/MAINNET/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeExpired != null) query.append("includeExpired", String(args.includeExpired)) if (args.nftId != null) query.append("nftId", String(args.nftId)) if (args.collection != null) query.append("collection", String(args.collection)) if (args.creator != null) query.append("creator", String(args.creator)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/offers${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(getApiOffersResponse, await res.json()) };