UNPKG

replicate-api

Version:

A typed client library for the replicate.com API

43 lines (42 loc) 1.59 kB
import { extractModelAndOwner } from "./helpers/extractModelAndOwner.js"; import { makeApiRequest } from "./helpers/makeApiRequest.js"; const getEmptyResult = async () => ({ version: undefined, versions: [], next: () => getEmptyResult(), }); /** List all versions that are availabe for a model * ```typescript * const {versions, version} = await listVersions({ * model: "stability-ai/stable-diffusion", * token: "Get your token at https://replicate.com/account" * }) * ``` */ export const listVersions = async (options) => { const { owner, model } = extractModelAndOwner(options.model); const response = await makeApiRequest(options, "GET", `models/${owner}/${model}/versions${options.cursor ? "?cursor=" + options.cursor : ""}`); const versions = response.results.map(version => ({ id: version.id, createdAt: new Date(version.created_at), cogVersion: version.cog_version, schema: version.openapi_schema, })); const nextCursor = response.next?.split("cursor=").pop(); const result = { version: versions[0]?.id, versions: versions, next: () => (versions.length && nextCursor ? listVersions({ ...options, cursor: nextCursor }) : getEmptyResult()), ...(nextCursor ? { nextCursor } : {}), }; if (!options.all) { return result; } const nextResult = await result.next(); const allResults = { version: result.version, versions: [...result.versions, ...nextResult.versions], next: () => getEmptyResult(), }; return allResults; };