Skip to content

Commit a0901a2

Browse files
authored
Merge pull request #1220 from Esri/f/request-option
feat(arcgis-rest-request): add option to override request function
2 parents 57c17ee + d4ca332 commit a0901a2

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

packages/arcgis-rest-request/src/request.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { encodeFormData } from "./utils/encode-form-data.js";
55
import { encodeQueryString } from "./utils/encode-query-string.js";
66
import { requiresFormData } from "./utils/process-params.js";
77
import { ArcGISRequestError } from "./utils/ArcGISRequestError.js";
8-
import { IRequestOptions } from "./utils/IRequestOptions.js";
8+
import {
9+
IRequestOptions,
10+
InternalRequestOptions
11+
} from "./utils/IRequestOptions.js";
912
import { IParams } from "./utils/IParams.js";
1013
import { warn } from "./utils/warn.js";
1114
import { IRetryAuthError } from "./utils/retryAuthError.js";
@@ -211,7 +214,7 @@ export function checkForErrors(
211214
*/
212215
export function internalRequest(
213216
url: string,
214-
requestOptions: IRequestOptions
217+
requestOptions: InternalRequestOptions
215218
): Promise<any> {
216219
const defaults = getDefaultRequestOptions();
217220
const options: IRequestOptions = {
@@ -531,19 +534,23 @@ export function request(
531534
url: string,
532535
requestOptions: IRequestOptions = { params: { f: "json" } }
533536
): Promise<any> {
534-
return internalRequest(url, requestOptions).catch((e) => {
535-
if (
536-
e instanceof ArcGISAuthError &&
537-
requestOptions.authentication &&
538-
typeof requestOptions.authentication !== "string" &&
539-
requestOptions.authentication.canRefresh &&
540-
requestOptions.authentication.refreshCredentials
541-
) {
542-
return e.retry(() => {
543-
return (requestOptions.authentication as any).refreshCredentials();
544-
}, 1);
545-
} else {
546-
return Promise.reject(e);
547-
}
548-
});
537+
const { request, ...internalOptions } = requestOptions;
538+
// if the user passed in a custom request function, use that instead of the default
539+
return request
540+
? request(url, internalOptions)
541+
: internalRequest(url, internalOptions).catch((e) => {
542+
if (
543+
e instanceof ArcGISAuthError &&
544+
requestOptions.authentication &&
545+
typeof requestOptions.authentication !== "string" &&
546+
requestOptions.authentication.canRefresh &&
547+
requestOptions.authentication.refreshCredentials
548+
) {
549+
return e.retry(() => {
550+
return (requestOptions.authentication as any).refreshCredentials();
551+
}, 1);
552+
} else {
553+
return Promise.reject(e);
554+
}
555+
});
549556
}

packages/arcgis-rest-request/src/utils/IRequestOptions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,19 @@ export interface IRequestOptions {
6262
* Suppress any ArcGIS REST JS related warnings for this request.
6363
*/
6464
suppressWarnings?: boolean;
65+
66+
/**
67+
* Override the default function for making the request. This is mainly useful for testing purposes (i.e. so you can pass in a spy).
68+
* @param requestOptions
69+
* @returns
70+
*/
71+
request?: (
72+
url: string,
73+
requestOptions: InternalRequestOptions
74+
) => Promise<any>;
6575
}
76+
77+
/**
78+
* Options for the function that will be making the actual request.
79+
*/
80+
export type InternalRequestOptions = Omit<IRequestOptions, "request">;

packages/arcgis-rest-request/src/utils/append-custom-params.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function appendCustomParams<T extends IRequestOptions>(
2323
"maxUrlLength",
2424
"headers",
2525
"signal",
26-
"suppressWarnings"
26+
"suppressWarnings",
27+
"request"
2728
];
2829

2930
const options: T = {

packages/arcgis-rest-request/test/request.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,25 @@ describe("request()", () => {
779779
});
780780
});
781781
});
782+
783+
it("should use a passed in request function", (done) => {
784+
const url =
785+
"https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data";
786+
const options = {
787+
httpMethod: "GET",
788+
params: { f: "text" }
789+
} as const;
790+
const requestSpy = jasmine
791+
.createSpy()
792+
.and.returnValue(Promise.resolve(WebMapAsText));
793+
request(url, { ...options, request: requestSpy })
794+
.then(() => {
795+
expect(requestSpy.calls.count()).toBe(1);
796+
expect(requestSpy.calls.argsFor(0)).toEqual([url, options]);
797+
done();
798+
})
799+
.catch((e) => {
800+
fail(e);
801+
});
802+
});
782803
});

0 commit comments

Comments
 (0)