@@ -55,25 +55,50 @@ const applyMustacheToLiterals: Parser.PostProcessBinding = (binding: Parser.Bind
5555 }
5656 return binding ;
5757} ;
58+
59+ /**
60+ * Response object from a fetch request with an additional `.content` property containing the awaited response content
61+ */
62+ type QueryResponse = Response & {
63+ content : string ;
64+ } ;
65+
66+ /**
67+ * Parser class for handling query responses from various formats.
68+ *
69+ * This class processes responses from SPARQL endpoints and converts them into standardized formats for further processing.
70+ * It can handle JSON, XML, CSV, TSV, and Turtle responses, detecting the format either from content-type headers
71+ * or by attempting to parse the content.
72+ *
73+ * The parser can process:
74+ * - Direct `QueryResponse` objects (regular fetch response with additional content property with the awaited response content)
75+ * - ResponseSummary objects
76+ * - Error objects
77+ * - Raw response data
78+ *
79+ * It provides methods to access parsed data in standardized formats, error information,
80+ * and utilities to convert the data for storage or export.
81+ */
5882class Parser {
59- private res : Response | undefined ;
83+ private res : QueryResponse | undefined ;
6084 private summary : Parser . ResponseSummary | undefined ;
6185 private errorSummary : Parser . ErrorSummary | undefined ;
6286 private error : Error | undefined ;
6387 private type : "json" | "xml" | "csv" | "tsv" | "ttl" | undefined ;
6488 private executionTime : number | undefined ;
65- constructor ( responseOrObject : Parser . ResponseSummary | Response | Error | any , executionTime ?: number ) {
89+
90+ constructor ( responseOrObject : Parser . ResponseSummary | QueryResponse | Error | any , executionTime ?: number ) {
6691 if ( responseOrObject . executionTime ) this . executionTime = responseOrObject . executionTime ;
6792 if ( executionTime ) this . executionTime = executionTime ; // Parameter has priority
6893 if ( responseOrObject instanceof Error ) {
6994 this . error = responseOrObject ;
70- } else if ( ( < any > responseOrObject ) . xhr ) {
71- this . setResponse ( < Response > responseOrObject ) ;
95+ } else if ( ( < any > responseOrObject ) . content ) {
96+ this . setResponse ( < QueryResponse > responseOrObject ) ;
7297 } else {
7398 this . setSummary ( < Parser . ResponseSummary > responseOrObject ) ;
7499 }
75100 }
76- public setResponse ( res : Response ) {
101+ public setResponse ( res : QueryResponse ) {
77102 this . res = res ;
78103 }
79104 public setSummary ( summary : Parser . ResponseSummary | any ) {
@@ -130,6 +155,7 @@ class Parser {
130155 }
131156 private getData ( ) : any {
132157 if ( this . res ) {
158+ if ( this . res . content ) return this . res . content ;
133159 if ( this . res . body ) return this . res . body ;
134160 if ( this . res . text ) return this . res . text ; //probably a construct or something
135161 }
@@ -236,10 +262,6 @@ class Parser {
236262 return data ;
237263 }
238264
239- getOriginalResponse ( ) {
240- return this . res ?. body ;
241- }
242-
243265 getType ( ) {
244266 if ( ! this . type ) this . getAsJson ( ) ; //detects type as well
245267 return this . type ;
0 commit comments