Skip to content

Commit

Permalink
Merge pull request #2544 from destyk/master
Browse files Browse the repository at this point in the history
feat: add `customSwaggerUiPath`
  • Loading branch information
kamilmysliwiec authored Aug 7, 2023
2 parents 2bc1a5e + cb299b1 commit 17af50a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
14 changes: 13 additions & 1 deletion e2e/express.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as request from 'supertest';
import * as SwaggerParser from 'swagger-parser';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import * as path from 'path';

describe('Express Swagger', () => {
let app: NestExpressApplication;
Expand Down Expand Up @@ -83,7 +84,10 @@ describe('Express Swagger', () => {
app,
builder.build()
);
SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument);
SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument, {
// to showcase that in new implementation u can use custom swagger-ui path. Useful when using e.g. webpack
customSwaggerUiPath: path.resolve(`./node_modules/swagger-ui-dist`),
});

await app.init();
});
Expand All @@ -100,6 +104,14 @@ describe('Express Swagger', () => {
expect(response.status).toEqual(200);
expect(Object.keys(response.body).length).toBeGreaterThan(0);
});

it('content type of served static should be available', async () => {
const response = await request(app.getHttpServer()).get(
`${SWAGGER_RELATIVE_URL}/swagger-ui-bundle.js`
);

expect(response.status).toEqual(200);
});
});

describe('custom documents endpoints', () => {
Expand Down
16 changes: 14 additions & 2 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as request from 'supertest';
import * as SwaggerParser from 'swagger-parser';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import * as path from 'path';

describe('Fastify Swagger', () => {
let app: NestFastifyApplication;
Expand Down Expand Up @@ -83,9 +84,12 @@ describe('Fastify Swagger', () => {
beforeEach(async () => {
const swaggerDocument = SwaggerModule.createDocument(
app,
builder.build()
builder.build(),
);
SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument);
SwaggerModule.setup(SWAGGER_RELATIVE_URL, app, swaggerDocument, {
// to showcase that in new implementation u can use custom swagger-ui path. Useful when using e.g. webpack
customSwaggerUiPath: path.resolve(`./node_modules/swagger-ui-dist`),
});

await app.init();
await app.getHttpAdapter().getInstance().ready();
Expand All @@ -103,6 +107,14 @@ describe('Fastify Swagger', () => {
expect(response.status).toEqual(200);
expect(Object.keys(response.body).length).toBeGreaterThan(0);
});

it('content type of served static should be available', async () => {
const response = await request(app.getHttpServer()).get(
`${SWAGGER_RELATIVE_URL}/swagger-ui-bundle.js`
);

expect(response.status).toEqual(200);
});
});

describe('custom documents endpoints', () => {
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces/swagger-custom-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SwaggerCustomOptions {
customJs?: string | string[];
customJsStr?: string | string[];
customfavIcon?: string;
customSwaggerUiPath?: string;
swaggerUrl?: string;
customSiteTitle?: string;
validatorUrl?: string;
Expand Down
16 changes: 10 additions & 6 deletions lib/swagger-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getGlobalPrefix } from './utils/get-global-prefix';
import { normalizeRelPath } from './utils/normalize-rel-path';
import { validateGlobalPrefix } from './utils/validate-global-prefix.util';
import { validatePath } from './utils/validate-path.util';
import { resolvePath } from './utils/resolve-path.util';

export class SwaggerModule {
private static readonly metadataLoader = new MetadataLoader();
Expand Down Expand Up @@ -53,19 +54,23 @@ export class SwaggerModule {
return this.metadataLoader.load(metadata);
}

private static serveStatic(finalPath: string, app: INestApplication) {
private static serveStatic(finalPath: string, app: INestApplication, customStaticPath?: string) {
const httpAdapter = app.getHttpAdapter();
const swaggerAssetsAbsoluteFSPath = getSwaggerAssetsAbsoluteFSPath();

// See <https://github.com/nestjs/swagger/issues/2543>
const swaggerAssetsPath = customStaticPath
? resolvePath(customStaticPath)
: getSwaggerAssetsAbsoluteFSPath();

if (httpAdapter && httpAdapter.getType() === 'fastify') {
(app as NestFastifyApplication).useStaticAssets({
root: swaggerAssetsAbsoluteFSPath,
root: swaggerAssetsPath,
prefix: finalPath,
decorateReply: false
});
} else {
(app as NestExpressApplication).useStaticAssets(
swaggerAssetsAbsoluteFSPath,
swaggerAssetsPath,
{ prefix: finalPath }
);
}
Expand Down Expand Up @@ -264,7 +269,6 @@ export class SwaggerModule {
: path
);
const urlLastSubdirectory = finalPath.split('/').slice(-1).pop() || '';

const validatedGlobalPrefix =
options?.useGlobalPrefix && validateGlobalPrefix(globalPrefix)
? validatePath(globalPrefix)
Expand Down Expand Up @@ -292,7 +296,7 @@ export class SwaggerModule {
}
);

SwaggerModule.serveStatic(finalPath, app);
SwaggerModule.serveStatic(finalPath, app, options?.customSwaggerUiPath);
/**
* Covers assets fetched through a relative path when Swagger url ends with a slash '/'.
* @see https://github.com/nestjs/swagger/issues/1976
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/resolve-path.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as pathLib from 'path';

export function resolvePath(path: string): string {
return path ? pathLib.resolve(path) : path;
}

0 comments on commit 17af50a

Please sign in to comment.