@@ -20,6 +20,7 @@ export class SparqlEndpointFetcher {
20
20
21
21
protected readonly method : 'GET' | 'POST' ;
22
22
protected readonly timeout ?: number ;
23
+ protected readonly forceGetIfUrlLengthBelow : number ;
23
24
public additionalUrlParams : URLSearchParams ;
24
25
protected readonly defaultHeaders : Headers ;
25
26
public readonly fetchCb ?: ( input : Request | string , init ?: RequestInit ) => Promise < Response > ;
@@ -31,6 +32,7 @@ export class SparqlEndpointFetcher {
31
32
public constructor ( args ?: ISparqlEndpointFetcherArgs ) {
32
33
this . method = args ?. method ?? 'POST' ;
33
34
this . timeout = args ?. timeout ;
35
+ this . forceGetIfUrlLengthBelow = args ?. forceGetIfUrlLengthBelow ?? 0 ;
34
36
this . additionalUrlParams = args ?. additionalUrlParams ?? new URLSearchParams ( ) ;
35
37
this . defaultHeaders = args ?. defaultHeaders ?? new Headers ( ) ;
36
38
this . fetchCb = args ?. fetch ;
@@ -74,7 +76,7 @@ export class SparqlEndpointFetcher {
74
76
* This will parse the update query and thrown an exception on syntax errors.
75
77
*
76
78
* @param {string } query An update query.
77
- * @return {'UNKNOWN' | UpdateTypes } The included update operations.
79
+ * @return {'UNKNOWN' | IUpdateTypes } The included update operations.
78
80
*/
79
81
public getUpdateTypes ( query : string ) : 'UNKNOWN' | IUpdateTypes {
80
82
const parsedQuery = new SparqlParser ( { sparqlStar : true } ) . parse ( query ) ;
@@ -191,14 +193,24 @@ export class SparqlEndpointFetcher {
191
193
query : string ,
192
194
acceptHeader : string ,
193
195
) : 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
+ }
195
207
196
208
// Initiate request
197
209
let body : URLSearchParams | undefined ;
198
210
const headers : Headers = new Headers ( this . defaultHeaders ) ;
199
211
headers . append ( 'Accept' , acceptHeader ) ;
200
212
201
- if ( this . method === 'POST' ) {
213
+ if ( method === 'POST' ) {
202
214
headers . append ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
203
215
body = new URLSearchParams ( ) ;
204
216
body . set ( 'query' , query ) ;
@@ -210,7 +222,7 @@ export class SparqlEndpointFetcher {
210
222
url += `&${ this . additionalUrlParams . toString ( ) } ` ;
211
223
}
212
224
213
- return this . handleFetchCall ( url , { headers, method : this . method , body } ) ;
225
+ return this . handleFetchCall ( url , { headers, method, body } ) ;
214
226
}
215
227
216
228
/**
@@ -270,6 +282,7 @@ export interface ISparqlEndpointFetcherArgs extends ISparqlJsonParserArgs, ISpar
270
282
method ?: 'POST' | 'GET' ;
271
283
additionalUrlParams ?: URLSearchParams ;
272
284
timeout ?: number ;
285
+ forceGetIfUrlLengthBelow ?: number ;
273
286
defaultHeaders ?: Headers ;
274
287
/**
275
288
* A custom fetch function.
0 commit comments