Skip to content

Commit

Permalink
chore: fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Feb 25, 2019
1 parent d8e798c commit e55e161
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
root: true,
env: {
node: true,
jest: true,
},
extends: ['xo-space/esnext', 'xo-typescript', 'prettier/@typescript-eslint'],
rules: {
'object-curly-spacing': ['error', 'always'],
'@typescript-eslint/indent': ['error', 2, { SwitchCase: 1 }],
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/promise-function-async': 0,
'capitalized-comments': 0,
'comma-dangle': ['error', 'always-multiline'],
},
};
14 changes: 10 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import readPkgUp from 'read-pkg-up';
import { Context } from 'koa';

export interface SwaggerOptions {
// eslint-disable-next-line @typescript-eslint/camelcase
dom_id: string;
url: string;
supportedSubmitMethods: string[];
Expand All @@ -32,6 +33,7 @@ const defaultOptions: KoaSwaggerUiOptions = {
title: 'Swagger UI',
oauthOptions: false,
swaggerOptions: {
// eslint-disable-next-line @typescript-eslint/camelcase
dom_id: '#swagger-ui',
url: 'https://petstore.swagger.io/v2/swagger.json',
layout: 'StandaloneLayout',
Expand All @@ -45,9 +47,10 @@ const defaultOptions: KoaSwaggerUiOptions = {

function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}) {
if (!config.swaggerVersion) {
const pkg: any = readPkgUp.sync({ cwd: __dirname }).pkg;
defaultOptions.swaggerVersion = pkg.devDependencies['swagger-ui-dist'];
const { pkg } = readPkgUp.sync({ cwd: __dirname });
defaultOptions.swaggerVersion = pkg.devDependencies!['swagger-ui-dist'];
}

// Setup icons
const extFavicon16 = config.favicon16;
const extFavicon32 = config.favicon32;
Expand All @@ -58,28 +61,31 @@ function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}) {
const options = defaultsDeep(config, defaultOptions);
Handlebars.registerHelper('json', context => JSON.stringify(context));
Handlebars.registerHelper('strfnc', fnc => fnc);
Handlebars.registerHelper('isset', function(this: any, conditional, opt) {
Handlebars.registerHelper('isset', function (this: any, conditional, opt) {
return conditional ? opt.fn(this) : opt.inverse(this);
});
const index = Handlebars.compile(fs.readFileSync(path.join(__dirname, './index.hbs'), 'utf-8'));

// tslint:disable-next-line:no-unused
// eslint-disable-next-line func-names, @typescript-eslint/ban-types
return function koaSwaggerUi(ctx: Context, next: Function) {
if (options.routePrefix === false || ctx.path === options.routePrefix) {
ctx.type = 'text/html';
ctx.body = index(options);
return true;
}

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

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

return next();
};
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"typings": "./index.d.ts",
"scripts": {
"start": "ts-node test/example.ts",
"lint": "tslint -t stylish -p .",
"lint:fix": "tslint -p tsconfig.json -t stylish --fix 'src/**/*.ts' 'test/**/*.ts'",
"lint": "eslint lib/**/*.ts test/**/*.ts",
"lint:fix": "eslint --fix lib/**/*.ts test/**/*.ts",
"prebuild": "rimraf dist",
"build": "tsc -p tsconfig.json",
"postbuild": "cp lib/index.hbs dist && cp lib/favicon-32x32.png dist && cp lib/favicon-16x16.png dist && cp package.json dist && cp LICENSE dist && cp README.md dist",
Expand Down
4 changes: 1 addition & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe('koa2-swagger-ui', () => {
.expect(200);
});
it('should return css', () => {
const pkg: any = readPkgUp.sync({
cwd: __dirname as string,
}).pkg;
const { pkg } = readPkgUp.sync({ cwd: __dirname });
const version = pkg.devDependencies['swagger-ui-dist'];
const url = `https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/${version}`;
return request(url)
Expand Down
12 changes: 0 additions & 12 deletions tslint.json

This file was deleted.

0 comments on commit e55e161

Please sign in to comment.