Skip to content

Commit

Permalink
fix(request): allow options.fetch without global fetch
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-request

ISSUES CLOSED: #108
  • Loading branch information
patrickarlt committed Feb 1, 2018
1 parent f12822c commit 99cf01c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function request(
requestOptions: IRequestOptions = { params: { f: "json" } }
): Promise<any> {
const options: IRequestOptions = {
...{ httpMethod: "POST", fetch: fetch.bind(Function("return this")()) },
...{ httpMethod: "POST", fetch },
...requestOptions
};

Expand Down Expand Up @@ -135,6 +135,10 @@ export function request(
);
}

if (options.fetch === fetch) {
options.fetch = fetch.bind(Function("return this")());
}

const { httpMethod, authentication } = options;

const params: IParams = {
Expand Down
38 changes: 35 additions & 3 deletions packages/arcgis-rest-request/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,52 @@ describe("request()", () => {
beforeEach(() => {
Promise = undefined;
FormData = undefined;
Function("return this")().fetch = undefined;
});

afterEach(() => {
Promise = oldPromise;
FormData = oldFormData;
Function("return this")().fetch = oldFetch;
});
it("should throw for missing dependencies", () => {
expect(() => {
request("https://www.arcgis.com/sharing/rest/info", {
fetch: undefined
});
request("https://www.arcgis.com/sharing/rest/info");
}).toThrowError(
"`arcgis-rest-request` requires global variables for `fetch`, `Promise` and `FormData` to be present in the global scope. You are missing `fetch`, `Promise`, `FormData`. We recommend installing the `isomorphic-fetch`, `es6-promise`, `isomorphic-form-data` modules at the root of your application to add these to the global scope. See http://bit.ly/2BXbqzq for more info."
);
});

it("should not throw if fetch is not present but a custom fetch is defined", done => {
Promise = oldPromise;
FormData = oldFormData;

const MockFetchResponse = {
json() {
return Promise.resolve(SharingRestInfo);
},
blob() {
return Promise.resolve(new Blob([JSON.stringify(SharingRestInfo)]));
},
text() {
return Promise.resolve(JSON.stringify(SharingRestInfo));
}
};

const MockFetch = function() {
return Promise.resolve(MockFetchResponse);
};

request("https://www.arcgis.com/sharing/rest/info", {
fetch: MockFetch as any
})
.then(response => {
expect(response).toEqual(SharingRestInfo);
done();
})
.catch(e => {
fail(e);
});
});
});
});

0 comments on commit 99cf01c

Please sign in to comment.