diff --git a/packages/arcgis-rest-common-types/src/group.ts b/packages/arcgis-rest-common-types/src/group.ts new file mode 100644 index 0000000000..1bf122fa00 --- /dev/null +++ b/packages/arcgis-rest-common-types/src/group.ts @@ -0,0 +1,51 @@ +/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. + * Apache-2.0 */ + +/** + * A [Group](https://developers.arcgis.com/rest/users-groups-and-items/common-parameters.htm) that has not been created yet. + */ +export interface IGroupAdd { + title: string; + owner?: string; + tags?: string[]; + description?: string; + access?: "private" | "org" | "public"; + phone?: string; + sortField?: + | "title" + | "owner" + | "avgrating" + | "numviews" + | "created" + | "modified"; + sortOrder?: "asc" | "desc"; + isViewOnly?: boolean; + isInvitationOnly?: boolean; + thumbnail?: string; + autoJoin?: boolean; + snippet?: string; + [key: string]: any; +} + +/** + * Existing Portal [Group](https://developers.arcgis.com/rest/users-groups-and-items/group.htm). + */ +export interface IGroup extends IGroupAdd { + id: string; + owner: string; + tags: string[]; + created: number; + modified: number; + protected: boolean; + isInvitationOnly: boolean; + isViewOnly: boolean; + isOpenData: boolean; + isFav: boolean; + autoJoin: boolean; + userMembership?: { + username?: string; + memberType?: string; + applications?: number; + }; + hasCategorySchema?: boolean; +} diff --git a/packages/arcgis-rest-common-types/src/index.ts b/packages/arcgis-rest-common-types/src/index.ts index 859c9d20ca..36d3366be7 100644 --- a/packages/arcgis-rest-common-types/src/index.ts +++ b/packages/arcgis-rest-common-types/src/index.ts @@ -1,7 +1,11 @@ -/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. +/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ +import { IGroup } from "./group"; + export * from "./webmap"; +export * from "./item"; +export * from "./group"; /** * an arc can be represented as a JSON curve object @@ -180,28 +184,6 @@ export interface IFont { decoration?: "line-through" | "underline" | "none"; } -/** - * Portal Item - */ -export interface IItem { - id?: string; - owner?: string; - title?: string; - type?: string; - tags?: string[]; - typeKeywords?: string[]; - description?: string; - snippet?: string; - documentation?: string; - extent?: number[][]; - categories?: string[]; - spatialReference?: any; - culture?: string; - properties?: any; - url?: string; - [key: string]: any; -} - /** * */ @@ -476,31 +458,6 @@ export interface IUser { provider?: "arcgis" | "enterprise" | "facebook" | "google"; } -export interface IGroup extends IItem { - isInvitationOnly?: boolean; - phone?: string; - sortField?: - | "title" - | "owner" - | "avgrating" - | "numviews" - | "created" - | "modified"; - isViewOnly?: boolean; - isFav?: boolean; - access?: "private" | "org" | "public"; - userMembership?: { - username?: string; - memberType?: string; - applications?: number; - }; - protected?: boolean; - autoJoin?: boolean; - hasCategorySchema?: boolean; - isOpenData?: boolean; - [key: string]: any; -} - export type esriUnits = | "esriSRUnit_Meter" | "esriSRUnit_StatuteMile" diff --git a/packages/arcgis-rest-common-types/src/item.ts b/packages/arcgis-rest-common-types/src/item.ts new file mode 100644 index 0000000000..8a44cdfaef --- /dev/null +++ b/packages/arcgis-rest-common-types/src/item.ts @@ -0,0 +1,45 @@ +/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. + * Apache-2.0 */ + +import { ISpatialReference } from "./index"; + +/** + * A Portal [Item](https://developers.arcgis.com/rest/users-groups-and-items/common-parameters.htm) that has not been created yet. + */ +export interface IItemAdd { + title: string; + type: string; + owner?: string; + typeKeywords?: string[]; + description?: string; + snippet?: string; + documentation?: string; + extent?: number[][]; + categories?: string[]; + spatialReference?: ISpatialReference; + culture?: string; + properties?: any; + url?: string; + tags?: string[]; + [key: string]: any; +} + +/** + * A Portal [Item](https://developers.arcgis.com/rest/users-groups-and-items/common-parameters.htm) to be updated. + */ +export interface IItemUpdate { + id: string; + [key: string]: any; +} + +/** + * Existing Portal [Item](https://developers.arcgis.com/rest/users-groups-and-items/item.htm). + */ +export interface IItem extends IItemAdd { + id: string; + owner: string; + tags: string[]; + created: number; + modified: number; + protected: boolean; +} diff --git a/packages/arcgis-rest-groups/src/groups.ts b/packages/arcgis-rest-groups/src/groups.ts index b37f8e3eb3..7416ddf1d0 100644 --- a/packages/arcgis-rest-groups/src/groups.ts +++ b/packages/arcgis-rest-groups/src/groups.ts @@ -1,5 +1,6 @@ -/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. +/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ + import { request, IRequestOptions, @@ -7,7 +8,13 @@ import { getPortalUrl } from "@esri/arcgis-rest-request"; -import { IPagingParams, IItem, IGroup } from "@esri/arcgis-rest-common-types"; +import { + IPagingParams, + IItem, + IItemUpdate, + IGroupAdd, + IGroup +} from "@esri/arcgis-rest-common-types"; export interface IPagingParamsRequestOptions extends IRequestOptions { paging: IPagingParams; @@ -17,8 +24,12 @@ export interface IGroupIdRequestOptions extends IRequestOptions { id: string; } -export interface IGroupRequestOptions extends IRequestOptions { - group: IGroup; +export interface IGroupAddRequestOptions extends IRequestOptions { + group: IGroupAdd; +} + +export interface IGroupUpdateRequestOptions extends IRequestOptions { + group: IItemUpdate; } export interface IGroupSearchRequest extends IPagingParams { @@ -156,21 +167,6 @@ export function getGroupUsers( return request(url, options); } -/** - * Serialize a group into a json format accepted by the Portal API - * for create and update operations - * - * @param group IGroup to be serialized - * @returns a formatted JSON object to be sent to Portal - */ -function serializeGroup(group: IGroup): any { - // create a clone so we're not messing with the original - const clone = JSON.parse(JSON.stringify(group)); - // join and tags... - clone.tags = clone.tags.join(", "); - return clone; -} - /** * Create a new Group. * Note: The group name must be unique within the user's organization. @@ -178,10 +174,10 @@ function serializeGroup(group: IGroup): any { * @returns A Promise that will resolve with the success/failure status of the request */ export function createGroup( - requestOptions: IGroupRequestOptions + requestOptions: IGroupAddRequestOptions ): Promise { const url = `${getPortalUrl(requestOptions)}/community/createGroup`; - const options: IGroupRequestOptions = { + const options: IGroupAddRequestOptions = { ...requestOptions }; // serialize the group into something Portal will accept @@ -195,13 +191,13 @@ export function createGroup( * @returns A Promise that will resolve with the success/failure status of the request */ export function updateGroup( - requestOptions: IGroupRequestOptions + requestOptions: IGroupUpdateRequestOptions ): Promise { const url = `${getPortalUrl(requestOptions)}/community/groups/${ requestOptions.group.id }/update`; - const options: IGroupRequestOptions = { + const options: IGroupUpdateRequestOptions = { ...requestOptions }; // serialize the group into something Portal will accept @@ -259,3 +255,18 @@ export function unprotectGroup( }; return request(url, options); } + +/** + * Serialize a group into a json format accepted by the Portal API + * for create and update operations + * + * @param group IGroup to be serialized + * @returns a formatted JSON object to be sent to Portal + */ +function serializeGroup(group: IGroupAdd | IItemUpdate | IGroup): any { + // create a clone so we're not messing with the original + const clone = JSON.parse(JSON.stringify(group)); + // join and tags... + clone.tags = clone.tags.join(", "); + return clone; +} diff --git a/packages/arcgis-rest-groups/test/mocks/responses.ts b/packages/arcgis-rest-groups/test/mocks/responses.ts index fede9c0668..03f3af6ca2 100644 --- a/packages/arcgis-rest-groups/test/mocks/responses.ts +++ b/packages/arcgis-rest-groups/test/mocks/responses.ts @@ -130,7 +130,8 @@ export const GroupContentResponse: IGroupContentResult = { avgRating: 0, numViews: 1301, groupCategories: [], - scoreCompleteness: 50 + scoreCompleteness: 50, + protected: false } ] }; diff --git a/packages/arcgis-rest-items/src/items.ts b/packages/arcgis-rest-items/src/items.ts index ec234a1ee2..41f2556cda 100644 --- a/packages/arcgis-rest-items/src/items.ts +++ b/packages/arcgis-rest-items/src/items.ts @@ -1,23 +1,20 @@ -/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. +/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ + import { request, IRequestOptions, getPortalUrl } from "@esri/arcgis-rest-request"; -import { IItem, IPagingParams } from "@esri/arcgis-rest-common-types"; - +import { + IItemAdd, + IItemUpdate, + IItem, + IPagingParams +} from "@esri/arcgis-rest-common-types"; import { IUserRequestOptions } from "@esri/arcgis-rest-auth"; -export interface IItemRequestOptions extends IUserRequestOptions { - item: IItem; -} - -// * @param id - Item Id -// * @param owner - Item owner username -// * @param data - Javascript object to store - export interface IItemIdRequestOptions extends IUserRequestOptions { /** * Unique identifier of the item. @@ -49,7 +46,6 @@ export interface IItemResourceRequestOptions extends IItemIdRequestOptions { } export interface IItemCrudRequestOptions extends IUserRequestOptions { - item: IItem; /** * The owner of the item. If this property is not present, `item.owner` will be passed, or lastly `authentication.username`. */ @@ -60,12 +56,18 @@ export interface IItemCrudRequestOptions extends IUserRequestOptions { folder?: string; } +export interface IItemAddRequestOptions extends IItemCrudRequestOptions { + item: IItemAdd; +} + +export interface IItemUpdateRequestOptions extends IItemCrudRequestOptions { + item: IItemUpdate; +} + // this interface still needs to be docced export interface ISearchRequest extends IPagingParams { q: string; [key: string]: any; - // start: number; - // num: number; } export interface ISearchRequestOptions extends IRequestOptions { @@ -131,12 +133,10 @@ export function searchItems( * @param requestOptions = Options for the request */ export function createItemInFolder( - requestOptions: IItemCrudRequestOptions + requestOptions: IItemAddRequestOptions ): Promise { - const owner = - requestOptions.owner || - requestOptions.item.owner || - requestOptions.authentication.username; + const owner = determineOwner(requestOptions); + const baseUrl = `${getPortalUrl(requestOptions)}/content/users/${owner}`; let url = `${baseUrl}/addItem`; @@ -156,16 +156,28 @@ export function createItemInFolder( /** * Create an Item in the user's root folder * + * ```js + * import { createItem } from '@esri/arcgis-rest-items'; + * + * createItem({ + * authentication: userSession, + * item: { + * title: "The Amazing Voyage", + * type: "Webmap" + * } + * }) + * ``` + * * @param requestOptions - Options for the request */ export function createItem( - requestOptions: IItemCrudRequestOptions + requestOptions: IItemAddRequestOptions ): Promise { // delegate to createItemInFolder placing in the root of the filestore const options = { folder: null, ...requestOptions - } as IItemCrudRequestOptions; + } as IItemAddRequestOptions; return createItemInFolder(options); } @@ -177,8 +189,7 @@ export function createItem( export function addItemJsonData( requestOptions: IItemJsonDataRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; - + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/update`; @@ -239,14 +250,29 @@ export function getItemData( /** * Update an Item * + * * ```js + * import { updateItem } from '@esri/arcgis-rest-items'; + * + * updateItem({ + * authentication: userSession, + * item: { + * id: "3ef", + * description: "A three hour tour" + * } + * }) + * ``` + * * @param item - The item to update. * @param requestOptions - Options for the request. * @returns A Promise that resolves with the status of the operation. */ -export function updateItem(requestOptions: IItemRequestOptions): Promise { - const url = `${getPortalUrl(requestOptions)}/content/users/${ - requestOptions.item.owner - }/items/${requestOptions.item.id}/update`; +export function updateItem( + requestOptions: IItemUpdateRequestOptions +): Promise { + const owner = determineOwner(requestOptions); + const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ + requestOptions.item.id + }/update`; // serialize the item into something Portal will accept requestOptions.params = { @@ -260,13 +286,23 @@ export function updateItem(requestOptions: IItemRequestOptions): Promise { /** * Remove an item from the portal * + * * + * ```js + * import { removeItem } from '@esri/arcgis-rest-items'; + * + * removeItem({ + * authentication: userSession, + * id: "3ef" + * }) + * ``` + * * @param requestOptions - Options for the request * @returns A Promise that deletes an item. */ export function removeItem( requestOptions: IItemIdRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/delete`; @@ -282,7 +318,7 @@ export function removeItem( export function protectItem( requestOptions: IItemIdRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/protect`; @@ -298,7 +334,7 @@ export function protectItem( export function unprotectItem( requestOptions: IItemIdRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/unprotect`; @@ -336,7 +372,7 @@ export function getItemResources( export function updateItemResource( requestOptions: IItemResourceRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/updateResources`; @@ -360,7 +396,7 @@ export function updateItemResource( export function removeItemResource( requestOptions: IItemResourceRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/removeResources`; @@ -380,11 +416,11 @@ export function removeItemResource( * @param item IItem to be serialized * @returns a formatted json object to be sent to Portal */ -function serializeItem(item: IItem): any { +function serializeItem(item: IItemAdd | IItemUpdate | IItem): any { // create a clone so we're not messing with the original const clone = JSON.parse(JSON.stringify(item)); // join keywords and tags... - const { typeKeywords=[], tags=[] } = item; + const { typeKeywords = [], tags = [] } = item; clone.typeKeywords = typeKeywords.join(", "); clone.tags = tags.join(", "); // convert .data to .text @@ -398,3 +434,17 @@ function serializeItem(item: IItem): any { } return clone; } + +/** + * requestOptions.owner is given priority, requestOptions.item.owner will be checked next. If neither are present, authentication.username will be assumed. + */ +function determineOwner(requestOptions: any): string { + if (requestOptions.owner) { + return requestOptions.owner; + } + if (requestOptions.item && requestOptions.item.owner) { + return requestOptions.item.owner; + } else { + return requestOptions.authentication.username; + } +} diff --git a/packages/arcgis-rest-items/test/mocks/item.ts b/packages/arcgis-rest-items/test/mocks/item.ts index 11377a2196..37bd12b11f 100644 --- a/packages/arcgis-rest-items/test/mocks/item.ts +++ b/packages/arcgis-rest-items/test/mocks/item.ts @@ -16,7 +16,10 @@ export const ItemResponse: IItem = { typeKeywords: ["Javascript", "hubSiteApplication"], properties: { parentId: "3eb" - } + }, + created: 123, + modified: 456, + protected: false }; export const ItemDataResponse: any = { diff --git a/packages/arcgis-rest-items/test/mocks/search.ts b/packages/arcgis-rest-items/test/mocks/search.ts index c55f754b5e..9faf97c325 100644 --- a/packages/arcgis-rest-items/test/mocks/search.ts +++ b/packages/arcgis-rest-items/test/mocks/search.ts @@ -53,7 +53,8 @@ export const SearchResponse: ISearchResult = { numComments: 0, numRatings: 0, avgRating: 0, - numViews: 4 + numViews: 4, + protected: false } ] }; diff --git a/packages/arcgis-rest-users/test/mocks/responses.ts b/packages/arcgis-rest-users/test/mocks/responses.ts index 8df8b4346e..11954501c3 100644 --- a/packages/arcgis-rest-users/test/mocks/responses.ts +++ b/packages/arcgis-rest-users/test/mocks/responses.ts @@ -65,7 +65,12 @@ export const GroupMemberUserResponse: IUser = { username: "jsmith", memberType: "user", applications: 0 - } + }, + protected: false, + isViewOnly: false, + isFav: false, + isOpenData: true, + autoJoin: false } ] }; @@ -91,7 +96,12 @@ export const GroupNonMemberUserResponse: IUser = { username: "jsmith", memberType: "user", applications: 0 - } + }, + protected: false, + isViewOnly: false, + isFav: false, + isOpenData: true, + autoJoin: false } ] }; @@ -117,7 +127,12 @@ export const GroupAdminUserResponse = { username: "jsmith", memberType: "admin", applications: 0 - } + }, + protected: false, + isViewOnly: false, + isFav: false, + isOpenData: true, + autoJoin: false } ] }; @@ -144,7 +159,12 @@ export const OrgAdminUserResponse = { username: "jsmith", memberType: "owner", applications: 0 - } + }, + protected: false, + isViewOnly: false, + isFav: false, + isOpenData: true, + autoJoin: false } ] };