You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By making Body a tagged union of success and error types, a type guard can properly narrow Body based on the ok key:
consthandleResponse=(res: HTTP.Body<SomeResponseType>)=>{if(!res.ok){returnconsole.error(res.body.message);// res.body is of type { message: string, code: string }}console.log(res.body);// res.body is SomeResponseType};
With that in place, execute could look something like:
Actually, we could do better by overloading execute to better preserve the type of the response, either a SPARQL select query result or ask query result. E.g.
query.execute<'Select','name'|'age'>(conn,'myDB','SELECT * WHERE { :personA :name ?name ; :age ?age }','application/sparql-results+json').then((res)=>{if(!res.ok){returnconsole.error(res.body.message);}res.body.results.bindings.map((result)=>(// result is of type { name: RDFTerm, age: RDFTerm }console.log(result.name,result.age)));});
We're currently working on a 2.0 release of stardog.js, which may improve things here. If it doesn't, it's something that we can consider doing then. Thanks!
It would be nice if functions like
execute
returned typed results. Some potential steps towards that could be:HTTP.Body
generic:By making
Body
a tagged union of success and error types, a type guard can properly narrowBody
based on theok
key:With that in place,
execute
could look something like:and be used like
If this is of interest, I'd be happy to open a PR.
The text was updated successfully, but these errors were encountered: