xBiggs / fightcade-api

An unofficial TypeScript wrapper for the Fightcade API.
MIT License
7 stars 2 forks source link

GetGame() fails on Flycast gameid #6

Closed xBiggs closed 4 months ago

xBiggs commented 4 months ago

A user on Discord informed me that that GetGame() is failing when given a Flycast gameid.

Here is a minimum example:

import { GetGame } from 'fightcade-api';

const replays = await GetGame('flycast_dc_mkg');
console.log(replays);

Output:

159 |     const json = JSON.stringify(obj, null, 2);
160 |     return json.replace(/"([^"]+)":/g, "$1:");
161 | };
162 | class ZodError extends Error {
163 |     constructor(issues) {
164 |         super();
              ^
ZodError: [
  {
    "code": "invalid_type",
    "expected": "boolean",
    "received": "undefined",
    "path": [
      "game",
      "training"
    ],
    "message": "Required"
  }
]
 errors: [
  {
    "code": "invalid_type",
    "expected": "boolean",
    "received": "undefined",
    "path": [
      "game",
      "training"
    ],
    "message": "Required"
  }
]

      at new ZodError (/home/biggs/fightcade-elo/node_modules/zod/lib/index.mjs:164:9)
      at error (/home/biggs/fightcade-elo/node_modules/zod/lib/index.mjs:587:31)
      at parse (/home/biggs/fightcade-elo/node_modules/zod/lib/index.mjs:692:15)
      at /home/biggs/fightcade-elo/node_modules/fightcade-api/dist/fightcade-api.js:165:12

It looks like the Zod Validator caught the missing training property. This illustrates exactly why I moved to runtime validation in v2.0.

I verified the actual shape of the object with this:

async function GetGame(gameid: string) {
  const response = await fetch("https://www.fightcade.com/api/", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ req: "gameinfo", gameid })
  });
  return await response.json();
}
const replays = await GetGame("flycast_dc_mkg");
console.log(replays);

Output:

{
  game: {
    gameid: "flycast_dc_mkg",
    name: "Mortal Kombat Gold (USA) (Rev 1) (DREAMCAST)",
    year: "1999",
    publisher: "Midway",
    emulator: "flycast",
    available_for: 1,
    system: "Dreamcast",
    ranked: false,
    genres: [ "Fighter" ],
  },
  res: "OK",
}

This should be easily fixed by making training optional.