All files / lib/prepare compat.ts

100% Statements 512/512
94.17% Branches 97/103
100% Functions 17/17
100% Lines 512/512

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 5131x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1155x 1155x 1155x 1155x 5x 5x 1155x 1x 1x 1x 1x 1x 1x 35x 35x 35x 1x 1x 35x 34x 34x 34x 181x 166x 166x 132x 132x 132x 34x 34x 34x 35x 1x 1x 1x 1x 1x 1x 44x 44x 44x 1x 1x 1x 1x 1x 1x 1x 1x 44x 253x 1x 1x 1x 1x 1x 1x 1x 1x 253x 44x 42x 44x 1x 1x 1x 1x 1x 1x 1x 1x 44x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 29x 29x 29x 29x 1x 30x 30x 2x 28x 30x 30x 2x 28x 1x 27x 30x 30x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 33x 32x 31x 2x 33x 1x 1x 1x 1x 1x 33x 31x 31x 33x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 49x 49x 49x 49x 2x 2x 2x 2x 2x 49x 47x 49x 1x 1x 1x 1x 1x 49x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 47x 47x 47x 49x 45x 47x 47x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 9x 9x 9x 9x 5x 5x 9x 5x 5x 9x 7x 7x 1x 1x 1x 1x 1x 1x 1x 47x 47x 47x 47x 47x 47x 47x 40x 1x 1x 1x 1x 1x 1x 1x 1x 40x 39x 39x 39x 39x 39x 39x 47x 47x 2x 2x 2x 2x 2x 2x 2x 2x 47x 47x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 76x 76x 76x 74x 74x 74x 616x 74x 76x 39x 39x 1x 1x 1x 1x 1x 1x 1x 41x 41x 41x 41x 40x 41x 2x 2x 41x 3x 3x 41x 41x 1x 1x 41x 1x 1x 1x 1x 1x 1x 35x 35x 35x 2x 2x 2x 2x 2x 2x 2x 2x 35x 35x 1x 1x 1x 1x 1x 1x 30x 30x 27x 27x 30x 3x 3x 3x 3x 3x 30x 1x 1x 1x 1x 1x 1x 30x 30x 2x 2x 30x 28x 28x 28x 28x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 49x 49x 49x 49x 1x 1x 49x 47x 47x 47x 47x 49x 1x 1x 1x 1x 49x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 49x  
"use strict";
/**
 * Compatibility checks for OpenAPI Generator typescript-fetch SDKs.
 *
 * oag-mcp only supports typescript-fetch output from a tested set of
 * generator versions and options. This module detects the SDK layout
 * (TypeScript sources or compiled JavaScript with declarations), the
 * generator version and the options that shape the generated code, and
 * rejects anything it cannot support with an actionable error.
 *
 * @module prepare/compat
 */
import fs from "node:fs";
import path from "node:path";
 
import { OagMcpError } from "../errors.js";
import {
  SUPPORTED_GENERATOR,
  SUPPORTED_OAG_VERSIONS,
} from "../util/version.js";
 
/** Options known to produce unsupported output. */
export const UNSUPPORTED_GENERATOR_OPTIONS: Record<string, string> = {
  sagasAndRecords:
    "generates redux-saga/immutablejs code that cannot run in the sandbox",
  dateLibrary:
    "only the default 'date' library is supported (Temporal is experimental)",
  withNodeImports: "not applicable to typescript-fetch",
  stringEnums:
    "TypeScript enums cannot be referenced from agent code (no imports); use the default union types",
};
 
/** Detected SDK layout. */
export interface SdkInspection {
  root: string;
  layout: "source" | "compiled";
  /** Directory containing runtime and apis/ implementation files. */
  implDir: string;
  /** Module bundled into the execution bundle. */
  entry: string;
  /** Directory containing compiled declarations (compiled layout only). */
  typesDir?: string;
  oagVersion: string;
  oagVersionTested: boolean;
  detectedOptions: Record<string, string | boolean>;
  pkg?: {
    name?: string;
    version?: string;
    dependencies?: Record<string, string>;
  };
  apiFiles: string[];
  warnings: string[];
}
 
/** Options for {@link inspectSdk}. */
export interface InspectOptions {
  layout?: "auto" | "source" | "compiled";
  declaredOagVersion?: string;
  allowUntestedOagVersion?: boolean;
}
 
const OTHER_GENERATOR_MARKERS: Array<[RegExp, string]> = [
  [/from ['"]axios['"]|globalAxios/, "typescript-axios"],
  [/@angular\/core/, "typescript-angular"],
  [/from ['"]rxjs['"]/, "typescript-rxjs"],
  [/localVarRequest|from ['"]request['"]/, "typescript-node"],
  [/from ['"]jquery['"]/, "typescript-jquery"],
  [/ObservableAPI|PromiseAPI/, "typescript"],
];
 
/**
 * Read a file if it exists.
 *
 * @private
 */
function _read(file: string): string | undefined {
  try {
    return fs.readFileSync(file, "utf8");
  } catch {
    return undefined;
  }
}
 
/**
 * List API implementation files in `<implDir>/apis`.
 *
 * @private
 */
function _apiFiles(implDir: string, ext: "ts" | "js"): string[] {
  const dir = path.join(implDir, "apis");
  if (!fs.existsSync(dir)) {
    return [];
  }
  return fs
    .readdirSync(dir)
    .filter(
      (f) =>
        f.endsWith(`.${ext}`) &&
        !f.endsWith(".d.ts") &&
        !/^index\.(ts|js)$/.test(f) &&
        !/Sagas\.(ts|js)$/.test(f) &&
        f !== "SagaApiManager.ts" &&
        f !== "allSagas.ts",
    )
    .sort()
    .map((f) => path.join(dir, f));
}
 
/**
 * Verify the runtime module is typescript-fetch output.
 *
 * @private
 */
function _checkRuntime(runtimeFile: string, root: string): void {
  const text = _read(runtimeFile);
  if (text === undefined) {
    throw new OagMcpError(
      "SDK_LAYOUT_UNSUPPORTED",
      `typescript-fetch runtime module not found in ${root}`,
      {
        hint: "Expected src/runtime.ts (sources) or dist/runtime.js with dist/runtime.d.ts (compiled). Only OpenAPI Generator typescript-fetch SDKs are supported.",
      },
    );
  }
  for (const [re, gen] of OTHER_GENERATOR_MARKERS) {
    if (re.test(text)) {
      throw new OagMcpError(
        "SDK_GENERATOR_UNSUPPORTED",
        `The SDK in ${root} looks like OpenAPI Generator "${gen}" output`,
        {
          hint: `Only the ${SUPPORTED_GENERATOR} generator is supported. Regenerate the SDK with OpenAPI Generator -g ${SUPPORTED_GENERATOR}.`,
        },
      );
    }
  }
  const markers = ["BaseAPI", "Configuration", "fetchApi", "ResponseError"];
  const missing = markers.filter((m) => !text.includes(m));
  if (missing.length > 0) {
    throw new OagMcpError(
      "SDK_GENERATOR_UNSUPPORTED",
      `The SDK runtime in ${root} is missing typescript-fetch markers: ${missing.join(", ")}`,
      {
        hint: `Only OpenAPI Generator ${SUPPORTED_GENERATOR} SDKs are supported.`,
      },
    );
  }
}
 
/**
 * Detect generator options from the generated code.
 *
 * @private
 */
function _detectOptions(
  implDir: string,
  apiFiles: string[],
  declarationText: string,
): Record<string, string | boolean> {
  const opts: Record<string, string | boolean> = {};
  const apiText = apiFiles.map((f) => _read(f) ?? "").join("\n");
  opts.withInterfaces = /ApiInterface\b/.test(apiText + declarationText);
  const modelDir = path.join(implDir, "models");
  const modelText = fs.existsSync(modelDir)
    ? fs
        .readdirSync(modelDir)
        .map((f) => _read(path.join(modelDir, f)) ?? "")
        .join("\n")
    : "";
  opts.withoutRuntimeChecks = !/FromJSON/.test(apiText + modelText);
  opts.importFileExtension = /from ['"]\.\.?\/[^'"]+\.js['"]/.test(apiText)
    ? ".js"
    : "";
  const base = apiFiles.map((f) => path.basename(f));
  opts.fileNaming = base.some((b) => b.includes("-"))
    ? "kebab-case"
    : base.some((b) => /^[a-z]/.test(b))
      ? "camelCase"
      : "PascalCase";
  return opts;
}
 
/**
 * Determine the generator version recorded in `.openapi-generator/VERSION`.
 *
 * @private
 */
function _oagVersion(
  root: string,
  opts: InspectOptions,
): { version: string; tested: boolean } {
  const recorded = _read(path.join(root, ".openapi-generator", "VERSION"));
  const version = (recorded ?? opts.declaredOagVersion ?? "").trim();
  if (!version) {
    throw new OagMcpError(
      "SDK_OAG_VERSION_UNKNOWN",
      `Cannot determine the OpenAPI Generator version of the SDK in ${root}`,
      {
        hint: "The generator writes .openapi-generator/VERSION. If it was removed, pass --sdk-oag-version <version> to declare it.",
      },
    );
  }
  if (
    recorded &&
    opts.declaredOagVersion &&
    recorded.trim() !== opts.declaredOagVersion
  ) {
    throw new OagMcpError(
      "SDK_OAG_VERSION_CONFLICT",
      `--sdk-oag-version ${opts.declaredOagVersion} conflicts with .openapi-generator/VERSION ${recorded.trim()}`,
    );
  }
  const tested = (SUPPORTED_OAG_VERSIONS as readonly string[]).includes(
    version,
  );
  if (!tested && !opts.allowUntestedOagVersion) {
    throw new OagMcpError(
      "SDK_OAG_VERSION_UNSUPPORTED",
      `OpenAPI Generator ${version} is not in the tested set (${SUPPORTED_OAG_VERSIONS.join(", ")})`,
      {
        hint: `Regenerate the SDK with a tested OpenAPI Generator version (${SUPPORTED_OAG_VERSIONS.join(", ")}), or pass --allow-untested-oag-version to proceed at your own risk.`,
      },
    );
  }
  return { version, tested };
}
 
/** A compiled SDK package's entry point and declarations. */
interface CompiledLayout {
  implDir: string;
  entry: string;
  typesDir: string;
}
 
/** The SDK layout chosen for preparation. */
interface ResolvedLayout {
  layout: "source" | "compiled";
  /** Directory with runtime, apis/ and models/. */
  implDir: string;
  /** Extension of the implementation files. */
  ext: "ts" | "js";
  entry: string;
  /** Declarations directory of a compiled package. */
  typesDir?: string;
}
 
/** The parsed package.json of an SDK. */
type SdkPackage = Record<string, unknown>;
 
/**
 * Read the SDK's package.json, if it has one.
 *
 * @private
 */
function _readPackage(
  root: string,
  warnings: string[],
): SdkPackage | undefined {
  const pkgText = _read(path.join(root, "package.json"));
  if (!pkgText) {
    warnings.push(
      "SDK has no package.json (generated without npmName); it can still be imported by path.",
    );
    return undefined;
  }
  try {
    return JSON.parse(pkgText) as SdkPackage;
  } catch {
    throw new OagMcpError(
      "SDK_PACKAGE_INVALID",
      `package.json in ${root} is not valid JSON`,
    );
  }
}
 
/**
 * Reject an SDK generated with an unsupported option.
 *
 * @private
 */
function _unsupported(option: string, setting: string): never {
  throw new OagMcpError(
    "SDK_OPTION_UNSUPPORTED",
    `The SDK was generated with ${setting}, which is not supported`,
    { hint: UNSUPPORTED_GENERATOR_OPTIONS[option] },
  );
}
 
/**
 * The directory with typescript-fetch sources (`index.ts` and
 * `runtime.ts`): `src/` or the root.
 *
 * @private
 */
function _sourceDir(root: string): string | undefined {
  return [path.join(root, "src"), root].find(
    (d) =>
      fs.existsSync(path.join(d, "runtime.ts")) &&
      fs.existsSync(path.join(d, "index.ts")),
  );
}
 
/**
 * The compiled package layout: the first of `module`, `main` and
 * `dist/index.js` with a runtime beside it and declarations.
 *
 * @private
 */
function _compiledLayout(
  root: string,
  pkg: SdkPackage | undefined,
): CompiledLayout | undefined {
  const p = (pkg ?? {}) as Record<string, string>;
  const typesFile = p.types ?? p.typings ?? "dist/index.d.ts";
  const typesDir = path.dirname(path.join(root, typesFile));
  for (const entryRel of [p.module, p.main, "dist/index.js"].filter(Boolean)) {
    const entry = path.join(root, entryRel);
    const implDir = path.dirname(entry);
    if (
      fs.existsSync(entry) &&
      fs.existsSync(path.join(implDir, "runtime.js")) &&
      fs.existsSync(path.join(typesDir, "index.d.ts"))
    ) {
      return { implDir, entry, typesDir };
    }
  }
  return undefined;
}
 
/**
 * Choose the layout: sources when requested or found (`auto`), otherwise
 * the compiled package.
 *
 * @private
 */
function _resolveLayout(
  root: string,
  pkg: SdkPackage | undefined,
  wanted: InspectOptions["layout"] = "auto",
): ResolvedLayout {
  const sourceDir = _sourceDir(root);
  if (wanted === "source" || (wanted === "auto" && sourceDir)) {
    if (!sourceDir) {
      throw new OagMcpError(
        "SDK_LAYOUT_UNSUPPORTED",
        `No typescript-fetch sources (src/index.ts, src/runtime.ts) found in ${root}`,
        {
          hint: "Use --sdk-layout compiled for packages that ship only dist/.",
        },
      );
    }
    return {
      layout: "source",
      implDir: sourceDir,
      ext: "ts",
      entry: path.join(sourceDir, "index.ts"),
    };
  }
  const compiled = _compiledLayout(root, pkg);
  if (!compiled) {
    throw new OagMcpError(
      "SDK_LAYOUT_UNSUPPORTED",
      `Unrecognised SDK layout in ${root}`,
      {
        hint: "Expected typescript-fetch sources (src/index.ts, src/runtime.ts, src/apis/) or a compiled package (dist/index.js, dist/runtime.js, dist/index.d.ts). See docs/sdk-compatibility.md.",
      },
    );
  }
  return { layout: "compiled", ext: "js", ...compiled };
}
 
/**
 * Whether any model is generated as a TypeScript enum (stringEnums=true).
 * Sources declare `export enum`; compiled SDKs are checked through their
 * declarations (`export declare enum`), which are the same whatever module
 * format the JavaScript was compiled to, and which a compiled SDK always
 * has even when its JavaScript directory holds none.
 *
 * @private
 */
function _hasEnumDeclarations(dirs: string[]): boolean {
  return dirs.some((dir) => {
    const modelsDir = path.join(dir, "models");
    return (
      fs.existsSync(modelsDir) &&
      fs
        .readdirSync(modelsDir)
        .some((f) =>
          /export (declare )?enum /.test(_read(path.join(modelsDir, f)) ?? ""),
        )
    );
  });
}
 
/**
 * Reject generator options oag-mcp does not support: sagasAndRecords,
 * stringEnums and the Temporal date library.
 *
 * @private
 */
function _checkGeneratorOptions(resolved: ResolvedLayout): void {
  const { implDir, ext } = resolved;
  if (
    fs.existsSync(path.join(implDir, "ApiEntitiesRecord.ts")) ||
    fs.existsSync(path.join(implDir, "apis", "allSagas.ts"))
  ) {
    _unsupported("sagasAndRecords", "sagasAndRecords");
  }
  if (_hasEnumDeclarations([implDir, resolved.typesDir ?? implDir])) {
    _unsupported("stringEnums", "stringEnums=true");
  }
  const runtimeText = _read(path.join(implDir, `runtime.${ext}`)) ?? "";
  if (/Temporal\./.test(runtimeText)) {
    _unsupported("dateLibrary", "dateLibrary=temporal");
  }
}
 
/**
 * The SDK's generated API class files, of which there must be at least one.
 *
 * @private
 */
function _requireApiFiles(resolved: ResolvedLayout): string[] {
  const apiFiles = _apiFiles(resolved.implDir, resolved.ext);
  if (apiFiles.length === 0) {
    throw new OagMcpError(
      "SDK_LAYOUT_UNSUPPORTED",
      `No API classes found in ${path.join(resolved.implDir, "apis")}`,
      {
        hint: "The SDK must contain generated API classes (apis/*Api). Check that the spec has operations and that generation succeeded.",
      },
    );
  }
  return apiFiles;
}
 
/**
 * The API declarations of a compiled package, for option detection.
 *
 * @private
 */
function _apiDeclarations(resolved: ResolvedLayout): string {
  if (resolved.layout !== "compiled") {
    return "";
  }
  const apisDir = path.join(resolved.typesDir!, "apis");
  return fs
    .readdirSync(apisDir)
    .filter((f) => f.endsWith(".d.ts"))
    .map((f) => _read(path.join(apisDir, f)) ?? "")
    .join("\n");
}
 
/**
 * The package fields reported in the inspection.
 *
 * @private
 */
function _packageInfo(pkg: SdkPackage | undefined): SdkInspection["pkg"] {
  if (!pkg) {
    return undefined;
  }
  return {
    name: pkg.name as string,
    version: pkg.version as string,
    dependencies: pkg.dependencies as Record<string, string> | undefined,
  };
}
 
/**
 * Inspect an SDK working copy and verify it is supported.
 *
 * @param {String} root - SDK working copy root
 * @param {Object} [opts] - inspection options
 * @return {Object} inspection result
 */
export function inspectSdk(
  root: string,
  opts: InspectOptions = {},
): SdkInspection {
  const warnings: string[] = [];
  const pkg = _readPackage(root, warnings);
  if (/redux-saga|immutable/.test(JSON.stringify(pkg?.dependencies ?? {}))) {
    _unsupported("sagasAndRecords", "sagasAndRecords");
  }
  const resolved = _resolveLayout(root, pkg, opts.layout);
  _checkRuntime(path.join(resolved.implDir, `runtime.${resolved.ext}`), root);
  _checkGeneratorOptions(resolved);
  const apiFiles = _requireApiFiles(resolved);
  const { version, tested } = _oagVersion(root, opts);
  if (!tested) {
    warnings.push(
      `OpenAPI Generator ${version} is untested; proceeding because --allow-untested-oag-version was given.`,
    );
  }
  return {
    root,
    layout: resolved.layout,
    implDir: resolved.implDir,
    entry: resolved.entry,
    typesDir: resolved.typesDir,
    oagVersion: version,
    oagVersionTested: tested,
    detectedOptions: _detectOptions(
      resolved.implDir,
      apiFiles,
      _apiDeclarations(resolved),
    ),
    pkg: _packageInfo(pkg),
    apiFiles,
    warnings,
  };
}