import { GraphQLScalarType , Kind } from 'graphql' ; import { filterXSS , IFilterXSSOptions , IWhiteList } from 'xss' ; const defaultName = 'IOSanitizedString' ; export const scalar = defaultName ; const noop = ( html ) => html ; export interface SanitizeOptions { allowHTMLTags ? : boolean ; stripIgnoreTag ? : boolean ; } const serialize = ( input , options ? ) => { return filterXSS ( input , options ) ; } ; const parseValue = ( value , options ? ) => { return filterXSS ( value , options ) ; } ; export class IOSanitizedStringType extends GraphQLScalarType { constructor ( options ? ) { const allowHTMLTags = options && options . allowHTMLTags ; const stripIgnoreTag = ! options || options . stripIgnoreTag !== false ; const xssOptions = { stripIgnoreTag , ... ! allowHTMLTags && { whiteList : [ ] as IWhiteList } , ... stripIgnoreTag && { escapeHtml : noop } , } ; super ( { name : options ? `Custom${ defaultName }` : defaultName , parseLiteral : ast => { switch ( ast . kind ) { case Kind . STRING : return parseValue ( ast . value , xssOptions ) ; default : return null ; } } , parseValue : value => parseValue ( value , xssOptions ) , serialize : value => serialize ( value , xssOptions ) , } ) ; } } export const resolvers = new IOSanitizedStringType ( ) ;