Skip to content

Commit

Permalink
Merge branch 'master' into feature/add-uiconfig-type
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Sep 28, 2021
2 parents b1a3be8 + a3cd446 commit b182249
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 14 deletions.
166 changes: 166 additions & 0 deletions lib/extra/swagger-shim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* eslint-disable @typescript-eslint/no-empty-function */
export function ApiProperty() {
return () => {};
}
export function ApiPropertyOptional() {
return () => {};
}
export function ApiResponseProperty() {
return () => {};
}
export function ApiBasicAuth() {
return () => {};
}
export function ApiBearerAuth() {
return () => {};
}
export function ApiBody() {
return () => {};
}
export function ApiConsumes() {
return () => {};
}
export function ApiCookieAuth() {
return () => {};
}
export function ApiExcludeEndpoint() {
return () => {};
}
export function ApiExcludeController() {
return () => {};
}
export function ApiExtraModels() {
return () => {};
}
export function ApiHeader() {
return () => {};
}
export function ApiHeaders() {
return () => {};
}
export function ApiHideProperty() {
return () => {};
}
export function ApiOAuth2() {
return () => {};
}
export function ApiOperation() {
return () => {};
}
export function ApiParam() {
return () => {};
}
export function ApiProduces() {
return () => {};
}
export function ApiQuery() {
return () => {};
}
export function ApiResponse() {
return () => {};
}
export function ApiOkResponse() {
return () => {};
}
export function ApiCreatedResponse() {
return () => {};
}
export function ApiAcceptedResponse() {
return () => {};
}
export function ApiNoContentResponse() {
return () => {};
}
export function ApiMovedPermanentlyResponse() {
return () => {};
}
export function ApiFoundResponse() {
return () => {};
}
export function ApiBadRequestResponse() {
return () => {};
}
export function ApiUnauthorizedResponse() {
return () => {};
}
export function ApiTooManyRequestsResponse() {
return () => {};
}
export function ApiNotFoundResponse() {
return () => {};
}
export function ApiInternalServerErrorResponse() {
return () => {};
}
export function ApiBadGatewayResponse() {
return () => {};
}
export function ApiConflictResponse() {
return () => {};
}
export function ApiForbiddenResponse() {
return () => {};
}
export function ApiGatewayTimeoutResponse() {
return () => {};
}
export function ApiGoneResponse() {
return () => {};
}
export function ApiMethodNotAllowedResponse() {
return () => {};
}
export function ApiNotAcceptableResponse() {
return () => {};
}
export function ApiNotImplementedResponse() {
return () => {};
}
export function ApiPreconditionFailedResponse() {
return () => {};
}
export function ApiPayloadTooLargeResponse() {
return () => {};
}
export function ApiRequestTimeoutResponse() {
return () => {};
}
export function ApiServiceUnavailableResponse() {
return () => {};
}
export function ApiUnprocessableEntityResponse() {
return () => {};
}
export function ApiUnsupportedMediaTypeResponse() {
return () => {};
}
export function ApiDefaultResponse() {
return () => {};
}
export function ApiSecurity() {
return () => {};
}
export function ApiTags() {
return () => {};
}
export function ApiExtension() {
return () => {};
}
export function DocumentBuilder() {
return () => {};
}
export function SwaggerModule() {
return () => {};
}
export function IntersectionType() {
return () => {};
}
export function OmitType() {
return () => {};
}
export function PartialType() {
return () => {};
}
export function PickType() {
return () => {};
}
8 changes: 6 additions & 2 deletions lib/interfaces/swagger-custom-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface ExpressSwaggerCustomOptions {
interface CommonSwaggerCustomOptions {
useGlobalPrefix?: boolean,
}

export interface ExpressSwaggerCustomOptions extends CommonSwaggerCustomOptions {
explorer?: boolean;
swaggerOptions?: Record<string, any>;
customCss?: string;
Expand All @@ -12,7 +16,7 @@ export interface ExpressSwaggerCustomOptions {
urls?: Record<'url' | 'name', string>[];
}

export interface FastifySwaggerCustomOptions {
export interface FastifySwaggerCustomOptions extends CommonSwaggerCustomOptions {
uiConfig?: Partial<{
deepLinking: boolean;
displayOperationId: boolean;
Expand Down
18 changes: 12 additions & 6 deletions lib/swagger-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './interfaces';
import { SwaggerScanner } from './swagger-scanner';
import { assignTwoLevelsDeep } from './utils/assign-two-levels-deep';
import { getGlobalPrefix } from './utils/get-global-prefix';
import { validatePath } from './utils/validate-path.util';

export class SwaggerModule {
Expand Down Expand Up @@ -41,16 +42,22 @@ export class SwaggerModule {
options?: SwaggerCustomOptions
) {
const httpAdapter = app.getHttpAdapter();
const globalPrefix = getGlobalPrefix(app);
const finalPath = validatePath(
(options?.useGlobalPrefix && globalPrefix && !globalPrefix.match(/^(\/?)$/))
? `${globalPrefix}${validatePath(path)}`
: path
);
if (httpAdapter && httpAdapter.getType() === 'fastify') {
return this.setupFastify(
path,
finalPath,
httpAdapter,
document,
options as FastifySwaggerCustomOptions
);
}
return this.setupExpress(
path,
finalPath,
app,
document,
options as ExpressSwaggerCustomOptions
Expand All @@ -64,15 +71,14 @@ export class SwaggerModule {
options?: ExpressSwaggerCustomOptions
) {
const httpAdapter = app.getHttpAdapter();
const finalPath = validatePath(path);
const swaggerUi = loadPackage('swagger-ui-express', 'SwaggerModule', () =>
require('swagger-ui-express')
);
const swaggerHtml = swaggerUi.generateHTML(document, options);
app.use(finalPath, swaggerUi.serveFiles(document, options));
app.use(path, swaggerUi.serveFiles(document, options));

httpAdapter.get(finalPath, (req, res) => res.send(swaggerHtml));
httpAdapter.get(finalPath + '-json', (req, res) => res.json(document));
httpAdapter.get(path, (req, res) => res.send(swaggerHtml));
httpAdapter.get(path + '-json', (req, res) => res.json(document));
}

private static setupFastify(
Expand Down
8 changes: 2 additions & 6 deletions lib/swagger-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SchemaObjectFactory } from './services/schema-object-factory';
import { SwaggerTypesMapper } from './services/swagger-types-mapper';
import { SwaggerExplorer } from './swagger-explorer';
import { SwaggerTransformer } from './swagger-transformer';
import { getGlobalPrefix } from './utils/get-global-prefix';
import { stripLastSlash } from './utils/strip-last-slash.util';

export class SwaggerScanner {
Expand Down Expand Up @@ -45,7 +46,7 @@ export class SwaggerScanner {
includedModules
);
const globalPrefix = !ignoreGlobalPrefix
? stripLastSlash(this.getGlobalPrefix(app))
? stripLastSlash(getGlobalPrefix(app))
: '';

const denormalizedPaths = modules.map(
Expand Down Expand Up @@ -139,11 +140,6 @@ export class SwaggerScanner {
});
}

private getGlobalPrefix(app: INestApplication): string {
const internalConfigRef = (app as any).config;
return (internalConfigRef && internalConfigRef.getGlobalPrefix()) || '';
}

private getModulePathMetadata(
container: NestContainer,
metatype: Type<unknown>
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/get-global-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { INestApplication } from "@nestjs/common";

export function getGlobalPrefix(app: INestApplication): string {
const internalConfigRef = (app as any).config;
return (internalConfigRef && internalConfigRef.getGlobalPrefix()) || '';
}
19 changes: 19 additions & 0 deletions test/extra/shim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Shim from '../../lib/extra/swagger-shim';
import * as Actual from '../../lib';

describe('Shim file', () => {
it('contains all types export by package', () => {
const exceptions = ['getSchemaPath', 'refs'];

const shimExportNames = Object.keys(Shim);
const packageExportNames = Object.keys(Actual);

const exportsMissingInShim = packageExportNames.filter(
(exportName) =>
shimExportNames.indexOf(exportName) === -1 &&
exceptions.indexOf(exportName) === -1
);

expect(exportsMissingInShim).toEqual([]);
});
});

0 comments on commit b182249

Please sign in to comment.