Skip to content

Commit

Permalink
refactor(chore): remove circular dependency in request package
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-request

ISSUES CLOSED: #565
  • Loading branch information
jgravois committed May 10, 2019
1 parent 3e72fe0 commit d7904f3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 124 deletions.
2 changes: 0 additions & 2 deletions packages/arcgis-rest-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
export * from "./request";
export * from "./utils/append-custom-params";
export * from "./utils/ArcGISRequestError";
export * from "./utils/ArcGISAuthError";
export * from "./utils/check-for-errors";
export * from "./utils/clean-url";
export * from "./utils/encode-form-data";
export * from "./utils/encode-query-string";
Expand Down
116 changes: 115 additions & 1 deletion packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import { encodeFormData } from "./utils/encode-form-data";
import { encodeQueryString } from "./utils/encode-query-string";
import { requiresFormData } from "./utils/process-params";
import { checkForErrors } from "./utils/check-for-errors";
// import { checkForErrors } from "./utils/check-for-errors";
import { ArcGISRequestError } from "./utils/ArcGISRequestError";
import { IRequestOptions } from "./utils/IRequestOptions";
import { IParams } from "./utils/IParams";
import { warn } from "./utils/warn";
import { IRetryAuthError } from "./utils/retryAuthError";

export const NODEJS_DEFAULT_REFERER_HEADER = `@esri/arcgis-rest-js`;

Expand Down Expand Up @@ -46,6 +47,119 @@ export function setDefaultRequestOptions(
DEFAULT_ARCGIS_REQUEST_OPTIONS = options;
}

export class ArcGISAuthError extends ArcGISRequestError {
/**
* Create a new `ArcGISAuthError` object.
*
* @param message - The error message from the API
* @param code - The error code from the API
* @param response - The original response from the API that caused the error
* @param url - The original url of the request
* @param options - The original options of the request
*/
constructor(
message = "AUTHENTICATION_ERROR",
code: string | number = "AUTHENTICATION_ERROR_CODE",
response?: any,
url?: string,
options?: IRequestOptions
) {
super(message, code, response, url, options);
this.name = "ArcGISAuthError";
this.message =
code === "AUTHENTICATION_ERROR_CODE" ? message : `${code}: ${message}`;
}

public retry(getSession: IRetryAuthError, retryLimit = 3) {
let tries = 0;

const retryRequest = (resolve: any, reject: any) => {
getSession(this.url, this.options)
.then(session => {
const newOptions = {
...this.options,
...{ authentication: session }
};

tries = tries + 1;
return request(this.url, newOptions);
})
.then(response => {
resolve(response);
})
.catch(e => {
if (e.name === "ArcGISAuthError" && tries < retryLimit) {
retryRequest(resolve, reject);
} else if (e.name === "ArcGISAuthError" && tries >= retryLimit) {
reject(this);
} else {
reject(e);
}
});
};

return new Promise((resolve, reject) => {
retryRequest(resolve, reject);
});
}
}

/**
* Checks for errors in a JSON response from the ArcGIS REST API. If there are no errors, it will return the `data` passed in. If there is an error, it will throw an `ArcGISRequestError` or `ArcGISAuthError`.
*
* @param data The response JSON to check for errors.
* @param url The url of the original request
* @param params The parameters of the original request
* @param options The options of the original request
* @returns The data that was passed in the `data` parameter
*/
export function checkForErrors(
response: any,
url?: string,
params?: IParams,
options?: IRequestOptions
): any {
// this is an error message from billing.arcgis.com backend
if (response.code >= 400) {
const { message, code } = response;
throw new ArcGISRequestError(message, code, response, url, options);
}

// error from ArcGIS Online or an ArcGIS Portal or server instance.
if (response.error) {
const { message, code, messageCode } = response.error;
const errorCode = messageCode || code || "UNKNOWN_ERROR_CODE";

if (
code === 498 ||
code === 499 ||
messageCode === "GWM_0003" ||
(code === 400 && message === "Unable to generate token.")
) {
throw new ArcGISAuthError(message, errorCode, response, url, options);
}

throw new ArcGISRequestError(message, errorCode, response, url, options);
}

// error from a status check
if (response.status === "failed" || response.status === "failure") {
let message: string;
let code: string = "UNKNOWN_ERROR_CODE";

try {
message = JSON.parse(response.statusMessage).message;
code = JSON.parse(response.statusMessage).code;
} catch (e) {
message = response.statusMessage || response.message;
}

throw new ArcGISRequestError(message, code, response, url, options);
}

return response;
}

/**
* ```js
* import { request } from '@esri/arcgis-rest-request';
Expand Down
61 changes: 0 additions & 61 deletions packages/arcgis-rest-request/src/utils/ArcGISAuthError.ts

This file was deleted.

60 changes: 0 additions & 60 deletions packages/arcgis-rest-request/src/utils/check-for-errors.ts

This file was deleted.

0 comments on commit d7904f3

Please sign in to comment.