How to use co-body - 10 common examples

To help you get started, we’ve selected a few co-body 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 chrisyip / koa-buddy / index.js View on Github external
function* parser (ctx) {
  var body

  if (ctx.is('text/*')) {
    body = yield text(ctx)
  } else if (ctx.is('json')) {
    body = yield co.json(ctx)
  } else if (ctx.is('xml')) {
    body = yield xml(ctx)
  } else if (ctx.is('urlencoded')) {
    body = yield co.form(ctx)
  } else if (ctx.is('multipart')) {
    body = yield multipart(ctx)
  } else {
    // return stream buffer for unsupported content-type
    body = yield buffer(ctx)
  }

  return body
}
github dylanpyle / node-api-boilerplate / middleware / json-body / index.js View on Github external
function* jsonBody(next) {
  if (['POST', 'PUT', 'PATCH'].includes(this.method)) {
    try {
      this.state.body = yield parse.json(this);
    } catch (e) {
      // Possibly revisit this choice; if the body wasn't JSON-parsable then
      // reject it immediately.
      this.throw(400, 'Invalid request body');
    }

    if (!this.state.body) { this.state.body = {}; }
  }

  yield next;
}
github Summerlve / koablog / controllers / User.js View on Github external
module.exports.add = function* add (next) {
    let body = yield parse.form(this);

    // create a new user
    let username = body.username;
    let password = body.password;
    let penName = body.penName;
    let groupName = body.groupName;
    let avatar = body.avatar;
    let introduce = body.introduce;

    if (!username || !password || !penName || !groupName) {
        this.status = 400;
        this.body = {
            statusCode: 400,
            reasonPhrase: "Bad Request",
            description: "username, password, penName, groupName is required, and must be not void",
            errorCode: 2000
github koajs / joi-router / joi-router.js View on Github external
return async function parseFormBody(ctx, next) {
    if (!ctx.request.is('urlencoded')) {
      return ctx.throw(400, 'expected x-www-form-urlencoded');
    }

    // eslint-disable-next-line require-atomic-updates
    ctx.request.body = ctx.request.body || await parse.form(ctx, opts);
    await next();
  };
}
github Summerlve / koablog / controllers / Group.js View on Github external
module.exports.add = function* add (next) {
    let body = yield parse.form(this);

    let groupName = body.groupName;
    let description = body.description;

    if (!groupName) {
        // groupName can not be void
        this.status = 400;
        this.body = {
            statusCode: 400,
            reasonPhrase: "Bad Request",
            description: "groupName can not be void",
            errorCode: 5000
        };
        return ;
    }
github Summerlve / koablog / controllers / User.js View on Github external
module.exports.update = function* update (next) {
    // get user from checkUser
    let user = this.user;

    let body = yield parse.form(this);

    // check which field need to update
    let username = "";
    let password = "";
    let penName = "";
    let introduce = "";

    let updater = {};

    if ("username" in body) {
        username = body.username;

        if (!username) {
            this.status = 400;
            this.body = {
                statusCode: 400,
github t2ee / vader / test-dist / src / core / Router.js View on Github external
return yield new Promise((resolve, reject) => {
                    parse.text(koaContext).then(resolve).catch(reject);
                });
            }
github bidanjun / koa-graphql-next / src / __test__ / http-post.spec.js View on Github external
app.use(async (ctx, next) => {
        if (ctx.is('application/graphql')) {
            ctx.request.body = await bodyParser.text(ctx);
        }
        await next();
    });
github freedomexio / rocketx-condenser / src / server / api / general.js View on Github external
router.post('/csp_violation', function*() {
        if (rateLimitReq(this, this.req)) return;
        let params;
        try {
            params = yield coBody(this);
        } catch (error) {
            console.log('-- /csp_violation error -->', error);
        }
        if (params && params['csp-report']) {
            const csp_report = params['csp-report'];
            const value = `${csp_report['document-uri']} : ${
                csp_report['blocked-uri']
            }`;
            console.log(
                '-- /csp_violation -->',
                value,
                '--',
                this.req.headers['user-agent']
            );
            recordWebEvent(this, 'csp_violation', value);
        } else {
github freedomexio / rocketx-condenser / src / server / api / general.js View on Github external
router.post('/csp_violation', function*() {
        if (rateLimitReq(this, this.req)) return;
        let params;
        try {
            params = yield coBody(this);
        } catch (error) {
            console.log('-- /csp_violation error -->', error);
        }
        if (params && params['csp-report']) {
            const csp_report = params['csp-report'];
            const value = `${csp_report['document-uri']} : ${
                csp_report['blocked-uri']
            }`;
            console.log(
                '-- /csp_violation -->',
                value,
                '--',
                this.req.headers['user-agent']
            );
        } else {
            console.log(

co-body

request body parsing for co

MIT
Latest version published 4 years ago

Package Health Score

71 / 100
Full package analysis