How to use the co-body.json function in co-body

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 joesonw / ts-router / dist / router.js View on Github external
body = yield new Promise((resolve, reject) => {
                            parse.json(context).then(resolve).catch(reject);
                        });
                    }
github kzwang / node-git-lfs / lib / routes / verify.js View on Github external
app.post('/:user/:repo/objects/verify', checkJWT('verify'), wrap(function* (req, res, next) {
        try {
            var body = yield parse.json(req);

            var oid = body.oid;
            var size = body.size;
            if (!oid || !size) {
                return res.status(422).end();
            }

            var objectSize = yield STORE.getSize(req.params.user, req.params.repo, oid);
            if (size !== objectSize) {
                return res.status(422).end();
            }

            res.status(200).end();
        } catch (err) {
            next(err);
        }
github vtex-apps / store-graphql / service / middlewares / bodyParserMiddleware.ts View on Github external
return async (ctx) => {
    const body = await json(ctx.request)
    const response = await handler(body, ctx.vtex)
    ctx.set('Content-Type', 'application/json')
    ctx.status = 200
    ctx.body = response
  }
})
github t2ee / vader / dist / core / Router.js View on Github external
return yield new Promise((resolve, reject) => {
                    parse.json(koaContext).then(resolve).catch(reject);
                });
            }
github adonisjs / adonis-middleware / src / BodyParser / index.js View on Github external
* _parse (request) {
    let formBody = {
      fields: {},
      files: {},
      raw: null
    }

    if (request.is(contentTypes.json)) {
      formBody.fields = yield coBody.json(request.request, this.formOptions)
    } else if (request.is(contentTypes.form)) {
      formBody.fields = yield coBody.form(request.request, this.formOptions)
    } else if (request.is(contentTypes.text)) {
      formBody.raw = yield coBody.text(request.request, this.formOptions)
    } else if (request.is(contentTypes.multipart)) {
      formBody = yield this._multipart(request, this.uploadOptions)
    }
    return formBody
  }
github koajs / bodyparser / index.js View on Github external
async function parseBody(ctx) {
    if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) {
      return await parse.json(ctx, jsonOpts);
    }
    if (enableForm && ctx.request.is(formTypes)) {
      return await parse.form(ctx, formOpts);
    }
    if (enableText && ctx.request.is(textTypes)) {
      return await parse.text(ctx, textOpts) || '';
    }
    return {};
  }
};
github xiongwilee / Gracejs / middleware / body / index.js View on Github external
async function parseBody(ctx) {
    if (enableJson && ctx.request.is(jsonTypes)) {
      return await parse.json(ctx, jsonOpts);
    }
    if (enableForm && ctx.request.is(formTypes)) {
      return await parse.form(ctx, formOpts);
    }
    if (enableText && ctx.request.is(textTypes)) {
      return await parse.text(ctx, textOpts) || '';
    }

    return {};
  }
};

co-body

request body parsing for co

MIT
Latest version published 7 months ago

Package Health Score

79 / 100
Full package analysis