Skip to content

Commit

Permalink
fix: let request do the serializing
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-groups
@esri/arcgis-rest-items
  • Loading branch information
jgravois committed Feb 28, 2019
1 parent 8f94d82 commit c2d2671
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 60 deletions.
10 changes: 5 additions & 5 deletions packages/arcgis-rest-groups/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
getPortalUrl
} from "@esri/arcgis-rest-request";

import { IGroupAdd } from "@esri/arcgis-rest-common-types";
import { IGroupAdd, IGroup } from "@esri/arcgis-rest-common-types";

import { serializeGroup } from "./helpers";
// import { serializeGroup } from "./helpers";

export interface IGroupAddRequestOptions extends IRequestOptions {
group: IGroupAdd;
Expand All @@ -36,12 +36,12 @@ export interface IGroupAddRequestOptions extends IRequestOptions {
*/
export function createGroup(
requestOptions: IGroupAddRequestOptions
): Promise<any> {
): Promise<{ success: boolean; group: IGroup }> {
const url = `${getPortalUrl(requestOptions)}/community/createGroup`;
const options: IGroupAddRequestOptions = {
...requestOptions
};
// serialize the group into something Portal will accept
options.params = serializeGroup(requestOptions.group);

options.params = requestOptions.group;
return request(url, options);
}
5 changes: 4 additions & 1 deletion packages/arcgis-rest-groups/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ export interface IGroupIdRequestOptions extends IRequestOptions {

/**
* Serialize a group into a json format accepted by the Portal API
* for create and update operations
* for create and update operations.
*
* Deprecated. Will be removed in v2.0.0.
*
* @param group IGroup to be serialized
* @returns a formatted JSON object to be sent to Portal
* @private
*/
/* istanbul ignore next - deprecated method */
export 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));
Expand Down
8 changes: 3 additions & 5 deletions packages/arcgis-rest-groups/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {

import { IItemUpdate } from "@esri/arcgis-rest-common-types";

import { serializeGroup } from "./helpers";

export interface IGroupUpdateRequestOptions extends IRequestOptions {
group: IItemUpdate;
}
Expand All @@ -31,15 +29,15 @@ export interface IGroupUpdateRequestOptions extends IRequestOptions {
*/
export function updateGroup(
requestOptions: IGroupUpdateRequestOptions
): Promise<any> {
): Promise<{ success: boolean; groupId: string }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.group.id
}/update`;

const options: IGroupUpdateRequestOptions = {
...requestOptions
};
// serialize the group into something Portal will accept
options.params = serializeGroup(requestOptions.group);

options.params = requestOptions.group;
return request(url, options);
}
4 changes: 2 additions & 2 deletions packages/arcgis-rest-groups/test/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("groups", () => {
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(encodeParam("owner", "fakeUser"));
// ensure the array props are serialized into strings
expect(options.body).toContain(encodeParam("tags", "foo, bar"));
expect(options.body).toContain(encodeParam("tags", "foo,bar"));
expect(options.body).toContain(encodeParam("access", "public"));
done();
})
Expand Down Expand Up @@ -103,7 +103,7 @@ describe("groups", () => {
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(encodeParam("owner", "fakeUser"));
// ensure the array props are serialized into strings
expect(options.body).toContain(encodeParam("tags", "foo, bar"));
expect(options.body).toContain(encodeParam("tags", "foo,bar"));
done();
})
.catch(e => {
Expand Down
57 changes: 26 additions & 31 deletions packages/arcgis-rest-items/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,26 @@ export interface IFolderIdRequestOptions extends IUserRequestOptions {
owner?: string;
}

export type ItemRelationshipType = "Map2Service" |
"WMA2Code" |
"Map2FeatureCollection" |
"MobileApp2Code" |
"Service2Data" |
"Service2Service" |
"Map2AppConfig" |
"Item2Attachment" |
"Item2Report" |
"Listed2Provisioned" |
"Style2Style" |
"Service2Style" |
"Survey2Service" |
"Survey2Data" |
"Service2Route" |
"Area2Package" |
"Map2Area" |
"Service2Layer" |
"Area2CustomPackage";
export type ItemRelationshipType =
| "Map2Service"
| "WMA2Code"
| "Map2FeatureCollection"
| "MobileApp2Code"
| "Service2Data"
| "Service2Service"
| "Map2AppConfig"
| "Item2Attachment"
| "Item2Report"
| "Listed2Provisioned"
| "Style2Style"
| "Service2Style"
| "Survey2Service"
| "Survey2Data"
| "Service2Route"
| "Area2Package"
| "Map2Area"
| "Service2Layer"
| "Area2CustomPackage";

export interface IItemRelationshipRequestOptions extends IRequestOptions {
/**
Expand All @@ -61,14 +62,15 @@ export interface IItemRelationshipRequestOptions extends IRequestOptions {
/**
* The type of relationship between the two items.
*/
relationshipType: ItemRelationshipType | ItemRelationshipType[]
relationshipType: ItemRelationshipType | ItemRelationshipType[];
/**
* The direction of the relationship. Either forward (from origin -> destination) or reverse (from destination -> origin).
*/
direction?: "forward" | "reverse";
}

export interface IManageItemRelationshipRequestOptions extends IUserRequestOptions {
export interface IManageItemRelationshipRequestOptions
extends IUserRequestOptions {
originItemId: string;
destinationItemId: string;
relationshipType: ItemRelationshipType;
Expand Down Expand Up @@ -159,28 +161,21 @@ export interface IItemMoveResponse {
}

/**
* Serialize an item into a json format accepted by the Portal API
* for create and update operations
* Serialize an item and its data into a json format accepted by the Portal API for create and update operations
*
* @param item Item to be serialized
* @returns a formatted json object to be sent to Portal
*/
export 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;
clone.typeKeywords = typeKeywords.join(", ");
clone.tags = tags.join(", ");

// convert .data to .text
if (clone.data) {
clone.text = JSON.stringify(clone.data);
delete clone.data;
}
// Convert properties to a string
if (clone.properties) {
clone.properties = JSON.stringify(clone.properties);
}

return clone;
}

Expand Down
24 changes: 12 additions & 12 deletions packages/arcgis-rest-items/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ describe("search", () => {
expect(options.body).toContain("owner=dbouwman");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
expect(options.body).toContain(
encodeParam("properties", JSON.stringify(fakeItem.properties))
Expand Down Expand Up @@ -115,10 +115,10 @@ describe("search", () => {
expect(options.body).toContain("owner=dbouwman");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
expect(options.body).toContain(
encodeParam("properties", JSON.stringify(fakeItem.properties))
Expand Down Expand Up @@ -161,10 +161,10 @@ describe("search", () => {
// expect(options.body).toContain("owner=casey");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
expect(options.body).toContain(
encodeParam("properties", JSON.stringify(fakeItem.properties))
Expand Down Expand Up @@ -245,10 +245,10 @@ describe("search", () => {
expect(options.body).toContain("owner=dbouwman");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
done();
})
Expand Down Expand Up @@ -290,10 +290,10 @@ describe("search", () => {
expect(options.body).toContain("foo=bar");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
done();
})
Expand Down Expand Up @@ -329,10 +329,10 @@ describe("search", () => {
expect(options.body).toContain("owner=casey");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
done();
})
Expand Down
8 changes: 4 additions & 4 deletions packages/arcgis-rest-items/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ describe("search", () => {
expect(options.body).toContain(encodeParam("owner", "dbouwman"));
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
expect(options.body).toContain(
encodeParam("text", JSON.stringify(fakeItem.data))
Expand Down Expand Up @@ -135,10 +135,10 @@ describe("search", () => {
);
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
encodeParam("typeKeywords", "fake,kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
encodeParam("tags", "fakey,mcfakepants")
);
expect(options.body).toContain(
encodeParam("text", JSON.stringify(fakeItem.data))
Expand Down

0 comments on commit c2d2671

Please sign in to comment.