Our family of users and contributors has grown (a lot!) since our first release so we decided to roll up our sleeves (groan) and overhaul the API. Our goals were to:
- Make ArcGIS REST JS as consistent (and simple) as possible
- Introduce building blocks for compositional/fluent APIs
- Provide a few new options to reuse common parameters
Fluent APIs
In v2.0.0
we introduced a new class called Search
to help compose complex parameters.
import { searchItems, SearchQueryBuilder } from "@esri/arcgis-rest-portal";
const q = new SearchQueryBuilder()
.match("Trees")
.and()
.match("US Forest Service")
.in("owner")
.and()
.startGroup()
.match("Web Mapping Application")
.in("type")
.or()
.match("Mobile Application")
.in("type")
.endGroup()
// "Trees AND owner: US Forest Service AND (type: "Web Mapping Application" OR type: "Mobile Application")"
searchItems({ q })
Currently only search
and search
accept Search
as an input, but we've included building blocks (groan) to create additional implementations as well.
Paging
The promises returned by search
and search
also got a handy new next
method to make it easier to sift through paginated results.
searchItems({ q })
.then(response => {
if (response.nextPage) {
r.nextPage()
.then(responsePageTwo)
}
})
Reuse parameters
We added two new methods to @esri/arcgis-rest-request
as helpers for passing repeat options through.
set Default Request Options()
Now, if you want to ensure that all requests include a custom option, you can use set
.
import { request, setDefaultRequestOptions } from "@esri/arcgis-rest-request";
setDefaultRequestOptions({
headers: { "Custom-Header": "Test Value" }
});
const url = `https://www.arcgis.com/sharing/rest/info`;
// the custom header will *always* be passed along
request(url)
You should not pass authentication
to this method if your code runs in a shared environment like a web server that handles requests for more than one user.
with Options()
If you'd like to selectively append common options to a specific method, you can use the new with
method.
import { request, withOptions } from "@esri/arcgis-rest-request";
const authenticatedRequest = withOptions({
authentication: session
}, request);
// includes authenticated session
authenticatedRequest(url)
// does NOT include authenticated session
request(url)
Breaking Changes
In order to make the API more consistent and a little easier to navigate, we had to break a few 🍳s.
One portal
package to rule them all (even for ArcGIS Online)
We consolidated four existing packages into a new one called @esri/arcgis-rest-portal
. Whether you want to talk to ArcGIS Online or Enterprise, now you'll do this:
# new
npm install @esri/arcgis-rest-portal
instead of this:
# old
npm install @esri/arcgis-rest-items &&
@esri/arcgis-rest-users &&
@esri/arcgis-rest-groups &&
@esri/arcgis-rest-sharing
The table below lists methods in this package that have been deprecated, given a facelift, or given a new home.
Old | New | Package |
---|---|---|
serialize | groups | |
add | add | items |
create | create | items portal |
~~`searchItems( string | opts )`~~ | [`searchItems( string |
search | [`searchGroups( string | opts |
get | get | items portal |
get | get | auth portal |
get | get | request portal |
get | get | request portal |
Search | portal |
@esri/arcgis-rest-request
The only breaking changes we made to request
were to refactor an internal method and move a couple others into the new portal
package.
Old | New | Package Name |
---|---|---|
get | get | request portal |
get | get | request portal |
If you work with private services (shhhh)
We didn't make many changes in @esri/arcgis-rest-auth
, but one method moved and two others got simpler.
Old | New | Package Name |
---|---|---|
get | get | auth portal |
`fetchToken(params | opts)` | fetch |
`generateToken(params | opts)` | generate |
@esri/arcgis-rest-routing
In this package, we renamed one constant.
Old | New |
---|---|
world | ARCGIS |
Geocoding addresses
In the interest of consistency, we renamed the geocoding package too.
# new
npm install @esri/arcgis-rest-geocoding
# old
npm install @esri/arcgis-rest-geocoder
We renamed one method (and one constant) as well.
Old | New |
---|---|
service | get |
world | ARCGIS |
Querying and editing feature layers
This package was also renamed. If you're already using query
or making edits inside hosted feature layers, now you'll install this:
# new
npm install @esri/arcgis-rest-feature-service
instead of this:
# old
npm install @esri/arcgis-rest-feature-service
The feature-layer
methods that were refactored or re-homed are listed below.
Old | New | Package |
---|---|---|
add | add | |
update | update | |
delete | delete | |
get | get |
Publishing and updating new hosted feature services
If you're already using rest-js
to publish new hosted feature services, that package has a new (shorter) name too.
# new
npm install @esri/arcgis-rest-service-admin
instead of this:
# old
npm install @esri/arcgis-rest-feature-service-admin
After you save those seven keystrokes, everything else will be familiar.
Helper methods
In this release we also made the decision to stop documenting internal helper methods like append
. In the future, undocumented methods may change without notice.
Breaking Changes for TypeScript developers
Each package now installs shared TypeScript typings automatically and re-exports them, so its no longer necessary to install a separate package yourself.
// old
import { IPoint } from "@esri/arcgis-rest-common-types";
import { reverseGeocode } from "@esri/arcgis-rest-geocoder";
reverseGeocode({ x: 34, y: -118} as IPoint);
// new
import { IPoint, reverseGeocode } from "@esri/arcgis-rest-geocoding";
reverseGeocode({ x: 34, y: -118} as IPoint);
If you'd like to install the typings yourself, there is a more concise package name for that too.
# new package name
npm install @esri/arcgis-rest-types
The table below lists interfaces and types that have been removed or renamed in the name of consistency and brevity. This also better aligns the names of options and response interfaces with their corresponding function.
Old Interface/Type | New Interface/Type |
---|---|
esri | Field |
esri | Geometry |
esri | Units |
I | IO |
I | I |
I | I |
I | |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | I |
I | |
I |
That's it! We know that moving so much 🧀 is a hassle, but developers of the future send their thanks! 🎩