Skip to content

Commit

Permalink
add support for blob item/data
Browse files Browse the repository at this point in the history
  • Loading branch information
jgravois committed Jul 23, 2018
1 parent 2dd7f00 commit be8628b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
25 changes: 19 additions & 6 deletions packages/arcgis-rest-items/src/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
} from "@esri/arcgis-rest-common-types";
import { IUserRequestOptions } from "@esri/arcgis-rest-auth";

export interface IItemRequestOptions extends IUserRequestOptions {
item: IItem;
}

export interface IItemIdRequestOptions extends IUserRequestOptions {
/**
* Unique identifier of the item.
Expand Down Expand Up @@ -74,6 +78,13 @@ export interface ISearchRequestOptions extends IRequestOptions {
searchForm?: ISearchRequest;
}

export interface IItemDataRequestOptions extends IRequestOptions {
/**
* Used to request binary data.
*/
file?: boolean;
}

/**
* Options to pass through when searching for items.
*/
Expand Down Expand Up @@ -227,23 +238,25 @@ export function getItem(

/**
* Get the /data for an item.
* Note: Some items do not return json from /data
* and this method will throw if that is the case.
*
* @param id - Item Id
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the json data for the item.
*/
export function getItemData(
id: string,
requestOptions?: IRequestOptions
requestOptions?: IItemDataRequestOptions
): Promise<any> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}/data`;
// default to a GET request
const options: IRequestOptions = {
...{ httpMethod: "GET" },
const options: IItemDataRequestOptions = {
...{ httpMethod: "GET", params: {} },
...requestOptions
};

if (options.file) {
options.params.f = null;
}

return request(url, options);
}

Expand Down
35 changes: 31 additions & 4 deletions packages/arcgis-rest-items/test/items.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fetchMock from "fetch-mock";

import {
searchItems,
getItem,
Expand All @@ -15,10 +17,6 @@ import {
ISearchRequestOptions
} from "../src/index";

import * as fetchMock from "fetch-mock";

import { SearchResponse } from "./mocks/search";

import {
ItemSuccessResponse,
ItemResponse,
Expand All @@ -31,6 +29,7 @@ import {
RemoveItemResourceResponse
} from "./mocks/resources";

import { SearchResponse } from "./mocks/search";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";
import { encodeParam } from "@esri/arcgis-rest-request";
Expand Down Expand Up @@ -146,6 +145,34 @@ describe("search", () => {
});
});

it("should return binary item data by id", done => {
// Blob() is only available in the browser
if (typeof window !== "undefined") {
fetchMock.once("*", {
sendAsJson: false,
headers: { "Content-Type": "application/zip" },
body: new Blob()
});

getItemData("3ef", { file: true })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/content/items/3ef/data"
);
expect(options.method).toBe("GET");
expect(response instanceof Blob).toBeTruthy();
done();
})
.catch(e => {
fail(e);
});
} else {
done();
}
});

describe("Authenticated methods", () => {
// setup a UserSession to use in all these tests
const MOCK_USER_SESSION = new UserSession({
Expand Down
Binary file added packages/arcgis-rest-items/test/mocks/foo.zip
Binary file not shown.
12 changes: 10 additions & 2 deletions packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ export function request(

if (fetchOptions.method === "GET") {
// encode the parameters into the query string
const urlWithQueryString = url + "?" + encodeQueryString(params);
const queryParams = encodeQueryString(params);
// dont append a '?' unless parameters are actually present
const urlWithQueryString =
queryParams === "" ? url : url + "?" + encodeQueryString(params);

if (
options.maxUrlLength &&
urlWithQueryString.length > options.maxUrlLength
Expand Down Expand Up @@ -236,9 +240,13 @@ export function request(
/* istanbul ignore next blob responses are difficult to make cross platform we will just have to trust the isomorphic fetch will do its job */
case "image":
return response.blob();
/* istanbul ignore next blob responses are difficult to make cross platform we will just have to trust the isomorphic fetch will do its job */
/* istanbul ignore next */
case "zip":
return response.blob();
/* istanbul ignore next */
default:
// hopefully we never need to handle JSON payloads when no f= parameter is set
return response.blob();
}
})
.then(data => {
Expand Down

0 comments on commit be8628b

Please sign in to comment.