openapi-typescript
Version:
Convert OpenAPI 3.0 & 3.1 schemas to TypeScript
1 lines • 9.88 kB
Source Map (JSON)
{"version":3,"file":"redoc.cjs","sources":["../../src/lib/redoc.ts"],"sourcesContent":["import {\n BaseResolver,\n bundle,\n makeDocumentFromString,\n type Config as RedoclyConfig,\n Source,\n type Document,\n lintDocument,\n type NormalizedProblem,\n} from \"@redocly/openapi-core\";\nimport { performance } from \"node:perf_hooks\";\nimport { Readable } from \"node:stream\";\nimport { fileURLToPath } from \"node:url\";\nimport parseJson from \"parse-json\";\nimport type { OpenAPI3 } from \"../types.js\";\nimport { debug, error, warn } from \"./utils.js\";\n\nexport interface ValidateAndBundleOptions {\n redoc: RedoclyConfig;\n silent: boolean;\n cwd?: URL;\n}\n\ninterface ParseSchemaOptions {\n absoluteRef: string;\n resolver: BaseResolver;\n}\n\nexport async function parseSchema(schema: unknown, { absoluteRef, resolver }: ParseSchemaOptions): Promise<Document> {\n if (!schema) {\n throw new Error(\"Can’t parse empty schema\");\n }\n if (schema instanceof URL) {\n const result = await resolver.resolveDocument(null, absoluteRef, true);\n if (\"parsed\" in result) {\n return result;\n }\n throw result.originalError;\n }\n if (schema instanceof Readable) {\n const contents = await new Promise<string>((resolve) => {\n schema.resume();\n schema.setEncoding(\"utf8\");\n let content = \"\";\n schema.on(\"data\", (chunk: string) => {\n content += chunk;\n });\n schema.on(\"end\", () => {\n resolve(content.trim());\n });\n });\n return parseSchema(contents, { absoluteRef, resolver });\n }\n if (schema instanceof Buffer) {\n return parseSchema(schema.toString(\"utf8\"), { absoluteRef, resolver });\n }\n if (typeof schema === \"string\") {\n // URL\n if (schema.startsWith(\"http://\") || schema.startsWith(\"https://\") || schema.startsWith(\"file://\")) {\n const url = new URL(schema);\n return parseSchema(url, {\n absoluteRef: url.protocol === \"file:\" ? fileURLToPath(url) : url.href,\n resolver,\n });\n }\n // JSON\n if (schema[0] === \"{\") {\n return {\n source: new Source(absoluteRef, schema, \"application/json\"),\n parsed: parseJson(schema),\n };\n }\n // YAML\n return makeDocumentFromString(schema, absoluteRef);\n }\n if (typeof schema === \"object\" && !Array.isArray(schema)) {\n return {\n source: new Source(absoluteRef, JSON.stringify(schema), \"application/json\"),\n parsed: schema,\n };\n }\n throw new Error(`Expected string, object, or Buffer. Got ${Array.isArray(schema) ? \"Array\" : typeof schema}`);\n}\n\nfunction _processProblems(problems: NormalizedProblem[], options: { silent: boolean }) {\n if (problems.length) {\n let errorMessage: string | undefined = undefined;\n for (const problem of problems) {\n const problemLocation = problem.location?.[0].pointer;\n const problemMessage = problemLocation ? `${problem.message} at ${problemLocation}` : problem.message;\n if (problem.severity === \"error\") {\n errorMessage = problemMessage;\n error(problemMessage);\n } else {\n warn(problemMessage, options.silent);\n }\n }\n if (errorMessage) {\n throw new Error(errorMessage);\n }\n }\n}\n\n/**\n * Validate an OpenAPI schema and flatten into a single schema using Redocly CLI\n */\nexport async function validateAndBundle(\n source: string | URL | OpenAPI3 | Readable | Buffer,\n options: ValidateAndBundleOptions,\n) {\n const redocConfigT = performance.now();\n debug(\"Loaded Redoc config\", \"redoc\", performance.now() - redocConfigT);\n const redocParseT = performance.now();\n let absoluteRef = fileURLToPath(new URL(options?.cwd ?? `file://${process.cwd()}/`));\n if (source instanceof URL) {\n absoluteRef = source.protocol === \"file:\" ? fileURLToPath(source) : source.href;\n }\n const resolver = new BaseResolver(options.redoc.resolve);\n const document = await parseSchema(source, {\n absoluteRef,\n resolver,\n });\n debug(\"Parsed schema\", \"redoc\", performance.now() - redocParseT);\n\n // 1. check for OpenAPI 3 or greater\n const openapiVersion = Number.parseFloat(document.parsed.openapi);\n if (\n document.parsed.swagger ||\n !document.parsed.openapi ||\n Number.isNaN(openapiVersion) ||\n openapiVersion < 3 ||\n openapiVersion >= 4\n ) {\n if (document.parsed.swagger) {\n throw new Error(\"Unsupported Swagger version: 2.x. Use OpenAPI 3.x instead.\");\n }\n if (document.parsed.openapi || openapiVersion < 3 || openapiVersion >= 4) {\n throw new Error(`Unsupported OpenAPI version: ${document.parsed.openapi}`);\n }\n throw new Error(\"Unsupported schema format, expected `openapi: 3.x`\");\n }\n\n // 2. lint\n const redocLintT = performance.now();\n const problems = await lintDocument({\n document,\n config: options.redoc.styleguide,\n externalRefResolver: resolver,\n });\n _processProblems(problems, options);\n debug(\"Linted schema\", \"lint\", performance.now() - redocLintT);\n\n // 3. bundle\n const redocBundleT = performance.now();\n const bundled = await bundle({\n config: options.redoc,\n dereference: false,\n doc: document,\n });\n _processProblems(bundled.problems, options);\n debug(\"Bundled schema\", \"bundle\", performance.now() - redocBundleT);\n\n return bundled.bundle.parsed;\n}\n"],"names":["Readable","fileURLToPath","Source","parseJson","makeDocumentFromString","error","warn","performance","debug","BaseResolver","lintDocument","bundle"],"mappings":";;;;;;;;;;;;;AA4BA,eAAsB,WAAY,CAAA,MAAA,EAAiB,EAAE,WAAA,EAAa,UAAmD,EAAA;AACnH,EAAA,IAAI,CAAC,MAAQ,EAAA;AACX,IAAM,MAAA,IAAI,MAAM,+BAA0B,CAAA;AAAA;AAE5C,EAAA,IAAI,kBAAkB,GAAK,EAAA;AACzB,IAAA,MAAM,SAAS,MAAM,QAAA,CAAS,eAAgB,CAAA,IAAA,EAAM,aAAa,IAAI,CAAA;AACrE,IAAA,IAAI,YAAY,MAAQ,EAAA;AACtB,MAAO,OAAA,MAAA;AAAA;AAET,IAAA,MAAM,MAAO,CAAA,aAAA;AAAA;AAEf,EAAA,IAAI,kBAAkBA,oBAAU,EAAA;AAC9B,IAAA,MAAM,QAAW,GAAA,MAAM,IAAI,OAAA,CAAgB,CAAC,OAAY,KAAA;AACtD,MAAA,MAAA,CAAO,MAAO,EAAA;AACd,MAAA,MAAA,CAAO,YAAY,MAAM,CAAA;AACzB,MAAA,IAAI,OAAU,GAAA,EAAA;AACd,MAAO,MAAA,CAAA,EAAA,CAAG,MAAQ,EAAA,CAAC,KAAkB,KAAA;AACnC,QAAW,OAAA,IAAA,KAAA;AAAA,OACZ,CAAA;AACD,MAAO,MAAA,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