-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
2,073 additions
and
1,976 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import * as SwaggerParser from 'swagger-parser'; | ||
import { DocumentBuilder, SwaggerModule } from '../lib'; | ||
import { ApplicationModule } from './src/app.module'; | ||
import { | ||
ExpressAdapter, | ||
NestExpressApplication | ||
} from '@nestjs/platform-express'; | ||
|
||
describe('Express Swagger', () => { | ||
let app: NestExpressApplication; | ||
let builder: DocumentBuilder; | ||
|
||
beforeEach(async () => { | ||
app = await NestFactory.create<NestExpressApplication>( | ||
ApplicationModule, | ||
new ExpressAdapter(), | ||
{ logger: false } | ||
); | ||
|
||
builder = new DocumentBuilder() | ||
.setTitle('Cats example') | ||
.setDescription('The cats API description') | ||
.setVersion('1.0') | ||
.addTag('cats') | ||
.addBasicAuth() | ||
.addBearerAuth() | ||
.addOAuth2() | ||
.addApiKey() | ||
.addApiKey({ type: 'apiKey' }, 'key1') | ||
.addApiKey({ type: 'apiKey' }, 'key2') | ||
.addCookieAuth() | ||
.addSecurityRequirements('bearer') | ||
.addSecurityRequirements({ basic: [], cookie: [] }); | ||
}); | ||
|
||
it('should produce a valid OpenAPI 3.0 schema', async () => { | ||
const document = SwaggerModule.createDocument(app, builder.build()); | ||
const doc = JSON.stringify(document, null, 2); | ||
|
||
try { | ||
let api = await SwaggerParser.validate(document as any); | ||
console.log( | ||
'API name: %s, Version: %s', | ||
api.info.title, | ||
api.info.version | ||
); | ||
expect(api.info.title).toEqual('Cats example'); | ||
} catch (err) { | ||
console.log(doc); | ||
expect(err).toBeUndefined(); | ||
} | ||
}); | ||
|
||
it('should fix colons in url', async () => { | ||
const document = SwaggerModule.createDocument(app, builder.build()); | ||
expect(document.paths['/express:colon:another/{prop}']).toBeDefined(); | ||
}); | ||
|
||
it('should setup multiple routes', async () => { | ||
const document1 = SwaggerModule.createDocument(app, builder.build()); | ||
SwaggerModule.setup('/swagger1', app, document1); | ||
|
||
const document2 = SwaggerModule.createDocument(app, builder.build()); | ||
SwaggerModule.setup('/swagger2', app, document2); | ||
|
||
await app.init(); | ||
expect(app.getHttpAdapter().getInstance()).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { ApplicationModule } from './src/app.module'; | ||
import { DocumentBuilder, SwaggerModule } from '../lib'; | ||
import { | ||
FastifyAdapter, | ||
NestFastifyApplication | ||
} from '@nestjs/platform-fastify'; | ||
import { INestApplication, Logger } from '@nestjs/common'; | ||
import { | ||
ExpressAdapter, | ||
NestExpressApplication | ||
} from '@nestjs/platform-express'; | ||
import { join } from 'path'; | ||
|
||
const port = 4001; | ||
const host = 'localhost'; | ||
const docRelPath = '/api-docs'; | ||
|
||
const USE_FASTIFY = true; | ||
const ENABLE_BASIC_AUTH = true; | ||
|
||
const adapter = USE_FASTIFY ? new FastifyAdapter() : new ExpressAdapter(); | ||
const publicFolderPath = join(__dirname, '../../e2e', 'public'); | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create<INestApplication>( | ||
ApplicationModule, | ||
adapter | ||
); | ||
const httpAdapter = app.getHttpAdapter(); | ||
|
||
ENABLE_BASIC_AUTH && | ||
httpAdapter.use('/api-docs', (req, res, next) => { | ||
function parse(input: string): { name: string; pass: string } { | ||
const [, encodedPart] = input.split(' '); | ||
|
||
const buff = Buffer.from(encodedPart, 'base64'); | ||
const text = buff.toString('ascii'); | ||
const [name, pass] = text.split(':'); | ||
|
||
return { name, pass }; | ||
} | ||
|
||
function unauthorizedResponse(): void { | ||
if (USE_FASTIFY) { | ||
res.statusCode = 401; | ||
res.setHeader('WWW-Authenticate', 'Basic'); | ||
} else { | ||
res.status(401); | ||
res.set('WWW-Authenticate', 'Basic'); | ||
} | ||
|
||
next(); | ||
} | ||
|
||
if (!req.headers.authorization) { | ||
return unauthorizedResponse(); | ||
} | ||
|
||
const credentials = parse(req.headers.authorization); | ||
|
||
if ( | ||
!credentials || | ||
credentials?.name !== 'admin' || | ||
credentials?.pass !== 'admin' | ||
) { | ||
return unauthorizedResponse(); | ||
} | ||
|
||
next(); | ||
}); | ||
|
||
app.setGlobalPrefix('/api/v1'); | ||
|
||
const swaggerSettings = new DocumentBuilder() | ||
.setTitle('Cats example') | ||
.setDescription('The cats API description') | ||
.setVersion('1.0') | ||
.addTag('cats') | ||
.addBasicAuth() | ||
.addBearerAuth() | ||
.addOAuth2() | ||
.addApiKey() | ||
.addApiKey({ type: 'apiKey' }, 'key1') | ||
.addApiKey({ type: 'apiKey' }, 'key2') | ||
.addCookieAuth() | ||
.addSecurityRequirements('bearer') | ||
.addSecurityRequirements({ basic: [], cookie: [] }) | ||
.build(); | ||
|
||
const document = SwaggerModule.createDocument(app, swaggerSettings, { | ||
deepScanRoutes: true, | ||
ignoreGlobalPrefix: false, | ||
extraModels: [] // add DTOs that are not explicitly registered here (like PaginatedDto, etc) | ||
}); | ||
|
||
SwaggerModule.setup(docRelPath, app, document, { | ||
customSiteTitle: 'Demo API - Swagger UI 1', | ||
swaggerOptions: { | ||
persistAuthorization: true, | ||
defaultModelsExpandDepth: -1, | ||
syntaxHighlight: { | ||
activate: true, | ||
theme: 'tomorrow-night' | ||
}, | ||
tryItOutEnabled: true | ||
}, | ||
customfavIcon: '/public/favicon.ico', | ||
customCssUrl: '/public/theme.css' // to showcase that in new implementation u can use custom css with fastify | ||
}); | ||
|
||
SwaggerModule.setup('/swagger-docs', app, document, { | ||
customSiteTitle: 'Demo API - Swagger UI 2', | ||
swaggerOptions: { | ||
persistAuthorization: true, | ||
defaultModelsExpandDepth: -1 | ||
} | ||
}); | ||
|
||
USE_FASTIFY | ||
? (app as NestFastifyApplication).useStaticAssets({ | ||
root: publicFolderPath, | ||
prefix: `/public`, | ||
decorateReply: false | ||
}) | ||
: (app as NestExpressApplication).useStaticAssets(publicFolderPath, { | ||
prefix: '/public' | ||
}); | ||
|
||
await app.listen(port, host); | ||
const baseUrl = `http://${host}:${port}`; | ||
const startMessage = `Server started at ${baseUrl}; SwaggerUI at ${ | ||
baseUrl + docRelPath | ||
};`; | ||
|
||
Logger.log(startMessage); | ||
} | ||
|
||
bootstrap(); |
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.swagger-ui .btn.authorize { | ||
line-height: 1; | ||
display: inline; | ||
color: #336E7B; | ||
border-color: #336E7B; | ||
} | ||
.swagger-ui .btn.authorize svg { | ||
fill: #ef0505; | ||
} | ||
|
||
|
||
.swagger-ui body { | ||
margin: 0; | ||
background: #fafafa | ||
} | ||
|
||
img[alt="Swagger UI"] { | ||
display: block; | ||
-moz-box-sizing: border-box; | ||
box-sizing: border-box; | ||
content: url('/public/logo.png'); | ||
max-width: 100%; | ||
max-height: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.