UNPKG

graphql-yoga

Version:
35 lines (34 loc) • 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleURLSearchParams = handleURLSearchParams; exports.parseURLSearchParams = parseURLSearchParams; exports.isBodyBasedRequest = isBodyBasedRequest; exports.isContentTypeMatch = isContentTypeMatch; const fetch_1 = require("@whatwg-node/fetch"); function handleURLSearchParams(searchParams) { const operationName = searchParams.get('operationName') || undefined; const query = searchParams.get('query') || undefined; const variablesStr = searchParams.get('variables') || undefined; const extensionsStr = searchParams.get('extensions') || undefined; return { operationName, query, variables: variablesStr ? JSON.parse(variablesStr) : undefined, extensions: extensionsStr ? JSON.parse(extensionsStr) : undefined, }; } function parseURLSearchParams(requestBody) { const searchParams = new fetch_1.URLSearchParams(requestBody); return handleURLSearchParams(searchParams); } /** Whether params for this method are carried in the request body (vs. the URL, as `GET` uses). */ function isBodyBasedRequest(request) { return request.method === 'POST' || request.method === 'QUERY'; } function isContentTypeMatch(request, expectedContentType) { const contentType = request.headers.get('content-type'); // a list of content-types is not valid as per HTTP spec, but some clients dont care // the media type is case-insensitive and may be followed by optional whitespace before the parameters const mediaType = contentType?.split(',')[0]?.split(';')[0]?.trim().toLowerCase(); return mediaType === expectedContentType; }