UNPKG

replicate-api

Version:

A typed client library for the replicate.com API

41 lines (40 loc) 1.45 kB
import { convertShallowPrediction, } from "./helpers/convertShallowPrediction.js"; import { makeApiRequest } from "./helpers/makeApiRequest.js"; const getEmptyResult = async () => ({ predictions: [], next: () => getEmptyResult(), }); /** List your past predictions. * * ```typescript * const result = await listPredictions({ * token: "Get your token at https://replicate.com/account" * }) * ``` * * Returns up to 100 predictions. To get more, use the `next` function: * ```typescript * const moreResults = await result.next() * ``` * * @returns A new `ShallowPredictionState`. */ export const listPredictions = async (options) => { const response = await makeApiRequest(options, "GET", `predictions${options.cursor ? "?cursor=" + options.cursor : ""}`); const predictions = response.results.map(prediction => convertShallowPrediction(options, prediction)); const nextCursor = response.next?.split("cursor=").pop(); const result = { predictions: predictions, next: () => predictions.length && nextCursor ? listPredictions({ ...options, cursor: nextCursor }) : getEmptyResult(), ...(nextCursor ? { nextCursor } : {}), }; if (!options.all) { return result; } const nextResult = await result.next(); const allResults = { predictions: [...result.predictions, ...nextResult.predictions], next: () => getEmptyResult(), }; return allResults; };