docorbit 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/LICENSE +21 -0
- package/README.md +660 -0
- package/apps/cli/bin/docorbit.js +8 -0
- package/apps/cli/src/commands/add.ts +44 -0
- package/apps/cli/src/commands/api.ts +38 -0
- package/apps/cli/src/commands/context.ts +47 -0
- package/apps/cli/src/commands/dashboard.ts +55 -0
- package/apps/cli/src/commands/diff.ts +30 -0
- package/apps/cli/src/commands/evaluate.ts +133 -0
- package/apps/cli/src/commands/examples.ts +39 -0
- package/apps/cli/src/commands/export.ts +89 -0
- package/apps/cli/src/commands/impact.ts +31 -0
- package/apps/cli/src/commands/init.ts +69 -0
- package/apps/cli/src/commands/inspect.ts +30 -0
- package/apps/cli/src/commands/mcp.ts +72 -0
- package/apps/cli/src/commands/pitfalls.ts +38 -0
- package/apps/cli/src/commands/recipes.ts +35 -0
- package/apps/cli/src/commands/search.ts +48 -0
- package/apps/cli/src/commands/update.ts +73 -0
- package/apps/cli/src/commands/verify.ts +48 -0
- package/apps/cli/src/formatters/colors.ts +23 -0
- package/apps/cli/src/formatters/inspection.ts +102 -0
- package/apps/cli/src/formatters/knowledge.ts +272 -0
- package/apps/cli/src/formatters/retrieval.ts +74 -0
- package/apps/cli/src/formatters/terminal.ts +6 -0
- package/apps/cli/src/formatters/verification.ts +126 -0
- package/apps/cli/src/index.ts +409 -0
- package/bin/docorbit.js +8 -0
- package/package.json +46 -0
- package/packages/core/src/dashboard/server.ts +314 -0
- package/packages/core/src/dashboard/ui.ts +586 -0
- package/packages/core/src/implementation-service.ts +451 -0
- package/packages/core/src/index.ts +7 -0
- package/packages/core/src/inspector.ts +71 -0
- package/packages/core/src/pipeline.ts +331 -0
- package/packages/crawler/src/config.ts +12 -0
- package/packages/crawler/src/fetcher.ts +185 -0
- package/packages/crawler/src/index.ts +2 -0
- package/packages/discovery/src/index.ts +31 -0
- package/packages/discovery/src/provider.ts +47 -0
- package/packages/discovery/src/providers/generic.ts +98 -0
- package/packages/discovery/src/providers/github.ts +61 -0
- package/packages/discovery/src/providers/llms-txt.ts +73 -0
- package/packages/discovery/src/providers/markdown.ts +48 -0
- package/packages/discovery/src/providers/openapi.ts +91 -0
- package/packages/discovery/src/providers/sitemap.ts +62 -0
- package/packages/discovery/src/providers/skill.ts +54 -0
- package/packages/discovery/src/ranker.ts +123 -0
- package/packages/evaluation/src/dataset.ts +963 -0
- package/packages/evaluation/src/index.ts +8 -0
- package/packages/evaluation/src/runner.ts +241 -0
- package/packages/evaluation/src/strategies/context7-runner.ts +269 -0
- package/packages/evaluation/src/strategies/docorbit-runner.ts +228 -0
- package/packages/evaluation/src/strategies/firecrawl-runner.ts +172 -0
- package/packages/evaluation/src/strategies/web-search-runner.ts +194 -0
- package/packages/evaluation/src/types.ts +34 -0
- package/packages/evaluation/src/version-matcher.ts +73 -0
- package/packages/export/src/agents-md.ts +200 -0
- package/packages/export/src/claude-md.ts +141 -0
- package/packages/export/src/docs-map.ts +150 -0
- package/packages/export/src/index.ts +6 -0
- package/packages/export/src/llms-txt.ts +96 -0
- package/packages/export/src/service.ts +250 -0
- package/packages/export/src/skill-md.ts +128 -0
- package/packages/mcp/src/index.ts +46 -0
- package/packages/mcp/src/resources/index.ts +189 -0
- package/packages/mcp/src/server.ts +278 -0
- package/packages/mcp/src/tools/analyze-impact.ts +74 -0
- package/packages/mcp/src/tools/check-api.ts +86 -0
- package/packages/mcp/src/tools/diff-docs.ts +68 -0
- package/packages/mcp/src/tools/export-context.ts +73 -0
- package/packages/mcp/src/tools/find-api.ts +99 -0
- package/packages/mcp/src/tools/find-example.ts +100 -0
- package/packages/mcp/src/tools/find-pitfall.ts +94 -0
- package/packages/mcp/src/tools/find-recipe.ts +98 -0
- package/packages/mcp/src/tools/get-doc.ts +130 -0
- package/packages/mcp/src/tools/get-docs-map.ts +64 -0
- package/packages/mcp/src/tools/get-version.ts +118 -0
- package/packages/mcp/src/tools/implementation-context.ts +88 -0
- package/packages/mcp/src/tools/index.ts +59 -0
- package/packages/mcp/src/tools/list-sources.ts +85 -0
- package/packages/mcp/src/tools/search-docs.ts +123 -0
- package/packages/mcp/src/tools/types.ts +28 -0
- package/packages/mcp/src/transports/http.ts +256 -0
- package/packages/mcp/src/transports/stdio.ts +105 -0
- package/packages/mcp/src/transports/types.ts +6 -0
- package/packages/mcp/src/types.ts +102 -0
- package/packages/normalizer/src/example-indexer.ts +240 -0
- package/packages/normalizer/src/html.ts +253 -0
- package/packages/normalizer/src/index.ts +8 -0
- package/packages/normalizer/src/llms.ts +83 -0
- package/packages/normalizer/src/openapi/endpoint-parser.ts +406 -0
- package/packages/normalizer/src/openapi/schema-resolver.ts +111 -0
- package/packages/normalizer/src/openapi.ts +2 -0
- package/packages/normalizer/src/page.ts +184 -0
- package/packages/normalizer/src/pitfall-extractor.ts +190 -0
- package/packages/normalizer/src/slicer.ts +455 -0
- package/packages/retrieval/src/engine.ts +120 -0
- package/packages/retrieval/src/index.ts +7 -0
- package/packages/retrieval/src/intent.ts +43 -0
- package/packages/retrieval/src/packer.ts +145 -0
- package/packages/retrieval/src/recipe-engine.ts +313 -0
- package/packages/retrieval/src/scorer.ts +139 -0
- package/packages/retrieval/src/weights.ts +31 -0
- package/packages/security/src/annotations.ts +112 -0
- package/packages/security/src/index.ts +2 -0
- package/packages/security/src/ssrf.ts +153 -0
- package/packages/shared/src/errors.ts +53 -0
- package/packages/shared/src/hashing.ts +23 -0
- package/packages/shared/src/index.ts +3 -0
- package/packages/shared/src/types.ts +881 -0
- package/packages/storage/src/db.ts +72 -0
- package/packages/storage/src/index.ts +11 -0
- package/packages/storage/src/interfaces.ts +115 -0
- package/packages/storage/src/repositories/api-repository.ts +219 -0
- package/packages/storage/src/repositories/chunk-repository.ts +316 -0
- package/packages/storage/src/repositories/example-repository.ts +206 -0
- package/packages/storage/src/repositories/page-repository.ts +205 -0
- package/packages/storage/src/repositories/pitfall-repository.ts +188 -0
- package/packages/storage/src/repositories/source-repository.ts +205 -0
- package/packages/storage/src/repository.ts +256 -0
- package/packages/storage/src/schema.ts +269 -0
- package/packages/storage/src/search-tokens.ts +28 -0
- package/packages/verification/src/diff-engine.ts +258 -0
- package/packages/verification/src/extractor.ts +339 -0
- package/packages/verification/src/impact-scanner.ts +203 -0
- package/packages/verification/src/index.ts +5 -0
- package/packages/verification/src/services.ts +238 -0
- package/packages/verification/src/verifier.ts +375 -0
- package/packages/workspace/src/detector.ts +143 -0
- package/packages/workspace/src/ecosystems/cargo.ts +84 -0
- package/packages/workspace/src/ecosystems/composer.ts +42 -0
- package/packages/workspace/src/ecosystems/go.ts +54 -0
- package/packages/workspace/src/ecosystems/index.ts +34 -0
- package/packages/workspace/src/ecosystems/maven.ts +34 -0
- package/packages/workspace/src/ecosystems/npm.ts +83 -0
- package/packages/workspace/src/ecosystems/pub.ts +40 -0
- package/packages/workspace/src/ecosystems/pypi.ts +100 -0
- package/packages/workspace/src/ecosystems/rubygems.ts +30 -0
- package/packages/workspace/src/ecosystems/types.ts +18 -0
- package/packages/workspace/src/index.ts +5 -0
- package/packages/workspace/src/lockfile.ts +194 -0
- package/packages/workspace/src/resolver.ts +234 -0
- package/packages/workspace/src/semver.ts +259 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type {
|
|
3
|
+
OpenApiSummary,
|
|
4
|
+
ApiEndpoint,
|
|
5
|
+
ApiParameter,
|
|
6
|
+
ApiSchema,
|
|
7
|
+
ApiResponse,
|
|
8
|
+
ApiAuthScheme,
|
|
9
|
+
ApiErrorResponse,
|
|
10
|
+
ApiPaginationMetadata,
|
|
11
|
+
Provenance,
|
|
12
|
+
} from '../../../shared/src/index.ts';
|
|
13
|
+
import { resolveJsonPointer, normalizeSchema } from './schema-resolver.ts';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Detects if a document content is an OpenAPI or Swagger specification.
|
|
17
|
+
*/
|
|
18
|
+
export function detectOpenApiSpec(content: string, sourceUrl: string): OpenApiSummary | null {
|
|
19
|
+
if (!content || typeof content !== 'string') return null;
|
|
20
|
+
|
|
21
|
+
let parsed: Record<string, unknown>;
|
|
22
|
+
try {
|
|
23
|
+
parsed = JSON.parse(content);
|
|
24
|
+
} catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof parsed !== 'object' || parsed === null) return null;
|
|
29
|
+
|
|
30
|
+
const openapiVer = typeof parsed.openapi === 'string' ? parsed.openapi : undefined;
|
|
31
|
+
const swaggerVer = typeof parsed.swagger === 'string' ? parsed.swagger : undefined;
|
|
32
|
+
|
|
33
|
+
const specVersion = openapiVer || swaggerVer;
|
|
34
|
+
if (!specVersion) return null;
|
|
35
|
+
|
|
36
|
+
const info = (typeof parsed.info === 'object' && parsed.info !== null)
|
|
37
|
+
? (parsed.info as Record<string, unknown>)
|
|
38
|
+
: {};
|
|
39
|
+
|
|
40
|
+
const title = typeof info.title === 'string' ? info.title : 'Untitled API';
|
|
41
|
+
const description = typeof info.description === 'string' ? info.description : undefined;
|
|
42
|
+
|
|
43
|
+
const servers: string[] = [];
|
|
44
|
+
if (Array.isArray(parsed.servers)) {
|
|
45
|
+
for (const server of parsed.servers) {
|
|
46
|
+
if (server && typeof server === 'object' && typeof (server as Record<string, unknown>).url === 'string') {
|
|
47
|
+
servers.push((server as Record<string, unknown>).url as string);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} else if (typeof parsed.host === 'string') {
|
|
51
|
+
const basePath = typeof parsed.basePath === 'string' ? parsed.basePath : '';
|
|
52
|
+
const schemes = Array.isArray(parsed.schemes) ? parsed.schemes : ['https'];
|
|
53
|
+
servers.push(`${schemes[0]}://${parsed.host}${basePath}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let pathCount = 0;
|
|
57
|
+
if (typeof parsed.paths === 'object' && parsed.paths !== null) {
|
|
58
|
+
pathCount = Object.keys(parsed.paths).length;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
specVersion,
|
|
63
|
+
title,
|
|
64
|
+
description,
|
|
65
|
+
servers,
|
|
66
|
+
pathCount,
|
|
67
|
+
sourceUrl,
|
|
68
|
+
rawJson: parsed,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Parses all API endpoints from an OpenAPI 3.x or Swagger 2.0 specification.
|
|
74
|
+
* Fully deterministic with internal $ref resolution, schema normalization,
|
|
75
|
+
* pagination heuristics, and error detection.
|
|
76
|
+
*/
|
|
77
|
+
export function parseOpenApiEndpoints(
|
|
78
|
+
rawSpec: string | Record<string, unknown>,
|
|
79
|
+
pageId: string,
|
|
80
|
+
snapshotId: string,
|
|
81
|
+
sourceUrl?: string,
|
|
82
|
+
docVersion?: string
|
|
83
|
+
): ApiEndpoint[] {
|
|
84
|
+
let root: Record<string, unknown>;
|
|
85
|
+
if (typeof rawSpec === 'string') {
|
|
86
|
+
try {
|
|
87
|
+
root = JSON.parse(rawSpec);
|
|
88
|
+
} catch {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
root = rawSpec;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!root || typeof root !== 'object') return [];
|
|
96
|
+
|
|
97
|
+
const isOpenApi3 = typeof root.openapi === 'string';
|
|
98
|
+
const isSwagger2 = typeof root.swagger === 'string';
|
|
99
|
+
if (!isOpenApi3 && !isSwagger2) return [];
|
|
100
|
+
|
|
101
|
+
const paths = (typeof root.paths === 'object' && root.paths !== null)
|
|
102
|
+
? (root.paths as Record<string, unknown>)
|
|
103
|
+
: {};
|
|
104
|
+
|
|
105
|
+
const endpoints: ApiEndpoint[] = [];
|
|
106
|
+
const supportedMethods = new Set(['get', 'post', 'put', 'delete', 'patch', 'head', 'options']);
|
|
107
|
+
|
|
108
|
+
// Extract root-level security schemes
|
|
109
|
+
const securitySchemesMap = new Map<string, ApiAuthScheme>();
|
|
110
|
+
if (isOpenApi3 && root.components && typeof root.components === 'object') {
|
|
111
|
+
const comps = root.components as Record<string, unknown>;
|
|
112
|
+
if (typeof comps.securitySchemes === 'object' && comps.securitySchemes !== null) {
|
|
113
|
+
for (const [name, schemeRaw] of Object.entries(comps.securitySchemes as Record<string, unknown>)) {
|
|
114
|
+
if (typeof schemeRaw === 'object' && schemeRaw !== null) {
|
|
115
|
+
const s = schemeRaw as Record<string, unknown>;
|
|
116
|
+
securitySchemesMap.set(name, {
|
|
117
|
+
type: (s.type as ApiAuthScheme['type']) || 'http',
|
|
118
|
+
name: typeof s.name === 'string' ? s.name : name,
|
|
119
|
+
in: s.in as ApiAuthScheme['in'],
|
|
120
|
+
scheme: typeof s.scheme === 'string' ? s.scheme : undefined,
|
|
121
|
+
bearerFormat: typeof s.bearerFormat === 'string' ? s.bearerFormat : undefined,
|
|
122
|
+
description: typeof s.description === 'string' ? s.description : undefined,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} else if (isSwagger2 && typeof root.securityDefinitions === 'object' && root.securityDefinitions !== null) {
|
|
128
|
+
for (const [name, schemeRaw] of Object.entries(root.securityDefinitions as Record<string, unknown>)) {
|
|
129
|
+
if (typeof schemeRaw === 'object' && schemeRaw !== null) {
|
|
130
|
+
const s = schemeRaw as Record<string, unknown>;
|
|
131
|
+
const rawType = typeof s.type === 'string' ? s.type : 'apiKey';
|
|
132
|
+
let mappedType: ApiAuthScheme['type'] = 'apiKey';
|
|
133
|
+
let mappedScheme: string | undefined = undefined;
|
|
134
|
+
|
|
135
|
+
if (rawType === 'basic') {
|
|
136
|
+
mappedType = 'http';
|
|
137
|
+
mappedScheme = 'basic';
|
|
138
|
+
} else if (rawType === 'oauth2') {
|
|
139
|
+
mappedType = 'oauth2';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
securitySchemesMap.set(name, {
|
|
143
|
+
type: mappedType,
|
|
144
|
+
name: typeof s.name === 'string' ? s.name : name,
|
|
145
|
+
in: s.in as ApiAuthScheme['in'],
|
|
146
|
+
scheme: mappedScheme,
|
|
147
|
+
description: typeof s.description === 'string' ? s.description : undefined,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Root security fallback
|
|
154
|
+
const rootSecurityReqs: Array<Record<string, unknown>> = Array.isArray(root.security)
|
|
155
|
+
? (root.security as Array<Record<string, unknown>>)
|
|
156
|
+
: [];
|
|
157
|
+
|
|
158
|
+
const nowIso = new Date().toISOString();
|
|
159
|
+
|
|
160
|
+
for (const [pathStr, pathItemRaw] of Object.entries(paths)) {
|
|
161
|
+
if (!pathItemRaw || typeof pathItemRaw !== 'object') continue;
|
|
162
|
+
const pathItem = pathItemRaw as Record<string, unknown>;
|
|
163
|
+
|
|
164
|
+
// Common path-level parameters
|
|
165
|
+
const pathCommonParams: unknown[] = Array.isArray(pathItem.parameters) ? pathItem.parameters : [];
|
|
166
|
+
|
|
167
|
+
for (const [methodKey, operationRaw] of Object.entries(pathItem)) {
|
|
168
|
+
const method = methodKey.toLowerCase();
|
|
169
|
+
if (!supportedMethods.has(method)) continue;
|
|
170
|
+
if (!operationRaw || typeof operationRaw !== 'object') continue;
|
|
171
|
+
|
|
172
|
+
const op = operationRaw as Record<string, unknown>;
|
|
173
|
+
|
|
174
|
+
// Summary & Description
|
|
175
|
+
const summary = typeof op.summary === 'string' ? op.summary : undefined;
|
|
176
|
+
const description = typeof op.description === 'string' ? op.description : undefined;
|
|
177
|
+
const operationId = typeof op.operationId === 'string' ? op.operationId : undefined;
|
|
178
|
+
const deprecated = Boolean(op.deprecated || pathItem.deprecated);
|
|
179
|
+
|
|
180
|
+
// Parameters
|
|
181
|
+
const rawParams: unknown[] = [...pathCommonParams, ...(Array.isArray(op.parameters) ? op.parameters : [])];
|
|
182
|
+
const parameters: ApiParameter[] = [];
|
|
183
|
+
let swaggerBodySchema: unknown = undefined;
|
|
184
|
+
|
|
185
|
+
for (const pRaw of rawParams) {
|
|
186
|
+
let p = pRaw as Record<string, unknown>;
|
|
187
|
+
if (typeof p.$ref === 'string') {
|
|
188
|
+
const resolved = resolveJsonPointer(root, p.$ref);
|
|
189
|
+
if (typeof resolved === 'object' && resolved !== null) {
|
|
190
|
+
p = resolved as Record<string, unknown>;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (isSwagger2 && p.in === 'body') {
|
|
195
|
+
swaggerBodySchema = p.schema;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const name = typeof p.name === 'string' ? p.name : '';
|
|
200
|
+
const paramIn = (typeof p.in === 'string' ? p.in : 'query') as ApiParameter['in'];
|
|
201
|
+
const required = paramIn === 'path' ? true : Boolean(p.required);
|
|
202
|
+
const pDesc = typeof p.description === 'string' ? p.description : undefined;
|
|
203
|
+
const pDefault = p.default !== undefined ? p.default : (p.schema && typeof p.schema === 'object' ? (p.schema as Record<string, unknown>).default : undefined);
|
|
204
|
+
const pExample = p.example !== undefined ? p.example : (p.schema && typeof p.schema === 'object' ? (p.schema as Record<string, unknown>).example : undefined);
|
|
205
|
+
|
|
206
|
+
let pType: string | undefined = typeof p.type === 'string' ? p.type : undefined;
|
|
207
|
+
let pSchema: Record<string, unknown> | undefined = undefined;
|
|
208
|
+
|
|
209
|
+
if (p.schema && typeof p.schema === 'object') {
|
|
210
|
+
pSchema = p.schema as Record<string, unknown>;
|
|
211
|
+
if (!pType && typeof pSchema.type === 'string') {
|
|
212
|
+
pType = pSchema.type;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
parameters.push({
|
|
217
|
+
name,
|
|
218
|
+
in: paramIn,
|
|
219
|
+
required,
|
|
220
|
+
type: pType,
|
|
221
|
+
description: pDesc,
|
|
222
|
+
default: pDefault,
|
|
223
|
+
schema: pSchema,
|
|
224
|
+
example: pExample,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Request Body
|
|
229
|
+
let requestSchema: ApiSchema | undefined = undefined;
|
|
230
|
+
if (isOpenApi3 && op.requestBody && typeof op.requestBody === 'object') {
|
|
231
|
+
let rb = op.requestBody as Record<string, unknown>;
|
|
232
|
+
if (typeof rb.$ref === 'string') {
|
|
233
|
+
const resolved = resolveJsonPointer(root, rb.$ref);
|
|
234
|
+
if (typeof resolved === 'object' && resolved !== null) {
|
|
235
|
+
rb = resolved as Record<string, unknown>;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (typeof rb.content === 'object' && rb.content !== null) {
|
|
239
|
+
const content = rb.content as Record<string, unknown>;
|
|
240
|
+
// Prefer application/json, fallback to first media type
|
|
241
|
+
const mediaTypeObj = (content['application/json'] || Object.values(content)[0]) as Record<string, unknown> | undefined;
|
|
242
|
+
if (mediaTypeObj && mediaTypeObj.schema) {
|
|
243
|
+
requestSchema = normalizeSchema(mediaTypeObj.schema, root);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} else if (isSwagger2 && swaggerBodySchema) {
|
|
247
|
+
requestSchema = normalizeSchema(swaggerBodySchema, root);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Responses & Errors
|
|
251
|
+
const responseSchema: Record<string, ApiResponse> = {};
|
|
252
|
+
const errors: ApiErrorResponse[] = [];
|
|
253
|
+
|
|
254
|
+
if (typeof op.responses === 'object' && op.responses !== null) {
|
|
255
|
+
for (const [statusCode, resRaw] of Object.entries(op.responses as Record<string, unknown>)) {
|
|
256
|
+
let res = resRaw as Record<string, unknown>;
|
|
257
|
+
if (typeof res.$ref === 'string') {
|
|
258
|
+
const resolved = resolveJsonPointer(root, res.$ref);
|
|
259
|
+
if (typeof resolved === 'object' && resolved !== null) {
|
|
260
|
+
res = resolved as Record<string, unknown>;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const resDesc = typeof res.description === 'string' ? res.description : '';
|
|
265
|
+
let contentType: string | undefined = undefined;
|
|
266
|
+
let resSchemaObj: unknown = undefined;
|
|
267
|
+
let resExample: unknown = undefined;
|
|
268
|
+
|
|
269
|
+
if (isOpenApi3 && typeof res.content === 'object' && res.content !== null) {
|
|
270
|
+
const content = res.content as Record<string, unknown>;
|
|
271
|
+
const ctKey = content['application/json'] ? 'application/json' : Object.keys(content)[0];
|
|
272
|
+
contentType = ctKey;
|
|
273
|
+
const mediaObj = content[ctKey] as Record<string, unknown> | undefined;
|
|
274
|
+
if (mediaObj) {
|
|
275
|
+
resSchemaObj = mediaObj.schema;
|
|
276
|
+
resExample = mediaObj.example || (mediaObj.examples ? Object.values(mediaObj.examples as Record<string, unknown>)[0] : undefined);
|
|
277
|
+
}
|
|
278
|
+
} else if (isSwagger2) {
|
|
279
|
+
resSchemaObj = res.schema;
|
|
280
|
+
contentType = 'application/json';
|
|
281
|
+
resExample = res.examples ? Object.values(res.examples as Record<string, unknown>)[0] : undefined;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const normalizedResSchema = normalizeSchema(resSchemaObj, root);
|
|
285
|
+
|
|
286
|
+
const apiResponse: ApiResponse = {
|
|
287
|
+
statusCode,
|
|
288
|
+
description: resDesc,
|
|
289
|
+
contentType,
|
|
290
|
+
schema: normalizedResSchema,
|
|
291
|
+
example: resExample,
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
responseSchema[statusCode] = apiResponse;
|
|
295
|
+
|
|
296
|
+
// Detect errors
|
|
297
|
+
const isErrorCode = statusCode.startsWith('4') || statusCode.startsWith('5') || statusCode === 'default';
|
|
298
|
+
if (isErrorCode) {
|
|
299
|
+
errors.push({
|
|
300
|
+
statusCode,
|
|
301
|
+
description: resDesc,
|
|
302
|
+
schema: normalizedResSchema,
|
|
303
|
+
example: resExample,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Authentication Schemes
|
|
310
|
+
const auth: ApiAuthScheme[] = [];
|
|
311
|
+
const opSecurityReqs: Array<Record<string, unknown>> = Array.isArray(op.security)
|
|
312
|
+
? (op.security as Array<Record<string, unknown>>)
|
|
313
|
+
: rootSecurityReqs;
|
|
314
|
+
|
|
315
|
+
for (const req of opSecurityReqs) {
|
|
316
|
+
if (typeof req !== 'object' || req === null) continue;
|
|
317
|
+
for (const schemeName of Object.keys(req)) {
|
|
318
|
+
const found = securitySchemesMap.get(schemeName);
|
|
319
|
+
if (found) {
|
|
320
|
+
// Avoid duplicate schemes
|
|
321
|
+
if (!auth.some(a => a.name === found.name && a.type === found.type)) {
|
|
322
|
+
auth.push(found);
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
auth.push({
|
|
326
|
+
type: 'apiKey',
|
|
327
|
+
name: schemeName,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Pagination Heuristics
|
|
334
|
+
let pagination: ApiPaginationMetadata | undefined = undefined;
|
|
335
|
+
const paramNames = parameters.map(p => p.name.toLowerCase());
|
|
336
|
+
const cursorParams = paramNames.filter(n => ['starting_after', 'ending_before', 'cursor', 'next_cursor', 'after', 'before'].includes(n));
|
|
337
|
+
const offsetParams = paramNames.filter(n => ['offset', 'skip', 'start'].includes(n));
|
|
338
|
+
const pageParams = paramNames.filter(n => ['page', 'page_number', 'pagenumber'].includes(n));
|
|
339
|
+
const limitParams = paramNames.filter(n => ['limit', 'per_page', 'page_size', 'pagesize', 'count', 'max'].includes(n));
|
|
340
|
+
|
|
341
|
+
// Inspect response properties for pagination clues
|
|
342
|
+
const okResponse = responseSchema['200'] || responseSchema['201'];
|
|
343
|
+
const responseProps = okResponse?.schema?.properties ? Object.keys(okResponse.schema.properties) : [];
|
|
344
|
+
const detectedResponseFields = responseProps.filter(prop =>
|
|
345
|
+
['has_more', 'next_cursor', 'cursor', 'next_page', 'page_info', 'total_count', 'data', 'items', 'results'].includes(prop.toLowerCase())
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
if (cursorParams.length > 0) {
|
|
349
|
+
pagination = {
|
|
350
|
+
type: 'cursor',
|
|
351
|
+
parameters: [...cursorParams, ...limitParams],
|
|
352
|
+
responseFields: detectedResponseFields,
|
|
353
|
+
};
|
|
354
|
+
} else if (offsetParams.length > 0) {
|
|
355
|
+
pagination = {
|
|
356
|
+
type: 'offset',
|
|
357
|
+
parameters: [...offsetParams, ...limitParams],
|
|
358
|
+
responseFields: detectedResponseFields,
|
|
359
|
+
};
|
|
360
|
+
} else if (pageParams.length > 0) {
|
|
361
|
+
pagination = {
|
|
362
|
+
type: 'page',
|
|
363
|
+
parameters: [...pageParams, ...limitParams],
|
|
364
|
+
responseFields: detectedResponseFields,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Deterministic Endpoint ID
|
|
369
|
+
const endpointId = createHash('sha256')
|
|
370
|
+
.update(`${pageId}:${method}:${pathStr}`)
|
|
371
|
+
.digest('hex')
|
|
372
|
+
.substring(0, 16);
|
|
373
|
+
|
|
374
|
+
const provenance: Provenance | undefined = sourceUrl
|
|
375
|
+
? {
|
|
376
|
+
sourceUrl,
|
|
377
|
+
retrievedAt: nowIso,
|
|
378
|
+
sourceAuthority: 'official',
|
|
379
|
+
}
|
|
380
|
+
: undefined;
|
|
381
|
+
|
|
382
|
+
endpoints.push({
|
|
383
|
+
id: endpointId,
|
|
384
|
+
pageId,
|
|
385
|
+
snapshotId,
|
|
386
|
+
method: method as ApiEndpoint['method'],
|
|
387
|
+
path: pathStr,
|
|
388
|
+
summary,
|
|
389
|
+
description,
|
|
390
|
+
operationId,
|
|
391
|
+
parameters,
|
|
392
|
+
requestSchema,
|
|
393
|
+
responseSchema,
|
|
394
|
+
auth,
|
|
395
|
+
errors,
|
|
396
|
+
pagination,
|
|
397
|
+
deprecated,
|
|
398
|
+
docVersion,
|
|
399
|
+
provenance,
|
|
400
|
+
createdAt: nowIso,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return endpoints;
|
|
406
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { ApiSchema } from '../../../shared/src/index.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a JSON Pointer (e.g. `#/components/schemas/Pet` or `#/definitions/Pet`)
|
|
5
|
+
* within the root document. Handles circular references safely.
|
|
6
|
+
*/
|
|
7
|
+
export function resolveJsonPointer(
|
|
8
|
+
root: Record<string, unknown>,
|
|
9
|
+
pointer: string,
|
|
10
|
+
visited: Set<string> = new Set()
|
|
11
|
+
): unknown {
|
|
12
|
+
if (visited.has(pointer)) {
|
|
13
|
+
return { $ref: pointer, description: '[Circular Reference]' };
|
|
14
|
+
}
|
|
15
|
+
visited.add(pointer);
|
|
16
|
+
|
|
17
|
+
if (!pointer.startsWith('#/')) {
|
|
18
|
+
return { $ref: pointer, description: '[External Reference]' };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const parts = pointer.slice(2).split('/').map((part) =>
|
|
22
|
+
part.replace(/~1/g, '/').replace(/~0/g, '~')
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
let current: unknown = root;
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
if (typeof current !== 'object' || current === null) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
current = (current as Record<string, unknown>)[part];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// If the resolved object itself is a reference, resolve further
|
|
34
|
+
if (
|
|
35
|
+
typeof current === 'object' &&
|
|
36
|
+
current !== null &&
|
|
37
|
+
typeof (current as Record<string, unknown>).$ref === 'string'
|
|
38
|
+
) {
|
|
39
|
+
return resolveJsonPointer(
|
|
40
|
+
root,
|
|
41
|
+
(current as Record<string, unknown>).$ref as string,
|
|
42
|
+
visited
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return current;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Normalizes an OpenAPI / JSON schema object into an ApiSchema model.
|
|
51
|
+
*/
|
|
52
|
+
export function normalizeSchema(
|
|
53
|
+
rawSchema: unknown,
|
|
54
|
+
root: Record<string, unknown>,
|
|
55
|
+
visited: Set<string> = new Set()
|
|
56
|
+
): ApiSchema | undefined {
|
|
57
|
+
if (!rawSchema || typeof rawSchema !== 'object') return undefined;
|
|
58
|
+
|
|
59
|
+
let schemaObj = rawSchema as Record<string, unknown>;
|
|
60
|
+
|
|
61
|
+
if (typeof schemaObj.$ref === 'string') {
|
|
62
|
+
const resolved = resolveJsonPointer(root, schemaObj.$ref, visited);
|
|
63
|
+
if (typeof resolved === 'object' && resolved !== null) {
|
|
64
|
+
schemaObj = resolved as Record<string, unknown>;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const type = typeof schemaObj.type === 'string'
|
|
69
|
+
? schemaObj.type
|
|
70
|
+
: (Array.isArray(schemaObj.type) ? schemaObj.type.join(' | ') : undefined);
|
|
71
|
+
|
|
72
|
+
const description = typeof schemaObj.description === 'string'
|
|
73
|
+
? schemaObj.description
|
|
74
|
+
: undefined;
|
|
75
|
+
|
|
76
|
+
const required = Array.isArray(schemaObj.required)
|
|
77
|
+
? schemaObj.required.filter((r): r is string => typeof r === 'string')
|
|
78
|
+
: undefined;
|
|
79
|
+
|
|
80
|
+
let properties: Record<string, unknown> | undefined;
|
|
81
|
+
if (typeof schemaObj.properties === 'object' && schemaObj.properties !== null) {
|
|
82
|
+
properties = {};
|
|
83
|
+
for (const [k, v] of Object.entries(schemaObj.properties as Record<string, unknown>)) {
|
|
84
|
+
if (typeof v === 'object' && v !== null && typeof (v as Record<string, unknown>).$ref === 'string') {
|
|
85
|
+
const refResolved = resolveJsonPointer(root, (v as Record<string, unknown>).$ref as string, new Set(visited));
|
|
86
|
+
properties[k] = refResolved || v;
|
|
87
|
+
} else {
|
|
88
|
+
properties[k] = v;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let items: Record<string, unknown> | undefined;
|
|
94
|
+
if (typeof schemaObj.items === 'object' && schemaObj.items !== null) {
|
|
95
|
+
if (typeof (schemaObj.items as Record<string, unknown>).$ref === 'string') {
|
|
96
|
+
const refResolved = resolveJsonPointer(root, (schemaObj.items as Record<string, unknown>).$ref as string, new Set(visited));
|
|
97
|
+
items = (refResolved as Record<string, unknown>) || (schemaObj.items as Record<string, unknown>);
|
|
98
|
+
} else {
|
|
99
|
+
items = schemaObj.items as Record<string, unknown>;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
type,
|
|
105
|
+
properties,
|
|
106
|
+
required,
|
|
107
|
+
description,
|
|
108
|
+
items,
|
|
109
|
+
raw: schemaObj,
|
|
110
|
+
};
|
|
111
|
+
}
|