Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New arcgis-rest-features package w/ getFeature() to get a feature b… #115

Merged
merged 10 commits into from
Feb 24, 2018
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Some useful commands include:
* [`@esri/arcgis-rest-geocoder`](./packages/arcgis-rest-geocoder) - Geocoding wrapper for `@esri/arcgis-rest-js`
* [`@esri/arcgis-rest-items`](./packages/arcgis-rest-items) - Methods for working with ArcGIS Online/Enterprise content.
* [`@esri/arcgis-rest-groups`](./packages/arcgis-rest-groups) - Methods for working with ArcGIS Online/Enterprise groups.
* [`@esri/arcgis-rest-feature-service`](./packages/arcgis-rest-feature-service) - Functions for working with feature services
* [`@esri/arcgis-rest-common-types`](./packages/arcgis-rest-common-types) - Stores objects common across the ArcGIS API.

### Frequently Asked Questions
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-common-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type ElipticArc = IArc;
*/
export interface IFeature {
geometry?: IGeometry;
attributes?: any;
attributes: { [key: string]: any };
}

/**
Expand Down
67 changes: 67 additions & 0 deletions packages/arcgis-rest-feature-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[![npm version][npm-img]][npm-url]
[![build status][travis-img]][travis-url]
[![apache licensed](https://img.shields.io/badge/license-Apache-green.svg?style=flat-square)](https://raw.githubusercontent.com/Esri/arcgis-rest-js/master/LICENSE)

[npm-img]: https://img.shields.io/npm/v/@esri/arcgis-rest-feature-service.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/@esri/arcgis-rest-feature-service
[travis-img]: https://img.shields.io/travis/Esri/arcgis-rest-js/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/Esri/arcgis-rest-js

# @esri/arcgis-rest-feature-service

> A module for working with ArcGIS feature services that runs in Node.js and modern browsers.

### Example

```bash
npm install @esri/arcgis-rest-feature-service
```

```js
import { getFeature } from '@esri/arcgis-rest-feature-service';

const params = {
url:
"https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0",
id: 42
};

getFeature(params)
.then(feature => {
console.log(feature.attributes.FID); // 42
});
```

### Issues

If something isn't working the way you expected, please take a look at [previously logged issues](https://github.com/Esri/arcgis-rest-js/issues) first. Have you found a new bug? Want to request a new feature? We'd [**love**](https://github.com/Esri/arcgis-rest-js/issues/new) to hear from you.

If you're looking for help you can also post issues on [GIS Stackexchange](http://gis.stackexchange.com/questions/ask?tags=esri-oss).

### Versioning

For transparency into the release cycle and in striving to maintain backward compatibility, @esri/arcgis-rest-js is maintained under Semantic Versioning guidelines and will adhere to these rules whenever possible.

For more information on SemVer, please visit <http://semver.org/>.

### Contributing

Esri welcomes contributions from anyone and everyone. Please see our [guidelines for contributing](CONTRIBUTING.md).

### License

Copyright 2018 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

> http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

A copy of the license is available in the repository's [LICENSE](./LICENSE) file.
11 changes: 11 additions & 0 deletions packages/arcgis-rest-feature-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions packages/arcgis-rest-feature-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@esri/arcgis-rest-feature-service",
"version": "1.0.2",
"description": "Feature service helpers for @esri/arcgis-rest-request",
"main": "dist/node/index.js",
"browser": "dist/umd/arcgis-rest-feature-service.umd.js",
"module": "dist/esm/index.js",
"js:next": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"license": "Apache-2.0",
"dependencies": {
"tslib": "^1.7.1"
},
"peerDependencies": {
"@esri/arcgis-rest-common-types": "^1.0.3",
"@esri/arcgis-rest-request": "^1.0.3"
},
"devDependencies": {
"@esri/arcgis-rest-common-types": "^1.0.3",
"@esri/arcgis-rest-request": "^1.0.3"
},
"scripts": {
"prepare": "npm run build",
"build": "npm run build:node && npm run build:umd && npm run build:esm",
"build:esm": "tsc -p ./tsconfig.json --module es2015 --outDir ./dist/esm --declaration",
"build:umd": "rollup -c ../../rollup.config.umd.js",
"build:node": "tsc -p ./tsconfig.json --module commonjs --outDir ./dist/node"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Esri/arcgis-rest-js.git"
},
"contributors": [
{
"name": "Tom Wayson",
"email": "twayson@esri.com",
"url": "http://tomwayson.com/"
}
],
"bugs": {
"url": "https://github.com/Esri/arcgis-rest-js/issues"
},
"homepage": "https://github.com/Esri/arcgis-rest-js#readme"
}
34 changes: 34 additions & 0 deletions packages/arcgis-rest-feature-service/src/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import { IFeature } from "@esri/arcgis-rest-common-types";
import { request, IRequestOptions } from "@esri/arcgis-rest-request";

/**
* parameters required to get a feature by id
*
* @param url - layer service url
* @param id - feature id
*/
export interface IFeatureRequestOptions extends IRequestOptions {
url: string;
id: number;
}

/**
* Get an feature by id
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the feature.
*/
export function getFeature(
requestOptions: IFeatureRequestOptions
): Promise<IFeature> {
const url = `${requestOptions.url}/${requestOptions.id}`;

// default to a GET request
const options: IFeatureRequestOptions = {
...{ httpMethod: "GET" },
...requestOptions
};
return request(url, options).then((response: any) => response.feature);
}
1 change: 1 addition & 0 deletions packages/arcgis-rest-feature-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./features";
26 changes: 26 additions & 0 deletions packages/arcgis-rest-feature-service/test/features.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getFeature } from "../src/index";

import * as fetchMock from "fetch-mock";

import { featureResponse } from "./mocks/feature";

describe("feature", () => {
afterEach(fetchMock.restore);

it("should return a feature by id", done => {
const params = {
url:
"https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0",
id: 42
};
fetchMock.once("*", featureResponse);
getFeature(params).then(response => {
expect(fetchMock.called()).toBeTruthy();
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(`${params.url}/42?f=json`);
expect(options.method).toBe("GET");
expect(response.attributes.FID).toEqual(42);
done();
});
});
});
13 changes: 13 additions & 0 deletions packages/arcgis-rest-feature-service/test/mocks/feature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IFeature } from "@esri/arcgis-rest-common-types";

export const featureResponse = {
feature: {
attributes: {
FID: 42
},
geometry: {
x: -9177311.62541634,
y: 4247151.205222242
}
}
};
6 changes: 6 additions & 0 deletions packages/arcgis-rest-feature-service/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"include": [
"src/**/*.ts"
]
}