Skip to content

Commit

Permalink
fix: fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Rudowski committed Jul 16, 2022
1 parent 282ff8e commit 2c94312
Show file tree
Hide file tree
Showing 10 changed files with 1,237 additions and 725 deletions.
18 changes: 8 additions & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"env": {
"node":true,
"node": true,
"es6": true
},
"extends": [
Expand All @@ -19,16 +19,10 @@
"error",
"all"
],
"func-names": "error",
"no-underscore-dangle": [
2,
{
"allow": [
"__"
]
}
],
"func-names": "warn",
"no-underscore-dangle": "warn",
"global-require": "off",
"guard-for-in": "warn",
"quotes": [
"warn",
"single",
Expand Down Expand Up @@ -70,6 +64,10 @@
"skipTemplates": false
}
],
"no-multi-assign": "warn",
"no-param-reassign": "warn",
"no-restricted-syntax": "warn",
"no-shadow": "warn",
"no-plusplus": [
"error",
{
Expand Down
18 changes: 0 additions & 18 deletions example/.eslintrc.js

This file was deleted.

13 changes: 8 additions & 5 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const koa = require('koa')
const app = new koa()
app.use(require('./routes').middleware())
const Koa = require('koa');

const app = new Koa();
app.use(require('./routes').middleware());

app.listen(5566, () => {
console.log('API docs url: http://localhost:5566/apiDocs')
})
// eslint-disable-next-line no-console
console.log('API docs url: http://localhost:5566/apiDocs');
});
265 changes: 136 additions & 129 deletions example/routes.js
Original file line number Diff line number Diff line change
@@ -1,158 +1,165 @@
const { SwaggerAPI } = require('../')
const Router = require('koa-joi-router')
const Joi = Router.Joi
const Router = require('@koa-better-modules/joi-router');
const { SwaggerAPI } = require('../src');

const Joi = Router.Joi;

/**
* Define routes
*/
const router = Router()
const router = Router();

// Get /user/:_id
router.get('/user/:_id', {
meta: {
swagger: {
summary: 'Get User Info',
description: `Note: \nSensitive data can only be viewed by the \`corresponding user\` or \`Admin\`.`,
tags: ['users']
}
},
validate: {
params: {
_id: Joi.string().alphanum().max(24).example('abcdefg').description('User id').required()
// Get /user/:id
router.get('/user/:id', {
meta: {
swagger: {
summary: 'Get User Info',
description: 'Note: \nSensitive data can only be viewed by the `corresponding user` or `Admin`.',
tags: ['users'],
},
},
output: {
'200-299': {
body: Joi.object({
userId: Joi.string().description('User id')
}).options({
allowUnknown: true
}).description('User object')
},
500: {
body: Joi.object({
message: Joi.string().description('error message')
}).description('error body')
}
}
},
handler: async ctx => {
console.log('getUser...')
ctx.body = {
userId: ctx.params._id
}
}
})
validate: {
params: {
id: Joi.string().alphanum().max(24).example('abcdefg').description('User id').required(),
},
output: {
'200-299': {
body: Joi.object({
userId: Joi.string().description('User id'),
})
.options({
allowUnknown: true,
})
.description('User object'),
},
500: {
body: Joi.object({
message: Joi.string().description('error message'),
}).description('error body'),
},
},
},
handler: async (ctx) => {
ctx.body = {
userId: ctx.params.id,
};
},
});

// POST /signup
router.post('/signup', {
meta: {
swagger: {
summary: 'User Signup',
description: 'Signup with username and password.',
tags: ['users']
}
},
validate: {
type: 'json',
body: Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().alphanum().min(6).max(30).required()
}).example({username: 'abcdefg', password: '123123'}),
output: {
200: {
body: {
userId: Joi.string().description('Newly created user id')
}
},
500: {
body: {
code: Joi.number().description('error code'),
}
}
}
},
handler: async ctx => {
ctx.body = {
userId: ctx.body.username
}
}
})
meta: {
swagger: {
summary: 'User Signup',
description: 'Signup with username and password.',
tags: ['users'],
},
},
validate: {
type: 'json',
body: Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().alphanum().min(6).max(30).required(),
}).example({ username: 'abcdefg', password: '123123' }),
output: {
200: {
body: {
userId: Joi.string().description('Newly created user id'),
},
},
500: {
body: {
code: Joi.number().description('error code'),
},
},
},
},
handler: async (ctx) => {
ctx.body = {
userId: ctx.body.username,
};
},
});

const ProfileJoi = Joi.object({
profileName: Joi.string(),
})
profileName: Joi.string(),
});
// POST /profile using schema references `ref`
router.post('/profile', {
meta: {
swagger: {
summary: 'Profile',
description: 'Signup with username and password.',
tags: ['users']
}
},
validate: {
type: 'json',
body: ProfileJoi,
ref: "#/definitions/Profile",
output: {
200: {
meta: {
swagger: {
summary: 'Profile',
description: 'Signup with username and password.',
tags: ['users'],
},
},
validate: {
type: 'json',
body: ProfileJoi,
ref: "#/definitions/Profile",
}
}
},
handler: async ctx => {
ctx.body = {
profileName: 'this is a profile name'
}
}
})
ref: '#/definitions/Profile',
output: {
200: {
body: ProfileJoi,
ref: '#/definitions/Profile',
},
},
},
handler: async (ctx) => {
ctx.body = {
profileName: 'this is a profile name',
};
},
});

/**
* Generate Swagger json from the router object
*/
const generator = new SwaggerAPI()
generator.addJoiRouter(router)
const generator = new SwaggerAPI();
generator.addJoiRouter(router);

const spec = generator.generateSpec({
info: {
title: 'Example API',
description: 'API for creating and editing examples.',
version: '1.1'
},
basePath: '/',
tags: [{
name: 'users',
description: `A User represents a person who can login
and take actions subject to their granted permissions.`
}],
// pass `definitions` if you need schema references
definitions: {
Profile: ProfileJoi
},
}, {
defaultResponses: {
200: {
description: 'OK'
const spec = generator.generateSpec(
{
info: {
title: 'Example API',
description: 'API for creating and editing examples.',
version: '1.1',
},
basePath: '/',
tags: [
{
name: 'users',
description: `A User represents a person who can login
and take actions subject to their granted permissions.`,
},
],
// pass `definitions` if you need schema references
definitions: {
Profile: ProfileJoi,
},
},
500: {
description: 'ERROR'
{
defaultResponses: {
200: {
description: 'OK',
},
500: {
description: 'ERROR',
},
}, // Custom default responses if you don't like default 200
}
} // Custom default responses if you don't like default 200
})
);

/**
* Swagger JSON API
*/
router.get('/_api.json', async ctx => {
ctx.body = JSON.stringify(spec, null, ' ')
})
router.get('/_api.json', async (ctx) => {
ctx.body = JSON.stringify(spec, null, ' ');
});

/**
* API documentation
*/
router.get('/apiDocs', async ctx => {
ctx.body = `
router.get('/apiDocs', async (ctx) => {
ctx.body = `
<!DOCTYPE html>
<html>
<head>
Expand All @@ -166,7 +173,7 @@ router.get('/apiDocs', async ctx => {
<script src="https://rebilly.github.io/ReDoc/releases/latest/redoc.min.js"></script>
</body>
</html>
`
})
`;
});

module.exports = router
module.exports = router;
Loading

0 comments on commit 2c94312

Please sign in to comment.