@3dverse/api 0.2.4 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ // index.ts
2
+ import axiosRetry, { isNetworkError, isIdempotentRequestError } from "axios-retry";
3
+
1
4
  // _prebuild/wrapper.ts
2
5
  import axios from "axios";
3
6
  var axiosInstance = axios.create({
@@ -844,9 +847,6 @@ function setUserToken(userToken) {
844
847
  axiosInstance.defaults.headers.common["user_token"] = userToken;
845
848
  delete axiosInstance.defaults.headers.common["api_key"];
846
849
  }
847
- function setBaseUrl(url) {
848
- axiosInstance.defaults.baseURL = url;
849
- }
850
850
  var ServiceError = class extends Error {
851
851
  errorCode;
852
852
  httpCode;
@@ -876,6 +876,11 @@ var UnexpectedServiceError = class extends ServiceError {
876
876
  function installInterceptors() {
877
877
  const CLIENT_ERROR = 400;
878
878
  const INTERNAL_SERVER_ERROR = 500;
879
+ axiosRetry(axiosInstance, {
880
+ retries: 5,
881
+ retryDelay: axiosRetry.exponentialDelay,
882
+ retryCondition: shouldRetryRequest
883
+ });
879
884
  axiosInstance.interceptors.response.use(
880
885
  (successFulResponse) => {
881
886
  if (successFulResponse.config.responseType === "stream") {
@@ -893,14 +898,24 @@ function installInterceptors() {
893
898
  if (!errorData && axiosError.request.method === "HEAD" && status >= CLIENT_ERROR && status < INTERNAL_SERVER_ERROR) {
894
899
  return axiosError.response;
895
900
  }
896
- if (!errorData || !("error" in errorData)) {
901
+ if (!errorData || typeof errorData !== "object") {
897
902
  return Promise.reject(new UnexpectedServiceError(status, errorData));
898
903
  }
899
- const serviceError = errorData.error;
904
+ const serviceError = errorData;
900
905
  return Promise.reject(new ApiError(serviceError.errorCode, status, serviceError.message, serviceError));
901
906
  }
902
907
  );
903
908
  }
909
+ function shouldRetryRequest(error) {
910
+ const status = error.response?.status;
911
+ switch (status) {
912
+ case 403:
913
+ case 404:
914
+ return true;
915
+ default:
916
+ return isNetworkError(error) || isIdempotentRequestError(error);
917
+ }
918
+ }
904
919
  installInterceptors();
905
920
  export {
906
921
  ApiError,
@@ -972,7 +987,6 @@ export {
972
987
  setApiKey,
973
988
  setAssetThumbnail,
974
989
  setBaseURL,
975
- setBaseUrl,
976
990
  setUserToken,
977
991
  updateAssetDescription,
978
992
  updateEntity,