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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | 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 524x 524x 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 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 566x 566x 1x 1x 566x 558x 558x 566x 2x 2x 566x 5x 2x 566x 1x 1x 1x 566x 2x 2x 2x 566x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 30705x 30705x 30705x 1113x 30705x 30705x 1x 1x 1x 1x 1x 1x 30705x 30705x 30705x 1469x 1469x 1469x 30705x 1468x 1468x 30705x 1x 1x 1x 1x 1x 1x 30705x 30705x 29592x 29592x 30705x 281x 281x 280x 280x 281x 30705x 279x 279x 30705x 1x 1x 1x 1x 1x 1x 30705x 30705x 30705x 1x 1x 30705x 1x 1x 30705x 1x 1x 1x 1x 1x 1x 545x 545x 545x 265x 265x 545x 267x 13x 545x 1x 1x 1x 1x 1x 1x 545x 545x 545x 545x 545x 545x 30705x 30705x 30705x 30705x 30705x 545x 545x 545x 1x 1x 1x 1x 1x 1x 813x 813x 813x 813x 813x 813x 813x 1x 1x 1x 1x 1x 1x 813x 813x 813x 813x 813x 813x 813x 813x 268x 268x 813x 813x 269x 269x 813x 813x 813x 813x 275x 275x 813x 813x 1x 1x 1x 1x 1x 1x 101x 101x 100x 101x 101x 1x 1x 1x 1x 1x 1x 55567x 55567x 55467x 55467x 55567x 55567x 55567x 810x 810x 810x 810x 55567x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 3x 4x 3x 3x 4x 4x 1x 1x 1x 1x 1x 1x 1x 55567x 55567x 55567x 1022x 429x 55567x 55563x 55563x 55567x 55567x 3x 3x 55567x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 101x 101x 101x 101x 101x 101x 101x 101x 101x 55567x 55567x 55567x 55567x 101x 101x 29x 29x 1x 1x 1x 1x 1x 1x 2629x 2629x 2629x 1x 1x 1x 1x 1x 1x 2440x 2440x 2440x 536x 433x 2440x 2440x 1x 1x 1x 1x 1x 1x 542x 542x 2x 2x 542x 540x 540x 537x 542x 542x 1x 1x 1x 1x 1x 1x 297x 297x 297x 297x 297x 297x 297x 297x 1x 1x 1x 1x 1x 1x 86x 86x 86x 86x 86x 1x 1x 1x 1x 1x 1x 108x 108x 108x 108x 108x 108x 1x 1x 1x 1x 1x 1x 181x 181x 181x 181x 181x 1x 1x 1x 1x 1x 1x 181x 181x 19x 162x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 3323x 3323x 1x 1x 1x 1x 1x 1x 1x 1x 2008x 2008x 2008x 1x 1x 1x 1x 1x 1x 1x 805x 805x 235x 235x 805x 805x 805x 805x 1x 1x 1x 1x 1x 1x 1x 1165x 1165x 1165x 1x 1x 1x 1x 1x 1x 1x 136x 136x 136x 1x 1x 1x 1x 1x 1x 1x 26x 26x 25x 21x 26x 26x 1x 1x 1x 1x 1x 1x 1x 434x 365x 365x 434x 68x 68x 434x 1x 1x 1x 1x 1x 434x 1x 1x 1x 1x 1x 1x 1x 365x 365x 1x 364x 365x 365x 365x 365x 365x 365x 365x 365x 365x 365x 365x 1x 1x 1x 1x 1x 1x 1x 984x 984x 984x 984x 984x 984x 1x 1x 1x 1x 1x 1x 1x 68x 68x 68x 65x 3x 68x 68x 68x 68x 68x 1x 1x 1x 1x 1x 1x 1x 181x 181x 181x 181x 181x 181x 181x 181x 393x 393x 5615x 698x 360x 360x 328x 328x 328x 328x 360x 698x 5615x 5615x 393x 393x 393x 393x 181x 1x 1x 1x 1x 1x 1x 1x 230x 20x 210x 230x 230x 210x 138x 230x 230x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 19x 20x 18x 18x 20x 1x 1x 1x 1x 1x 1x 20x 1x 1x 1x 1x 1x 1x 1x 138x 138x 138x 138x 138x 1x 1x 138x 1x 1x 1x 1x 138x 132x 132x 138x 138x 1x 1x 1x 1x 1x 1x 1x 132x 132x 132x 132x 46x 46x 132x 86x 113x 113x 86x 86x 132x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1315x 235x 235x 1315x 18x 67x 1x 1x 1x 1x 67x 18x 2x 2x 2x 2x 2x 2x 2x 2x 2x 18x 264x 264x 264x 16x 18x 1x 1x 1x 1x 1x 1x 1x 2166x 1952x 1952x 2166x 214x 2166x 148x 148x 2166x 2166x 183x 183x 181x 181x 181x 181x 181x 181x 181x 181x 181x 183x 2x 2x 2x 2x 183x 2166x 1x 1x 1x 1x 1x 1x 1x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 180x 1x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 297x 297x 297x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 181x 1x 1x 1x 1x 1x 1x 1x 1x 181x 108x 108x 181x 8x 8x 181x 65x 65x 65x 65x 65x 181x 1x 1x 1x 1x 1x 1x 1x 108x 108x 108x 108x 108x 107x 1x 108x 108x 108x 108x 108x 108x 108x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 7x 3x 7x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 108x 108x 108x 108x 108x 150x 150x 126x 126x 24x 23x 23x 24x 19x 19x 150x 108x 108x 1x | "use strict";
/**
* Static analysis of a typescript-fetch SDK.
*
* Two independent sources are combined:
*
* 1. Implementation files (TypeScript sources or compiled JavaScript) are
* parsed to find, for every public API method, the HTTP method and path
* template it sends. typescript-fetch 7.x builds these in
* `<method>RequestOpts`; older output builds them in `<method>Raw`. This
* gives a reliable binding without assuming operationId equals the
* generated method name.
* 2. Declarations (`.d.ts`) are type-checked to extract exact signatures,
* documentation and the declarations of referenced types.
*
* @module prepare/analyze
*/
import fs from "node:fs";
import path from "node:path";
import ts from "typescript";
import { OagMcpError } from "../errors.js";
import type { TypeDeclaration } from "../types.js";
import { sdkCompilerOptions } from "./bundle.js";
/** HTTP binding found in an implementation method. */
export interface ImplBinding {
httpMethod: string;
path: string;
source: "RequestOpts" | "Raw";
}
/** A public API method of the SDK with its binding and types. */
export interface AnalyzedMethod {
className: string;
methodName: string;
httpMethod: string;
path: string;
signature: string;
parameters: Array<{ name: string; type: string; optional: boolean }>;
requestStyle: "object" | "positional" | "none";
inputType?: TypeDeclaration;
returnType: string;
referencedTypes: TypeDeclaration[];
requiredArguments: string[];
documentation: string;
deprecated: boolean;
example: string;
}
/** Example hints keyed by normalised parameter/property name. */
export type ExampleHints = Record<string, unknown>;
const MAX_REFERENCED_TYPES = 15;
const MAX_TYPE_DEPTH = 3;
const MAX_MEMBERS = 40;
/**
* Normalise a name for loose matching between OpenAPI and SDK naming.
*
* @param {String} name - name
* @return {String} normalised name
*/
export function normaliseName(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9]/g, "");
}
/** Class name -> method name -> binding. */
type Bindings = Map<string, Map<string, ImplBinding>>;
/** What a method body scan has found so far. */
interface BindingScan {
httpMethod?: string;
pathInit?: ts.Expression;
variables: Map<string, ts.Expression>;
}
/** A property assignment with a plain or quoted name. */
type NamedAssignment = ts.PropertyAssignment & {
name: ts.Identifier | ts.StringLiteral;
};
/** A class method with a plain name. */
type NamedMethod = ts.MethodDeclaration & { name: ts.Identifier };
/** The SDK declaration kinds that are rendered for agents. */
type SdkDeclaration =
ts.InterfaceDeclaration | ts.TypeAliasDeclaration | ts.EnumDeclaration;
/** A method parameter as written in the declarations. */
interface Param {
name: string;
type: string;
optional: boolean;
node: ts.ParameterDeclaration;
}
/** How a method takes its arguments, and an example call's arguments. */
interface RequestArgs {
requestStyle: AnalyzedMethod["requestStyle"];
inputType: TypeDeclaration | undefined;
requiredArguments: string[];
exampleArgs: string;
}
/** State shared while analysing the classes of every declaration file. */
interface AnalyzeRun {
impl: Bindings;
hintsFor: (httpMethod: string, p: string) => ExampleHints;
found: Set<string>;
problems: string[];
methods: AnalyzedMethod[];
}
/** Implementation method suffixes, most authoritative first. */
const BINDING_SOURCES: ReadonlyArray<ImplBinding["source"]> = [
"RequestOpts",
"Raw",
];
const HIDDEN_MODIFIERS =
ts.ModifierFlags.Private |
ts.ModifierFlags.Protected |
ts.ModifierFlags.Static;
/** Example renderers for scalar types, by type flag. */
const SCALAR_EXAMPLES: ReadonlyArray<{
flags: ts.TypeFlags;
render: (hint: unknown) => string;
}> = [
{
flags: ts.TypeFlags.StringLike,
render: (hint) =>
JSON.stringify(typeof hint === "string" ? hint : "example"),
},
{
flags: ts.TypeFlags.NumberLike,
render: (hint) => (typeof hint === "number" ? String(hint) : "1"),
},
{
flags: ts.TypeFlags.BooleanLike,
render: (hint) => (typeof hint === "boolean" ? String(hint) : "true"),
},
];
/**
* Reconstruct a string from a literal-ish expression.
*
* @private
*/
function _literalText(node: ts.Expression | undefined): string | undefined {
if (!node) {
return undefined;
}
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
return node.text;
}
if (ts.isTemplateExpression(node)) {
return _templateText(node);
}
if (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression)
) {
// `/pets/{petId}`.replace(...) in older generator output
return _literalText(node.expression.expression);
}
if (ts.isParenthesizedExpression(node)) {
return _literalText(node.expression);
}
return undefined;
}
/**
* Reconstruct a template whose substitutions are all literal-ish.
*
* @private
*/
function _templateText(node: ts.TemplateExpression): string | undefined {
let out = node.head.text;
for (const span of node.templateSpans) {
const inner = _literalText(span.expression);
if (inner === undefined) {
return undefined;
}
out += inner + span.literal.text;
}
return out;
}
/**
* Whether a node is a property assignment with a plain or quoted name.
*
* @private
*/
function _isNamedAssignment(n: ts.Node): n is NamedAssignment {
return (
ts.isPropertyAssignment(n) &&
(ts.isIdentifier(n.name) || ts.isStringLiteral(n.name))
);
}
/**
* Remember the first initialiser of each local variable.
*
* @private
*/
function _noteVariable(n: ts.Node, scan: BindingScan): void {
if (
ts.isVariableDeclaration(n) &&
ts.isIdentifier(n.name) &&
n.initializer &&
!scan.variables.has(n.name.text)
) {
scan.variables.set(n.name.text, n.initializer);
}
}
/**
* Take the first `method:` and `path:` properties.
*
* @private
*/
function _noteProperty(n: ts.Node, scan: BindingScan): void {
if (!_isNamedAssignment(n)) {
return;
}
if (n.name.text === "method" && scan.httpMethod === undefined) {
const text = _literalText(n.initializer);
if (text) {
scan.httpMethod = text.toUpperCase();
}
}
if (n.name.text === "path" && scan.pathInit === undefined) {
scan.pathInit = n.initializer;
}
}
/**
* Take a shorthand `path` property as the path.
*
* @private
*/
function _noteShorthand(n: ts.Node, scan: BindingScan): void {
if (
ts.isShorthandPropertyAssignment(n) &&
n.name.text === "path" &&
scan.pathInit === undefined
) {
scan.pathInit = n.name;
}
}
/**
* The path text, following a variable the path property names.
*
* @private
*/
function _pathText(scan: BindingScan): string | undefined {
const init = scan.pathInit;
if (!init) {
return undefined;
}
return ts.isIdentifier(init)
? _literalText(scan.variables.get(init.text))
: _literalText(init);
}
/**
* Find the HTTP method and path built inside a method body.
*
* @private
*/
function _extractBinding(body: ts.Node): {
httpMethod?: string;
path?: string;
} {
const scan: BindingScan = { variables: new Map() };
const visit = (n: ts.Node) => {
_noteVariable(n, scan);
_noteProperty(n, scan);
_noteShorthand(n, scan);
ts.forEachChild(n, visit);
};
visit(body);
return { httpMethod: scan.httpMethod, path: _pathText(scan) };
}
/**
* The public method an implementation method builds the request for.
*
* @private
*/
function _bindingTarget(
name: string,
): { base: string; source: ImplBinding["source"] } | undefined {
const source = BINDING_SOURCES.find((s) => name.endsWith(s));
const base = source ? name.slice(0, -source.length) : "";
return source && base ? { base, source } : undefined;
}
/**
* Record the binding an implementation method builds, if it builds one.
*
* @private
*/
function _recordBinding(
out: Bindings,
cls: string,
name: string,
body: ts.Node | undefined,
): void {
const target = _bindingTarget(name);
if (!body || !target) {
return;
}
const { httpMethod, path: p } = _extractBinding(body);
if (!httpMethod || !p) {
return;
}
const methods = out.get(cls) ?? new Map<string, ImplBinding>();
const existing = methods.get(target.base);
// RequestOpts is authoritative when both are present.
if (!existing || existing.source === "Raw") {
methods.set(target.base, { httpMethod, path: p, source: target.source });
}
out.set(cls, methods);
}
/**
* Whether a class extends the generated runtime's BaseAPI.
*
* @private
*/
function _extendsBaseApi(n: ts.ClassDeclaration, sf: ts.SourceFile): boolean {
return (n.heritageClauses ?? []).some((h) =>
h.types.some((t) => /BaseAPI$/.test(t.expression.getText(sf))),
);
}
/**
* Record the bindings of an API class declaration's methods.
*
* @private
*/
function _scanClass(n: ts.Node, sf: ts.SourceFile, out: Bindings): void {
if (!ts.isClassDeclaration(n) || !n.name || !_extendsBaseApi(n, sf)) {
return;
}
const cls = n.name.text;
out.set(cls, out.get(cls) ?? new Map());
for (const m of n.members) {
if (ts.isMethodDeclaration(m) && ts.isIdentifier(m.name)) {
_recordBinding(out, cls, m.name.text, m.body);
}
}
}
/**
* The class and method an ES5 `Foo.prototype.bar` target names.
*
* @private
*/
function _prototypeMember(
left: ts.Expression,
): { cls: string; name: string } | undefined {
if (
ts.isPropertyAccessExpression(left) &&
ts.isPropertyAccessExpression(left.expression) &&
left.expression.name.text === "prototype" &&
ts.isIdentifier(left.expression.expression)
) {
return { cls: left.expression.expression.text, name: left.name.text };
}
return undefined;
}
/**
* Record the binding of an ES5 method assignment:
* `Foo.prototype.barRequestOpts = function () { ... }`.
*
* @private
*/
function _scanPrototypeAssignment(n: ts.Node, out: Bindings): void {
if (
!ts.isBinaryExpression(n) ||
n.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
!(ts.isFunctionExpression(n.right) || ts.isArrowFunction(n.right))
) {
return;
}
const member = _prototypeMember(n.left);
if (member) {
_recordBinding(out, member.cls, member.name, n.right.body);
}
}
/**
* Scan implementation files for API classes and method bindings.
*
* @param {Array} files - implementation files under apis/
* @return {Map} className -> methodName -> binding
*/
export function scanImplementation(files: string[]): Bindings {
const out: Bindings = new Map();
for (const file of files) {
const text = fs.readFileSync(file, "utf8");
const sf = ts.createSourceFile(
file,
text,
ts.ScriptTarget.ES2022,
true,
file.endsWith(".js") ? ts.ScriptKind.JS : ts.ScriptKind.TS,
);
const visit = (n: ts.Node) => {
_scanClass(n, sf, out);
_scanPrototypeAssignment(n, out);
ts.forEachChild(n, visit);
};
visit(sf);
}
return out;
}
/**
* Collapse whitespace in a declaration snippet.
*
* @private
*/
function _compact(text: string): string {
return text.replace(/\s+/g, " ").trim();
}
/**
* Whether a declaration is of a kind rendered for agents.
*
* @private
*/
function _isRenderable(d: ts.Declaration): d is SdkDeclaration {
return (
ts.isInterfaceDeclaration(d) ||
ts.isTypeAliasDeclaration(d) ||
ts.isEnumDeclaration(d)
);
}
/**
* Whether a class member is a public, non-implementation API method.
*
* @private
*/
function _isApiMethod(m: ts.ClassElement): m is NamedMethod {
if (!ts.isMethodDeclaration(m) || !ts.isIdentifier(m.name)) {
return false;
}
const name = m.name.text;
return (
!(ts.getCombinedModifierFlags(m) & HIDDEN_MODIFIERS) &&
!BINDING_SOURCES.some((s) => name.endsWith(s))
);
}
/**
* Describe a method parameter.
*
* @private
*/
function _param(p: ts.ParameterDeclaration, sf: ts.SourceFile): Param {
return {
name: p.name.getText(sf),
type: p.type ? _compact(p.type.getText(sf)) : "any",
optional: Boolean(p.questionToken || p.initializer),
node: p,
};
}
/**
* Example hints keyed by normalised property name.
*
* @private
*/
function _hintsByName(hint: unknown): Map<string, unknown> {
const h =
hint && typeof hint === "object" ? (hint as Record<string, unknown>) : {};
return new Map(Object.entries(h).map(([k, v]) => [normaliseName(k), v]));
}
/**
* The names of a type's required properties.
*
* @private
*/
function _requiredProperties(type: ts.Type): string[] {
return type
.getProperties()
.filter((s) => !(s.flags & ts.SymbolFlags.Optional))
.map((s) => s.name);
}
/**
* The type nodes whose SDK declarations a method's documentation shows.
*
* @private
*/
function _typeRoots(apiParams: Param[], m: ts.MethodDeclaration): ts.Node[] {
return [...apiParams.map((p) => p.node.type), m.type].filter(
(t): t is ts.TypeNode => t !== undefined,
);
}
/**
* The example body that calls a method and returns its result.
*
* @private
*/
function _exampleBody(call: string, returnType: string): string {
return /Promise<void>/.test(returnType)
? `await ${call};\nreturn { done: true };`
: `const result = await ${call};\nreturn result;`;
}
/**
* Analyse SDK declarations together with implementation bindings.
*/
export class SdkAnalyzer {
private readonly program: ts.Program;
private readonly checker: ts.TypeChecker;
private readonly typesDir: string;
private readonly printer = ts.createPrinter({ removeComments: true });
/**
* @param {String} typesIndex - path to the SDK declarations index.d.ts
*/
constructor(typesIndex: string) {
this.typesDir = path.dirname(path.resolve(typesIndex));
this.program = ts.createProgram([typesIndex], {
...sdkCompilerOptions(),
noEmit: true,
});
this.checker = this.program.getTypeChecker();
}
/**
* Whether a file is one of the SDK's declaration files.
*
* @private
*/
private _isSdkFile(fileName: string): boolean {
return path.resolve(fileName).startsWith(this.typesDir + path.sep);
}
/**
* Whether a declaration belongs to the SDK (not lib, not the runtime
* module).
*
* @private
*/
private _isSdkDecl(decl: ts.Declaration): boolean {
const file = decl.getSourceFile().fileName;
return this._isSdkFile(file) && !/[\\/]runtime\.d\.ts$/.test(file);
}
/**
* Resolve a type reference to SDK declarations.
*
* @private
*/
private _resolveRef(node: ts.TypeReferenceNode): SdkDeclaration[] {
let sym = this.checker.getSymbolAtLocation(node.typeName);
if (sym && sym.flags & ts.SymbolFlags.Alias) {
sym = this.checker.getAliasedSymbol(sym);
}
return (sym?.declarations ?? []).filter(
(d): d is SdkDeclaration => _isRenderable(d) && this._isSdkDecl(d),
);
}
/**
* The documentation comment of the symbol a name refers to.
*
* @private
*/
private _docOf(name: ts.Node | undefined): string {
const sym = name ? this.checker.getSymbolAtLocation(name) : undefined;
return ts.displayPartsToString(sym?.getDocumentationComment(this.checker));
}
/**
* The type of a property symbol, when it has a declaration to read it from.
*
* @private
*/
private _propertyType(p: ts.Symbol): ts.Type | undefined {
const decl = p.valueDeclaration ?? p.declarations?.[0];
return decl ? this.checker.getTypeOfSymbolAtLocation(p, decl) : undefined;
}
/**
* Whether a type is an object type other than an array.
*
* @private
*/
private _isPlainObject(t: ts.Type | undefined): boolean {
return (
t !== undefined &&
Boolean(t.flags & ts.TypeFlags.Object) &&
!this.checker.isArrayType(t)
);
}
/**
* Render an SDK declaration compactly with member documentation.
*
* @private
*/
private _render(decl: SdkDeclaration): TypeDeclaration {
if (ts.isInterfaceDeclaration(decl)) {
return this._renderInterface(decl);
}
if (ts.isTypeAliasDeclaration(decl)) {
return this._renderAlias(decl);
}
return {
name: decl.name.text,
declaration: this.printer
.printNode(ts.EmitHint.Unspecified, decl, decl.getSourceFile())
.trim(),
};
}
/**
* Render an interface, one documented member per line.
*
* @private
*/
private _renderInterface(decl: ts.InterfaceDeclaration): TypeDeclaration {
const sf = decl.getSourceFile();
const heritage = decl.heritageClauses
? ` ${decl.heritageClauses.map((h) => h.getText(sf)).join(" ")}`
: "";
const hidden = decl.members.length - MAX_MEMBERS;
const lines = [
`interface ${decl.name.text}${heritage} {`,
...decl.members
.slice(0, MAX_MEMBERS)
.map((m) => ` ${this._renderMember(m, sf)}`),
...(hidden > 0 ? [` // … ${hidden} more members`] : []),
"}",
];
return { name: decl.name.text, declaration: lines.join("\n") };
}
/**
* Render an interface member with its documentation as a comment.
*
* @private
*/
private _renderMember(m: ts.TypeElement, sf: ts.SourceFile): string {
const doc = _compact(this._docOf(m.name));
const text = _compact(
this.printer.printNode(ts.EmitHint.Unspecified, m, sf),
);
return `${text}${doc ? ` // ${doc.slice(0, 160)}` : ""}`;
}
/**
* Render a type alias, spelling out a union of literals.
*
* @private
*/
private _renderAlias(decl: ts.TypeAliasDeclaration): TypeDeclaration {
const type = this.checker.getTypeAtLocation(decl.name);
const body =
type.isUnion() && type.types.every((t) => t.isLiteral())
? type.types.map((t) => this.checker.typeToString(t)).join(" | ")
: _compact(decl.type.getText(decl.getSourceFile()));
return {
name: decl.name.text,
declaration: `type ${decl.name.text} = ${body};`,
};
}
/**
* Collect SDK declarations referenced from type nodes, breadth first.
*
* @private
*/
private _collectTypes(roots: ts.Node[]): TypeDeclaration[] {
const out: TypeDeclaration[] = [];
const seen = new Set<string>();
let frontier: ts.Node[] = roots;
for (
let depth = 0;
depth < MAX_TYPE_DEPTH && frontier.length > 0;
depth++
) {
const next: ts.Node[] = [];
const visit = (n: ts.Node) => {
if (ts.isTypeReferenceNode(n)) {
for (const decl of this._resolveRef(n)) {
const name = decl.name.text;
if (!seen.has(name) && out.length < MAX_REFERENCED_TYPES) {
seen.add(name);
out.push(this._render(decl));
next.push(decl);
}
}
}
ts.forEachChild(n, visit);
};
frontier.forEach(visit);
frontier = next;
}
return out;
}
/**
* Produce an example expression for a type.
*
* @private
*/
private _example(type: ts.Type, hint: unknown, depth: number): string {
const fromUnion = type.isUnion()
? this._unionExample(type, hint, depth)
: undefined;
return (
fromUnion ??
SCALAR_EXAMPLES.find((s) => type.flags & s.flags)?.render(hint) ??
this._structuredExample(type, hint, depth)
);
}
/**
* Example for a union: the hinted literal, the first literal, or an
* example of the first non-null member.
*
* @private
*/
private _unionExample(
type: ts.UnionType,
hint: unknown,
depth: number,
): string | undefined {
const nonNull = type.types.filter(
(t) => !(t.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)),
);
const literals = nonNull.filter((t) => t.isLiteral());
if (
hint !== undefined &&
literals.some((t) => (t as ts.LiteralType).value === hint)
) {
return JSON.stringify(hint);
}
if (literals.length > 0) {
return this.checker.typeToString(literals[0]);
}
// Under the SDK program's non-strict checking a union has no null or
// undefined members; the filter guards a stricter configuration.
const [first] = nonNull;
return first && this._example(first, hint, depth);
}
/**
* Example for a date, array or object type.
*
* @private
*/
private _structuredExample(
type: ts.Type,
hint: unknown,
depth: number,
): string {
if (this.checker.typeToString(type) === "Date") {
return `new Date(${JSON.stringify(typeof hint === "string" ? hint : "2026-01-31")})`;
}
if (this.checker.isArrayType(type)) {
const [elem] = this.checker.getTypeArguments(type as ts.TypeReference);
const h = Array.isArray(hint) ? hint[0] : undefined;
return elem ? `[${this._example(elem, h, depth + 1)}]` : "[]";
}
if (type.flags & ts.TypeFlags.Object && depth < 3) {
return this._objectExample(type, hint, depth);
}
return "undefined";
}
/**
* Example object literal with the type's required properties.
*
* @private
*/
private _objectExample(type: ts.Type, hint: unknown, depth: number): string {
const props = type
.getProperties()
.filter((p) => !(p.flags & ts.SymbolFlags.Optional));
if (props.length === 0) {
return "{}";
}
const hintByName = _hintsByName(hint);
const parts = props.slice(0, 12).map((p) => {
const pt = this._propertyType(p) ?? this.checker.getAnyType();
return `${p.name}: ${this._example(pt, hintByName.get(normaliseName(p.name)), depth + 1)}`;
});
return `{ ${parts.join(", ")} }`;
}
/**
* Analyse every public method of every API class.
*
* @param {Map} impl - implementation bindings from {@link scanImplementation}
* @param {Function} hintsFor - returns example hints for (method, path)
* @return {Array} analysed methods
*/
analyze(
impl: Bindings,
hintsFor: (httpMethod: string, p: string) => ExampleHints,
): AnalyzedMethod[] {
const run: AnalyzeRun = {
impl,
hintsFor,
found: new Set(),
problems: [],
methods: [],
};
for (const sf of this.program.getSourceFiles()) {
if (this._isSdkFile(sf.fileName)) {
ts.forEachChild(sf, (node) => this._analyzeClass(sf, node, run));
}
}
for (const cls of impl.keys()) {
if (!run.found.has(cls)) {
run.problems.push(
`${cls}: implementation found but no matching class in declarations`,
);
}
}
if (run.problems.length > 0) {
throw new OagMcpError(
"SDK_BINDING_AMBIGUOUS",
`Could not reliably map SDK methods to HTTP operations: ${run.problems.slice(0, 5).join("; ")}`,
{
hint: "The SDK may have been modified after generation, or generated by an unsupported version. Regenerate it with a supported OpenAPI Generator version.",
details: { problems: run.problems },
},
);
}
return run.methods.sort((a, b) =>
`${a.className}.${a.methodName}`.localeCompare(
`${b.className}.${b.methodName}`,
),
);
}
/**
* Analyse the public methods of a declared class that has bindings.
*
* @private
*/
private _analyzeClass(sf: ts.SourceFile, node: ts.Node, run: AnalyzeRun) {
if (!ts.isClassDeclaration(node) || !node.name) {
return;
}
const cls = node.name.text;
const bindings = run.impl.get(cls);
if (!bindings) {
return;
}
run.found.add(cls);
for (const m of node.members.filter(_isApiMethod)) {
const binding = bindings.get(m.name.text);
if (binding) {
run.methods.push(
this._method(
sf,
cls,
m,
binding,
run.hintsFor(binding.httpMethod, binding.path),
),
);
} else {
run.problems.push(
`${cls}.${m.name.text}: no HTTP method/path found in its RequestOpts/Raw implementation`,
);
}
}
}
/**
* Analyse a single method declaration.
*
* @private
*/
private _method(
sf: ts.SourceFile,
cls: string,
m: NamedMethod,
binding: ImplBinding,
hints: ExampleHints,
): AnalyzedMethod {
const name = m.name.text;
const params = m.parameters.map((p) => _param(p, sf));
const apiParams = params.filter((p) => p.name !== "initOverrides");
const returnType = m.type
? _compact(m.type.getText(sf))
: "Promise<unknown>";
const args = this._requestArgs(apiParams, hints);
const referencedTypes = this._collectTypes(_typeRoots(apiParams, m));
return {
className: cls,
methodName: name,
httpMethod: binding.httpMethod,
path: binding.path,
signature: _compact(
this.printer.printNode(ts.EmitHint.Unspecified, m, sf),
).replace(/;$/, ""),
parameters: params.map(({ name: n, type, optional }) => ({
name: n,
type,
optional,
})),
requestStyle: args.requestStyle,
inputType: args.inputType,
returnType,
referencedTypes: referencedTypes.filter(
(t) => t.name !== args.inputType?.name,
),
requiredArguments: args.requiredArguments,
documentation: this._docOf(m.name).trim(),
deprecated: ts
.getJSDocTags(m)
.some((t) => t.tagName.text === "deprecated"),
example: _exampleBody(
`sdk.${cls}.${name}(${args.exampleArgs})`,
returnType,
),
};
}
/**
* How a method takes its arguments: one request object, positional
* arguments, or none.
*
* @private
*/
private _requestArgs(apiParams: Param[], hints: ExampleHints): RequestArgs {
if (apiParams.length === 1 && apiParams[0].name === "requestParameters") {
return this._objectArgs(apiParams[0], hints);
}
if (apiParams.length > 0) {
return this._positionalArgs(apiParams, hints);
}
return {
requestStyle: "none",
inputType: undefined,
requiredArguments: [],
exampleArgs: "",
};
}
/**
* Arguments of a method taking a single request object.
*
* @private
*/
private _objectArgs(p: Param, hints: ExampleHints): RequestArgs {
const type = this.checker.getNonNullableType(
this.checker.getTypeAtLocation(p.node),
);
const ref =
p.node.type && ts.isTypeReferenceNode(p.node.type)
? this._resolveRef(p.node.type)[0]
: undefined;
return {
requestStyle: "object",
inputType: ref ? this._render(ref) : undefined,
requiredArguments: _requiredProperties(type),
exampleArgs: this._example(type, this._hintObject(type, hints), 0),
};
}
/**
* Arguments of a method taking positional parameters.
*
* @private
*/
private _positionalArgs(
apiParams: Param[],
hints: ExampleHints,
): RequestArgs {
const required = apiParams.filter((p) => !p.optional);
return {
requestStyle: "positional",
inputType: undefined,
requiredArguments: required.map((p) => p.name),
exampleArgs: required
.map((p) => {
const t = this.checker.getTypeAtLocation(p.node);
const hint =
hints[normaliseName(p.name)] ??
(this._isPlainObject(t) ? hints.__body : undefined);
return this._example(t, hint, 0);
})
.join(", "),
};
}
/**
* Map example hints onto the properties of a request type.
*
* @private
*/
private _hintObject(
type: ts.Type,
hints: ExampleHints,
): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const p of type.getProperties()) {
const h = hints[normaliseName(p.name)];
if (h !== undefined) {
out[p.name] = h;
} else if (
hints.__body !== undefined &&
// The body parameter is usually the only object-typed property.
this._isPlainObject(this._propertyType(p))
) {
out[p.name] = hints.__body;
}
}
return out;
}
}
|