UNPKG

@pact-foundation/pact

Version:
47 lines 1.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.escapeGraphQlQuery = exports.escapeRegexChars = exports.escapeSpace = void 0; exports.validateQuery = validateQuery; const graphql_1 = require("graphql"); const graphql_tag_1 = __importDefault(require("graphql-tag")); const configurationError_1 = require("./configurationError"); const graphQLQueryError_1 = require("./graphQLQueryError"); /** * Accepts a raw or pre-parsed query, validating in the former case, and * returns a normalized raw query. * @param query {string|ASTNode} the query to validate * @param type the operation type */ function validateQuery(query, type) { if (!query) { throw new configurationError_1.ConfigurationError(`You must provide a GraphQL ${type}.`); } if (typeof query !== 'string') { if (query?.kind === 'Document') { // Already parsed, store in string form return (0, graphql_1.print)(query); } throw new configurationError_1.ConfigurationError('You must provide a either a string or parsed GraphQL.'); } else { // String, so validate it try { (0, graphql_tag_1.default)(query); } catch (e) { const error = e instanceof Error ? e : new Error(String(e)); throw new graphQLQueryError_1.GraphQLQueryError(`GraphQL ${type} is invalid: ${error.message}`); } return query; } } const escapeSpace = (s) => s.replace(/\s+/g, '\\s*'); exports.escapeSpace = escapeSpace; const escapeRegexChars = (s) => s.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); exports.escapeRegexChars = escapeRegexChars; const escapeGraphQlQuery = (s) => (0, exports.escapeSpace)((0, exports.escapeRegexChars)(s)); exports.escapeGraphQlQuery = escapeGraphQlQuery; //# sourceMappingURL=graphQL.js.map