Express 与 Typescript 中的 Swagger UI 类型错误

问题描述 投票:0回答:1

类型错误:尝试获取资源时出现网络错误。

每当我访问此路线(http://localhost:4001/api/4a7ed517)时,它都会在我的浏览器或任何其他 http 客户端(如 insomnia)中打开,但它在 swagger UI 中不起作用。

除了这条以外,所有其他路线都工作正常。

Swagger UI Routes

Error

有人可以帮忙吗?

我尝试获取生成的短网址,但它给了我一个错误,正如我上面提到的。

const urlSchema = new Schema<IUrl>(
  {
    shortID: {
      type: String,
      required: true,
      unique: true,
    },
    redirectURL: {
      type: String,
      required: true,
    },
    visitHistory: [{ timestamp: { type: Number } }],
  },
  { timestamps: true }
);

{
  "openapi": "3.0.0",
  "info": {
    "title": "URL Shortener API",
    "description": "API for shortening URLs and retrieving analytics",
    "version": "1.0.0",
    "license": {
      "name": "MIT",
      "url": "https://opensource.org/licenses/MIT"
    }
  },
  "paths": {
    "/api/generate": {
      "post": {
        "summary": "Generate Short URL",
        "description": "Generates a new short URL for the provided long URL.",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "url": {
                    "type": "string",
                    "description": "The original long URL to be shortened."
                  }
                },
                "required": ["url"]
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Short URL generated successfully.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string",
                      "description": "The generated short URL."
                    }
                  }
                }
              }
            }
          }
        },
        "tags": [
          "Short URL"
        ]
      }
    },
    "/api/{shortID}": {
      "get": {
        "summary": "Redirect to the original URL using the short ID",
        "parameters": [
          {
            "in": "path",
            "name": "shortID",
            "required": true,
            "description": "The short ID associated with the URL",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "302": {
            "description": "Redirect to the original URL"
          },
          "404": {
            "description": "Short ID not found"
          }
        },
        "tags": [
          "Redirect"
        ]
      }
    },
    "/api/analytics/{shortID}": {
      "get": {
        "summary": "Get Analytics for Short URL",
        "description": "Retrieves analytics data for a specific short URL, including visit history.",
        "parameters": [
          {
            "name": "shortID",
            "in": "path",
            "required": true,
            "description": "Short identifier for the URL.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successfully retrieved analytics data.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "redirectURL": {
                      "type": "string",
                      "description": "The short URL."
                    },
                    "url": {
                      "type": "string",
                      "description": "The original long URL."
                    },
                    "visitHistory": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "timestamp": {
                            "type": "integer",
                            "description": "Visit timestamp."
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "URL not found."
          }
        },
        "tags": [
          "Analytics"
        ]
      }
    }
  }
}

typescript express swagger swagger-ui
1个回答
0
投票

您定义了 2 个不明确的端点。

/api/generate
/api/{shortID}
// 从技术上讲,这可以解析为
/api/generate
,因为
shortID
any
string

每个端点都可能解析为相同的资源。您需要重新定义这些模式以避免后端服务发生任何冲突。

© www.soinside.com 2019 - 2024. All rights reserved.