Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function main(routes: IRoute[]): Koa {
const app: Koa = new Koa();
const router = joiRouter();
const prefix = process.env.ROUTER_APP || "/";
router.prefix(prefix);
console.log("Router setup:", ` prefix: ${prefix}`);
routes.forEach((route: IRoute) => {
if (route.method === "static") {
if (!route.folder) throw new Error("missing route.folder");
const srv = serve(route.folder, { defer: false, gzip: false });
const mnt = mount(route.path, srv);
app.use(mnt);
console.log(` ${route.path} => ${route.folder}`);
} else {
if (!route.handler) throw new Error("missing route.handler");
// tslint:disable-next-line:no-any
}
};
const getPetByStatus = {
method: 'put',
path: '/pet',
meta: {
friendlyName: 'Find pets by status'
},
validate: {
type: 'json',
query: {
status: t.array()
.description('Status values that need to be considered for filter')
.items(t.string())
.min(1) // At least 1 should be provided
.single() // If only one is provided, wrap it in an array
},
output: {
200: {
body: t.array().items(Pet.requiredKeys('id', 'status')),
header: {
'Content-Type': t.string()
}
}
}
},
*handler () {
const query = this.request.query;
return yield this.db()
body: {
code: t.number().integer().min(0).max(100).default(0).description('Code to explain the response.'),
errors: t.object().keys({
name: {
message: t.string().required().default('Some pet has no name!').description('Thrown when some pets has no name.')
}
}),
tags: t.array().items(t.object().keys({
label: t.string().example('Hello').example('World'),
signal: t.array().items(t.string())
})),
error: t.string().valid('Pets not found!').description('Pets not found!')
}
},
500: {
body: t.string().default('Server Internal Error.')
}
}
},
*handler () {
// This route does not have any validations
return this.db().table('store')
.groupBy('statusCode')
.map('quantity')
.run();
}
};
const orderPet = {
method: 'post',
path: '/order',
meta: {
try {
const account = await accounts.get(ctx.request.params.id)
ctx.body = account
} catch (error) {
ctx.response.status = 404
}
}
})
middlewareRouter.route({
method: 'post',
path: '/peers',
validate: {
body: {
id: Joi.string().required(),
relation: Joi.string().required(),
url: Joi.string().optional()
},
type: 'json'
},
handler: async (ctx: Context) => {
const peerInfo = ctx.request.body
const peer = await peers.add(peerInfo)
// const account = await accounts.set()
// TODO: Do we create the token automatically
// await tokenService.create({ sub: peerInfo.id, active: true })
ctx.response.body = peer
ctx.response.status = 201
}
})
middlewareRouter.route({
method: 'get',
path: '/peers',
body: {
available: Quantity.description('Pets available for sale'),
pending: Quantity.description('# of pets awaiting processing'),
sold: Quantity.description('# of pets sold')
}
},
400: {
body: {
code: t.number().integer().min(0).max(100).default(0).description('Code to explain the response.'),
errors: t.object().keys({
name: {
message: t.string().required().default('Some pet has no name!').description('Thrown when some pets has no name.')
}
}),
tags: t.array().items(t.object().keys({
label: t.string().example('Hello').example('World'),
signal: t.array().items(t.string())
})),
error: t.string().valid('Pets not found!').description('Pets not found!')
}
},
500: {
body: t.string().default('Server Internal Error.')
}
}
},
*handler () {
// This route does not have any validations
return this.db().table('store')
.groupBy('statusCode')
.map('quantity')
.run();
sold: Quantity.description('# of pets sold')
}
},
400: {
body: {
code: t.number().integer().min(0).max(100).default(0).description('Code to explain the response.'),
errors: t.object().keys({
name: {
message: t.string().required().default('Some pet has no name!').description('Thrown when some pets has no name.')
}
}),
tags: t.array().items(t.object().keys({
label: t.string().example('Hello').example('World'),
signal: t.array().items(t.string())
})),
error: t.string().valid('Pets not found!').description('Pets not found!')
}
},
500: {
body: t.string().default('Server Internal Error.')
}
}
},
*handler () {
// This route does not have any validations
return this.db().table('store')
.groupBy('statusCode')
.map('quantity')
.run();
}
};
friendlyName: 'Find pets by status'
},
validate: {
type: 'json',
query: {
status: t.array()
.description('Status values that need to be considered for filter')
.items(t.string())
.min(1) // At least 1 should be provided
.single() // If only one is provided, wrap it in an array
},
output: {
200: {
body: t.array().items(Pet.requiredKeys('id', 'status')),
header: {
'Content-Type': t.string()
}
}
}
},
*handler () {
const query = this.request.query;
return yield this.db()
.table('pets')
.getAll(query.status)
.run();
}
};
module.exports = [
createPet,
return this.db().table('pets').update(pet).run();
}
};
const getPetByStatus = {
method: 'put',
path: '/pet',
meta: {
friendlyName: 'Find pets by status'
},
validate: {
type: 'json',
query: {
status: t.array()
.description('Status values that need to be considered for filter')
.items(t.string())
.min(1) // At least 1 should be provided
.single() // If only one is provided, wrap it in an array
},
output: {
200: {
body: t.array().items(Pet.requiredKeys('id', 'status')),
header: {
'Content-Type': t.string()
}
}
}
},
*handler () {
const query = this.request.query;
200: {
body: {
available: Quantity.description('Pets available for sale'),
pending: Quantity.description('# of pets awaiting processing'),
sold: Quantity.description('# of pets sold')
}
},
400: {
body: {
code: t.number().integer().min(0).max(100).default(0).description('Code to explain the response.'),
errors: t.object().keys({
name: {
message: t.string().required().default('Some pet has no name!').description('Thrown when some pets has no name.')
}
}),
tags: t.array().items(t.object().keys({
label: t.string().example('Hello').example('World'),
signal: t.array().items(t.string())
})),
error: t.string().valid('Pets not found!').description('Pets not found!')
}
},
500: {
body: t.string().default('Server Internal Error.')
}
}
},
*handler () {
// This route does not have any validations
return this.db().table('store')
.groupBy('statusCode')
.map('quantity')
const path = require('path')
const router = require('koa-joi-router')
const Joi = router.Joi
module.exports = {
host: 'localhost',
port: 5001,
root: path.resolve('.'),
socket: true,
cloudbase: true,
routes: [
{
path: 'routes/api.js',
route: '/api',
method: 'post',
validate: {
type: 'json',
continueOnError: false,
body: {