diff --git a/packages/tome-kolmafia-lib/src/api/batch.ts b/packages/tome-kolmafia-lib/src/api/batch.ts index db5af32..dcdf19c 100644 --- a/packages/tome-kolmafia-lib/src/api/batch.ts +++ b/packages/tome-kolmafia-lib/src/api/batch.ts @@ -4,18 +4,43 @@ import { getHash } from "./hash"; export async function batchFunction( functions: readonly { name: string; args: unknown[] }[], ) { + const map = functions.map(({ name, args }) => + name === "getProperty" && args.length === 1 && typeof args[0] === "string" + ? "P" + : "F", + ); + const apiFunctions = functions.filter((_, index) => map[index] === "F"); + const properties = functions + .filter((_, index) => map[index] === "P") + .map(({ args }) => args[0]) as string[]; const result = await apiCall( { - functions, + properties, + functions: apiFunctions, }, await getHash(), ); - if (!result?.functions) { + if (!result?.functions || !result?.properties) { throw new Error( result && "error" in result && typeof result.error === "string" ? result.error - : undefined, + : "No functions property in response.", ); + } else if (result.functions.length !== apiFunctions.length) { + throw new Error("Mismatched functions response."); + } else if (result.properties.length !== properties.length) { + throw new Error("Mismatched properties result."); + } + + const outFunctions = []; + let functionsIdx = 0; + let propertiesIdx = 0; + for (let i = 0; i < map.length; i++) { + if (map[i] === "P") { + outFunctions.push(result.properties[propertiesIdx++]); + } else if (map[i] === "F") { + outFunctions.push(result.functions[functionsIdx++]); + } } - return result?.functions; + return outFunctions; }