@@ -20,6 +20,7 @@ export class SparqlEndpointFetcher {
2020
2121 protected readonly method : 'GET' | 'POST' ;
2222 protected readonly timeout ?: number ;
23+ protected readonly forceGetIfUrlLengthBelow : number ;
2324 public additionalUrlParams : URLSearchParams ;
2425 protected readonly defaultHeaders : Headers ;
2526 public readonly fetchCb ?: ( input : Request | string , init ?: RequestInit ) => Promise < Response > ;
@@ -31,6 +32,7 @@ export class SparqlEndpointFetcher {
3132 public constructor ( args ?: ISparqlEndpointFetcherArgs ) {
3233 this . method = args ?. method ?? 'POST' ;
3334 this . timeout = args ?. timeout ;
35+ this . forceGetIfUrlLengthBelow = args ?. forceGetIfUrlLengthBelow ?? 0 ;
3436 this . additionalUrlParams = args ?. additionalUrlParams ?? new URLSearchParams ( ) ;
3537 this . defaultHeaders = args ?. defaultHeaders ?? new Headers ( ) ;
3638 this . fetchCb = args ?. fetch ;
@@ -74,7 +76,7 @@ export class SparqlEndpointFetcher {
7476 * This will parse the update query and thrown an exception on syntax errors.
7577 *
7678 * @param {string } query An update query.
77- * @return {'UNKNOWN' | UpdateTypes } The included update operations.
79+ * @return {'UNKNOWN' | IUpdateTypes } The included update operations.
7880 */
7981 public getUpdateTypes ( query : string ) : 'UNKNOWN' | IUpdateTypes {
8082 const parsedQuery = new SparqlParser ( { sparqlStar : true } ) . parse ( query ) ;
@@ -191,14 +193,24 @@ export class SparqlEndpointFetcher {
191193 query : string ,
192194 acceptHeader : string ,
193195 ) : Promise < [ string , NodeJS . ReadableStream ] > {
194- let url : string = this . method === 'POST' ? endpoint : `${ endpoint } ?query=${ encodeURIComponent ( query ) } ` ;
196+ let method : 'GET' | 'POST' ;
197+ let url : string ;
198+
199+ if ( this . method === 'POST' && this . forceGetIfUrlLengthBelow <= endpoint . length ) {
200+ method = this . method ;
201+ url = endpoint ;
202+ } else {
203+ const getEndpoint = `${ endpoint } ?query=${ encodeURIComponent ( query ) } ` ;
204+ method = this . method === 'GET' || getEndpoint . length < this . forceGetIfUrlLengthBelow ? 'GET' : 'POST' ;
205+ url = method === 'POST' ? endpoint : getEndpoint ;
206+ }
195207
196208 // Initiate request
197209 let body : URLSearchParams | undefined ;
198210 const headers : Headers = new Headers ( this . defaultHeaders ) ;
199211 headers . append ( 'Accept' , acceptHeader ) ;
200212
201- if ( this . method === 'POST' ) {
213+ if ( method === 'POST' ) {
202214 headers . append ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
203215 body = new URLSearchParams ( ) ;
204216 body . set ( 'query' , query ) ;
@@ -210,7 +222,7 @@ export class SparqlEndpointFetcher {
210222 url += `&${ this . additionalUrlParams . toString ( ) } ` ;
211223 }
212224
213- return this . handleFetchCall ( url , { headers, method : this . method , body } ) ;
225+ return this . handleFetchCall ( url , { headers, method, body } ) ;
214226 }
215227
216228 /**
@@ -270,6 +282,7 @@ export interface ISparqlEndpointFetcherArgs extends ISparqlJsonParserArgs, ISpar
270282 method ?: 'POST' | 'GET' ;
271283 additionalUrlParams ?: URLSearchParams ;
272284 timeout ?: number ;
285+ forceGetIfUrlLengthBelow ?: number ;
273286 defaultHeaders ?: Headers ;
274287 /**
275288 * A custom fetch function.
0 commit comments