Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with...

37
Node.js and browser applications with ArcGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Transcript of Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with...

Page 1: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Node.jsandbrowserapplicationswithArcGISRESTJSAllisonDavis

NoahMulnger

slides:

@araedavis

@noahmulnger

bit.ly/3aLDzaz

Page 2: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Agenda1.-WhatisArcGISRESTJS?Why?2.-Whoisusingit?Forwhat?3.-What'snewin2020?4.-CommonPatterns5.-Demos/code(tolearnhowitworks)

Page 3: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

OpenSourceonGitHubCode

Doc

github.com/Esri/arcgis-rest-js

esri.github.io/arcgis-rest-js

Page 4: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

@esri/arcgis-rest-jshelpsyoutalk

toArcGISOnlineandEnterprise

frommodernbrowsersandNode.js.

Page 5: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

VanillaXMLHttpRequest

//constructtheurlyourselfanddon'tforgettotackonf=jsonconsturl="https://www.arcgis.com/sharing/rest/community/users/dmfenton";url+="?f=json";varxhr=newXMLHttpRequest();xhr.onreadystatechange=function()if(xhr.readyState==XMLHttpRequest.DONE)//makesureJSONresponsedoesntindicateanerrorif(!xhr.responseText.error)xhr.responseText;//firstName:"Daniel",description:"opensourcegeodev";xhr.open("GET",url,true);xhr.send(null);

Page 6: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

VanillaFetch

consturl="https://www.arcgis.com/sharing/rest/community/users/dmfenton";fetch(url,method:"POST",//settherequesttypeheaders:"Content­Type":"application/x­www­form­urlencoded"//appendtherightheader,//concatandencodeparameters,appendf=jsonbody:encodeURIComponent("f=json")).then(response=>if(response.ok)returnresponse.json();//digoutthejson).then(response=>//trapforerrorsinsidea200responseif(!response.error)returnresponse;);

Page 7: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

EvenmorecomplexityWhatarealltheerrorcodes?Howdoyouhandleauth?Howaredatessupposedtobeencoded?Properencodingforobjects?Howdoyoumanagetokensforfederatedservers?Refreshingauthenticationwhennecessary?

Page 8: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz
Page 9: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

@esri/arcgis­rest­js

importrequestfrom"@esri/arcgis­rest­request";request(url).then(response)//firstName:"Daniel",description:"opensourcegeodev"....catch((error=>if(err.name==="ArcGISAuthError")//handleanautherrorelse//handlearegularerror)

Page 10: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Valueaddsappendsf=jsonandrequestheadersencodesquerystringparameterscreatesFormData(whenrequired)clearandinformativeerrorhandlingproperparameterencodingsupportforauthenticationdisplayamapclientsideanalysis

Page 11: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

requestonlyexpectsaurl,butitexposesrequestOptionstoo.

//url,IRequestOptionsrequest(url,params://anythingyouwanttopassfoo:true,bar:"baz",more:File(),num:999,when:Date().now()//etc.//httpMethod:"GET",//authentication//portal,//headers,//fetch);

Page 12: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

therestoftheAPIbuildsontopofrequest

importgeocodefrom"@esri/arcgis­rest­geocoding";//assumesyouwanttouseArcGISOnlinegeocode("LAX").then(response);//...candidates:[]//IRequestOptionsisstillavailablegeocode(singleLine:"LAX",params:forStorage:true,authentication);

Page 13: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

GoalsNode.jsand(modern)browsersalacarte/svelteframeworkagnosticshavedownthesharpedgesalignwithJSecosystem

Page 14: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Disclaimer*notaproduct,noroadmapworkscratchingourownitch

inprogress

Page 15: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

ComparisonkindofanalogoustoArcGISAPIforPythonmuchdifferentthantheArcGISAPIforJavaScript

Page 16: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Inthebeginning...

customers!

ArcGISforDevelopersArcGISHub

Page 17: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Asof2020ArcGISHubArcGISforDevelopersStorymapsWebAppBuilder(nextgeneration)ArcGISSolutionsArcGISEnterpriseArcGISAnalyticsforIoTEsriUKStartups/PartnersCustomersYou?

Page 18: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

packages!request2.8kBauth3.6kBportal5.1kBfeature­layer1.3kBservice­admin746Bgeocoding1kBrouting642B

Page 19: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

CommonPatterns

Page 20: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

whenonlyonepieceofinformationisrequired

youcanpassinitindirectly.

importsearchItemsfrom"@esri/arcgis­rest­portal";searchItems("water").then(response);//total:355,etc...//orsearchItems(query:"water",httpMethod:"GET",authentication);

Page 21: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

ifmorethanonepieceofinformationisneeded

onlyanobjectcanbepassedin, IRequestOptions

deleteFeatures(url:"https://server.arcgis.com/arcgis/rest/objectIds:[123]).then(response)

//response"deleteResults":["objectId":123,"success":true]

extends

Page 22: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

updatewhocanaccessanitemimportsetItemAccessfrom"@esri/arcgis­rest­sharing";setItemAccess(id:`fe8`,//whichitem?access:`public`,//whoshouldbeabletoseeit?authentication//userallowedtoupdate).then(response);

ISetItemAccessOptions

Page 23: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Simplieddeveloperexperience,evenwhentheunderlyinglogicis

weensuretheresponseisdeterministicwegureoutwhichurltocall(basedonrole)

complicated

Page 24: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

AuthenticationimportUserSessionfrom"@esri/arcgis­rest­auth";//ArcGISOnlinecredentialsconstauthentication=newUserSession(username,password);//ArcGISEnterprisecredentialsconstenterpriseAuth=newUserSession(username,password,portal:`https://gis.city.gov/sharing/rest`);

Page 25: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

UserSessionkeepstrackoftokenexpiration

andwhetherornotaserveristrusted(federated)

consturl=`http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/`;constauthentication=newUserSession(username,password);request(url,authentication).then(response=>//thesametokenwillbereusedforthesecondrequestrequest(url,authentication););

Page 26: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

sinceDevSummit2019...rest-jsv2.0.0!

(plus20additionalreleases)

Page 27: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

What'snewinv2+

Improved

setDefaultRequestOptions()andwithOptions()

Packageandtypereorganization

SearchQueryBuilder

paging

Page 28: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Buildingsearchqueries//1.xconstq="TreesANDowner:USForestServiceAND(type:'WebMappingApplication'ORtype://2.xconstq=newSearchQueryBuilder().match("Trees").and().match("USForestService").in("owner").and().startGroup().match("WebMappingApplication").in("type").or().match("MobileApplication").in("type").endGroup();

Page 29: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Oneportalpackagetorulethemall//1.xnpminstall@esri/arcgis­rest­items&&@esri/arcgis­rest­users&&@esri/arcgis­rest­groups&&@esri/arcgis­rest­sharing//2.xnpminstall@esri/arcgis­rest­portal

Page 30: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Packagesinstalltypesautomatically//1.ximportIPointfrom"@esri/arcgis­rest­common­types";importreverseGeocodefrom"@esri/arcgis­rest­geocoder";reverseGeocode(x:34,y:­118asIPoint);//2.ximportIPoint,reverseGeocodefrom"@esri/arcgis­rest­geocoding";reverseGeocode(x:34,y:­118asIPoint);

Page 31: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz

Checkoutthe forthefulllistreleasenotes

Page 37: Node.js and Browser Applications with ArcGIS REST JS€¦ · Node.js and browser applications with Ar cGIS REST JS Allison Davis Noah Mulnger slides: @araedavis @noahmulnger bit.ly/3aLDzaz