openapi-ai-generator 0.1.1 → 0.1.3

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/dist/cli.js CHANGED
@@ -85,10 +85,11 @@ function createModel(provider) {
85
85
  }
86
86
  function createAzureModel() {
87
87
  const endpoint = requireEnv("AZURE_OPENAI_ENDPOINT");
88
+ const resourceName = endpoint?.match(/https?:\/\/([^.]+)/)?.[1];
88
89
  const apiKey = requireEnv("AZURE_OPENAI_API_KEY");
89
90
  const deployment = requireEnv("AZURE_OPENAI_DEPLOYMENT");
90
91
  const { createAzure } = require("@ai-sdk/azure");
91
- const azure = createAzure({ endpoint, apiKey });
92
+ const azure = createAzure({ resourceName, apiKey });
92
93
  return azure(deployment);
93
94
  }
94
95
  function createOpenAIModel() {
@@ -225,12 +226,12 @@ ${jsDocSection}
225
226
 
226
227
  For each exported function named GET, POST, PUT, PATCH, or DELETE, document:
227
228
  - operationId: a unique camelCase identifier
228
- - summary: a short one-line description
229
+ - summary: a short one-line description (max ~4 words, avoid starting with "Get", "Post", "Put", "Patch", "Delete", these are sometimes inferred from the operationId, and are sometimes in the format of "Create a ___", "Update a ___", "Delete a ___", "Get a ___", "List ___")
229
230
  - description: a fuller explanation of what the endpoint does
230
231
  - parameters: path params from URL segments like {id}, and query params from searchParams usage
231
232
  - requestBody: schema inferred from request.json() calls and TypeScript types (POST/PUT/PATCH only)
232
233
  - responses: per status code, inferred from NextResponse.json() calls and return type annotations
233
- - tags: inferred from the URL path segments
234
+ - tags: inferred from the URL path segments, but typically only one tag should be used. If more than one exists, they show up in multiple categories, and that can be confusing. If there's a question, use the more specific option.
234
235
  - security: noted if the code checks for auth tokens, session cookies, or middleware guards
235
236
 
236
237
  ## Output format
@@ -275,12 +276,22 @@ var defaults = {
275
276
  cache: true,
276
277
  cacheDir: ".openapi-cache",
277
278
  include: ["src/app/api/**/route.ts"],
278
- exclude: []
279
+ exclude: [],
280
+ envFile: [".env", ".env.local"]
279
281
  };
280
282
  function resolveConfig(config) {
283
+ let envFile;
284
+ if (config.envFile === false) {
285
+ envFile = false;
286
+ } else if (typeof config.envFile === "string") {
287
+ envFile = [config.envFile];
288
+ } else {
289
+ envFile = config.envFile ?? defaults.envFile;
290
+ }
281
291
  return {
282
292
  ...defaults,
283
293
  ...config,
294
+ envFile,
284
295
  output: {
285
296
  scalarDocs: false,
286
297
  scalarPath: "src/app/api/docs/route.ts",
@@ -289,6 +300,7 @@ function resolveConfig(config) {
289
300
  openapi: {
290
301
  description: "",
291
302
  servers: [],
303
+ components: {},
292
304
  ...config.openapi
293
305
  }
294
306
  };
@@ -308,7 +320,9 @@ async function loadConfig(configPath) {
308
320
  return resolveConfig(config);
309
321
  }
310
322
  }
311
- throw new Error("No openapi-gen.config.ts found. Create one at your project root.");
323
+ throw new Error(
324
+ "No openapi-gen.config.ts found. Create one at your project root."
325
+ );
312
326
  }
313
327
  async function importConfig(filePath) {
314
328
  if (filePath.endsWith(".ts")) {
@@ -318,20 +332,8 @@ async function importConfig(filePath) {
318
332
  return import(url);
319
333
  }
320
334
  async function importTypeScriptConfig(filePath) {
321
- try {
322
- await import("module");
323
- const url = (0, import_url.pathToFileURL)(filePath).href;
324
- return await import(url);
325
- } catch {
326
- try {
327
- require("ts-node/register");
328
- return require(filePath);
329
- } catch {
330
- throw new Error(
331
- `Cannot load TypeScript config file: ${filePath}. Install tsx or ts-node, or use a .js config file.`
332
- );
333
- }
334
- }
335
+ require("tsx/cjs");
336
+ return require(filePath);
335
337
  }
336
338
 
337
339
  // src/generator.ts
@@ -356,6 +358,12 @@ function assembleSpec(config, routes) {
356
358
  if (config.openapi.servers && config.openapi.servers.length > 0) {
357
359
  spec.servers = config.openapi.servers;
358
360
  }
361
+ if (config.openapi.security && config.openapi.security.length > 0) {
362
+ spec.security = config.openapi.security;
363
+ }
364
+ if (config.openapi.components && Object.keys(config.openapi.components).length > 0) {
365
+ spec.components = config.openapi.components;
366
+ }
359
367
  return spec;
360
368
  }
361
369
  function writeOutputFiles(config, spec, cwd = process.cwd()) {
@@ -469,13 +477,15 @@ function extractJsdoc(sourceCode) {
469
477
  const jsdocComments = [];
470
478
  let hasExactJsdoc = false;
471
479
  let exactPathItem;
472
- const match = jsdocRegex.exec(sourceCode);
473
- while (match !== null) {
480
+ let match;
481
+ while ((match = jsdocRegex.exec(sourceCode)) !== null) {
474
482
  const comment = match[0];
475
483
  jsdocComments.push(comment);
476
484
  if (/@openapi-exact/.test(comment)) {
477
485
  hasExactJsdoc = true;
478
- const openapiMatch = comment.match(/@openapi\s+([\s\S]*?)(?=\s*\*\/|\s*\*\s*@)/);
486
+ const openapiMatch = comment.match(
487
+ /@openapi\s+([\s\S]*?)(?=\s*\*\/|\s*\*\s*@)/
488
+ );
479
489
  if (openapiMatch) {
480
490
  try {
481
491
  const jsonStr = openapiMatch[1].split("\n").map((line) => line.replace(/^\s*\*\s?/, "")).join("\n").trim();
@@ -490,11 +500,18 @@ function extractJsdoc(sourceCode) {
490
500
  }
491
501
 
492
502
  // src/index.ts
503
+ var import_node_path = require("path");
493
504
  async function generate(options = {}) {
494
505
  const cwd = options.cwd ?? process.cwd();
495
506
  const config = await loadConfig(options.config);
496
507
  if (options.provider) config.provider = options.provider;
497
508
  if (options.cache === false) config.cache = false;
509
+ if (config.envFile !== false) {
510
+ const { config: dotenvConfig } = await import("dotenv");
511
+ for (const file of config.envFile) {
512
+ dotenvConfig({ path: (0, import_node_path.resolve)(cwd, file), override: false });
513
+ }
514
+ }
498
515
  console.log(`[openapi-ai-generator] Scanning routes...`);
499
516
  const routes = await scanRoutes(config.include, config.exclude, cwd);
500
517
  console.log(`[openapi-ai-generator] Found ${routes.length} route(s)`);
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cli.ts","../src/analyzer.ts","../src/cache.ts","../src/providers/index.ts","../src/config.ts","../src/generator.ts","../src/scanner.ts","../src/index.ts"],"sourcesContent":["import { Command } from 'commander';\n\nimport type { Provider } from './config.js';\n\nimport { generate } from './index.js';\n\nconst program = new Command();\n\nprogram\n\t.name('openapi-ai-generator')\n\t.description('Generate OpenAPI 3.1 specs from Next.js API routes using AI')\n\t.version('0.1.0');\n\nprogram\n\t.command('generate')\n\t.description('Scan Next.js API routes and generate an OpenAPI spec')\n\t.option('-c, --config <path>', 'Path to config file (default: openapi-gen.config.ts)')\n\t.option('-p, --provider <provider>', 'Override provider (azure | openai | anthropic)')\n\t.option('--no-cache', 'Disable caching (always re-analyze all routes)')\n\t.action(async (opts: { config?: string; provider?: string; cache: boolean }) => {\n\t\ttry {\n\t\t\tconst result = await generate({\n\t\t\t\tconfig: opts.config,\n\t\t\t\tprovider: opts.provider as Provider | undefined,\n\t\t\t\tcache: opts.cache,\n\t\t\t});\n\n\t\t\tconsole.log(`\\n✓ OpenAPI spec generated successfully`);\n\t\t\tconsole.log(` Routes analyzed: ${result.routesAnalyzed}`);\n\t\t\tconsole.log(` From cache: ${result.routesFromCache}`);\n\t\t\tconsole.log(\n\t\t\t\t` LLM calls made: ${result.routesAnalyzed - result.routesFromCache - (result.routesSkippedLLM - result.routesFromCache)}`,\n\t\t\t);\n\t\t\tconsole.log(` Spec written to: ${result.specPath}`);\n\t\t} catch (err) {\n\t\t\tconsole.error('[openapi-ai-generator] Error:', err instanceof Error ? err.message : err);\n\t\t\tprocess.exit(1);\n\t\t}\n\t});\n\nprogram.parse();\n","import type { LanguageModel } from 'ai';\nimport { generateText } from 'ai';\n\nimport type { JSDocMode, Provider } from './config.js';\nimport type { RouteInfo } from './scanner.js';\n\nimport { computeHash, RouteCache } from './cache.js';\nimport { createModel, getModelId } from './providers/index.js';\n\nexport interface AnalyzeOptions {\n\tprovider: Provider;\n\tjsdocMode: JSDocMode;\n\tcache: boolean;\n\tcacheDir: string;\n}\n\nexport interface AnalyzedRoute {\n\turlPath: string;\n\tpathItem: Record<string, unknown>;\n\tfromCache: boolean;\n\tskippedLLM: boolean;\n}\n\nexport async function analyzeRoutes(\n\troutes: RouteInfo[],\n\toptions: AnalyzeOptions,\n): Promise<AnalyzedRoute[]> {\n\tconst modelId = getModelId(options.provider);\n\tconst cache = options.cache ? new RouteCache(options.cacheDir) : null;\n\n\t// Lazy-init the model only when we actually need it\n\tlet model: LanguageModel | null = null;\n\tconst getModel = (): LanguageModel => {\n\t\tif (!model) model = createModel(options.provider);\n\t\treturn model;\n\t};\n\n\tconst results: AnalyzedRoute[] = [];\n\n\tfor (const route of routes) {\n\t\tconst result = await analyzeRoute(route, options, modelId, cache, getModel);\n\t\tresults.push(result);\n\t}\n\n\treturn results;\n}\n\nasync function analyzeRoute(\n\troute: RouteInfo,\n\toptions: AnalyzeOptions,\n\tmodelId: string,\n\tcache: RouteCache | null,\n\tgetModel: () => LanguageModel,\n): Promise<AnalyzedRoute> {\n\t// If @openapi-exact is present or jsdocMode is 'exact', skip LLM\n\tif (route.hasExactJsdoc && route.exactPathItem) {\n\t\treturn {\n\t\t\turlPath: route.urlPath,\n\t\t\tpathItem: route.exactPathItem,\n\t\t\tfromCache: false,\n\t\t\tskippedLLM: true,\n\t\t};\n\t}\n\n\tif (options.jsdocMode === 'exact') {\n\t\t// In exact mode, if there's a @openapi tag, use it; otherwise still use LLM\n\t\tif (route.hasExactJsdoc && route.exactPathItem) {\n\t\t\treturn {\n\t\t\t\turlPath: route.urlPath,\n\t\t\t\tpathItem: route.exactPathItem,\n\t\t\t\tfromCache: false,\n\t\t\t\tskippedLLM: true,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Compute cache hash\n\tconst hash = computeHash(route.sourceCode, options.provider, modelId);\n\n\t// Check cache\n\tif (cache) {\n\t\tconst cached = cache.get(hash);\n\t\tif (cached) {\n\t\t\treturn {\n\t\t\t\turlPath: route.urlPath,\n\t\t\t\tpathItem: cached,\n\t\t\t\tfromCache: true,\n\t\t\t\tskippedLLM: true,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Call LLM\n\tlet pathItem: Record<string, unknown>;\n\ttry {\n\t\tpathItem = await callLLM(route, options.jsdocMode, getModel());\n\t} catch (err) {\n\t\tconst message = err instanceof Error ? err.message : String(err);\n\t\tconst isContentFilter =\n\t\t\tmessage.includes('content filtering policy') ||\n\t\t\tmessage.includes('content_filter') ||\n\t\t\t(err as { status?: number })?.status === 400;\n\n\t\tif (isContentFilter) {\n\t\t\tconsole.warn(\n\t\t\t\t`Warning: Content filter blocked response for ${route.urlPath}. ` +\n\t\t\t\t\t`Skipping route. Use @openapi-exact JSDoc to provide the spec manually.`,\n\t\t\t);\n\t\t\treturn {\n\t\t\t\turlPath: route.urlPath,\n\t\t\t\tpathItem: {},\n\t\t\t\tfromCache: false,\n\t\t\t\tskippedLLM: false,\n\t\t\t};\n\t\t}\n\t\tthrow err;\n\t}\n\n\t// Store in cache\n\tif (cache) {\n\t\tcache.set(hash, pathItem);\n\t}\n\n\treturn {\n\t\turlPath: route.urlPath,\n\t\tpathItem,\n\t\tfromCache: false,\n\t\tskippedLLM: false,\n\t};\n}\n\nfunction buildPrompt(route: RouteInfo, jsdocMode: JSDocMode): string {\n\tconst jsDocSection =\n\t\troute.jsdocComments.length > 0\n\t\t\t? `JSDoc COMMENTS (use as ${jsdocMode === 'context' ? 'additional context' : 'primary source'}):\\n${route.jsdocComments.join('\\n\\n')}`\n\t\t\t: 'No JSDoc comments found.';\n\n\treturn `You are a technical documentation tool that reads existing source code and produces OpenAPI 3.1 documentation data. You do not write or execute code — you only read and describe it.\n\n## Task\n\nRead the Next.js API route source file below and produce a JSON object that documents its HTTP endpoints according to the OpenAPI 3.1 PathItem schema.\n\n## Route metadata\n\n- File: ${route.relativePath}\n- URL path: ${route.urlPath}\n\n## Source file contents\n\n\\`\\`\\`typescript\n${route.sourceCode}\n\\`\\`\\`\n\n${jsDocSection}\n\n## Instructions\n\nFor each exported function named GET, POST, PUT, PATCH, or DELETE, document:\n- operationId: a unique camelCase identifier\n- summary: a short one-line description\n- description: a fuller explanation of what the endpoint does\n- parameters: path params from URL segments like {id}, and query params from searchParams usage\n- requestBody: schema inferred from request.json() calls and TypeScript types (POST/PUT/PATCH only)\n- responses: per status code, inferred from NextResponse.json() calls and return type annotations\n- tags: inferred from the URL path segments\n- security: noted if the code checks for auth tokens, session cookies, or middleware guards\n\n## Output format\n\nReturn a single raw JSON object matching the OpenAPI 3.1 PathItem schema. No explanation, no markdown fences, no extra text — only the JSON object.`;\n}\n\nasync function callLLM(\n\troute: RouteInfo,\n\tjsdocMode: JSDocMode,\n\tmodel: LanguageModel,\n): Promise<Record<string, unknown>> {\n\tconst prompt = buildPrompt(route, jsdocMode);\n\n\tconst { text } = await generateText({\n\t\tmodel,\n\t\tprompt,\n\t\ttemperature: 0,\n\t});\n\n\treturn parsePathItem(text, route.urlPath);\n}\n\nfunction parsePathItem(text: string, urlPath: string): Record<string, unknown> {\n\t// Strip any accidental markdown code fences\n\tlet json = text.trim();\n\tif (json.startsWith('```')) {\n\t\tjson = json\n\t\t\t.replace(/^```(?:json)?\\s*/i, '')\n\t\t\t.replace(/\\s*```$/, '')\n\t\t\t.trim();\n\t}\n\n\ttry {\n\t\tconst parsed = JSON.parse(json);\n\t\tif (typeof parsed !== 'object' || Array.isArray(parsed) || parsed === null) {\n\t\t\tthrow new Error('Response is not a JSON object');\n\t\t}\n\t\treturn parsed;\n\t} catch (err) {\n\t\tconsole.warn(\n\t\t\t`Warning: Failed to parse LLM response for ${urlPath}. Using empty PathItem.`,\n\t\t\terr,\n\t\t);\n\t\treturn {};\n\t}\n}\n","import { createHash } from 'crypto';\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';\nimport { join } from 'path';\n\nexport interface CacheEntry {\n\thash: string;\n\tpathItem: Record<string, unknown>;\n\tcachedAt: string;\n}\n\nexport function computeHash(content: string, provider: string, modelId: string): string {\n\treturn createHash('sha256').update(content).update(provider).update(modelId).digest('hex');\n}\n\nexport class RouteCache {\n\tprivate readonly cacheDir: string;\n\n\tconstructor(cacheDir: string) {\n\t\tthis.cacheDir = cacheDir;\n\t}\n\n\tprivate ensureDir(): void {\n\t\tif (!existsSync(this.cacheDir)) {\n\t\t\tmkdirSync(this.cacheDir, { recursive: true });\n\t\t}\n\t}\n\n\tget(hash: string): Record<string, unknown> | null {\n\t\tconst filePath = join(this.cacheDir, `${hash}.json`);\n\t\tif (!existsSync(filePath)) return null;\n\t\ttry {\n\t\t\tconst entry: CacheEntry = JSON.parse(readFileSync(filePath, 'utf8'));\n\t\t\treturn entry.pathItem;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tset(hash: string, pathItem: Record<string, unknown>): void {\n\t\tthis.ensureDir();\n\t\tconst entry: CacheEntry = {\n\t\t\thash,\n\t\t\tpathItem,\n\t\t\tcachedAt: new Date().toISOString(),\n\t\t};\n\t\tconst filePath = join(this.cacheDir, `${hash}.json`);\n\t\twriteFileSync(filePath, JSON.stringify(entry, null, 2), 'utf8');\n\t}\n}\n","import type { LanguageModel } from 'ai';\n\nimport type { Provider } from '../config.js';\n\nexport function createModel(provider: Provider): LanguageModel {\n\tswitch (provider) {\n\t\tcase 'azure':\n\t\t\treturn createAzureModel();\n\t\tcase 'openai':\n\t\t\treturn createOpenAIModel();\n\t\tcase 'anthropic':\n\t\t\treturn createAnthropicModel();\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = provider;\n\t\t\tthrow new Error(`Unknown provider: ${_exhaustive}`);\n\t\t}\n\t}\n}\n\nfunction createAzureModel(): LanguageModel {\n\tconst endpoint = requireEnv('AZURE_OPENAI_ENDPOINT');\n\tconst apiKey = requireEnv('AZURE_OPENAI_API_KEY');\n\tconst deployment = requireEnv('AZURE_OPENAI_DEPLOYMENT');\n\n\t// Dynamic import to avoid loading unused provider SDKs\n\tconst { createAzure } = require('@ai-sdk/azure');\n\tconst azure = createAzure({ endpoint, apiKey });\n\treturn azure(deployment);\n}\n\nfunction createOpenAIModel(): LanguageModel {\n\tconst apiKey = requireEnv('OPENAI_API_KEY');\n\tconst model = process.env.OPENAI_MODEL ?? 'gpt-4o';\n\n\tconst { createOpenAI } = require('@ai-sdk/openai');\n\tconst openai = createOpenAI({ apiKey });\n\treturn openai(model);\n}\n\nfunction createAnthropicModel(): LanguageModel {\n\tconst apiKey = requireEnv('ANTHROPIC_API_KEY');\n\tconst model = process.env.ANTHROPIC_MODEL ?? 'claude-sonnet-4-6';\n\n\tconst { createAnthropic } = require('@ai-sdk/anthropic');\n\tconst anthropic = createAnthropic({ apiKey });\n\treturn anthropic(model);\n}\n\nfunction requireEnv(name: string): string {\n\tconst val = process.env[name];\n\tif (!val) {\n\t\tthrow new Error(`Required environment variable ${name} is not set.`);\n\t}\n\treturn val;\n}\n\nexport function getModelId(provider: Provider): string {\n\tswitch (provider) {\n\t\tcase 'azure':\n\t\t\treturn process.env.AZURE_OPENAI_DEPLOYMENT ?? 'unknown';\n\t\tcase 'openai':\n\t\t\treturn process.env.OPENAI_MODEL ?? 'gpt-4o';\n\t\tcase 'anthropic':\n\t\t\treturn process.env.ANTHROPIC_MODEL ?? 'claude-sonnet-4-6';\n\t}\n}\n","import { existsSync } from 'fs';\nimport { resolve } from 'path';\nimport { pathToFileURL } from 'url';\n\nexport type Provider = 'azure' | 'openai' | 'anthropic';\nexport type JSDocMode = 'context' | 'exact';\n\nexport interface OpenAPIGenConfig {\n\tprovider: Provider;\n\toutput: {\n\t\tspecPath: string;\n\t\tscalarDocs?: boolean;\n\t\tscalarPath?: string;\n\t};\n\topenapi: {\n\t\ttitle: string;\n\t\tversion: string;\n\t\tdescription?: string;\n\t\tservers?: Array<{ url: string; description?: string }>;\n\t};\n\tjsdocMode?: JSDocMode;\n\tcache?: boolean;\n\tcacheDir?: string;\n\tinclude?: string[];\n\texclude?: string[];\n}\n\nexport interface ResolvedConfig extends Required<OpenAPIGenConfig> {\n\toutput: Required<OpenAPIGenConfig['output']>;\n\topenapi: Required<OpenAPIGenConfig['openapi']>;\n}\n\nconst defaults: Omit<ResolvedConfig, 'provider' | 'output' | 'openapi'> = {\n\tjsdocMode: 'context',\n\tcache: true,\n\tcacheDir: '.openapi-cache',\n\tinclude: ['src/app/api/**/route.ts'],\n\texclude: [],\n};\n\nexport function resolveConfig(config: OpenAPIGenConfig): ResolvedConfig {\n\treturn {\n\t\t...defaults,\n\t\t...config,\n\t\toutput: {\n\t\t\tscalarDocs: false,\n\t\t\tscalarPath: 'src/app/api/docs/route.ts',\n\t\t\t...config.output,\n\t\t},\n\t\topenapi: {\n\t\t\tdescription: '',\n\t\t\tservers: [],\n\t\t\t...config.openapi,\n\t\t},\n\t};\n}\n\nexport async function loadConfig(configPath?: string): Promise<ResolvedConfig> {\n\tconst searchPaths = configPath\n\t\t? [configPath]\n\t\t: [\n\t\t\t\t'openapi-gen.config.ts',\n\t\t\t\t'openapi-gen.config.js',\n\t\t\t\t'openapi-gen.config.mjs',\n\t\t\t\t'openapi-gen.config.cjs',\n\t\t\t];\n\n\tfor (const p of searchPaths) {\n\t\tconst abs = resolve(process.cwd(), p);\n\t\tif (existsSync(abs)) {\n\t\t\tconst mod = await importConfig(abs);\n\t\t\tconst config: OpenAPIGenConfig = mod.default ?? mod;\n\t\t\treturn resolveConfig(config);\n\t\t}\n\t}\n\n\tthrow new Error('No openapi-gen.config.ts found. Create one at your project root.');\n}\n\nasync function importConfig(\n\tfilePath: string,\n): Promise<{ default?: OpenAPIGenConfig } & OpenAPIGenConfig> {\n\t// For .ts files, try to use tsx/ts-node if available, else fall back to require\n\tif (filePath.endsWith('.ts')) {\n\t\treturn importTypeScriptConfig(filePath);\n\t}\n\tconst url = pathToFileURL(filePath).href;\n\treturn import(url);\n}\n\nasync function importTypeScriptConfig(\n\tfilePath: string,\n): Promise<{ default?: OpenAPIGenConfig } & OpenAPIGenConfig> {\n\t// Try dynamic import with tsx register if available\n\ttry {\n\t\t// Check if tsx is available\n\t\tawait import('module');\n\t\t// Use tsx/ts-node loader\n\t\tconst url = pathToFileURL(filePath).href;\n\t\treturn await import(url);\n\t} catch {\n\t\t// Fall back: try require with ts-node\n\t\ttry {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-require-imports\n\t\t\trequire('ts-node/register');\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-require-imports\n\t\t\treturn require(filePath);\n\t\t} catch {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot load TypeScript config file: ${filePath}. ` +\n\t\t\t\t\t'Install tsx or ts-node, or use a .js config file.',\n\t\t\t);\n\t\t}\n\t}\n}\n","import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { dirname, join, resolve } from 'path';\n\nimport type { AnalyzedRoute } from './analyzer.js';\nimport type { ResolvedConfig } from './config.js';\n\nexport interface OpenAPISpec {\n\topenapi: '3.1.0';\n\tinfo: {\n\t\ttitle: string;\n\t\tversion: string;\n\t\tdescription?: string;\n\t};\n\tservers?: Array<{ url: string; description?: string }>;\n\tpaths: Record<string, unknown>;\n}\n\nexport function assembleSpec(config: ResolvedConfig, routes: AnalyzedRoute[]): OpenAPISpec {\n\tconst paths: Record<string, unknown> = {};\n\n\tfor (const route of routes) {\n\t\tif (Object.keys(route.pathItem).length > 0) {\n\t\t\tpaths[route.urlPath] = route.pathItem;\n\t\t}\n\t}\n\n\tconst spec: OpenAPISpec = {\n\t\topenapi: '3.1.0',\n\t\tinfo: {\n\t\t\ttitle: config.openapi.title,\n\t\t\tversion: config.openapi.version,\n\t\t\t...(config.openapi.description ? { description: config.openapi.description } : {}),\n\t\t},\n\t\tpaths,\n\t};\n\n\tif (config.openapi.servers && config.openapi.servers.length > 0) {\n\t\tspec.servers = config.openapi.servers;\n\t}\n\n\treturn spec;\n}\n\nexport function writeOutputFiles(\n\tconfig: ResolvedConfig,\n\tspec: OpenAPISpec,\n\tcwd: string = process.cwd(),\n): void {\n\twriteSpecFiles(config, spec, cwd);\n\n\tif (config.output.scalarDocs) {\n\t\twriteScalarRoute(config, cwd);\n\t}\n}\n\nfunction writeSpecFiles(config: ResolvedConfig, spec: OpenAPISpec, cwd: string): void {\n\tconst specRoutePath = resolve(cwd, config.output.specPath);\n\tconst specDir = dirname(specRoutePath);\n\n\tensureDir(specDir);\n\n\t// Write spec.json co-located with the route\n\tconst specJsonPath = join(specDir, 'spec.json');\n\twriteFileSync(specJsonPath, JSON.stringify(spec, null, 2), 'utf8');\n\n\t// Write the Next.js route that serves the spec\n\tconst routeContent = `import spec from './spec.json';\n\nexport const dynamic = 'force-static';\n\nexport function GET() {\n return Response.json(spec);\n}\n`;\n\twriteFileSync(specRoutePath, routeContent, 'utf8');\n}\n\nfunction writeScalarRoute(config: ResolvedConfig, cwd: string): void {\n\tconst scalarRoutePath = resolve(cwd, config.output.scalarPath);\n\tensureDir(dirname(scalarRoutePath));\n\n\t// Derive the spec URL from the specPath\n\tconst specUrl = filePathToApiUrl(config.output.specPath);\n\n\tconst routeContent = `export const dynamic = 'force-static';\n\nexport function GET() {\n return new Response(\n \\`<!doctype html>\n<html>\n <head>\n <title>API Docs</title>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n data-url=\"${specUrl}\"\n ></script>\n <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n </body>\n</html>\\`,\n { headers: { 'Content-Type': 'text/html' } }\n );\n}\n`;\n\twriteFileSync(scalarRoutePath, routeContent, 'utf8');\n}\n\n/**\n * Convert a file path like src/app/api/openapi.json/route.ts to /api/openapi.json\n */\nfunction filePathToApiUrl(filePath: string): string {\n\tlet path = filePath.replace(/\\\\/g, '/');\n\tpath = path.replace(/^(src\\/)?app\\//, '');\n\tpath = path.replace(/\\/route\\.(ts|tsx|js|jsx)$/, '');\n\tif (!path.startsWith('/')) path = `/${path}`;\n\treturn path;\n}\n\nfunction ensureDir(dir: string): void {\n\tif (!existsSync(dir)) {\n\t\tmkdirSync(dir, { recursive: true });\n\t}\n}\n","import { readFileSync } from 'fs';\nimport { relative } from 'path';\n\nexport interface RouteInfo {\n\tfilePath: string;\n\trelativePath: string;\n\turlPath: string;\n\tsourceCode: string;\n\tjsdocComments: string[];\n\thasExactJsdoc: boolean;\n\texactPathItem?: Record<string, unknown>;\n}\n\nexport async function scanRoutes(\n\tinclude: string[],\n\texclude: string[],\n\tcwd: string = process.cwd(),\n): Promise<RouteInfo[]> {\n\t// Lazy import so that importing this module does not eagerly load fast-glob.\n\t// This keeps the module lightweight and allows Vitest to mock it cleanly.\n\tconst { default: fg } = await import('fast-glob');\n\tconst files = await fg(include, {\n\t\tcwd,\n\t\tignore: exclude,\n\t\tabsolute: true,\n\t});\n\n\treturn files.map((filePath) => parseRoute(filePath, cwd));\n}\n\nfunction parseRoute(filePath: string, cwd: string): RouteInfo {\n\tconst relativePath = relative(cwd, filePath);\n\tconst sourceCode = readFileSync(filePath, 'utf8');\n\tconst urlPath = filePathToUrlPath(relativePath);\n\tconst { jsdocComments, hasExactJsdoc, exactPathItem } = extractJsdoc(sourceCode);\n\n\treturn {\n\t\tfilePath,\n\t\trelativePath,\n\t\turlPath,\n\t\tsourceCode,\n\t\tjsdocComments,\n\t\thasExactJsdoc,\n\t\texactPathItem,\n\t};\n}\n\n/**\n * Convert a Next.js route file path to an OpenAPI URL path.\n * e.g. src/app/api/users/[id]/route.ts -> /api/users/{id}\n */\nexport function filePathToUrlPath(filePath: string): string {\n\t// Normalize separators\n\tlet path = filePath.replace(/\\\\/g, '/');\n\n\t// Remove leading src/ or app/ prefixes\n\tpath = path.replace(/^(src\\/)?app\\//, '');\n\n\t// Remove trailing /route.ts or /route.js\n\tpath = path.replace(/\\/route\\.(ts|tsx|js|jsx)$/, '');\n\n\t// Convert Next.js dynamic segments [param] to OpenAPI {param}\n\tpath = path.replace(/\\[([^\\]]+)\\]/g, (_, param) => {\n\t\t// Handle catch-all [...param] and optional [[...param]]\n\t\tif (param.startsWith('...')) {\n\t\t\treturn `{${param.slice(3)}}`;\n\t\t}\n\t\treturn `{${param}}`;\n\t});\n\n\t// Ensure leading slash\n\tif (!path.startsWith('/')) {\n\t\tpath = `/${path}`;\n\t}\n\n\treturn path;\n}\n\ninterface JsdocResult {\n\tjsdocComments: string[];\n\thasExactJsdoc: boolean;\n\texactPathItem?: Record<string, unknown>;\n}\n\nfunction extractJsdoc(sourceCode: string): JsdocResult {\n\t// Match all JSDoc comment blocks /** ... */\n\tconst jsdocRegex = /\\/\\*\\*([\\s\\S]*?)\\*\\//g;\n\tconst jsdocComments: string[] = [];\n\tlet hasExactJsdoc = false;\n\tlet exactPathItem: Record<string, unknown> | undefined;\n\n\tconst match: RegExpExecArray | null = jsdocRegex.exec(sourceCode);\n\twhile (match !== null) {\n\t\tconst comment = match[0];\n\t\tjsdocComments.push(comment);\n\n\t\t// Check for @openapi-exact tag\n\t\tif (/@openapi-exact/.test(comment)) {\n\t\t\thasExactJsdoc = true;\n\t\t\t// Try to extract the JSON from @openapi tag\n\t\t\tconst openapiMatch = comment.match(/@openapi\\s+([\\s\\S]*?)(?=\\s*\\*\\/|\\s*\\*\\s*@)/);\n\t\t\tif (openapiMatch) {\n\t\t\t\ttry {\n\t\t\t\t\t// Clean up JSDoc asterisks from the JSON\n\t\t\t\t\tconst jsonStr = openapiMatch[1]\n\t\t\t\t\t\t.split('\\n')\n\t\t\t\t\t\t.map((line) => line.replace(/^\\s*\\*\\s?/, ''))\n\t\t\t\t\t\t.join('\\n')\n\t\t\t\t\t\t.trim();\n\t\t\t\t\texactPathItem = JSON.parse(jsonStr);\n\t\t\t\t} catch {\n\t\t\t\t\t// If JSON parse fails, fall through to LLM\n\t\t\t\t\thasExactJsdoc = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { jsdocComments, hasExactJsdoc, exactPathItem };\n}\n","export type { JSDocMode, OpenAPIGenConfig, Provider, ResolvedConfig } from './config.js';\n\nexport { analyzeRoutes } from './analyzer.js';\nexport { loadConfig, resolveConfig } from './config.js';\nexport { assembleSpec, writeOutputFiles } from './generator.js';\nexport { filePathToUrlPath, scanRoutes } from './scanner.js';\n\nimport type { OpenAPIGenConfig } from './config.js';\n\nimport { analyzeRoutes } from './analyzer.js';\nimport { loadConfig } from './config.js';\nimport { assembleSpec, writeOutputFiles } from './generator.js';\nimport { scanRoutes } from './scanner.js';\n\nexport interface GenerateOptions {\n\tconfig?: string;\n\tprovider?: OpenAPIGenConfig['provider'];\n\tcache?: boolean;\n\tcwd?: string;\n}\n\nexport interface GenerateResult {\n\troutesAnalyzed: number;\n\troutesFromCache: number;\n\troutesSkippedLLM: number;\n\tspecPath: string;\n}\n\nexport async function generate(options: GenerateOptions = {}): Promise<GenerateResult> {\n\tconst cwd = options.cwd ?? process.cwd();\n\tconst config = await loadConfig(options.config);\n\n\t// Allow CLI overrides\n\tif (options.provider) config.provider = options.provider;\n\tif (options.cache === false) config.cache = false;\n\n\tconsole.log(`[openapi-ai-generator] Scanning routes...`);\n\tconst routes = await scanRoutes(config.include, config.exclude, cwd);\n\tconsole.log(`[openapi-ai-generator] Found ${routes.length} route(s)`);\n\n\tconsole.log(`[openapi-ai-generator] Analyzing routes with provider: ${config.provider}`);\n\tconst analyzed = await analyzeRoutes(routes, {\n\t\tprovider: config.provider,\n\t\tjsdocMode: config.jsdocMode,\n\t\tcache: config.cache,\n\t\tcacheDir: config.cacheDir,\n\t});\n\n\tconst fromCache = analyzed.filter((r) => r.fromCache).length;\n\tconst skippedLLM = analyzed.filter((r) => r.skippedLLM).length;\n\tconsole.log(\n\t\t`[openapi-ai-generator] ${analyzed.length} routes analyzed (${fromCache} from cache, ${skippedLLM - fromCache} exact JSDoc)`,\n\t);\n\n\tconst spec = assembleSpec(config, analyzed);\n\twriteOutputFiles(config, spec, cwd);\n\n\tconsole.log(`[openapi-ai-generator] Spec written to ${config.output.specPath}`);\n\tif (config.output.scalarDocs) {\n\t\tconsole.log(`[openapi-ai-generator] Scalar docs written to ${config.output.scalarPath}`);\n\t}\n\n\treturn {\n\t\troutesAnalyzed: analyzed.length,\n\t\troutesFromCache: fromCache,\n\t\troutesSkippedLLM: skippedLLM,\n\t\tspecPath: config.output.specPath,\n\t};\n}\n"],"mappings":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AAA,IACjB,kBAAkB;AAAA,IAClB,UAAU,OAAO,OAAO;AAAA,EACzB;AACD;;;AP9DA,IAAM,UAAU,IAAI,yBAAQ;AAE5B,QACE,KAAK,sBAAsB,EAC3B,YAAY,6DAA6D,EACzE,QAAQ,OAAO;AAEjB,QACE,QAAQ,UAAU,EAClB,YAAY,sDAAsD,EAClE,OAAO,uBAAuB,sDAAsD,EACpF,OAAO,6BAA6B,gDAAgD,EACpF,OAAO,cAAc,gDAAgD,EACrE,OAAO,OAAO,SAAiE;AAC/E,MAAI;AACH,UAAM,SAAS,MAAM,SAAS;AAAA,MAC7B,QAAQ,KAAK;AAAA,MACb,UAAU,KAAK;AAAA,MACf,OAAO,KAAK;AAAA,IACb,CAAC;AAED,YAAQ,IAAI;AAAA,2CAAyC;AACrD,YAAQ,IAAI,yBAAyB,OAAO,cAAc,EAAE;AAC5D,YAAQ,IAAI,yBAAyB,OAAO,eAAe,EAAE;AAC7D,YAAQ;AAAA,MACP,yBAAyB,OAAO,iBAAiB,OAAO,mBAAmB,OAAO,mBAAmB,OAAO,gBAAgB;AAAA,IAC7H;AACA,YAAQ,IAAI,yBAAyB,OAAO,QAAQ,EAAE;AAAA,EACvD,SAAS,KAAK;AACb,YAAQ,MAAM,iCAAiC,eAAe,QAAQ,IAAI,UAAU,GAAG;AACvF,YAAQ,KAAK,CAAC;AAAA,EACf;AACD,CAAC;AAEF,QAAQ,MAAM;","names":["import_fs","import_path","import_fs","import_path","import_fs","import_path"]}
1
+ {"version":3,"sources":["../src/cli.ts","../src/analyzer.ts","../src/cache.ts","../src/providers/index.ts","../src/config.ts","../src/generator.ts","../src/scanner.ts","../src/index.ts"],"sourcesContent":["import { Command } from 'commander';\n\nimport type { Provider } from './config.js';\n\nimport { generate } from './index.js';\n\nconst program = new Command();\n\nprogram\n\t.name('openapi-ai-generator')\n\t.description('Generate OpenAPI 3.1 specs from Next.js API routes using AI')\n\t.version('0.1.0');\n\nprogram\n\t.command('generate')\n\t.description('Scan Next.js API routes and generate an OpenAPI spec')\n\t.option('-c, --config <path>', 'Path to config file (default: openapi-gen.config.ts)')\n\t.option('-p, --provider <provider>', 'Override provider (azure | openai | anthropic)')\n\t.option('--no-cache', 'Disable caching (always re-analyze all routes)')\n\t.action(async (opts: { config?: string; provider?: string; cache: boolean }) => {\n\t\ttry {\n\t\t\tconst result = await generate({\n\t\t\t\tconfig: opts.config,\n\t\t\t\tprovider: opts.provider as Provider | undefined,\n\t\t\t\tcache: opts.cache,\n\t\t\t});\n\n\t\t\tconsole.log(`\\n✓ OpenAPI spec generated successfully`);\n\t\t\tconsole.log(` Routes analyzed: ${result.routesAnalyzed}`);\n\t\t\tconsole.log(` From cache: ${result.routesFromCache}`);\n\t\t\tconsole.log(\n\t\t\t\t` LLM calls made: ${result.routesAnalyzed - result.routesFromCache - (result.routesSkippedLLM - result.routesFromCache)}`,\n\t\t\t);\n\t\t\tconsole.log(` Spec written to: ${result.specPath}`);\n\t\t} catch (err) {\n\t\t\tconsole.error('[openapi-ai-generator] Error:', err instanceof Error ? err.message : err);\n\t\t\tprocess.exit(1);\n\t\t}\n\t});\n\nprogram.parse();\n","import type { LanguageModel } from \"ai\";\nimport { generateText } from \"ai\";\n\nimport type { JSDocMode, Provider } from \"./config.js\";\nimport type { RouteInfo } from \"./scanner.js\";\n\nimport { computeHash, RouteCache } from \"./cache.js\";\nimport { createModel, getModelId } from \"./providers/index.js\";\n\nexport interface AnalyzeOptions {\n provider: Provider;\n jsdocMode: JSDocMode;\n cache: boolean;\n cacheDir: string;\n}\n\nexport interface AnalyzedRoute {\n urlPath: string;\n pathItem: Record<string, unknown>;\n fromCache: boolean;\n skippedLLM: boolean;\n}\n\nexport async function analyzeRoutes(\n routes: RouteInfo[],\n options: AnalyzeOptions,\n): Promise<AnalyzedRoute[]> {\n const modelId = getModelId(options.provider);\n const cache = options.cache ? new RouteCache(options.cacheDir) : null;\n\n // Lazy-init the model only when we actually need it\n let model: LanguageModel | null = null;\n const getModel = (): LanguageModel => {\n if (!model) model = createModel(options.provider);\n return model;\n };\n\n const results: AnalyzedRoute[] = [];\n\n for (const route of routes) {\n const result = await analyzeRoute(route, options, modelId, cache, getModel);\n results.push(result);\n }\n\n return results;\n}\n\nasync function analyzeRoute(\n route: RouteInfo,\n options: AnalyzeOptions,\n modelId: string,\n cache: RouteCache | null,\n getModel: () => LanguageModel,\n): Promise<AnalyzedRoute> {\n // If @openapi-exact is present or jsdocMode is 'exact', skip LLM\n if (route.hasExactJsdoc && route.exactPathItem) {\n return {\n urlPath: route.urlPath,\n pathItem: route.exactPathItem,\n fromCache: false,\n skippedLLM: true,\n };\n }\n\n if (options.jsdocMode === \"exact\") {\n // In exact mode, if there's a @openapi tag, use it; otherwise still use LLM\n if (route.hasExactJsdoc && route.exactPathItem) {\n return {\n urlPath: route.urlPath,\n pathItem: route.exactPathItem,\n fromCache: false,\n skippedLLM: true,\n };\n }\n }\n\n // Compute cache hash\n const hash = computeHash(route.sourceCode, options.provider, modelId);\n\n // Check cache\n if (cache) {\n const cached = cache.get(hash);\n if (cached) {\n return {\n urlPath: route.urlPath,\n pathItem: cached,\n fromCache: true,\n skippedLLM: true,\n };\n }\n }\n\n // Call LLM\n let pathItem: Record<string, unknown>;\n try {\n pathItem = await callLLM(route, options.jsdocMode, getModel());\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n const isContentFilter =\n message.includes(\"content filtering policy\") ||\n message.includes(\"content_filter\") ||\n (err as { status?: number })?.status === 400;\n\n if (isContentFilter) {\n console.warn(\n `Warning: Content filter blocked response for ${route.urlPath}. ` +\n `Skipping route. Use @openapi-exact JSDoc to provide the spec manually.`,\n );\n return {\n urlPath: route.urlPath,\n pathItem: {},\n fromCache: false,\n skippedLLM: false,\n };\n }\n throw err;\n }\n\n // Store in cache\n if (cache) {\n cache.set(hash, pathItem);\n }\n\n return {\n urlPath: route.urlPath,\n pathItem,\n fromCache: false,\n skippedLLM: false,\n };\n}\n\nfunction buildPrompt(route: RouteInfo, jsdocMode: JSDocMode): string {\n const jsDocSection =\n route.jsdocComments.length > 0\n ? `JSDoc COMMENTS (use as ${jsdocMode === \"context\" ? \"additional context\" : \"primary source\"}):\\n${route.jsdocComments.join(\"\\n\\n\")}`\n : \"No JSDoc comments found.\";\n\n return `You are a technical documentation tool that reads existing source code and produces OpenAPI 3.1 documentation data. You do not write or execute code — you only read and describe it.\n\n## Task\n\nRead the Next.js API route source file below and produce a JSON object that documents its HTTP endpoints according to the OpenAPI 3.1 PathItem schema.\n\n## Route metadata\n\n- File: ${route.relativePath}\n- URL path: ${route.urlPath}\n\n## Source file contents\n\n\\`\\`\\`typescript\n${route.sourceCode}\n\\`\\`\\`\n\n${jsDocSection}\n\n## Instructions\n\nFor each exported function named GET, POST, PUT, PATCH, or DELETE, document:\n- operationId: a unique camelCase identifier\n- summary: a short one-line description (max ~4 words, avoid starting with \"Get\", \"Post\", \"Put\", \"Patch\", \"Delete\", these are sometimes inferred from the operationId, and are sometimes in the format of \"Create a ___\", \"Update a ___\", \"Delete a ___\", \"Get a ___\", \"List ___\")\n- description: a fuller explanation of what the endpoint does\n- parameters: path params from URL segments like {id}, and query params from searchParams usage\n- requestBody: schema inferred from request.json() calls and TypeScript types (POST/PUT/PATCH only)\n- responses: per status code, inferred from NextResponse.json() calls and return type annotations\n- tags: inferred from the URL path segments, but typically only one tag should be used. If more than one exists, they show up in multiple categories, and that can be confusing. If there's a question, use the more specific option.\n- security: noted if the code checks for auth tokens, session cookies, or middleware guards\n\n## Output format\n\nReturn a single raw JSON object matching the OpenAPI 3.1 PathItem schema. No explanation, no markdown fences, no extra text — only the JSON object.`;\n}\n\nasync function callLLM(\n route: RouteInfo,\n jsdocMode: JSDocMode,\n model: LanguageModel,\n): Promise<Record<string, unknown>> {\n const prompt = buildPrompt(route, jsdocMode);\n\n const { text } = await generateText({\n model,\n prompt,\n temperature: 0,\n });\n\n return parsePathItem(text, route.urlPath);\n}\n\nfunction parsePathItem(text: string, urlPath: string): Record<string, unknown> {\n // Strip any accidental markdown code fences\n let json = text.trim();\n if (json.startsWith(\"```\")) {\n json = json\n .replace(/^```(?:json)?\\s*/i, \"\")\n .replace(/\\s*```$/, \"\")\n .trim();\n }\n\n try {\n const parsed = JSON.parse(json);\n if (\n typeof parsed !== \"object\" ||\n Array.isArray(parsed) ||\n parsed === null\n ) {\n throw new Error(\"Response is not a JSON object\");\n }\n return parsed;\n } catch (err) {\n console.warn(\n `Warning: Failed to parse LLM response for ${urlPath}. Using empty PathItem.`,\n err,\n );\n return {};\n }\n}\n","import { createHash } from 'crypto';\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';\nimport { join } from 'path';\n\nexport interface CacheEntry {\n\thash: string;\n\tpathItem: Record<string, unknown>;\n\tcachedAt: string;\n}\n\nexport function computeHash(content: string, provider: string, modelId: string): string {\n\treturn createHash('sha256').update(content).update(provider).update(modelId).digest('hex');\n}\n\nexport class RouteCache {\n\tprivate readonly cacheDir: string;\n\n\tconstructor(cacheDir: string) {\n\t\tthis.cacheDir = cacheDir;\n\t}\n\n\tprivate ensureDir(): void {\n\t\tif (!existsSync(this.cacheDir)) {\n\t\t\tmkdirSync(this.cacheDir, { recursive: true });\n\t\t}\n\t}\n\n\tget(hash: string): Record<string, unknown> | null {\n\t\tconst filePath = join(this.cacheDir, `${hash}.json`);\n\t\tif (!existsSync(filePath)) return null;\n\t\ttry {\n\t\t\tconst entry: CacheEntry = JSON.parse(readFileSync(filePath, 'utf8'));\n\t\t\treturn entry.pathItem;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tset(hash: string, pathItem: Record<string, unknown>): void {\n\t\tthis.ensureDir();\n\t\tconst entry: CacheEntry = {\n\t\t\thash,\n\t\t\tpathItem,\n\t\t\tcachedAt: new Date().toISOString(),\n\t\t};\n\t\tconst filePath = join(this.cacheDir, `${hash}.json`);\n\t\twriteFileSync(filePath, JSON.stringify(entry, null, 2), 'utf8');\n\t}\n}\n","import type { LanguageModel } from \"ai\";\n\nimport type { Provider } from \"../config.js\";\n\nexport function createModel(provider: Provider): LanguageModel {\n switch (provider) {\n case \"azure\":\n return createAzureModel();\n case \"openai\":\n return createOpenAIModel();\n case \"anthropic\":\n return createAnthropicModel();\n default: {\n const _exhaustive: never = provider;\n throw new Error(`Unknown provider: ${_exhaustive}`);\n }\n }\n}\n\nfunction createAzureModel(): LanguageModel {\n const endpoint = requireEnv(\"AZURE_OPENAI_ENDPOINT\");\n const resourceName = endpoint?.match(/https?:\\/\\/([^.]+)/)?.[1];\n const apiKey = requireEnv(\"AZURE_OPENAI_API_KEY\");\n const deployment = requireEnv(\"AZURE_OPENAI_DEPLOYMENT\");\n\n // Dynamic import to avoid loading unused provider SDKs\n const { createAzure } = require(\"@ai-sdk/azure\");\n const azure = createAzure({ resourceName, apiKey });\n return azure(deployment);\n}\n\nfunction createOpenAIModel(): LanguageModel {\n const apiKey = requireEnv(\"OPENAI_API_KEY\");\n const model = process.env.OPENAI_MODEL ?? \"gpt-4o\";\n\n const { createOpenAI } = require(\"@ai-sdk/openai\");\n const openai = createOpenAI({ apiKey });\n return openai(model);\n}\n\nfunction createAnthropicModel(): LanguageModel {\n const apiKey = requireEnv(\"ANTHROPIC_API_KEY\");\n const model = process.env.ANTHROPIC_MODEL ?? \"claude-sonnet-4-6\";\n\n const { createAnthropic } = require(\"@ai-sdk/anthropic\");\n const anthropic = createAnthropic({ apiKey });\n return anthropic(model);\n}\n\nfunction requireEnv(name: string): string {\n const val = process.env[name];\n if (!val) {\n throw new Error(`Required environment variable ${name} is not set.`);\n }\n return val;\n}\n\nexport function getModelId(provider: Provider): string {\n switch (provider) {\n case \"azure\":\n return process.env.AZURE_OPENAI_DEPLOYMENT ?? \"unknown\";\n case \"openai\":\n return process.env.OPENAI_MODEL ?? \"gpt-4o\";\n case \"anthropic\":\n return process.env.ANTHROPIC_MODEL ?? \"claude-sonnet-4-6\";\n }\n}\n","import { existsSync } from \"fs\";\nimport { resolve } from \"path\";\nimport { pathToFileURL } from \"url\";\n\nexport type Provider = \"azure\" | \"openai\" | \"anthropic\";\nexport type JSDocMode = \"context\" | \"exact\";\n\nexport interface OpenAPIGenConfig {\n provider: Provider;\n output: {\n specPath: string;\n scalarDocs?: boolean;\n scalarPath?: string;\n };\n openapi: {\n title: string;\n version: string;\n description?: string;\n servers?: Array<{ url: string; description?: string }>;\n security: Array<{ [key: string]: string[] }>;\n components?: {\n securitySchemes?: {\n [key: string]: {\n type: string;\n in: string;\n name: string;\n };\n };\n };\n };\n jsdocMode?: JSDocMode;\n cache?: boolean;\n cacheDir?: string;\n include?: string[];\n exclude?: string[];\n /**\n * Path(s) to .env files to load before running. Defaults to ['.env', '.env.local'].\n * Set to false to disable automatic .env loading.\n */\n envFile?: string | string[] | false;\n}\n\nexport interface ResolvedConfig extends Omit<\n Required<OpenAPIGenConfig>,\n \"envFile\"\n> {\n output: Required<OpenAPIGenConfig[\"output\"]>;\n openapi: Required<OpenAPIGenConfig[\"openapi\"]>;\n envFile: string[] | false;\n}\n\nconst defaults: Omit<ResolvedConfig, \"provider\" | \"output\" | \"openapi\"> = {\n jsdocMode: \"context\",\n cache: true,\n cacheDir: \".openapi-cache\",\n include: [\"src/app/api/**/route.ts\"],\n exclude: [],\n envFile: [\".env\", \".env.local\"],\n};\n\nexport function resolveConfig(config: OpenAPIGenConfig): ResolvedConfig {\n let envFile: string[] | false;\n if (config.envFile === false) {\n envFile = false;\n } else if (typeof config.envFile === \"string\") {\n envFile = [config.envFile];\n } else {\n envFile = config.envFile ?? (defaults.envFile as string[]);\n }\n\n return {\n ...defaults,\n ...config,\n envFile,\n output: {\n scalarDocs: false,\n scalarPath: \"src/app/api/docs/route.ts\",\n ...config.output,\n },\n openapi: {\n description: \"\",\n servers: [],\n components: {},\n ...config.openapi,\n },\n };\n}\n\nexport async function loadConfig(configPath?: string): Promise<ResolvedConfig> {\n const searchPaths = configPath\n ? [configPath]\n : [\n \"openapi-gen.config.ts\",\n \"openapi-gen.config.js\",\n \"openapi-gen.config.mjs\",\n \"openapi-gen.config.cjs\",\n ];\n\n for (const p of searchPaths) {\n const abs = resolve(process.cwd(), p);\n if (existsSync(abs)) {\n const mod = await importConfig(abs);\n const config: OpenAPIGenConfig = mod.default ?? mod;\n return resolveConfig(config);\n }\n }\n\n throw new Error(\n \"No openapi-gen.config.ts found. Create one at your project root.\",\n );\n}\n\nasync function importConfig(\n filePath: string,\n): Promise<{ default?: OpenAPIGenConfig } & OpenAPIGenConfig> {\n // For .ts files, try to use tsx/ts-node if available, else fall back to require\n if (filePath.endsWith(\".ts\")) {\n return importTypeScriptConfig(filePath);\n }\n const url = pathToFileURL(filePath).href;\n return import(url);\n}\n\nasync function importTypeScriptConfig(\n filePath: string,\n): Promise<{ default?: OpenAPIGenConfig } & OpenAPIGenConfig> {\n // Use tsx (bundled as a dependency) to register CJS TypeScript hooks\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n require(\"tsx/cjs\");\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n return require(filePath);\n}\n","import { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { dirname, join, resolve } from 'path';\n\nimport type { AnalyzedRoute } from './analyzer.js';\nimport type { ResolvedConfig } from './config.js';\n\nexport interface OpenAPISpec {\n\topenapi: '3.1.0';\n\tinfo: {\n\t\ttitle: string;\n\t\tversion: string;\n\t\tdescription?: string;\n\t};\n\tservers?: Array<{ url: string; description?: string }>;\n\tsecurity?: Array<{ [key: string]: string[] }>;\n\tcomponents?: {\n\t\tsecuritySchemes?: Record<string, unknown>;\n\t\t[key: string]: unknown;\n\t};\n\tpaths: Record<string, unknown>;\n}\n\nexport function assembleSpec(config: ResolvedConfig, routes: AnalyzedRoute[]): OpenAPISpec {\n\tconst paths: Record<string, unknown> = {};\n\n\tfor (const route of routes) {\n\t\tif (Object.keys(route.pathItem).length > 0) {\n\t\t\tpaths[route.urlPath] = route.pathItem;\n\t\t}\n\t}\n\n\tconst spec: OpenAPISpec = {\n\t\topenapi: '3.1.0',\n\t\tinfo: {\n\t\t\ttitle: config.openapi.title,\n\t\t\tversion: config.openapi.version,\n\t\t\t...(config.openapi.description ? { description: config.openapi.description } : {}),\n\t\t},\n\t\tpaths,\n\t};\n\n\tif (config.openapi.servers && config.openapi.servers.length > 0) {\n\t\tspec.servers = config.openapi.servers;\n\t}\n\n\tif (config.openapi.security && config.openapi.security.length > 0) {\n\t\tspec.security = config.openapi.security;\n\t}\n\n\tif (config.openapi.components && Object.keys(config.openapi.components).length > 0) {\n\t\tspec.components = config.openapi.components;\n\t}\n\n\treturn spec;\n}\n\nexport function writeOutputFiles(\n\tconfig: ResolvedConfig,\n\tspec: OpenAPISpec,\n\tcwd: string = process.cwd(),\n): void {\n\twriteSpecFiles(config, spec, cwd);\n\n\tif (config.output.scalarDocs) {\n\t\twriteScalarRoute(config, cwd);\n\t}\n}\n\nfunction writeSpecFiles(config: ResolvedConfig, spec: OpenAPISpec, cwd: string): void {\n\tconst specRoutePath = resolve(cwd, config.output.specPath);\n\tconst specDir = dirname(specRoutePath);\n\n\tensureDir(specDir);\n\n\t// Write spec.json co-located with the route\n\tconst specJsonPath = join(specDir, 'spec.json');\n\twriteFileSync(specJsonPath, JSON.stringify(spec, null, 2), 'utf8');\n\n\t// Write the Next.js route that serves the spec\n\tconst routeContent = `import spec from './spec.json';\n\nexport const dynamic = 'force-static';\n\nexport function GET() {\n return Response.json(spec);\n}\n`;\n\twriteFileSync(specRoutePath, routeContent, 'utf8');\n}\n\nfunction writeScalarRoute(config: ResolvedConfig, cwd: string): void {\n\tconst scalarRoutePath = resolve(cwd, config.output.scalarPath);\n\tensureDir(dirname(scalarRoutePath));\n\n\t// Derive the spec URL from the specPath\n\tconst specUrl = filePathToApiUrl(config.output.specPath);\n\n\tconst routeContent = `export const dynamic = 'force-static';\n\nexport function GET() {\n return new Response(\n \\`<!doctype html>\n<html>\n <head>\n <title>API Docs</title>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n data-url=\"${specUrl}\"\n ></script>\n <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n </body>\n</html>\\`,\n { headers: { 'Content-Type': 'text/html' } }\n );\n}\n`;\n\twriteFileSync(scalarRoutePath, routeContent, 'utf8');\n}\n\n/**\n * Convert a file path like src/app/api/openapi.json/route.ts to /api/openapi.json\n */\nfunction filePathToApiUrl(filePath: string): string {\n\tlet path = filePath.replace(/\\\\/g, '/');\n\tpath = path.replace(/^(src\\/)?app\\//, '');\n\tpath = path.replace(/\\/route\\.(ts|tsx|js|jsx)$/, '');\n\tif (!path.startsWith('/')) path = `/${path}`;\n\treturn path;\n}\n\nfunction ensureDir(dir: string): void {\n\tif (!existsSync(dir)) {\n\t\tmkdirSync(dir, { recursive: true });\n\t}\n}\n","import { readFileSync } from \"fs\";\nimport { relative } from \"path\";\n\nexport interface RouteInfo {\n filePath: string;\n relativePath: string;\n urlPath: string;\n sourceCode: string;\n jsdocComments: string[];\n hasExactJsdoc: boolean;\n exactPathItem?: Record<string, unknown>;\n}\n\nexport async function scanRoutes(\n include: string[],\n exclude: string[],\n cwd: string = process.cwd(),\n): Promise<RouteInfo[]> {\n // Lazy import so that importing this module does not eagerly load fast-glob.\n // This keeps the module lightweight and allows Vitest to mock it cleanly.\n const { default: fg } = await import(\"fast-glob\");\n const files = await fg(include, {\n cwd,\n ignore: exclude,\n absolute: true,\n });\n\n return files.map((filePath) => parseRoute(filePath, cwd));\n}\n\nfunction parseRoute(filePath: string, cwd: string): RouteInfo {\n const relativePath = relative(cwd, filePath);\n const sourceCode = readFileSync(filePath, \"utf8\");\n const urlPath = filePathToUrlPath(relativePath);\n const { jsdocComments, hasExactJsdoc, exactPathItem } =\n extractJsdoc(sourceCode);\n\n return {\n filePath,\n relativePath,\n urlPath,\n sourceCode,\n jsdocComments,\n hasExactJsdoc,\n exactPathItem,\n };\n}\n\n/**\n * Convert a Next.js route file path to an OpenAPI URL path.\n * e.g. src/app/api/users/[id]/route.ts -> /api/users/{id}\n */\nexport function filePathToUrlPath(filePath: string): string {\n // Normalize separators\n let path = filePath.replace(/\\\\/g, \"/\");\n\n // Remove leading src/ or app/ prefixes\n path = path.replace(/^(src\\/)?app\\//, \"\");\n\n // Remove trailing /route.ts or /route.js\n path = path.replace(/\\/route\\.(ts|tsx|js|jsx)$/, \"\");\n\n // Convert Next.js dynamic segments [param] to OpenAPI {param}\n path = path.replace(/\\[([^\\]]+)\\]/g, (_, param) => {\n // Handle catch-all [...param] and optional [[...param]]\n if (param.startsWith(\"...\")) {\n return `{${param.slice(3)}}`;\n }\n return `{${param}}`;\n });\n\n // Ensure leading slash\n if (!path.startsWith(\"/\")) {\n path = `/${path}`;\n }\n\n return path;\n}\n\ninterface JsdocResult {\n jsdocComments: string[];\n hasExactJsdoc: boolean;\n exactPathItem?: Record<string, unknown>;\n}\n\nfunction extractJsdoc(sourceCode: string): JsdocResult {\n // Match all JSDoc comment blocks /** ... */\n const jsdocRegex = /\\/\\*\\*([\\s\\S]*?)\\*\\//g;\n const jsdocComments: string[] = [];\n let hasExactJsdoc = false;\n let exactPathItem: Record<string, unknown> | undefined;\n\n let match: RegExpExecArray | null;\n // biome-ignore lint/suspicious/noAssignInExpressions: required for while loop\n while ((match = jsdocRegex.exec(sourceCode)) !== null) {\n const comment = match[0];\n jsdocComments.push(comment);\n\n // Check for @openapi-exact tag\n if (/@openapi-exact/.test(comment)) {\n hasExactJsdoc = true;\n // Try to extract the JSON from @openapi tag\n const openapiMatch = comment.match(\n /@openapi\\s+([\\s\\S]*?)(?=\\s*\\*\\/|\\s*\\*\\s*@)/,\n );\n if (openapiMatch) {\n try {\n // Clean up JSDoc asterisks from the JSON\n const jsonStr = openapiMatch[1]\n .split(\"\\n\")\n .map((line) => line.replace(/^\\s*\\*\\s?/, \"\"))\n .join(\"\\n\")\n .trim();\n exactPathItem = JSON.parse(jsonStr);\n } catch {\n // If JSON parse fails, fall through to LLM\n hasExactJsdoc = false;\n }\n }\n }\n }\n\n return { jsdocComments, hasExactJsdoc, exactPathItem };\n}\n","export type { JSDocMode, OpenAPIGenConfig, Provider, ResolvedConfig } from './config.js';\n\nexport { analyzeRoutes } from './analyzer.js';\nexport { loadConfig, resolveConfig } from './config.js';\nexport { assembleSpec, writeOutputFiles } from './generator.js';\nexport { filePathToUrlPath, scanRoutes } from './scanner.js';\n\nimport { resolve } from 'node:path';\nimport type { OpenAPIGenConfig } from './config.js';\n\nimport { analyzeRoutes } from './analyzer.js';\nimport { loadConfig } from './config.js';\nimport { assembleSpec, writeOutputFiles } from './generator.js';\nimport { scanRoutes } from './scanner.js';\n\nexport interface GenerateOptions {\n\tconfig?: string;\n\tprovider?: OpenAPIGenConfig['provider'];\n\tcache?: boolean;\n\tcwd?: string;\n}\n\nexport interface GenerateResult {\n\troutesAnalyzed: number;\n\troutesFromCache: number;\n\troutesSkippedLLM: number;\n\tspecPath: string;\n}\n\nexport async function generate(options: GenerateOptions = {}): Promise<GenerateResult> {\n\tconst cwd = options.cwd ?? process.cwd();\n\tconst config = await loadConfig(options.config);\n\n\t// Allow CLI overrides\n\tif (options.provider) config.provider = options.provider;\n\tif (options.cache === false) config.cache = false;\n\n\t// Load .env files before provider env vars are read\n\tif (config.envFile !== false) {\n\t\tconst { config: dotenvConfig } = await import('dotenv');\n\t\tfor (const file of config.envFile) {\n\t\t\tdotenvConfig({ path: resolve(cwd, file), override: false });\n\t\t}\n\t}\n\n\tconsole.log(`[openapi-ai-generator] Scanning routes...`);\n\tconst routes = await scanRoutes(config.include, config.exclude, cwd);\n\tconsole.log(`[openapi-ai-generator] Found ${routes.length} route(s)`);\n\n\tconsole.log(`[openapi-ai-generator] Analyzing routes with provider: ${config.provider}`);\n\tconst analyzed = await analyzeRoutes(routes, {\n\t\tprovider: config.provider,\n\t\tjsdocMode: config.jsdocMode,\n\t\tcache: config.cache,\n\t\tcacheDir: config.cacheDir,\n\t});\n\n\tconst fromCache = analyzed.filter((r) => r.fromCache).length;\n\tconst skippedLLM = analyzed.filter((r) => r.skippedLLM).length;\n\tconsole.log(\n\t\t`[openapi-ai-generator] ${analyzed.length} routes analyzed (${fromCache} from cache, ${skippedLLM - fromCache} exact JSDoc)`,\n\t);\n\n\tconst spec = assembleSpec(config, analyzed);\n\twriteOutputFiles(config, spec, cwd);\n\n\tconsole.log(`[openapi-ai-generator] Spec written to ${config.output.specPath}`);\n\tif (config.output.scalarDocs) {\n\t\tconsole.log(`[openapi-ai-generator] Scalar docs written to ${config.output.scalarPath}`);\n\t}\n\n\treturn {\n\t\troutesAnalyzed: analyzed.length,\n\t\troutesFromCache: fromCache,\n\t\troutesSkippedLLM: skippedLLM,\n\t\tspecPath: config.output.specPath,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uBAAwB;;;ACCxB,gBAA6B;;;ACD7B,oBAA2B;AAC3B,gBAAmE;AACnE,kBAAqB;AAQd,SAAS,YAAY,SAAiB,UAAkB,SAAyB;AACvF,aAAO,0BAAW,QAAQ,EAAE,OAAO,OAAO,EAAE,OAAO,QAAQ,EAAE,OAAO,OAAO,EAAE,OAAO,KAAK;AAC1F;AAEO,IAAM,aAAN,MAAiB;AAAA,EACN;AAAA,EAEjB,YAAY,UAAkB;AAC7B,SAAK,WAAW;AAAA,EACjB;AAAA,EAEQ,YAAkB;AACzB,QAAI,KAAC,sBAAW,KAAK,QAAQ,GAAG;AAC/B,+BAAU,KAAK,UAAU,EAAE,WAAW,KAAK,CAAC;AAAA,IAC7C;AAAA,EACD;AAAA,EAEA,IAAI,MAA8C;AACjD,UAAM,eAAW,kBAAK,KAAK,UAAU,GAAG,IAAI,OAAO;AACnD,QAAI,KAAC,sBAAW,QAAQ,EAAG,QAAO;AAClC,QAAI;AACH,YAAM,QAAoB,KAAK,UAAM,wBAAa,UAAU,MAAM,CAAC;AACnE,aAAO,MAAM;AAAA,IACd,QAAQ;AACP,aAAO;AAAA,IACR;AAAA,EACD;AAAA,EAEA,IAAI,MAAc,UAAyC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,uBAAuB,sDAAsD,EACpF,OAAO,6BAA6B,gDAAgD,EACpF,OAAO,cAAc,gDAAgD,EACrE,OAAO,OAAO,SAAiE;AAC/E,MAAI;AACH,UAAM,SAAS,MAAM,SAAS;AAAA,MAC7B,QAAQ,KAAK;AAAA,MACb,UAAU,KAAK;AAAA,MACf,OAAO,KAAK;AAAA,IACb,CAAC;AAED,YAAQ,IAAI;AAAA,2CAAyC;AACrD,YAAQ,IAAI,yBAAyB,OAAO,cAAc,EAAE;AAC5D,YAAQ,IAAI,yBAAyB,OAAO,eAAe,EAAE;AAC7D,YAAQ;AAAA,MACP,yBAAyB,OAAO,iBAAiB,OAAO,mBAAmB,OAAO,mBAAmB,OAAO,gBAAgB;AAAA,IAC7H;AACA,YAAQ,IAAI,yBAAyB,OAAO,QAAQ,EAAE;AAAA,EACvD,SAAS,KAAK;AACb,YAAQ,MAAM,iCAAiC,eAAe,QAAQ,IAAI,UAAU,GAAG;AACvF,YAAQ,KAAK,CAAC;AAAA,EACf;AACD,CAAC;AAEF,QAAQ,MAAM;","names":["import_fs","import_path","import_fs","import_path","import_fs","import_path"]}
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- type Provider = 'azure' | 'openai' | 'anthropic';
2
- type JSDocMode = 'context' | 'exact';
1
+ type Provider = "azure" | "openai" | "anthropic";
2
+ type JSDocMode = "context" | "exact";
3
3
  interface OpenAPIGenConfig {
4
4
  provider: Provider;
5
5
  output: {
@@ -15,16 +15,34 @@ interface OpenAPIGenConfig {
15
15
  url: string;
16
16
  description?: string;
17
17
  }>;
18
+ security: Array<{
19
+ [key: string]: string[];
20
+ }>;
21
+ components?: {
22
+ securitySchemes?: {
23
+ [key: string]: {
24
+ type: string;
25
+ in: string;
26
+ name: string;
27
+ };
28
+ };
29
+ };
18
30
  };
19
31
  jsdocMode?: JSDocMode;
20
32
  cache?: boolean;
21
33
  cacheDir?: string;
22
34
  include?: string[];
23
35
  exclude?: string[];
36
+ /**
37
+ * Path(s) to .env files to load before running. Defaults to ['.env', '.env.local'].
38
+ * Set to false to disable automatic .env loading.
39
+ */
40
+ envFile?: string | string[] | false;
24
41
  }
25
- interface ResolvedConfig extends Required<OpenAPIGenConfig> {
26
- output: Required<OpenAPIGenConfig['output']>;
27
- openapi: Required<OpenAPIGenConfig['openapi']>;
42
+ interface ResolvedConfig extends Omit<Required<OpenAPIGenConfig>, "envFile"> {
43
+ output: Required<OpenAPIGenConfig["output"]>;
44
+ openapi: Required<OpenAPIGenConfig["openapi"]>;
45
+ envFile: string[] | false;
28
46
  }
29
47
  declare function resolveConfig(config: OpenAPIGenConfig): ResolvedConfig;
30
48
  declare function loadConfig(configPath?: string): Promise<ResolvedConfig>;
@@ -70,6 +88,13 @@ interface OpenAPISpec {
70
88
  url: string;
71
89
  description?: string;
72
90
  }>;
91
+ security?: Array<{
92
+ [key: string]: string[];
93
+ }>;
94
+ components?: {
95
+ securitySchemes?: Record<string, unknown>;
96
+ [key: string]: unknown;
97
+ };
73
98
  paths: Record<string, unknown>;
74
99
  }
75
100
  declare function assembleSpec(config: ResolvedConfig, routes: AnalyzedRoute[]): OpenAPISpec;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- type Provider = 'azure' | 'openai' | 'anthropic';
2
- type JSDocMode = 'context' | 'exact';
1
+ type Provider = "azure" | "openai" | "anthropic";
2
+ type JSDocMode = "context" | "exact";
3
3
  interface OpenAPIGenConfig {
4
4
  provider: Provider;
5
5
  output: {
@@ -15,16 +15,34 @@ interface OpenAPIGenConfig {
15
15
  url: string;
16
16
  description?: string;
17
17
  }>;
18
+ security: Array<{
19
+ [key: string]: string[];
20
+ }>;
21
+ components?: {
22
+ securitySchemes?: {
23
+ [key: string]: {
24
+ type: string;
25
+ in: string;
26
+ name: string;
27
+ };
28
+ };
29
+ };
18
30
  };
19
31
  jsdocMode?: JSDocMode;
20
32
  cache?: boolean;
21
33
  cacheDir?: string;
22
34
  include?: string[];
23
35
  exclude?: string[];
36
+ /**
37
+ * Path(s) to .env files to load before running. Defaults to ['.env', '.env.local'].
38
+ * Set to false to disable automatic .env loading.
39
+ */
40
+ envFile?: string | string[] | false;
24
41
  }
25
- interface ResolvedConfig extends Required<OpenAPIGenConfig> {
26
- output: Required<OpenAPIGenConfig['output']>;
27
- openapi: Required<OpenAPIGenConfig['openapi']>;
42
+ interface ResolvedConfig extends Omit<Required<OpenAPIGenConfig>, "envFile"> {
43
+ output: Required<OpenAPIGenConfig["output"]>;
44
+ openapi: Required<OpenAPIGenConfig["openapi"]>;
45
+ envFile: string[] | false;
28
46
  }
29
47
  declare function resolveConfig(config: OpenAPIGenConfig): ResolvedConfig;
30
48
  declare function loadConfig(configPath?: string): Promise<ResolvedConfig>;
@@ -70,6 +88,13 @@ interface OpenAPISpec {
70
88
  url: string;
71
89
  description?: string;
72
90
  }>;
91
+ security?: Array<{
92
+ [key: string]: string[];
93
+ }>;
94
+ components?: {
95
+ securitySchemes?: Record<string, unknown>;
96
+ [key: string]: unknown;
97
+ };
73
98
  paths: Record<string, unknown>;
74
99
  }
75
100
  declare function assembleSpec(config: ResolvedConfig, routes: AnalyzedRoute[]): OpenAPISpec;
package/dist/index.js CHANGED
@@ -64,10 +64,11 @@ function createModel(provider) {
64
64
  }
65
65
  function createAzureModel() {
66
66
  const endpoint = requireEnv("AZURE_OPENAI_ENDPOINT");
67
+ const resourceName = endpoint?.match(/https?:\/\/([^.]+)/)?.[1];
67
68
  const apiKey = requireEnv("AZURE_OPENAI_API_KEY");
68
69
  const deployment = requireEnv("AZURE_OPENAI_DEPLOYMENT");
69
70
  const { createAzure } = __require("@ai-sdk/azure");
70
- const azure = createAzure({ endpoint, apiKey });
71
+ const azure = createAzure({ resourceName, apiKey });
71
72
  return azure(deployment);
72
73
  }
73
74
  function createOpenAIModel() {
@@ -204,12 +205,12 @@ ${jsDocSection}
204
205
 
205
206
  For each exported function named GET, POST, PUT, PATCH, or DELETE, document:
206
207
  - operationId: a unique camelCase identifier
207
- - summary: a short one-line description
208
+ - summary: a short one-line description (max ~4 words, avoid starting with "Get", "Post", "Put", "Patch", "Delete", these are sometimes inferred from the operationId, and are sometimes in the format of "Create a ___", "Update a ___", "Delete a ___", "Get a ___", "List ___")
208
209
  - description: a fuller explanation of what the endpoint does
209
210
  - parameters: path params from URL segments like {id}, and query params from searchParams usage
210
211
  - requestBody: schema inferred from request.json() calls and TypeScript types (POST/PUT/PATCH only)
211
212
  - responses: per status code, inferred from NextResponse.json() calls and return type annotations
212
- - tags: inferred from the URL path segments
213
+ - tags: inferred from the URL path segments, but typically only one tag should be used. If more than one exists, they show up in multiple categories, and that can be confusing. If there's a question, use the more specific option.
213
214
  - security: noted if the code checks for auth tokens, session cookies, or middleware guards
214
215
 
215
216
  ## Output format
@@ -249,12 +250,22 @@ var defaults = {
249
250
  cache: true,
250
251
  cacheDir: ".openapi-cache",
251
252
  include: ["src/app/api/**/route.ts"],
252
- exclude: []
253
+ exclude: [],
254
+ envFile: [".env", ".env.local"]
253
255
  };
254
256
  function resolveConfig(config) {
257
+ let envFile;
258
+ if (config.envFile === false) {
259
+ envFile = false;
260
+ } else if (typeof config.envFile === "string") {
261
+ envFile = [config.envFile];
262
+ } else {
263
+ envFile = config.envFile ?? defaults.envFile;
264
+ }
255
265
  return {
256
266
  ...defaults,
257
267
  ...config,
268
+ envFile,
258
269
  output: {
259
270
  scalarDocs: false,
260
271
  scalarPath: "src/app/api/docs/route.ts",
@@ -263,6 +274,7 @@ function resolveConfig(config) {
263
274
  openapi: {
264
275
  description: "",
265
276
  servers: [],
277
+ components: {},
266
278
  ...config.openapi
267
279
  }
268
280
  };
@@ -282,7 +294,9 @@ async function loadConfig(configPath) {
282
294
  return resolveConfig(config);
283
295
  }
284
296
  }
285
- throw new Error("No openapi-gen.config.ts found. Create one at your project root.");
297
+ throw new Error(
298
+ "No openapi-gen.config.ts found. Create one at your project root."
299
+ );
286
300
  }
287
301
  async function importConfig(filePath) {
288
302
  if (filePath.endsWith(".ts")) {
@@ -292,20 +306,8 @@ async function importConfig(filePath) {
292
306
  return import(url$1);
293
307
  }
294
308
  async function importTypeScriptConfig(filePath) {
295
- try {
296
- await import('module');
297
- const url$1 = url.pathToFileURL(filePath).href;
298
- return await import(url$1);
299
- } catch {
300
- try {
301
- __require("ts-node/register");
302
- return __require(filePath);
303
- } catch {
304
- throw new Error(
305
- `Cannot load TypeScript config file: ${filePath}. Install tsx or ts-node, or use a .js config file.`
306
- );
307
- }
308
- }
309
+ __require("tsx/cjs");
310
+ return __require(filePath);
309
311
  }
310
312
  function assembleSpec(config, routes) {
311
313
  const paths = {};
@@ -326,6 +328,12 @@ function assembleSpec(config, routes) {
326
328
  if (config.openapi.servers && config.openapi.servers.length > 0) {
327
329
  spec.servers = config.openapi.servers;
328
330
  }
331
+ if (config.openapi.security && config.openapi.security.length > 0) {
332
+ spec.security = config.openapi.security;
333
+ }
334
+ if (config.openapi.components && Object.keys(config.openapi.components).length > 0) {
335
+ spec.components = config.openapi.components;
336
+ }
329
337
  return spec;
330
338
  }
331
339
  function writeOutputFiles(config, spec, cwd = process.cwd()) {
@@ -435,13 +443,15 @@ function extractJsdoc(sourceCode) {
435
443
  const jsdocComments = [];
436
444
  let hasExactJsdoc = false;
437
445
  let exactPathItem;
438
- const match = jsdocRegex.exec(sourceCode);
439
- while (match !== null) {
446
+ let match;
447
+ while ((match = jsdocRegex.exec(sourceCode)) !== null) {
440
448
  const comment = match[0];
441
449
  jsdocComments.push(comment);
442
450
  if (/@openapi-exact/.test(comment)) {
443
451
  hasExactJsdoc = true;
444
- const openapiMatch = comment.match(/@openapi\s+([\s\S]*?)(?=\s*\*\/|\s*\*\s*@)/);
452
+ const openapiMatch = comment.match(
453
+ /@openapi\s+([\s\S]*?)(?=\s*\*\/|\s*\*\s*@)/
454
+ );
445
455
  if (openapiMatch) {
446
456
  try {
447
457
  const jsonStr = openapiMatch[1].split("\n").map((line) => line.replace(/^\s*\*\s?/, "")).join("\n").trim();
@@ -454,13 +464,17 @@ function extractJsdoc(sourceCode) {
454
464
  }
455
465
  return { jsdocComments, hasExactJsdoc, exactPathItem };
456
466
  }
457
-
458
- // src/index.ts
459
467
  async function generate(options = {}) {
460
468
  const cwd = options.cwd ?? process.cwd();
461
469
  const config = await loadConfig(options.config);
462
470
  if (options.provider) config.provider = options.provider;
463
471
  if (options.cache === false) config.cache = false;
472
+ if (config.envFile !== false) {
473
+ const { config: dotenvConfig } = await import('dotenv');
474
+ for (const file of config.envFile) {
475
+ dotenvConfig({ path: path.resolve(cwd, file), override: false });
476
+ }
477
+ }
464
478
  console.log(`[openapi-ai-generator] Scanning routes...`);
465
479
  const routes = await scanRoutes(config.include, config.exclude, cwd);
466
480
  console.log(`[openapi-ai-generator] Found ${routes.length} route(s)`);