-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new users package): added rest-users with a single method
didnt implement getSelf() because of namespace collision AFFECTS PACKAGES: @esri/arcgis-rest-common-types @esri/arcgis-rest-users ISSUES CLOSED: #159
- Loading branch information
Showing
8 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
[![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-users.svg?style=flat-square | ||
[npm-url]: https://www.npmjs.com/package/@esri/arcgis-rest-users | ||
[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-users | ||
|
||
> A module for working with users in the ArcGIS REST API that runs in Node.js and modern browsers. | ||
### Example | ||
|
||
```bash | ||
npm install @esri/arcgis-rest-users | ||
``` | ||
|
||
```js | ||
import { getUser } from '@esri/arcgis-rest-groups'; | ||
|
||
getUser("johnqpublic") | ||
.then(response => { | ||
console.log(response) // {} | ||
}); | ||
``` | ||
|
||
### 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "@esri/arcgis-rest-users", | ||
"version": "1.1.1", | ||
"description": "Portal user helpers for @esri/arcgis-rest-request", | ||
"main": "dist/node/index.js", | ||
"browser": "dist/umd/arcgis-rest-users.umd.js", | ||
"module": "dist/esm/index.js", | ||
"js:next": "dist/esm/index.js", | ||
"types": "dist/esm/index.d.ts", | ||
"license": "Apache-2.0", | ||
"files": [ | ||
"dist/**" | ||
], | ||
"dependencies": { | ||
"tslib": "^1.7.1" | ||
}, | ||
"peerDependencies": { | ||
"@esri/arcgis-rest-auth": "^1.1.1", | ||
"@esri/arcgis-rest-common-types": "^1.1.1", | ||
"@esri/arcgis-rest-request": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"@esri/arcgis-rest-auth": "^1.1.1", | ||
"@esri/arcgis-rest-common-types": "^1.1.1", | ||
"@esri/arcgis-rest-request": "^1.1.1" | ||
}, | ||
"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 ../../umd-base-profile.js && rollup -c ../../umd-production-profile.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": "John Gravois", | ||
"email": "john@esri.com" | ||
} | ||
], | ||
"bugs": { | ||
"url": "https://github.com/Esri/arcgis-rest-js/issues" | ||
}, | ||
"homepage": "https://github.com/Esri/arcgis-rest-js#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./users"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
request, | ||
IRequestOptions, | ||
getPortalUrl | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { IUser } from "@esri/arcgis-rest-common-types"; | ||
import { UserSession, IUserRequestOptions } from "@esri/arcgis-rest-auth"; | ||
|
||
export interface IGetUserRequestOptions extends IUserRequestOptions { | ||
username?: string; | ||
} | ||
|
||
/** | ||
* Get information about a user | ||
* | ||
* ```js | ||
* import { getUser } from '@esri/arcgis-rest-users'; | ||
* | ||
* getUser("johnqpublic") | ||
* .then( | ||
* results => console.log(response); // IUser | ||
* ) | ||
* ``` | ||
* | ||
* @param requestOptions - options to pass through in the request | ||
* @returns A Promise that will resolve with metdata about the user | ||
*/ | ||
export function getUser( | ||
requestOptions?: string | IGetUserRequestOptions | ||
): Promise<IUser> { | ||
let url; | ||
let options = { httpMethod: "GET" } as IGetUserRequestOptions; | ||
|
||
// if a username is passed, assume ArcGIS Online | ||
if (typeof requestOptions === "string") { | ||
url = `http://www.arcgis.com/sharing/rest/community/users/${requestOptions}`; | ||
} else { | ||
// if an authenticated session is passed, default to that user/portal unless another username is provided manually | ||
const username = | ||
requestOptions.username || requestOptions.authentication.username; | ||
url = `${getPortalUrl(requestOptions)}/community/users/${encodeURIComponent( | ||
username | ||
)}`; | ||
options = { | ||
...requestOptions, | ||
...options | ||
}; | ||
} | ||
|
||
// send the request | ||
return request(url, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { IUser } from "@esri/arcgis-rest-common-types"; | ||
|
||
export const AnonUserResponse: IUser = { | ||
username: "jsmith", | ||
fullName: "John Smith", | ||
firstName: "John", | ||
lastName: "Smith", | ||
description: "Senior GIS Analyst for the city of Redlands.", | ||
tags: ["GIS Analyst", "City of Redlands"], | ||
culture: "en", | ||
region: "US", | ||
units: "metric", | ||
thumbnail: "myProfile.jpg", | ||
created: 1258501046000, | ||
modified: 1290625562000, | ||
provider: "arcgis" | ||
}; | ||
|
||
export const UserResponse = { | ||
...AnonUserResponse, | ||
availableCredits: 479.50998, | ||
assignedCredits: 500.0, | ||
preferredView: "Web", | ||
email: "jsmith33@esri.com", | ||
idpUsername: "null", | ||
favGroupId: "829e32cca4dd475a8bb63bb56b16fe3e", | ||
lastLogin: 1385766284000, | ||
mfaEnabled: false, | ||
access: "public", | ||
storageUsage: 583650, | ||
storageQuota: 2147483648, | ||
orgId: "qWAReEOCnD7eTxOe", | ||
role: "org_admin", | ||
privileges: [ | ||
"portal:admin:deleteItems", | ||
"portal:admin:reassignItems", | ||
"portal:admin:updateItems", | ||
"portal:admin:viewItems", | ||
"portal:admin:viewUsers", | ||
"portal:user:createGroup", | ||
"portal:user:createItem", | ||
"portal:user:joinGroup", | ||
"portal:user:shareToGroup", | ||
"portal:user:shareToOrg" | ||
], | ||
roleId: "RDnHQBSBbsJuIkUp", | ||
level: "2", | ||
disabled: false, | ||
groups: [ | ||
{ | ||
id: "0657d48d0c0841d793ea6ada2e6955f3", | ||
title: "Street Maps", | ||
isInvitationOnly: false, | ||
owner: "jsmith", | ||
description: | ||
"The street map group provides street maps for the city of Redlands.", | ||
snippet: "City of Redlands maps", | ||
tags: ["Redlands", "street", "maps"], | ||
phone: "http://www.esri.com", | ||
thumbnail: "streets.jpg", | ||
created: 1258501221000, | ||
modified: 1272309404000, | ||
access: "org", | ||
userMembership: { | ||
username: "jsmith", | ||
memberType: "owner", | ||
applications: 0 | ||
} | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { getUser } from "../src/index"; | ||
|
||
import { AnonUserResponse, UserResponse } from "./mocks/responses"; | ||
|
||
import { encodeParam } from "@esri/arcgis-rest-request"; | ||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import * as fetchMock from "fetch-mock"; | ||
|
||
const TOMORROW = (function() { | ||
const now = new Date(); | ||
now.setDate(now.getDate() + 1); | ||
return now; | ||
})(); | ||
|
||
describe("users", () => { | ||
afterEach(fetchMock.restore); | ||
|
||
describe("getUser", () => { | ||
const session = new UserSession({ | ||
username: "c@sey", | ||
password: "123456", | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest" | ||
}); | ||
|
||
fetchMock.postOnce( | ||
"https://myorg.maps.arcgis.com/sharing/rest/generateToken", | ||
{ | ||
token: "token", | ||
expires: TOMORROW.getTime(), | ||
username: " c@sey" | ||
} | ||
); | ||
|
||
session.refreshSession(); | ||
|
||
it("should make a simple, unauthenticated request for information about a user", done => { | ||
fetchMock.once("*", AnonUserResponse); | ||
|
||
getUser("jsmith") | ||
.then(response => { | ||
expect(fetchMock.called()).toEqual(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); | ||
expect(url).toEqual( | ||
"http://www.arcgis.com/sharing/rest/community/users/jsmith?f=json" | ||
); | ||
expect(options.method).toBe("GET"); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should make an authenticated request for information about a user", done => { | ||
fetchMock.once("*", UserResponse); | ||
|
||
getUser({ authentication: session }) | ||
.then(response => { | ||
expect(fetchMock.called()).toEqual(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); | ||
expect(url).toEqual( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey?f=json&token=token" | ||
); | ||
expect(options.method).toBe("GET"); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
|
||
it("should make an authenticated request for information about a different user", done => { | ||
fetchMock.once("*", UserResponse); | ||
|
||
getUser({ | ||
username: "jsmith", | ||
authentication: session | ||
}) | ||
.then(response => { | ||
expect(fetchMock.called()).toEqual(true); | ||
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); | ||
expect(url).toEqual( | ||
"https://myorg.maps.arcgis.com/sharing/rest/community/users/jsmith?f=json&token=token" | ||
); | ||
expect(options.method).toBe("GET"); | ||
done(); | ||
}) | ||
.catch(e => { | ||
fail(e); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"src/**/*.ts" | ||
] | ||
} |