async function parseBody(req) {
    const { body } = req;
    // If express has already parsed a body as a keyed object, use it.
    if (typeof body === 'object' && !(body instanceof Buffer)) {
        return body;
    }
    // Skip requests without content types.
    if (req.headers['content-type'] === undefined) {
        return {};
    }
    const typeInfo = content_type_1.default.parse(req);
    // If express has already parsed a body as a string, and the content-type
    // was application/graphql, parse the string body.
    if (typeof body === 'string' && typeInfo.type === 'application/graphql') {
        return { query: body };
    }
    // Already parsed body we didn't recognise? Parse nothing.
    if (body != null) {
        return {};
    }
    const rawBody = await readBody(req, typeInfo);
    // Use the correct body parser based on Content-Type header.
    switch (typeInfo.type) {
        case 'application/graphql':
            return { query: rawBody };
        case 'application/json':
            if (jsonObjRegex.test(rawBody)) {
                try {
                    return JSON.parse(rawBody);
                }
                catch (_a) {
                    // Do nothing
                }
            }
            throw http_errors_1.default(400, 'POST body sent invalid JSON.');
        case 'application/x-www-form-urlencoded':
            return querystring_1.default.parse(rawBody);
    }
    // If no Content-Type header matches, parse nothing.
    return {};
}
/**
 * Provided a "Request" provided by express or connect (typically a node style
 * HTTPClientRequest), Promise the body data contained.
 */
Provided a "Request" provided by express or connect (typically a node style
HTTPClientRequest), Promise the body data contained.
Provided a " Request " provided by express or connect ( typically a node style HTTPClientRequest ) Promise the body data contained .
async function parseBody(req) {
    const { body } = req;
    if (typeof body === 'object' && !(body instanceof Buffer)) {
        return body;
    }
    if (req.headers['content-type'] === undefined) {
        return {};
    }
    const typeInfo = content_type_1.default.parse(req);
    if (typeof body === 'string' && typeInfo.type === 'application/graphql') {
        return { query: body };
    }
    if (body != null) {
        return {};
    }
    const rawBody = await readBody(req, typeInfo);
    switch (typeInfo.type) {
        case 'application/graphql':
            return { query: rawBody };
        case 'application/json':
            if (jsonObjRegex.test(rawBody)) {
                try {
                    return JSON.parse(rawBody);
                }
                catch (_a) {
                }
            }
            throw http_errors_1.default(400, 'POST body sent invalid JSON.');
        case 'application/x-www-form-urlencoded':
            return querystring_1.default.parse(rawBody);
    }
    return {};
}
async function parseBody ( req ) { const { body } = req ; if ( typeof body === 'object' && ! ( body instanceof Buffer ) ) { return body ; } if ( req . headers [ 'content-type' ] === undefined ) { return { } ; } const typeInfo = content_type_1 . default . parse ( req ) ; if ( typeof body === 'string' && typeInfo . type === 'application/graphql' ) { return { query : body } ; } if ( body != null ) { return { } ; } const rawBody = await readBody ( req , typeInfo ) ; switch ( typeInfo . type ) { case 'application/graphql' : return { query : rawBody } ; case 'application/json' : if ( jsonObjRegex . test ( rawBody ) ) { try { return JSON . parse ( rawBody ) ; } catch ( _a ) { } } throw http_errors_1 . default ( 400 , 'POST body sent invalid JSON.' ) ; case 'application/x-www-form-urlencoded' : return querystring_1 . default . parse ( rawBody ) ; } return { } ; }
Provided a "Request" provided by express or connect (typically a node style
 HTTPClientRequest), Promise the body data contained.
Provided a " Request " provided by express or connect ( typically a node style HTTPClientRequest ) Promise the body data contained .
// If express has already parsed a body as a keyed object, use it. // Skip requests without content types. // If express has already parsed a body as a string, and the content-type // was application/graphql, parse the string body. // Already parsed body we didn't recognise? Parse nothing. // Use the correct body parser based on Content-Type header. // Do nothing // If no Content-Type header matches, parse nothing.