How to use koa-joi-router - 10 common examples

To help you get started, we’ve selected a few koa-joi-router examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eram / tensorflow-stack-ts / src / app.ts View on Github external
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
github a-s-o / koa-docs / example / routes / pets.js View on Github external
}
};


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()
github a-s-o / koa-docs / example / routes / store.js View on Github external
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: {
github interledgerjs / rafiki / packages / rafiki-admin-api / src / index.ts View on Github external
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',
github a-s-o / koa-docs / example / routes / store.js View on Github external
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();
github a-s-o / koa-docs / example / routes / store.js View on Github external
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();
   }
};
github a-s-o / koa-docs / example / routes / pets.js View on Github external
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,
github a-s-o / koa-docs / example / routes / pets.js View on Github external
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;
github a-s-o / koa-docs / example / routes / store.js View on Github external
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')
github vemoteam / vemo / test / server / vemofile.js View on Github external
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: {

koa-joi-router

Configurable, input validated routing for koa.

MIT
Latest version published 4 years ago

Package Health Score

53 / 100
Full package analysis