elysiajs / elysia-swagger

A plugin for Elysia to auto-generate Swagger page
MIT License
90 stars 46 forks source link

Swagger path responds with `NOT_FOUND`. #151

Open jonasfroeller opened 1 month ago

jonasfroeller commented 1 month ago

It worked using elysia@1.0.15 and @elysiajs/swagger@^1.0.2.
I tried removing my swagger configuration and checked out the default /swagger path. Same issue.
I upgraded bun, deleted node_modules and reinstalled them. Same issue.
I also tried removing every other path and plugin and just adding swagger. Same issue.
I am so confused. Did something change in elysiajs, that breaks my code now?

My dependencies:

  "dependencies": {
    "@apollo/client": "^3.11.8",
    "@bogeychan/elysia-logger": "^0.1.4",
    "@elysiajs/bearer": "^1.1.2",
    "@elysiajs/cors": "^1.1.1",
    "@elysiajs/html": "^1.1.1",
    "@elysiajs/jwt": "^1.1.1",
    "@elysiajs/static": "^1.1.1",
    "@elysiajs/swagger": "^1.1.3",
    "@octokit/graphql-schema": "^15.25.0",
    "drizzle-orm": "^0.33.0",
    "elysia": "^1.1.17",
    "graphql": "^16.9.0",
    "octokit": "^4.0.2",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@biomejs/biome": "^1.9.2",
    "@octokit/types": "^13.6.0",
    "@types/html-validator": "^5.0.6",
    "@types/pg": "^8.11.10",
    "bun-types": "^1.1.29",
    "drizzle-kit": "^0.24.2",
    "html-validator": "^6.0.1"
  },

Old dependencies:

  "dependencies": {
    "@bogeychan/elysia-logger": "^0.0.20",
    "@elysiajs/bearer": "^1.0.2",
    "@elysiajs/cors": "^1.0.2",
    "@elysiajs/html": "^1.0.2",
    "@elysiajs/jwt": "^1.0.2",
    "@elysiajs/static": "^1.0.2",
    "@elysiajs/swagger": "^1.0.2",
    "@octokit/graphql-schema": "^14.47.1",
    "drizzle-orm": "^0.30.9",
    "elysia": "^1.0.9",
    "octokit": "^3.1.2",
    "pg": "^8.11.5"
  },
  "devDependencies": {
    "@biomejs/biome": "^1.6.1",
    "@octokit/types": "^12.6.0",
    "@types/html-validator": "^5.0.6",
    "@types/pg": "^8.11.5",
    "bun-types": "^1.0.33",
    "drizzle-kit": "^0.20.17",
    "html-validator": "^6.0.1"
  },

My code:

export const app = new Elysia()
    .use(logger({ autoLogging: true }))
    .use(
        staticPlugin({
            assets: "static",
            prefix: "/",
        }),
    )
    .use(
        cors({
            origin: CORS_ORIGINS,
        }),
    )
    .use(html())
    .use(ROOT_ROUTES)

    // VERSIONS
    .use(API_FORWARD_ROUTES)
    .group(SWAGGER_PATH, (app) =>
        app
            .get("", async ({ redirect }) => {
                return redirect(`/${LATEST_SWAGGER_PATH}`);
            })
            .get(
                "/json",
                async ({ redirect }) => {
                    return redirect(`/${LATEST_SWAGGER_PATH}/json`);
                },
            ),
    )
    .use(v1)
    .listen(PORT);

v1:

export const v1 = new Elysia({ prefix: `/${V1_PATH}` })
    .group("/xxx", (app) =>
        app
            .use(XXX)
    )
    .use(
        swagger({
            scalarVersion: "1.25.24",
            path: SWAGGER_PATH,
            exclude: [
                ...ROOT_PATHS,
                ...SWAGGER_PATH_EXCLUDE,
                new RegExp("(/github/webhooks/)[A-Za-z/{_}]*"),
            ],
            scalarConfig: {
                metaData: {
                    ogImage: {
                        url: "http://localhost:3000/favicon.png",
                        secureUrl: "https://xxx.com/favicon.png",
                        type: "image/png",
                        width: 512,
                        height: 512,
                        alt: "favicon",
                    },
                },
            },
        }),
    );
jonasfroeller commented 1 month ago

elysia@1.1.7 is the last version that works for me.

dodas commented 1 week ago

Experiencing the same issue. It seems it only occurs when swagger plugin is applied in non-root Elysia instance (in your case the v1 router). It only works when used on the root Elysia instance. Can also confirm downgrading to 1.1.7 fixes the issue.

154 doesn't fix this – in this case the whole page is responding with 404, instead of just the client fetch to get the schema.

dodas commented 4 days ago

Found out that if you await the return value of elysia-swagger, then it works on child instances:

const api = new Elysia().use(await swagger()).get("/hello");
const app = new Elysia().use(api);