{"version":3,"file":"openapi.cjs","names":["yaml"],"sources":["../../src/util/openapi.ts"],"sourcesContent":["import * as yaml from \"js-yaml\";\nimport { OpenAPIV3, OpenAPIV3_1 } from \"openapi-types\";\n\nexport class OpenAPISpec {\n  constructor(public document: OpenAPIV3_1.Document) {}\n\n  get baseUrl() {\n    return this.document.servers ? this.document.servers[0].url : undefined;\n  }\n\n  getPathsStrict() {\n    if (!this.document.paths) {\n      throw new Error(\"No paths found in spec\");\n    }\n    return this.document.paths;\n  }\n\n  getParametersStrict() {\n    if (!this.document.components?.parameters) {\n      throw new Error(\"No parameters found in spec\");\n    }\n    return this.document.components.parameters;\n  }\n\n  getSchemasStrict() {\n    if (!this.document.components?.schemas) {\n      throw new Error(\"No schemas found in spec.\");\n    }\n    return this.document.components.schemas;\n  }\n\n  getRequestBodiesStrict() {\n    if (!this.document.components?.requestBodies) {\n      throw new Error(\"No request body found in spec.\");\n    }\n    return this.document.components.requestBodies;\n  }\n\n  getPathStrict(path: string) {\n    const pathItem = this.getPathsStrict()[path];\n    if (pathItem === undefined) {\n      throw new Error(`No path found for \"${path}\".`);\n    }\n    return pathItem;\n  }\n\n  getReferencedParameter(ref: OpenAPIV3_1.ReferenceObject) {\n    const refComponents = ref.$ref.split(\"/\");\n    const refName = refComponents[refComponents.length - 1];\n    if (this.getParametersStrict()[refName] === undefined) {\n      throw new Error(`No parameter found for \"${refName}\".`);\n    }\n    return this.getParametersStrict()[refName];\n  }\n\n  getRootReferencedParameter(\n    ref: OpenAPIV3_1.ReferenceObject\n  ): OpenAPIV3_1.ParameterObject {\n    let parameter = this.getReferencedParameter(ref);\n    while ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n      parameter = this.getReferencedParameter(\n        parameter as OpenAPIV3_1.ReferenceObject\n      );\n    }\n    return parameter as OpenAPIV3_1.ParameterObject;\n  }\n\n  getReferencedSchema(\n    ref: OpenAPIV3_1.ReferenceObject\n  ): OpenAPIV3_1.SchemaObject {\n    const refComponents = ref.$ref.split(\"/\");\n    const refName = refComponents[refComponents.length - 1];\n    const schema = this.getSchemasStrict()[refName];\n    if (schema === undefined) {\n      throw new Error(`No schema found for \"${refName}\".`);\n    }\n    return schema;\n  }\n\n  getSchema(\n    schema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject\n  ): OpenAPIV3_1.SchemaObject {\n    if ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n      return this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n    }\n    return schema;\n  }\n\n  getRootReferencedSchema(ref: OpenAPIV3_1.ReferenceObject) {\n    let schema = this.getReferencedSchema(ref);\n    while ((schema as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n      schema = this.getReferencedSchema(schema as OpenAPIV3_1.ReferenceObject);\n    }\n    return schema as OpenAPIV3_1.ParameterObject;\n  }\n\n  getReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n    const refComponents = ref.$ref.split(\"/\");\n    const refName = refComponents[refComponents.length - 1];\n    const requestBodies = this.getRequestBodiesStrict();\n    if (requestBodies[refName] === undefined) {\n      throw new Error(`No request body found for \"${refName}\"`);\n    }\n    return requestBodies[refName];\n  }\n\n  getRootReferencedRequestBody(ref: OpenAPIV3_1.ReferenceObject) {\n    let requestBody = this.getReferencedRequestBody(ref);\n    while ((requestBody as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n      requestBody = this.getReferencedRequestBody(\n        requestBody as OpenAPIV3_1.ReferenceObject\n      );\n    }\n    return requestBody as OpenAPIV3_1.RequestBodyObject;\n  }\n\n  getMethodsForPath(path: string): OpenAPIV3.HttpMethods[] {\n    const pathItem = this.getPathStrict(path);\n    // This is an enum in the underlying package.\n    // Werestate here to allow \"import type\" above and not cause warnings in certain envs.\n    const possibleMethods = [\n      \"get\",\n      \"put\",\n      \"post\",\n      \"delete\",\n      \"options\",\n      \"head\",\n      \"patch\",\n      \"trace\",\n    ];\n    return possibleMethods.filter(\n      (possibleMethod) =>\n        pathItem[possibleMethod as OpenAPIV3.HttpMethods] !== undefined\n    ) as OpenAPIV3.HttpMethods[];\n  }\n\n  getParametersForPath(path: string) {\n    const pathItem = this.getPathStrict(path);\n    if (pathItem.parameters === undefined) {\n      return [];\n    }\n    return pathItem.parameters.map((parameter) => {\n      if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n        return this.getRootReferencedParameter(\n          parameter as OpenAPIV3_1.ReferenceObject\n        );\n      }\n      return parameter as OpenAPIV3_1.ParameterObject;\n    });\n  }\n\n  getOperation(path: string, method: OpenAPIV3.HttpMethods) {\n    const pathItem = this.getPathStrict(path);\n    if (pathItem[method] === undefined) {\n      throw new Error(`No ${method} method found for \"path\".`);\n    }\n    return pathItem[method];\n  }\n\n  getParametersForOperation(operation: OpenAPIV3_1.OperationObject) {\n    if (operation.parameters === undefined) {\n      return [];\n    }\n    return operation.parameters.map((parameter) => {\n      if ((parameter as OpenAPIV3_1.ReferenceObject).$ref !== undefined) {\n        return this.getRootReferencedParameter(\n          parameter as OpenAPIV3_1.ReferenceObject\n        );\n      }\n      return parameter as OpenAPIV3_1.ParameterObject;\n    });\n  }\n\n  getRequestBodyForOperation(\n    operation: OpenAPIV3_1.OperationObject\n  ): OpenAPIV3_1.RequestBodyObject {\n    const { requestBody } = operation;\n    if ((requestBody as OpenAPIV3_1.ReferenceObject)?.$ref !== undefined) {\n      return this.getRootReferencedRequestBody(\n        requestBody as OpenAPIV3_1.ReferenceObject\n      );\n    }\n    return requestBody as OpenAPIV3_1.RequestBodyObject;\n  }\n\n  static getCleanedOperationId(\n    operation: OpenAPIV3_1.OperationObject,\n    path: string,\n    method: OpenAPIV3_1.HttpMethods\n  ) {\n    let { operationId } = operation;\n    if (operationId === undefined) {\n      const updatedPath = path.replaceAll(/[^a-zA-Z0-9]/g, \"_\");\n      operationId = `${\n        updatedPath.startsWith(\"/\") ? updatedPath.slice(1) : updatedPath\n      }_${method}`;\n    }\n    return operationId\n      .replaceAll(/-/g, \"_\")\n      .replaceAll(/\\./g, \"_\")\n      .replaceAll(/\\//g, \"_\");\n  }\n\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  static alertUnsupportedSpec(document: Record<string, any>) {\n    const warningMessage =\n      \"This may result in degraded performance. Convert your OpenAPI spec to 3.1.0 for better support.\";\n    const swaggerVersion = document.swagger;\n    const openAPIVersion = document.openapi;\n    if (openAPIVersion !== undefined && openAPIVersion !== \"3.1.0\") {\n      console.warn(\n        `Attempting to load an OpenAPI ${openAPIVersion} spec. ${warningMessage}`\n      );\n    } else if (swaggerVersion !== undefined) {\n      console.warn(\n        `Attempting to load a Swagger ${swaggerVersion} spec. ${warningMessage}`\n      );\n    } else {\n      throw new Error(\n        `Attempting to load an unsupported spec:\\n\\n${JSON.stringify(\n          document,\n          null,\n          2\n        )}.`\n      );\n    }\n  }\n\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  static fromObject(document: Record<string, any>) {\n    OpenAPISpec.alertUnsupportedSpec(document);\n    return new OpenAPISpec(document as OpenAPIV3_1.Document);\n  }\n\n  static fromString(rawString: string) {\n    let document;\n    try {\n      document = JSON.parse(rawString);\n    } catch {\n      document = yaml.load(rawString);\n    }\n    return OpenAPISpec.fromObject(document);\n  }\n\n  static async fromURL(url: string) {\n    const response = await fetch(url);\n    const rawDocument = await response.text();\n    return OpenAPISpec.fromString(rawDocument);\n  }\n}\n"],"mappings":";;;;AAGA,IAAa,cAAb,MAAa,YAAY;CACvB,YAAY,UAAuC;AAAhC,OAAA,WAAA;;CAEnB,IAAI,UAAU;AACZ,SAAO,KAAK,SAAS,UAAU,KAAK,SAAS,QAAQ,GAAG,MAAM,KAAA;;CAGhE,iBAAiB;AACf,MAAI,CAAC,KAAK,SAAS,MACjB,OAAM,IAAI,MAAM,yBAAyB;AAE3C,SAAO,KAAK,SAAS;;CAGvB,sBAAsB;AACpB,MAAI,CAAC,KAAK,SAAS,YAAY,WAC7B,OAAM,IAAI,MAAM,8BAA8B;AA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