Skip to content

Commit

Permalink
fix(swagger): avoid swagger-ui-dist import to resolve swagger-ui asse…
Browse files Browse the repository at this point in the history
…ts path
  • Loading branch information
Romakita committed Apr 25, 2024
1 parent 8381d27 commit 83ea061
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions packages/specs/swagger/scripts/constants.js
@@ -0,0 +1,7 @@
import {dirname} from "node:path";
import {fileURLToPath} from "node:url";

// @ts-ignore
export const SWAGGER_UI_DIST = dirname(import.meta.resolve("swagger-ui-dist"));
// @ts-expect-error
export const ROOT_DIR = dirname(fileURLToPath(import.meta.url));
16 changes: 6 additions & 10 deletions packages/specs/swagger/src/SwaggerModule.ts
@@ -1,31 +1,27 @@
import {
OnRoutesInit,
Configuration,
Constant,
Inject,
InjectorService,
Module,
OnReady,
OnRoutesInit,
PlatformApplication,
PlatformContext
} from "@tsed/common";
import {Env} from "@tsed/core";
import {normalizePath} from "@tsed/normalize-path";
import {PlatformRouter, useContextHandler} from "@tsed/platform-router";
import Fs from "fs";
import {join} from "path";
import {Env} from "@tsed/core";
import {normalizePath} from "@tsed/normalize-path";
import filedirname from "filedirname";
import {absolutePath} from "swagger-ui-dist";
import {ROOT_DIR, SWAGGER_UI_DIST} from "./constants";
import {SwaggerSettings} from "./interfaces/SwaggerSettings";
import {cssMiddleware} from "./middlewares/cssMiddleware";
import {indexMiddleware} from "./middlewares/indexMiddleware";
import {jsMiddleware} from "./middlewares/jsMiddleware";
import {redirectMiddleware} from "./middlewares/redirectMiddleware";
import {SwaggerService} from "./services/SwaggerService";

// FIXME remove when esm is ready
const [, rootDir] = filedirname();

/**
* @ignore
*/
Expand Down Expand Up @@ -130,7 +126,7 @@ export class SwaggerModule implements OnRoutesInit, OnReady {
* @param urls
*/
private createRouter(conf: SwaggerSettings, urls: string[]) {
const {disableSpec = false, fileName = "swagger.json", cssPath, jsPath, viewPath = join(rootDir, "../views/index.ejs")} = conf;
const {disableSpec = false, fileName = "swagger.json", cssPath, jsPath, viewPath = join(ROOT_DIR, "../views/index.ejs")} = conf;
const router = new PlatformRouter(this.injector);

if (!disableSpec) {
Expand All @@ -147,7 +143,7 @@ export class SwaggerModule implements OnRoutesInit, OnReady {
}

router.get("/", useContextHandler(indexMiddleware(viewPath, {urls, ...conf})));
router.statics("/", {root: absolutePath()});
router.statics("/", {root: SWAGGER_UI_DIST});
}

return router;
Expand Down
4 changes: 4 additions & 0 deletions packages/specs/swagger/src/constants.ts
@@ -0,0 +1,4 @@
import {dirname} from "path";

export const SWAGGER_UI_DIST = dirname(require.resolve("swagger-ui-dist"));
export const ROOT_DIR = __dirname;
1 change: 1 addition & 0 deletions packages/specs/swagger/src/index.ts
Expand Up @@ -3,6 +3,7 @@
*/

export * from "./SwaggerModule";
export * from "./constants";
export * from "./decorators/docs";
export * from "./decorators/hidden";
export * from "./interfaces/SwaggerSettings";
Expand Down

0 comments on commit 83ea061

Please sign in to comment.