explorbot 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -26
- package/bin/explorbot-cli.ts +680 -0
- package/boat/api-tester/src/ai/chief/styles.ts +15 -0
- package/boat/api-tester/src/ai/chief.ts +335 -0
- package/boat/api-tester/src/ai/curler-tools.ts +278 -0
- package/boat/api-tester/src/ai/curler.ts +306 -0
- package/boat/api-tester/src/api-client.ts +28 -0
- package/boat/api-tester/src/apibot.ts +203 -0
- package/boat/api-tester/src/cli.ts +301 -0
- package/boat/api-tester/src/config.ts +190 -0
- package/dist/bin/explorbot-cli.js +23 -101
- package/dist/boat/api-tester/bin/apibot-cli.js +0 -1
- package/dist/boat/api-tester/src/ai/chief/styles.js +0 -1
- package/dist/boat/api-tester/src/ai/chief.js +0 -1
- package/dist/boat/api-tester/src/ai/curler-tools.js +0 -1
- package/dist/boat/api-tester/src/ai/curler.js +0 -1
- package/dist/boat/api-tester/src/api-client.js +0 -1
- package/dist/boat/api-tester/src/apibot.js +0 -1
- package/dist/boat/api-tester/src/cli.js +0 -1
- package/dist/boat/api-tester/src/config.js +0 -1
- package/dist/src/action-result.js +0 -1
- package/dist/src/action.js +14 -12
- package/dist/src/activity.js +0 -1
- package/dist/src/ai/agent.js +0 -1
- package/dist/src/ai/bosun.js +0 -1
- package/dist/src/ai/captain/idle-mode.js +0 -1
- package/dist/src/ai/captain/mixin.js +0 -1
- package/dist/src/ai/captain/test-mode.js +0 -1
- package/dist/src/ai/captain/web-mode.js +0 -1
- package/dist/src/ai/captain.js +0 -1
- package/dist/src/ai/conversation.js +0 -1
- package/dist/src/ai/experience-compactor.js +0 -1
- package/dist/src/ai/fisherman-tools.js +0 -1
- package/dist/src/ai/fisherman.js +0 -1
- package/dist/src/ai/historian.js +0 -1
- package/dist/src/ai/navigator.js +0 -1
- package/dist/src/ai/pilot.js +0 -1
- package/dist/src/ai/planner/session-dedup.js +0 -1
- package/dist/src/ai/planner/styles.js +0 -1
- package/dist/src/ai/planner/subpages.js +42 -7
- package/dist/src/ai/planner.js +15 -4
- package/dist/src/ai/provider.js +0 -1
- package/dist/src/ai/quartermaster.js +0 -1
- package/dist/src/ai/researcher/cache.js +13 -9
- package/dist/src/ai/researcher/coordinates.js +4 -3
- package/dist/src/ai/researcher/deep-analysis.js +16 -20
- package/dist/src/ai/researcher/fingerprint-worker.js +0 -1
- package/dist/src/ai/researcher/focus.js +0 -1
- package/dist/src/ai/researcher/locators.js +1 -2
- package/dist/src/ai/researcher/mixin.js +0 -1
- package/dist/src/ai/researcher/parser.js +4 -4
- package/dist/src/ai/researcher/research-result.js +2 -1
- package/dist/src/ai/researcher.js +6 -6
- package/dist/src/ai/rules.js +0 -1
- package/dist/src/ai/task-agent.js +0 -1
- package/dist/src/ai/tester.js +0 -1
- package/dist/src/ai/tools.js +4 -1
- package/dist/src/api/api-client.js +0 -1
- package/dist/src/api/request-result.js +0 -1
- package/dist/src/api/request-store.js +0 -1
- package/dist/src/api/spec-reader.js +0 -1
- package/dist/src/api/xhr-capture.js +0 -1
- package/dist/src/browser-server.js +0 -1
- package/dist/src/command-handler.js +0 -1
- package/dist/src/commands/add-rule-command.js +0 -1
- package/dist/src/commands/base-command.js +0 -1
- package/dist/src/commands/clean-command.js +0 -1
- package/dist/src/commands/context-aria-command.js +0 -1
- package/dist/src/commands/context-command.js +2 -3
- package/dist/src/commands/context-data-command.js +0 -1
- package/dist/src/commands/context-experience-command.js +0 -1
- package/dist/src/commands/context-html-command.js +0 -1
- package/dist/src/commands/context-knowledge-command.js +0 -1
- package/dist/src/commands/debug-command.js +0 -1
- package/dist/src/commands/drill-command.js +0 -1
- package/dist/src/commands/exit-command.js +0 -1
- package/dist/src/commands/explore-command.js +3 -3
- package/dist/src/commands/freesail-command.js +0 -1
- package/dist/src/commands/help-command.js +0 -1
- package/dist/src/commands/index.js +0 -1
- package/dist/src/commands/init-command.js +117 -0
- package/dist/src/commands/knows-command.js +0 -1
- package/dist/src/commands/learn-command.js +0 -1
- package/dist/src/commands/navigate-command.js +0 -1
- package/dist/src/commands/path-command.js +0 -1
- package/dist/src/commands/plan-clear-command.js +0 -1
- package/dist/src/commands/plan-command.js +6 -2
- package/dist/src/commands/plan-edit-command.js +0 -1
- package/dist/src/commands/plan-load-command.js +0 -1
- package/dist/src/commands/plan-reload-command.js +0 -1
- package/dist/src/commands/plan-save-command.js +0 -1
- package/dist/src/commands/research-command.js +0 -1
- package/dist/src/commands/start-command.js +0 -1
- package/dist/src/commands/status-command.js +0 -1
- package/dist/src/commands/test-command.js +0 -1
- package/dist/src/components/ActivityPane.js +0 -1
- package/dist/src/components/AddKnowledge.js +0 -1
- package/dist/src/components/AddRule.js +0 -1
- package/dist/src/components/App.js +0 -1
- package/dist/src/components/Autocomplete.js +0 -1
- package/dist/src/components/InputPane.js +0 -1
- package/dist/src/components/InputReadline.js +0 -1
- package/dist/src/components/LogPane.js +0 -1
- package/dist/src/components/PlanEditor.js +0 -1
- package/dist/src/components/PlanPane.js +0 -1
- package/dist/src/components/SessionTimer.js +0 -1
- package/dist/src/components/StateTransitionPane.js +0 -1
- package/dist/src/components/StatusPane.js +0 -1
- package/dist/src/components/TaskPane.js +0 -1
- package/dist/src/components/Welcome.js +0 -1
- package/dist/src/components/WelcomeChecklist.js +0 -1
- package/dist/src/components/WelcomeCommands.js +0 -1
- package/dist/src/components/autocomplete-store.js +0 -1
- package/dist/src/components/parse-keypress.js +0 -1
- package/dist/src/config.js +0 -1
- package/dist/src/execution-controller.js +0 -1
- package/dist/src/experience-tracker.js +0 -1
- package/dist/src/explorbot.js +1 -2
- package/dist/src/explorer.js +58 -17
- package/dist/src/index.js +0 -1
- package/dist/src/knowledge-tracker.js +2 -2
- package/dist/src/observability.js +0 -1
- package/dist/src/reporter.js +0 -1
- package/dist/src/state-manager.js +0 -1
- package/dist/src/stats.js +0 -1
- package/dist/src/test-plan.js +0 -1
- package/dist/src/utils/aria.js +0 -1
- package/dist/src/utils/cli-name.js +16 -0
- package/dist/src/utils/code-extractor.js +0 -1
- package/dist/src/utils/context-formatter.js +0 -1
- package/dist/src/utils/error-page.js +0 -1
- package/dist/src/utils/expandable.js +0 -1
- package/dist/src/utils/hooks-runner.js +0 -1
- package/dist/src/utils/html-diff.js +0 -1
- package/dist/src/utils/html.js +0 -1
- package/dist/src/utils/logger.js +0 -1
- package/dist/src/utils/loop.js +0 -1
- package/dist/src/utils/markdown-parser.js +0 -1
- package/dist/src/utils/markdown-query.js +0 -1
- package/dist/src/utils/markdown-terminal.js +0 -1
- package/dist/src/utils/research-parser.js +0 -1
- package/dist/src/utils/retry.js +0 -1
- package/dist/src/utils/rules-loader.js +0 -1
- package/dist/src/utils/strings.js +0 -1
- package/dist/src/utils/test-plan-markdown.js +0 -1
- package/dist/src/utils/throttle.js +0 -1
- package/dist/src/utils/unique-names.js +0 -1
- package/dist/src/utils/url-matcher.js +0 -1
- package/dist/src/utils/web-element.js +6 -5
- package/dist/src/utils/xpath.js +0 -1
- package/package.json +28 -4
- package/src/action-result.ts +694 -0
- package/src/action.ts +449 -0
- package/src/activity.ts +111 -0
- package/src/ai/agent.ts +3 -0
- package/src/ai/bosun.ts +557 -0
- package/src/ai/captain/idle-mode.ts +116 -0
- package/src/ai/captain/mixin.ts +22 -0
- package/src/ai/captain/test-mode.ts +262 -0
- package/src/ai/captain/web-mode.ts +136 -0
- package/src/ai/captain.ts +504 -0
- package/src/ai/conversation.ts +205 -0
- package/src/ai/experience-compactor.ts +284 -0
- package/src/ai/fisherman-tools.ts +181 -0
- package/src/ai/fisherman.ts +223 -0
- package/src/ai/historian.ts +457 -0
- package/src/ai/navigator.ts +572 -0
- package/src/ai/pilot.ts +776 -0
- package/src/ai/planner/session-dedup.ts +35 -0
- package/src/ai/planner/styles.ts +17 -0
- package/src/ai/planner/subpages.ts +171 -0
- package/src/ai/planner.ts +549 -0
- package/src/ai/provider.ts +613 -0
- package/src/ai/quartermaster.ts +286 -0
- package/src/ai/researcher/cache.ts +109 -0
- package/src/ai/researcher/coordinates.ts +239 -0
- package/src/ai/researcher/deep-analysis.ts +412 -0
- package/src/ai/researcher/fingerprint-worker.ts +59 -0
- package/src/ai/researcher/focus.ts +42 -0
- package/src/ai/researcher/locators.ts +282 -0
- package/src/ai/researcher/mixin.ts +4 -0
- package/src/ai/researcher/parser.ts +186 -0
- package/src/ai/researcher/research-result.ts +116 -0
- package/src/ai/researcher.ts +858 -0
- package/src/ai/rules.ts +376 -0
- package/src/ai/task-agent.ts +141 -0
- package/src/ai/tester.ts +939 -0
- package/src/ai/tools.ts +1122 -0
- package/src/api/api-client.ts +109 -0
- package/src/api/request-result.ts +212 -0
- package/src/api/request-store.ts +130 -0
- package/src/api/spec-reader.ts +174 -0
- package/src/api/xhr-capture.ts +100 -0
- package/src/browser-server.ts +74 -0
- package/src/command-handler.ts +454 -0
- package/src/commands/add-rule-command.ts +63 -0
- package/src/commands/base-command.ts +27 -0
- package/src/commands/clean-command.ts +73 -0
- package/src/commands/context-aria-command.ts +22 -0
- package/src/commands/context-command.ts +67 -0
- package/src/commands/context-data-command.ts +30 -0
- package/src/commands/context-experience-command.ts +48 -0
- package/src/commands/context-html-command.ts +33 -0
- package/src/commands/context-knowledge-command.ts +43 -0
- package/src/commands/debug-command.ts +13 -0
- package/src/commands/drill-command.ts +34 -0
- package/src/commands/exit-command.ts +32 -0
- package/src/commands/explore-command.ts +129 -0
- package/src/commands/freesail-command.ts +95 -0
- package/src/commands/help-command.ts +8 -0
- package/src/commands/index.ts +69 -0
- package/src/commands/init-command.ts +131 -0
- package/src/commands/knows-command.ts +68 -0
- package/src/commands/learn-command.ts +44 -0
- package/src/commands/navigate-command.ts +18 -0
- package/src/commands/path-command.ts +83 -0
- package/src/commands/plan-clear-command.ts +14 -0
- package/src/commands/plan-command.ts +46 -0
- package/src/commands/plan-edit-command.ts +9 -0
- package/src/commands/plan-load-command.ts +18 -0
- package/src/commands/plan-reload-command.ts +28 -0
- package/src/commands/plan-save-command.ts +25 -0
- package/src/commands/research-command.ts +45 -0
- package/src/commands/start-command.ts +13 -0
- package/src/commands/status-command.tsx +23 -0
- package/src/commands/test-command.ts +84 -0
- package/src/components/ActivityPane.tsx +80 -0
- package/src/components/AddKnowledge.tsx +169 -0
- package/src/components/AddRule.tsx +174 -0
- package/src/components/App.tsx +377 -0
- package/src/components/Autocomplete.tsx +63 -0
- package/src/components/InputPane.tsx +259 -0
- package/src/components/InputReadline.tsx +704 -0
- package/src/components/LogPane.tsx +187 -0
- package/src/components/PlanEditor.tsx +150 -0
- package/src/components/PlanPane.tsx +71 -0
- package/src/components/SessionTimer.tsx +35 -0
- package/src/components/StateTransitionPane.tsx +149 -0
- package/src/components/StatusPane.tsx +62 -0
- package/src/components/TaskPane.tsx +119 -0
- package/src/components/Welcome.tsx +83 -0
- package/src/components/WelcomeChecklist.tsx +118 -0
- package/src/components/WelcomeCommands.tsx +102 -0
- package/src/components/autocomplete-store.ts +35 -0
- package/src/components/parse-keypress.ts +170 -0
- package/src/config.ts +491 -0
- package/src/execution-controller.ts +109 -0
- package/src/experience-tracker.ts +350 -0
- package/src/explorbot.ts +405 -0
- package/src/explorer.ts +760 -0
- package/src/index.tsx +62 -0
- package/src/knowledge-tracker.ts +230 -0
- package/src/observability.ts +150 -0
- package/src/reporter.ts +224 -0
- package/src/state-manager.ts +556 -0
- package/src/stats.ts +53 -0
- package/src/test-plan.ts +432 -0
- package/src/utils/aria.ts +629 -0
- package/src/utils/cli-name.ts +13 -0
- package/src/utils/code-extractor.ts +22 -0
- package/src/utils/context-formatter.ts +239 -0
- package/src/utils/error-page.ts +23 -0
- package/src/utils/expandable.ts +38 -0
- package/src/utils/hooks-runner.ts +79 -0
- package/src/utils/html-diff.ts +918 -0
- package/src/utils/html.ts +1316 -0
- package/src/utils/logger.ts +534 -0
- package/src/utils/loop.ts +176 -0
- package/src/utils/markdown-parser.ts +127 -0
- package/src/utils/markdown-query.ts +466 -0
- package/src/utils/markdown-terminal.ts +43 -0
- package/src/utils/research-parser.ts +11 -0
- package/src/utils/retry.ts +73 -0
- package/src/utils/rules-loader.ts +118 -0
- package/src/utils/strings.ts +13 -0
- package/src/utils/test-plan-markdown.ts +332 -0
- package/src/utils/throttle.ts +18 -0
- package/src/utils/unique-names.ts +14 -0
- package/src/utils/url-matcher.ts +45 -0
- package/src/utils/web-element.ts +147 -0
- package/src/utils/xpath.ts +129 -0
- package/dist/bin/explorbot-cli.js.map +0 -1
- package/dist/boat/api-tester/bin/apibot-cli.js.map +0 -1
- package/dist/boat/api-tester/example/apibot.config.js +0 -31
- package/dist/boat/api-tester/example/apibot.config.js.map +0 -1
- package/dist/boat/api-tester/src/ai/chief/styles.js.map +0 -1
- package/dist/boat/api-tester/src/ai/chief.js.map +0 -1
- package/dist/boat/api-tester/src/ai/curler-tools.js.map +0 -1
- package/dist/boat/api-tester/src/ai/curler.js.map +0 -1
- package/dist/boat/api-tester/src/api-client.js.map +0 -1
- package/dist/boat/api-tester/src/apibot.js.map +0 -1
- package/dist/boat/api-tester/src/cli.js.map +0 -1
- package/dist/boat/api-tester/src/config.js.map +0 -1
- package/dist/prompts/audit-rules.md +0 -124
- package/dist/src/action-result.js.map +0 -1
- package/dist/src/action.js.map +0 -1
- package/dist/src/activity.js.map +0 -1
- package/dist/src/ai/agent.js.map +0 -1
- package/dist/src/ai/bosun.js.map +0 -1
- package/dist/src/ai/captain/idle-mode.js.map +0 -1
- package/dist/src/ai/captain/mixin.js.map +0 -1
- package/dist/src/ai/captain/test-mode.js.map +0 -1
- package/dist/src/ai/captain/web-mode.js.map +0 -1
- package/dist/src/ai/captain.js.map +0 -1
- package/dist/src/ai/conversation.js.map +0 -1
- package/dist/src/ai/experience-compactor.js.map +0 -1
- package/dist/src/ai/fisherman-tools.js.map +0 -1
- package/dist/src/ai/fisherman.js.map +0 -1
- package/dist/src/ai/historian.js.map +0 -1
- package/dist/src/ai/navigator.js.map +0 -1
- package/dist/src/ai/pilot.js.map +0 -1
- package/dist/src/ai/planner/session-dedup.js.map +0 -1
- package/dist/src/ai/planner/styles.js.map +0 -1
- package/dist/src/ai/planner/subpages.js.map +0 -1
- package/dist/src/ai/planner.js.map +0 -1
- package/dist/src/ai/provider.js.map +0 -1
- package/dist/src/ai/quartermaster.js.map +0 -1
- package/dist/src/ai/researcher/cache.js.map +0 -1
- package/dist/src/ai/researcher/coordinates.js.map +0 -1
- package/dist/src/ai/researcher/deep-analysis.js.map +0 -1
- package/dist/src/ai/researcher/fingerprint-worker.js.map +0 -1
- package/dist/src/ai/researcher/focus.js.map +0 -1
- package/dist/src/ai/researcher/locators.js.map +0 -1
- package/dist/src/ai/researcher/mixin.js.map +0 -1
- package/dist/src/ai/researcher/parser.js.map +0 -1
- package/dist/src/ai/researcher/research-result.js.map +0 -1
- package/dist/src/ai/researcher.js.map +0 -1
- package/dist/src/ai/rules.js.map +0 -1
- package/dist/src/ai/task-agent.js.map +0 -1
- package/dist/src/ai/tester.js.map +0 -1
- package/dist/src/ai/tools.js.map +0 -1
- package/dist/src/api/api-client.js.map +0 -1
- package/dist/src/api/request-result.js.map +0 -1
- package/dist/src/api/request-store.js.map +0 -1
- package/dist/src/api/spec-reader.js.map +0 -1
- package/dist/src/api/xhr-capture.js.map +0 -1
- package/dist/src/browser-server.js.map +0 -1
- package/dist/src/command-handler.js.map +0 -1
- package/dist/src/commands/add-rule-command.js.map +0 -1
- package/dist/src/commands/base-command.js.map +0 -1
- package/dist/src/commands/clean-command.js.map +0 -1
- package/dist/src/commands/context-aria-command.js.map +0 -1
- package/dist/src/commands/context-command.js.map +0 -1
- package/dist/src/commands/context-data-command.js.map +0 -1
- package/dist/src/commands/context-experience-command.js.map +0 -1
- package/dist/src/commands/context-html-command.js.map +0 -1
- package/dist/src/commands/context-knowledge-command.js.map +0 -1
- package/dist/src/commands/debug-command.js.map +0 -1
- package/dist/src/commands/drill-command.js.map +0 -1
- package/dist/src/commands/exit-command.js.map +0 -1
- package/dist/src/commands/explore-command.js.map +0 -1
- package/dist/src/commands/freesail-command.js.map +0 -1
- package/dist/src/commands/help-command.js.map +0 -1
- package/dist/src/commands/index.js.map +0 -1
- package/dist/src/commands/knows-command.js.map +0 -1
- package/dist/src/commands/learn-command.js.map +0 -1
- package/dist/src/commands/navigate-command.js.map +0 -1
- package/dist/src/commands/path-command.js.map +0 -1
- package/dist/src/commands/plan-clear-command.js.map +0 -1
- package/dist/src/commands/plan-command.js.map +0 -1
- package/dist/src/commands/plan-edit-command.js.map +0 -1
- package/dist/src/commands/plan-load-command.js.map +0 -1
- package/dist/src/commands/plan-reload-command.js.map +0 -1
- package/dist/src/commands/plan-save-command.js.map +0 -1
- package/dist/src/commands/research-command.js.map +0 -1
- package/dist/src/commands/start-command.js.map +0 -1
- package/dist/src/commands/status-command.js.map +0 -1
- package/dist/src/commands/test-command.js.map +0 -1
- package/dist/src/components/ActivityPane.js.map +0 -1
- package/dist/src/components/AddKnowledge.js.map +0 -1
- package/dist/src/components/AddRule.js.map +0 -1
- package/dist/src/components/App.js.map +0 -1
- package/dist/src/components/Autocomplete.js.map +0 -1
- package/dist/src/components/InputPane.js.map +0 -1
- package/dist/src/components/InputReadline.js.map +0 -1
- package/dist/src/components/LogPane.js.map +0 -1
- package/dist/src/components/PlanEditor.js.map +0 -1
- package/dist/src/components/PlanPane.js.map +0 -1
- package/dist/src/components/SessionTimer.js.map +0 -1
- package/dist/src/components/StateTransitionPane.js.map +0 -1
- package/dist/src/components/StatusPane.js.map +0 -1
- package/dist/src/components/TaskPane.js.map +0 -1
- package/dist/src/components/Welcome.js.map +0 -1
- package/dist/src/components/WelcomeChecklist.js.map +0 -1
- package/dist/src/components/WelcomeCommands.js.map +0 -1
- package/dist/src/components/autocomplete-store.js.map +0 -1
- package/dist/src/components/parse-keypress.js.map +0 -1
- package/dist/src/config.js.map +0 -1
- package/dist/src/execution-controller.js.map +0 -1
- package/dist/src/experience-tracker.js.map +0 -1
- package/dist/src/explorbot.js.map +0 -1
- package/dist/src/explorer.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/knowledge-tracker.js.map +0 -1
- package/dist/src/observability.js.map +0 -1
- package/dist/src/reporter.js.map +0 -1
- package/dist/src/state-manager.js.map +0 -1
- package/dist/src/stats.js.map +0 -1
- package/dist/src/test-plan.js.map +0 -1
- package/dist/src/utils/aria.js.map +0 -1
- package/dist/src/utils/code-extractor.js.map +0 -1
- package/dist/src/utils/context-formatter.js.map +0 -1
- package/dist/src/utils/error-page.js.map +0 -1
- package/dist/src/utils/expandable.js.map +0 -1
- package/dist/src/utils/hooks-runner.js.map +0 -1
- package/dist/src/utils/html-diff.js.map +0 -1
- package/dist/src/utils/html.js.map +0 -1
- package/dist/src/utils/logger.js.map +0 -1
- package/dist/src/utils/loop.js.map +0 -1
- package/dist/src/utils/markdown-parser.js.map +0 -1
- package/dist/src/utils/markdown-query.js.map +0 -1
- package/dist/src/utils/markdown-terminal.js.map +0 -1
- package/dist/src/utils/research-parser.js.map +0 -1
- package/dist/src/utils/retry.js.map +0 -1
- package/dist/src/utils/rules-loader.js.map +0 -1
- package/dist/src/utils/strings.js.map +0 -1
- package/dist/src/utils/test-plan-markdown.js.map +0 -1
- package/dist/src/utils/throttle.js.map +0 -1
- package/dist/src/utils/unique-names.js.map +0 -1
- package/dist/src/utils/url-matcher.js.map +0 -1
- package/dist/src/utils/web-element.js.map +0 -1
- package/dist/src/utils/xpath.js.map +0 -1
- package/prompts/audit-rules.md +0 -124
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { RequestResult, generateRequestId, resetRequestCounter } from './request-result.ts';
|
|
2
|
+
|
|
3
|
+
export class ApiClient {
|
|
4
|
+
private baseEndpoint: string;
|
|
5
|
+
private defaultHeaders: Record<string, string>;
|
|
6
|
+
|
|
7
|
+
constructor(baseEndpoint: string, defaultHeaders: Record<string, string> = {}) {
|
|
8
|
+
this.baseEndpoint = baseEndpoint.replace(/\/$/, '');
|
|
9
|
+
this.defaultHeaders = defaultHeaders;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
setHeaders(headers: Record<string, string>): void {
|
|
13
|
+
Object.assign(this.defaultHeaders, headers);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getHeaders(): Record<string, string> {
|
|
17
|
+
return { ...this.defaultHeaders };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getBaseEndpoint(): string {
|
|
21
|
+
return this.baseEndpoint;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async request(opts: {
|
|
25
|
+
method: string;
|
|
26
|
+
path: string;
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
body?: any;
|
|
29
|
+
queryParams?: Record<string, string>;
|
|
30
|
+
}): Promise<RequestResult> {
|
|
31
|
+
const method = opts.method.toUpperCase();
|
|
32
|
+
const urlPath = opts.path.startsWith('/') ? opts.path : `/${opts.path}`;
|
|
33
|
+
let fullUrl = `${this.baseEndpoint}${urlPath}`;
|
|
34
|
+
|
|
35
|
+
if (opts.queryParams && Object.keys(opts.queryParams).length > 0) {
|
|
36
|
+
const params = new URLSearchParams(opts.queryParams);
|
|
37
|
+
fullUrl += `?${params.toString()}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const headers: Record<string, string> = {
|
|
41
|
+
...this.defaultHeaders,
|
|
42
|
+
...opts.headers,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const fetchOptions: RequestInit = { method, headers };
|
|
46
|
+
|
|
47
|
+
if (opts.body && !['GET', 'HEAD'].includes(method)) {
|
|
48
|
+
if (typeof opts.body === 'string') {
|
|
49
|
+
fetchOptions.body = opts.body;
|
|
50
|
+
} else {
|
|
51
|
+
fetchOptions.body = JSON.stringify(opts.body);
|
|
52
|
+
if (!headers['Content-Type'] && !headers['content-type']) {
|
|
53
|
+
headers['Content-Type'] = 'application/json';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const id = generateRequestId(method, urlPath);
|
|
59
|
+
const start = performance.now();
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch(fullUrl, fetchOptions);
|
|
63
|
+
const rawBody = await response.text();
|
|
64
|
+
const timing = Math.round(performance.now() - start);
|
|
65
|
+
|
|
66
|
+
const responseHeaders: Record<string, string> = {};
|
|
67
|
+
response.headers.forEach((value, key) => {
|
|
68
|
+
responseHeaders[key] = value;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const result = new RequestResult({
|
|
72
|
+
id,
|
|
73
|
+
method,
|
|
74
|
+
path: urlPath,
|
|
75
|
+
fullUrl,
|
|
76
|
+
requestHeaders: headers,
|
|
77
|
+
requestBody: opts.body,
|
|
78
|
+
status: response.status,
|
|
79
|
+
statusText: response.statusText,
|
|
80
|
+
responseHeaders,
|
|
81
|
+
timing,
|
|
82
|
+
timestamp: new Date(),
|
|
83
|
+
});
|
|
84
|
+
result.rawResponseBodyValue = rawBody;
|
|
85
|
+
return result;
|
|
86
|
+
} catch (err: any) {
|
|
87
|
+
const result = new RequestResult({
|
|
88
|
+
id,
|
|
89
|
+
method,
|
|
90
|
+
path: urlPath,
|
|
91
|
+
fullUrl,
|
|
92
|
+
requestHeaders: headers,
|
|
93
|
+
requestBody: opts.body,
|
|
94
|
+
status: 0,
|
|
95
|
+
statusText: 'Network Error',
|
|
96
|
+
responseHeaders: {},
|
|
97
|
+
timing: Math.round(performance.now() - start),
|
|
98
|
+
timestamp: new Date(),
|
|
99
|
+
error: err.message || String(err),
|
|
100
|
+
});
|
|
101
|
+
result.rawResponseBodyValue = '';
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static resetCounter(): void {
|
|
107
|
+
resetRequestCounter();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { sanitizeFilename } from '../utils/strings.ts';
|
|
4
|
+
|
|
5
|
+
let requestCounter = 0;
|
|
6
|
+
|
|
7
|
+
export function generateRequestId(method: string, urlPath: string, prefix = ''): string {
|
|
8
|
+
requestCounter++;
|
|
9
|
+
const num = String(requestCounter).padStart(3, '0');
|
|
10
|
+
const sanitized = sanitizeFilename(urlPath.replace(/^\//, ''));
|
|
11
|
+
return `${prefix}${num}_${method}_${sanitized}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function resetRequestCounter(): void {
|
|
15
|
+
requestCounter = 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class RequestResult {
|
|
19
|
+
id: string;
|
|
20
|
+
method: string;
|
|
21
|
+
path: string;
|
|
22
|
+
fullUrl: string;
|
|
23
|
+
requestHeaders: Record<string, string>;
|
|
24
|
+
requestBody?: any;
|
|
25
|
+
status: number;
|
|
26
|
+
statusText: string;
|
|
27
|
+
responseHeaders: Record<string, string>;
|
|
28
|
+
timing: number;
|
|
29
|
+
timestamp: Date;
|
|
30
|
+
error?: string;
|
|
31
|
+
|
|
32
|
+
requestFile = '';
|
|
33
|
+
responseFile = '';
|
|
34
|
+
|
|
35
|
+
private _rawResponseBody?: string;
|
|
36
|
+
|
|
37
|
+
constructor(opts: {
|
|
38
|
+
id: string;
|
|
39
|
+
method: string;
|
|
40
|
+
path: string;
|
|
41
|
+
fullUrl: string;
|
|
42
|
+
requestHeaders: Record<string, string>;
|
|
43
|
+
requestBody?: any;
|
|
44
|
+
status: number;
|
|
45
|
+
statusText: string;
|
|
46
|
+
responseHeaders: Record<string, string>;
|
|
47
|
+
timing: number;
|
|
48
|
+
timestamp: Date;
|
|
49
|
+
error?: string;
|
|
50
|
+
}) {
|
|
51
|
+
this.id = opts.id;
|
|
52
|
+
this.method = opts.method;
|
|
53
|
+
this.path = opts.path;
|
|
54
|
+
this.fullUrl = opts.fullUrl;
|
|
55
|
+
this.requestHeaders = opts.requestHeaders;
|
|
56
|
+
this.requestBody = opts.requestBody;
|
|
57
|
+
this.status = opts.status;
|
|
58
|
+
this.statusText = opts.statusText;
|
|
59
|
+
this.responseHeaders = opts.responseHeaders;
|
|
60
|
+
this.timing = opts.timing;
|
|
61
|
+
this.timestamp = opts.timestamp;
|
|
62
|
+
this.error = opts.error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get rawResponseBody(): string {
|
|
66
|
+
if (this._rawResponseBody !== undefined) return this._rawResponseBody;
|
|
67
|
+
if (this.responseFile && existsSync(this.responseFile)) {
|
|
68
|
+
this._rawResponseBody = readFileSync(this.responseFile, 'utf8');
|
|
69
|
+
return this._rawResponseBody;
|
|
70
|
+
}
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
set rawResponseBodyValue(value: string) {
|
|
75
|
+
this._rawResponseBody = value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get responseBody(): any {
|
|
79
|
+
const raw = this.rawResponseBody;
|
|
80
|
+
if (!raw) return null;
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(raw);
|
|
83
|
+
} catch {
|
|
84
|
+
return raw;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
save(outputDir: string): void {
|
|
89
|
+
const requestsDir = path.join(outputDir, 'requests');
|
|
90
|
+
if (!existsSync(requestsDir)) {
|
|
91
|
+
mkdirSync(requestsDir, { recursive: true });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this.requestFile = path.join(requestsDir, `${this.id}.request.yaml`);
|
|
95
|
+
this.responseFile = path.join(requestsDir, `${this.id}.response.json`);
|
|
96
|
+
|
|
97
|
+
const headerLines = Object.entries(this.requestHeaders)
|
|
98
|
+
.map(([k, v]) => ` ${k}: ${v}`)
|
|
99
|
+
.join('\n');
|
|
100
|
+
|
|
101
|
+
const responseHeaderLines = Object.entries(this.responseHeaders)
|
|
102
|
+
.map(([k, v]) => ` ${k}: ${v}`)
|
|
103
|
+
.join('\n');
|
|
104
|
+
|
|
105
|
+
let yaml = '---\n';
|
|
106
|
+
yaml += `method: ${this.method}\n`;
|
|
107
|
+
yaml += `url: ${this.path}\n`;
|
|
108
|
+
yaml += `fullUrl: ${this.fullUrl}\n`;
|
|
109
|
+
yaml += `headers:\n${headerLines}\n`;
|
|
110
|
+
yaml += `status: ${this.status}\n`;
|
|
111
|
+
yaml += `statusText: ${this.statusText}\n`;
|
|
112
|
+
yaml += `responseHeaders:\n${responseHeaderLines}\n`;
|
|
113
|
+
yaml += `timing: ${this.timing}\n`;
|
|
114
|
+
yaml += `timestamp: ${this.timestamp.toISOString()}\n`;
|
|
115
|
+
yaml += '---\n';
|
|
116
|
+
|
|
117
|
+
if (this.requestBody) {
|
|
118
|
+
const body = typeof this.requestBody === 'string' ? this.requestBody : JSON.stringify(this.requestBody, null, 2);
|
|
119
|
+
yaml += body;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
writeFileSync(this.requestFile, yaml, 'utf8');
|
|
123
|
+
writeFileSync(this.responseFile, this._rawResponseBody || '', 'utf8');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static load(requestFile: string): RequestResult {
|
|
127
|
+
const content = readFileSync(requestFile, 'utf8');
|
|
128
|
+
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
|
|
129
|
+
if (!frontmatterMatch) throw new Error(`Invalid request file: ${requestFile}`);
|
|
130
|
+
|
|
131
|
+
const meta: Record<string, any> = {};
|
|
132
|
+
const lines = frontmatterMatch[1].split('\n');
|
|
133
|
+
let currentKey = '';
|
|
134
|
+
let currentObj: Record<string, string> | null = null;
|
|
135
|
+
|
|
136
|
+
for (const line of lines) {
|
|
137
|
+
const indentedMatch = line.match(/^ {2}(\S+):\s*(.*)$/);
|
|
138
|
+
if (indentedMatch && currentObj) {
|
|
139
|
+
currentObj[indentedMatch[1]] = indentedMatch[2];
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const kvMatch = line.match(/^(\S+):\s*(.*)$/);
|
|
144
|
+
if (!kvMatch) continue;
|
|
145
|
+
|
|
146
|
+
currentKey = kvMatch[1];
|
|
147
|
+
const value = kvMatch[2];
|
|
148
|
+
|
|
149
|
+
if (!value) {
|
|
150
|
+
currentObj = {};
|
|
151
|
+
meta[currentKey] = currentObj;
|
|
152
|
+
} else {
|
|
153
|
+
currentObj = null;
|
|
154
|
+
meta[currentKey] = value;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const id = path.basename(requestFile).replace('.request.yaml', '');
|
|
159
|
+
const responseFile = requestFile.replace('.request.yaml', '.response.json');
|
|
160
|
+
|
|
161
|
+
const result = new RequestResult({
|
|
162
|
+
id,
|
|
163
|
+
method: meta.method || 'GET',
|
|
164
|
+
path: meta.url || '',
|
|
165
|
+
fullUrl: meta.fullUrl || '',
|
|
166
|
+
requestHeaders: meta.headers || {},
|
|
167
|
+
requestBody: frontmatterMatch[2] || undefined,
|
|
168
|
+
status: Number.parseInt(meta.status) || 0,
|
|
169
|
+
statusText: meta.statusText || '',
|
|
170
|
+
responseHeaders: meta.responseHeaders || {},
|
|
171
|
+
timing: Number.parseInt(meta.timing) || 0,
|
|
172
|
+
timestamp: new Date(meta.timestamp || Date.now()),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
result.requestFile = requestFile;
|
|
176
|
+
result.responseFile = responseFile;
|
|
177
|
+
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
toSummary(): string {
|
|
182
|
+
return `${this.method} ${this.path} → ${this.status} (${this.timing}ms)`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
extractIdAndTitle(): { id?: string | number; title?: string } {
|
|
186
|
+
const body = this.responseBody;
|
|
187
|
+
if (!body || typeof body !== 'object') return {};
|
|
188
|
+
|
|
189
|
+
const data = body.data || body;
|
|
190
|
+
if (Array.isArray(data)) return {};
|
|
191
|
+
|
|
192
|
+
const id = data.id ?? data._id ?? data.uuid;
|
|
193
|
+
const title = data.name ?? data.title ?? data.label;
|
|
194
|
+
|
|
195
|
+
const result: { id?: string | number; title?: string } = {};
|
|
196
|
+
if (id !== undefined) result.id = id;
|
|
197
|
+
if (title !== undefined) result.title = String(title);
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
toCurlCommand(): string {
|
|
202
|
+
let cmd = `curl -X ${this.method} '${this.fullUrl}'`;
|
|
203
|
+
for (const [k, v] of Object.entries(this.requestHeaders)) {
|
|
204
|
+
cmd += ` -H '${k}: ${v}'`;
|
|
205
|
+
}
|
|
206
|
+
if (this.requestBody) {
|
|
207
|
+
const body = typeof this.requestBody === 'string' ? this.requestBody : JSON.stringify(this.requestBody);
|
|
208
|
+
cmd += ` -d '${body}'`;
|
|
209
|
+
}
|
|
210
|
+
return cmd;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { RequestResult } from './request-result.ts';
|
|
4
|
+
|
|
5
|
+
const AUTH_HEADERS = ['authorization', 'cookie', 'x-api-key', 'x-csrf-token'];
|
|
6
|
+
|
|
7
|
+
export class RequestStore {
|
|
8
|
+
private capturedRequests: RequestResult[] = [];
|
|
9
|
+
private madeRequests: RequestResult[] = [];
|
|
10
|
+
private outputDir: string;
|
|
11
|
+
|
|
12
|
+
constructor(outputDir: string) {
|
|
13
|
+
this.outputDir = outputDir;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
addCapturedRequest(result: RequestResult): void {
|
|
17
|
+
this.capturedRequests.push(result);
|
|
18
|
+
result.save(this.outputDir);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
addMadeRequest(result: RequestResult): void {
|
|
22
|
+
this.madeRequests.push(result);
|
|
23
|
+
result.save(this.outputDir);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
addRequest(result: RequestResult): void {
|
|
27
|
+
this.addMadeRequest(result);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getCapturedRequests(): RequestResult[] {
|
|
31
|
+
return this.capturedRequests;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getMadeRequests(): RequestResult[] {
|
|
35
|
+
return this.madeRequests;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getRequests(): RequestResult[] {
|
|
39
|
+
return this.madeRequests;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getLastRequest(): RequestResult | undefined {
|
|
43
|
+
return this.madeRequests[this.madeRequests.length - 1];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getRequestsByEndpoint(pathPrefix: string): RequestResult[] {
|
|
47
|
+
return this.madeRequests.filter((r) => r.path.startsWith(pathPrefix));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getRequestsByMethod(method: string): RequestResult[] {
|
|
51
|
+
const upper = method.toUpperCase();
|
|
52
|
+
return this.madeRequests.filter((r) => r.method === upper);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getRequestsByStatus(status: number): RequestResult[] {
|
|
56
|
+
return this.madeRequests.filter((r) => r.status === status);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
toEndpointList(): string {
|
|
60
|
+
const seen = new Set<string>();
|
|
61
|
+
const lines: string[] = [];
|
|
62
|
+
|
|
63
|
+
for (const req of this.capturedRequests) {
|
|
64
|
+
const normalized = normalizePathPattern(req.path);
|
|
65
|
+
const key = `${req.method} ${normalized}`;
|
|
66
|
+
if (seen.has(key)) continue;
|
|
67
|
+
seen.add(key);
|
|
68
|
+
lines.push(key);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return lines.join('\n');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
extractAuthHeaders(): Record<string, string> {
|
|
75
|
+
const headers: Record<string, string> = {};
|
|
76
|
+
|
|
77
|
+
for (let i = this.capturedRequests.length - 1; i >= 0; i--) {
|
|
78
|
+
const req = this.capturedRequests[i];
|
|
79
|
+
for (const [key, value] of Object.entries(req.requestHeaders)) {
|
|
80
|
+
if (AUTH_HEADERS.includes(key.toLowerCase()) && !headers[key]) {
|
|
81
|
+
headers[key] = value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (AUTH_HEADERS.every((h) => Object.keys(headers).some((k) => k.toLowerCase() === h))) break;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return headers;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
findCapturedRequest(method: string, pathPrefix: string): RequestResult | undefined {
|
|
91
|
+
const upper = method.toUpperCase();
|
|
92
|
+
return this.capturedRequests.find((r) => r.method === upper && r.path.startsWith(pathPrefix));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
toLog(): string {
|
|
96
|
+
return this.madeRequests.map((r, i) => `${i + 1}. ${r.toSummary()} [${r.id}]`).join('\n');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
loadFromDisk(): void {
|
|
100
|
+
const requestsDir = path.join(this.outputDir, 'requests');
|
|
101
|
+
if (!existsSync(requestsDir)) return;
|
|
102
|
+
|
|
103
|
+
const existingIds = new Set(this.capturedRequests.map((r) => r.id));
|
|
104
|
+
const files = readdirSync(requestsDir).filter((f) => f.endsWith('.request.yaml'));
|
|
105
|
+
|
|
106
|
+
for (const file of files) {
|
|
107
|
+
try {
|
|
108
|
+
const result = RequestResult.load(path.join(requestsDir, file));
|
|
109
|
+
if (existingIds.has(result.id)) continue;
|
|
110
|
+
this.capturedRequests.push(result);
|
|
111
|
+
} catch {
|
|
112
|
+
// skip invalid files
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
getWriteRequestsForScope(scopePath: string): RequestResult[] {
|
|
118
|
+
const writeMethods = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
|
|
119
|
+
return this.capturedRequests.filter((r) => writeMethods.has(r.method) && r.path.startsWith(scopePath));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
clear(): void {
|
|
123
|
+
this.capturedRequests = [];
|
|
124
|
+
this.madeRequests = [];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function normalizePathPattern(urlPath: string): string {
|
|
129
|
+
return urlPath.replace(/\/[0-9a-f]{24}\b/g, '/{id}').replace(/\/\d+\b/g, '/{id}');
|
|
130
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { mkdirSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { dereference } from '@scalar/openapi-parser';
|
|
5
|
+
import { tag } from '../utils/logger.ts';
|
|
6
|
+
|
|
7
|
+
export function validateSpecs(specs?: string[]): void {
|
|
8
|
+
if (!specs?.length) {
|
|
9
|
+
throw new Error('API spec is required. Set api.spec in your config file.');
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function loadSpec(specPaths: string[], outputDir: string): Promise<any> {
|
|
14
|
+
const contents: string[] = [];
|
|
15
|
+
|
|
16
|
+
for (const specPath of specPaths) {
|
|
17
|
+
if (specPath.startsWith('http://') || specPath.startsWith('https://')) {
|
|
18
|
+
const response = await fetch(specPath);
|
|
19
|
+
if (!response.ok) throw new Error(`Failed to fetch spec from ${specPath}: ${response.status}`);
|
|
20
|
+
const content = await response.text();
|
|
21
|
+
|
|
22
|
+
const specDir = path.join(outputDir, 'spec');
|
|
23
|
+
mkdirSync(specDir, { recursive: true });
|
|
24
|
+
const filename = specPath.split('/').pop() || 'spec.yaml';
|
|
25
|
+
writeFileSync(path.join(specDir, filename), content, 'utf8');
|
|
26
|
+
tag('info').log(`Spec downloaded: ${filename}`);
|
|
27
|
+
|
|
28
|
+
contents.push(content);
|
|
29
|
+
} else {
|
|
30
|
+
contents.push(readFileSync(specPath, 'utf8'));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const combined = contents[0];
|
|
35
|
+
const result = dereference(combined);
|
|
36
|
+
|
|
37
|
+
if (result.errors?.length) {
|
|
38
|
+
tag('warning').log(`Spec parse warnings: ${result.errors.map((e) => e.message).join(', ')}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!result.schema) {
|
|
42
|
+
throw new Error('Failed to parse OpenAPI spec');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return result.schema;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function extractEndpointDefinition(schema: any, endpoint: string, baseEndpoint?: string): string {
|
|
49
|
+
if (!schema?.paths) {
|
|
50
|
+
throw new Error('OpenAPI spec has no paths defined');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const basePath = toBasePath(baseEndpoint);
|
|
54
|
+
const matched = collectMatchingPaths(schema, basePath, (normalized) => matchesEndpoint(normalized, endpoint));
|
|
55
|
+
|
|
56
|
+
if (!Object.keys(matched).length) {
|
|
57
|
+
const available = listNormalizedPaths(schema, basePath);
|
|
58
|
+
throw new Error(`Endpoint "${endpoint}" not found in spec. Available: ${available}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return safeStringify(matched);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function searchEndpoints(schema: any, query: string, baseEndpoint?: string): string {
|
|
65
|
+
if (!schema?.paths) return 'No endpoints available';
|
|
66
|
+
|
|
67
|
+
const basePath = toBasePath(baseEndpoint);
|
|
68
|
+
|
|
69
|
+
if (!query || query === '*') {
|
|
70
|
+
const lines = Object.keys(schema.paths).map((p) => {
|
|
71
|
+
const normalized = stripBasePath(p, basePath);
|
|
72
|
+
const methods = Object.keys(schema.paths[p])
|
|
73
|
+
.filter((m) => HTTP_METHODS.includes(m))
|
|
74
|
+
.map((m) => m.toUpperCase())
|
|
75
|
+
.join(',');
|
|
76
|
+
return `${methods} ${normalized}`;
|
|
77
|
+
});
|
|
78
|
+
return lines.join('\n');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const lowerQuery = query.toLowerCase();
|
|
82
|
+
const matched = collectMatchingPaths(schema, basePath, (normalized) => normalized.toLowerCase().includes(lowerQuery));
|
|
83
|
+
|
|
84
|
+
if (!Object.keys(matched).length) {
|
|
85
|
+
return `No endpoints matching "${query}". Available: ${listNormalizedPaths(schema, basePath)}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return safeStringify(matched);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const HTTP_METHODS = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options'];
|
|
92
|
+
|
|
93
|
+
export function listAllEndpoints(schema: any, baseEndpoint?: string): string {
|
|
94
|
+
if (!schema?.paths) return '';
|
|
95
|
+
|
|
96
|
+
const basePath = toBasePath(baseEndpoint);
|
|
97
|
+
const lines: string[] = [];
|
|
98
|
+
|
|
99
|
+
for (const specPath of Object.keys(schema.paths)) {
|
|
100
|
+
const normalized = stripBasePath(specPath, basePath);
|
|
101
|
+
const pathDef = schema.paths[specPath];
|
|
102
|
+
|
|
103
|
+
for (const method of HTTP_METHODS) {
|
|
104
|
+
if (!pathDef[method]) continue;
|
|
105
|
+
const summary = pathDef[method].summary || pathDef[method].description || '';
|
|
106
|
+
const desc = summary ? ` - ${summary}` : '';
|
|
107
|
+
lines.push(`${method.toUpperCase()} ${normalized}${desc}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return lines.join('\n');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function toBasePath(baseEndpoint?: string): string {
|
|
115
|
+
return baseEndpoint ? new URL(baseEndpoint).pathname.replace(/\/$/, '') : '';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function collectMatchingPaths(schema: any, basePath: string, predicate: (normalized: string) => boolean): Record<string, any> {
|
|
119
|
+
const matched: Record<string, any> = {};
|
|
120
|
+
for (const specPath of Object.keys(schema.paths)) {
|
|
121
|
+
const normalized = stripBasePath(specPath, basePath);
|
|
122
|
+
if (!predicate(normalized)) continue;
|
|
123
|
+
matched[specPath] = schema.paths[specPath];
|
|
124
|
+
}
|
|
125
|
+
return matched;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function listNormalizedPaths(schema: any, basePath: string): string {
|
|
129
|
+
return Object.keys(schema.paths)
|
|
130
|
+
.map((p) => stripBasePath(p, basePath))
|
|
131
|
+
.join(', ');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function safeStringify(obj: any): string {
|
|
135
|
+
const seen = new WeakSet();
|
|
136
|
+
return JSON.stringify(
|
|
137
|
+
obj,
|
|
138
|
+
(_key, value) => {
|
|
139
|
+
if (typeof value === 'object' && value !== null) {
|
|
140
|
+
if (seen.has(value)) return '[Circular]';
|
|
141
|
+
seen.add(value);
|
|
142
|
+
}
|
|
143
|
+
return value;
|
|
144
|
+
},
|
|
145
|
+
2
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function stripBasePath(specPath: string, basePath: string): string {
|
|
150
|
+
if (!basePath) return specPath;
|
|
151
|
+
|
|
152
|
+
const specSegments = specPath.split('/').filter(Boolean);
|
|
153
|
+
const baseSegments = basePath.split('/').filter(Boolean);
|
|
154
|
+
|
|
155
|
+
let i = 0;
|
|
156
|
+
while (i < baseSegments.length && i < specSegments.length) {
|
|
157
|
+
const spec = specSegments[i];
|
|
158
|
+
const base = baseSegments[i];
|
|
159
|
+
if (spec === base || spec.startsWith('{')) {
|
|
160
|
+
i++;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return `/${specSegments.slice(i).join('/')}`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function matchesEndpoint(specPath: string, endpoint: string): boolean {
|
|
170
|
+
if (specPath === endpoint) return true;
|
|
171
|
+
if (specPath.startsWith(`${endpoint}/`)) return true;
|
|
172
|
+
if (specPath.startsWith(`${endpoint}/{`)) return true;
|
|
173
|
+
return false;
|
|
174
|
+
}
|