Skip to content

Commit

Permalink
feat: use cloudflare js cdn, use all minified css and js
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses cloudflare cdn
  • Loading branch information
scttcper committed Jul 6, 2020
1 parent 4850330 commit 9237274
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 35 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ routePrefix: '/docs', // route where the view is returned
specPrefix: '/docs/spec', // route where the spec is returned
exposeSpec: false, // expose spec file
hideTopbar: false, // hide swagger top bar
favicon16: '/favicon-16x16.png', // default icon 16x16, set for self icon
favicon32: '/favicon-32x32.png', // default icon 32x32, set for self icon
favicon: '/favicon.png', // default favicon
```

## example
Expand Down
Binary file removed lib/favicon-32x32.png
Binary file not shown.
File renamed without changes
9 changes: 4 additions & 5 deletions lib/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{{title}}</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@{{swaggerVersion}}/swagger-ui.css">
<link rel="icon" type="image/png" href="{{favicon32}}" sizes="32x32" />
<link rel="icon" type="image/png" href="{{favicon16}}" sizes="16x16" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/{{swaggerVersion}}/swagger-ui.min.css">
<link rel="icon" type="image/png" href="{{favicon}}" />
<style>
html
{
Expand Down Expand Up @@ -67,8 +66,8 @@

<div id="swagger-ui"></div>

<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@{{swaggerVersion}}/swagger-ui-bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@{{swaggerVersion}}/swagger-ui-standalone-preset.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/{{swaggerVersion}}/swagger-ui-bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/{{swaggerVersion}}/swagger-ui-standalone-preset.js"></script>
<script>
function HideTopbarPlugin() {
// this plugin overrides the Topbar component to return nothing
Expand Down
22 changes: 6 additions & 16 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export interface KoaSwaggerUiOptions {
specPrefix: string;
exposeSpec: boolean;
hideTopbar: boolean;
favicon16: string;
favicon32: string;
favicon: string;
}

const defaultOptions: KoaSwaggerUiOptions = {
Expand All @@ -44,8 +43,7 @@ const defaultOptions: KoaSwaggerUiOptions = {
swaggerVersion: '',
exposeSpec: false,
hideTopbar: false,
favicon16: '/favicon-16x16.png',
favicon32: '/favicon-32x32.png',
favicon: '/favicon.png',
};

function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}): Middleware {
Expand All @@ -59,10 +57,8 @@ function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}): Middleware {
}

// Setup icons
const extFavicon16 = config.favicon16;
const extFavicon32 = config.favicon32;
const favicon16Path = join(__dirname, defaultOptions.favicon16);
const favicon32Path = join(__dirname, defaultOptions.favicon32);
const extFavicon = config.favicon;
const faviconPath = join(__dirname, defaultOptions.favicon);

// Setup default options
const options: KoaSwaggerUiOptions = defaultsDeep(config, defaultOptions);
Expand All @@ -86,15 +82,9 @@ function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}): Middleware {
return true;
}

if (extFavicon16 === undefined && ctx.path === defaultOptions.favicon16) {
if (extFavicon === undefined && ctx.path === defaultOptions.favicon) {
ctx.type = 'image/png';
ctx.body = createReadStream(favicon16Path);
return true;
}

if (extFavicon32 === undefined && ctx.path === defaultOptions.favicon32) {
ctx.type = 'image/png';
ctx.body = createReadStream(favicon32Path);
ctx.body = createReadStream(faviconPath);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint:fix": "eslint --ext .ts,.js --fix .",
"prepare": "npm run build",
"build": "del-cli dist && tsc -p tsconfig.build.json",
"postbuild": "cp lib/index.hbs dist && cp lib/favicon-32x32.png dist && cp lib/favicon-16x16.png dist",
"postbuild": "cp lib/index.hbs dist && cp lib/favicon.png dist",
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --ci --coverage --no-cache"
Expand Down
16 changes: 5 additions & 11 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,18 @@ describe('koa2-swagger-ui', () => {
it('should return css', async () => {
const result = readPkgUp.sync({ cwd: __dirname });
const version = result!.packageJson.devDependencies!['swagger-ui-dist'];
const url = `https://cdn.jsdelivr.net/npm/swagger-ui-dist@${version}`;
const url = `https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/${version}`;
await request(url)
.get('/swagger-ui.css')
.expect('Content-Type', 'text/css; charset=utf-8')
.get('/swagger-ui.min.css')
.expect('Content-Type', 'text/css')
.expect(200);
});
it('should return spec', async () => {
await request(app.callback()).get('/docs/spec').expect('Content-Type', /json/).expect(200);
});
it('should return icon16x16', async () => {
it('should return favicon', async () => {
await request(app.callback())
.get('/favicon-16x16.png')
.expect('Content-Type', /png/)
.expect(200);
});
it('should return icon32x32', async () => {
await request(app.callback())
.get('/favicon-32x32.png')
.get('/favicon.png')
.expect('Content-Type', /png/)
.expect(200);
});
Expand Down

0 comments on commit 9237274

Please sign in to comment.