Update typings for functions that wrap `request()` to return `Response` when using `rawResponse`
See original GitHub issueAs I commented on #462, the current typings for e.g. queryFeatures
say that the method returns Promise<IQueryFeaturesResponse | IQueryResponse>
. The method actually returns a Promise<Response>
when the rawResponse
option is true.
It would be nice if the method could return the correct promise type directly based on the shape of the options object passed, either via implied generics or using a (granted, pretty verbose) overload. Overloading would be verbose but I don’t think especially difficult – see this Angular class for an example. Much like this library, the Angular HTTPClient has a request
method that changes its return type based on an options-object, and multiple helpers that call request
and also vary the return type based on arguments.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:16 (10 by maintainers)
Top GitHub Comments
@gavinr @tomwayson and I discussed this in an internal meeting. We all agreed that this is pretty important for some methods (
queryFeatures
,request
) but really pointless for most methodscreateItem
,geocode
.There are 2 main downsides to this:
request
has to be wrapped in anif
statement for each format in order for TypeScript to trace the types properly. Istanbul will treat eachif
statement as something we have to write tests for to get test coverage. Which really isn’t needed.I want to sit on this for a bit. I think there definitely needs to be some trimming down of the API surface area. For example:
/addItem
endpoint only supportsjson
,pjson
andhtml
as validf
params. My preference is to actually drop support for bothrawResponse
and forcef=json
. I’m having a hard time justifying use cases wheref=html
orf=pjson
would really be valid OR where I would need to stream a tiny JSON response. This would really reduce the surface area of the API and make most of these cases obsolete.rawResponse
anymore? The main motivation forrawResponse
comes from https://github.com/Esri/arcgis-rest-js/issues/373 when our recommended fetch implementation didn’t supportresponse.blob()
. With 4.0 we will ship the latest version ofnode-fetch
which DOES supportresponse.blob()
. The main motivation forrawResponse
now seems to be streaming responses, which is useful for working with large responses like huge queries or files OR for parsing formats we don’t support in REST JS for whatever reason.I’m leaning towards the following:
f=html
everywhere unless the endpoint REALLY returns a valid HTML page that developers would want to use. In its current statef=html
is really only used for the forms that exist on the server itself. I don’t think there is anywhere in REST JS currently wheref=html
makes any sense for someone to want to use so we should drop it.rawResponse
. Instead REST JS should intelligently handle things likegetItemData()
,getItemResource()
and the missinggetAttachment()
methods or any other methods that return files/blobs by doing the following:f=json
you get an HTML error by default so this request should be done first to check for errors.Specified output format not supported.
and you can retry the request withoutf=json
and return a Response object that the user can decide how they want to handle withresponse.blob()
or steamresponse.body
.Response
object for everything else and users should either handle both cases OR check the type of item up front so they know how to parse it.queryFeatures
that support additional response types likegeojson
orpbf
we can write intelligent overrides like I described above. I thinkqueryFeatures
might be the only one at this point.queryFeatures
also has lots of other special cases likereturnCentroids
which would be good to handle as well.request
like I described above because that is much better for TypeScript.This REALLY minimizes the surface area of the API to the point where the original issue simply disappears. The only thing we really loose is the ability to stream any request which for 99.9% of requests really doesn’t make sense anyway. Streaming responses also bypasses all error checking authentication management anyway.
If we REALLY feel like specific methods need the option for streaming responses then we could add a
stream
option inrequest
that could optionally be reflected in parent methods with the caveat that it turns off most of fancy REST JS features.Sorry, I updated my comment for clarity. I do think we should still provide: