Skip to content

Commit

Permalink
feat: define return type as koa middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Jan 18, 2020
1 parent 85602c1 commit 9fc5c62
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module.exports = {
rules: {
'object-curly-spacing': ['error', 'always'],
'@typescript-eslint/indent': ['error', 2, { SwitchCase: 1 }],
'@typescript-eslint/explicit-function-return-type': 0,
'capitalized-comments': 0,
'@typescript-eslint/no-explicit-any': 0,
'comma-dangle': ['error', 'always-multiline'],
},
Expand Down
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.1
jobs:
test:
docker:
- image: circleci/node:10
- image: circleci/node:12
steps:
- checkout
- restore_cache:
Expand All @@ -25,7 +25,7 @@ jobs:
command: bash <(curl -s https://codecov.io/bash)
release:
docker:
- image: circleci/node:10
- image: circleci/node:12
steps:
- checkout
- restore_cache:
Expand Down
11 changes: 5 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import Handlebars from 'handlebars';
import { defaultsDeep } from 'lodash';
import path from 'path';
import readPkgUp from 'read-pkg-up';
import { Context } from 'koa';
import { Context, Middleware } from 'koa';

export interface SwaggerOptions {
[key: string]: string | boolean | string[] | object;
// eslint-disable-next-line @typescript-eslint/camelcase
dom_id: string;
url: string;
Expand All @@ -16,7 +17,6 @@ export interface SwaggerOptions {
showRequestHeaders: boolean;
layout: string;
spec: object;
[key: string]: string | boolean | string[] | object;
}

export interface KoaSwaggerUiOptions {
Expand Down Expand Up @@ -46,7 +46,7 @@ const defaultOptions: KoaSwaggerUiOptions = {
favicon32: '/favicon-32x32.png',
};

function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}) {
function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}): Middleware {
if (config.swaggerVersion === undefined) {
const pkg = readPkgUp.sync({ cwd: __dirname });
if (pkg === undefined) {
Expand All @@ -67,13 +67,12 @@ function koaSwagger(config: Partial<KoaSwaggerUiOptions> = {}) {
Handlebars.registerHelper('json', context => JSON.stringify(context));
Handlebars.registerHelper('strfnc', fnc => fnc);
Handlebars.registerHelper('isset', function (this: any, conditional: any, opt) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
return conditional ? opt.fn(this) : opt.inverse(this);
});
const index = Handlebars.compile(fs.readFileSync(path.join(__dirname, './index.hbs'), 'utf-8'));

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

0 comments on commit 9fc5c62

Please sign in to comment.