@ampsec/platform-client 46.3.0 → 46.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,8 @@ export type RestClientOptions = {
22
22
  rateLimitStrategy?: RestClientRateLimitStrategy;
23
23
  logger?: AmpLogger;
24
24
  client?: AxiosStatic;
25
+ /** Throws errors on non-200 responses if `true`. Default `false` */
26
+ strict?: boolean;
25
27
  };
26
28
  /**
27
29
  * RestClient is a generic interface for making REST calls.
@@ -41,6 +43,7 @@ export declare class DefaultRestClient {
41
43
  protected readonly retryStrategy: RestClientRetryStrategy;
42
44
  protected readonly rateLimitStrategy: RestClientRateLimitStrategy;
43
45
  protected readonly logger: AmpLogger;
46
+ protected readonly strict: boolean;
44
47
  constructor(options?: RestClientOptions);
45
48
  call: (request: RestRequest) => Promise<RestResponse>;
46
49
  }
@@ -43,7 +43,7 @@ const logging_1 = require("../../logging");
43
43
  */
44
44
  class DefaultRestClient {
45
45
  constructor(options = {}) {
46
- var _a, _b, _c, _d, _e;
46
+ var _a, _b, _c, _d, _e, _f;
47
47
  this.call = async (request) => {
48
48
  var _a, _b;
49
49
  const req = lodash_1.default.merge(request);
@@ -51,6 +51,9 @@ class DefaultRestClient {
51
51
  await this.rateLimitStrategy.reserve(req);
52
52
  this.logger.debug({ req }); // noop logger by default
53
53
  const res = await this.ampAxios.request(req);
54
+ if (this.strict && res.status >= 400) {
55
+ throw new Error(`[${res.status}] ${res.statusText} (P8ZZJp-1)`);
56
+ }
54
57
  return {
55
58
  status: res.status,
56
59
  headers: (0, utils_1.convertHeaders)(res),
@@ -61,8 +64,15 @@ class DefaultRestClient {
61
64
  catch (err) {
62
65
  if (err instanceof axios_1.AxiosError) {
63
66
  const axiosError = err;
67
+ const status = (_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 500;
68
+ // ! DO NOT print the entire error here because axiosError.request likely contains sensitive data
69
+ this.logger.error(`[${status}] ${axiosError} ${axiosError.message}`);
70
+ // ! DO NOT print the entire error here because axiosError.request likely contains sensitive data
71
+ if (this.strict && status >= 400) {
72
+ throw new Error(`[${status}] ${axiosError.message} (hGRxLB-2)`);
73
+ }
64
74
  return {
65
- status: (_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 500,
75
+ status: status,
66
76
  headers: axiosError.response ? (0, utils_1.convertHeaders)(axiosError.response) : {},
67
77
  data: null,
68
78
  error: err,
@@ -90,6 +100,7 @@ class DefaultRestClient {
90
100
  this.ampAxios.defaults.headers.put['Content-Type'] = 'application/json';
91
101
  this.retryStrategy = (_d = options.retryStrategy) !== null && _d !== void 0 ? _d : retry_rest_1.noopRestRetryStrategy;
92
102
  this.rateLimitStrategy = (_e = options.rateLimitStrategy) !== null && _e !== void 0 ? _e : rateLimit_rest_1.noopRestClientRateLimitStrategy;
103
+ this.strict = (_f = options.strict) !== null && _f !== void 0 ? _f : false;
93
104
  }
94
105
  }
95
106
  exports.DefaultRestClient = DefaultRestClient;
@@ -1 +1 @@
1
- {"version":3,"file":"RestClient.js","sourceRoot":"","sources":["../../../../src/services/rest/RestClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAuB;AACvB,4CAAoB;AACpB,+CAA4D;AAG5D,6CAA4E;AAC5E,mCAAmD;AACnD,qDAAiI;AACjI,2CAAoD;AA6BpD;;;;;;GAMG;AACH,MAAa,iBAAiB;IAM5B,YAAY,UAA6B,EAAE;;QAoB3C,SAAI,GAAG,KAAK,EAAE,OAAoB,EAAyB,EAAE;;YAC3D,MAAM,GAAG,GAAG,gBAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7B,IAAI;gBACF,MAAM,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBAC1C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAC,GAAG,EAAC,CA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AAC,GAAG,kBAAkB,CAAC;QACtE,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC;QACzE,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC;QACxE,IAAI,CAAC,aAAa,GAAG,MAAA,OAAO,CAAC,aAAa,mCAAI,kCAAqB,CAAC;QACpE,IAAI,CAAC,iBAAiB,GAAG,MAAA,OAAO,CAAC,iBAAiB,mCAAI,gDAA+B,CAAC;QACtF,IAAI,CAAC,MAAM,GAAG,MAAA,OAAO,CAAC,MAAM,mCAAI,KAAK,CAAC;IACxC,CAAC;CAoCF;AA/DD,8CA+DC;AAEM,MAAM,gBAAgB,GAAG,CAAC,OAA6B,EAAc,EAAE;IAC5E,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC;QACtC,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO,EAAE;YACP,aAAa,EAAE,UAAU,OAAO,CAAC,KAAK,EAAE;SACzC;QACD,iBAAiB,EAAE,IAAI,kDAAiC,CAAC,EAAE,CAAC;QAC5D,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,MAAM,EAAE,OAAO,CAAC,MAAM;KACvB,CAAC,CAAC;IACH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAXW,QAAA,gBAAgB,oBAW3B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ampsec/platform-client",
3
- "version": "46.3.0",
3
+ "version": "46.4.0",
4
4
  "description": "",
5
5
  "main": "build/src/index.js",
6
6
  "runkitExampleFilename": "example/main.js",
@@ -26,6 +26,8 @@ export type RestClientOptions = {
26
26
  rateLimitStrategy?: RestClientRateLimitStrategy;
27
27
  logger?: AmpLogger;
28
28
  client?: AxiosStatic;
29
+ /** Throws errors on non-200 responses if `true`. Default `false` */
30
+ strict?: boolean;
29
31
  };
30
32
 
31
33
  /**
@@ -47,6 +49,7 @@ export class DefaultRestClient {
47
49
  protected readonly retryStrategy: RestClientRetryStrategy;
48
50
  protected readonly rateLimitStrategy: RestClientRateLimitStrategy;
49
51
  protected readonly logger: AmpLogger;
52
+ protected readonly strict: boolean;
50
53
 
51
54
  constructor(options: RestClientOptions = {}) {
52
55
  // TODO: rest client
@@ -67,6 +70,7 @@ export class DefaultRestClient {
67
70
  this.ampAxios.defaults.headers.put['Content-Type'] = 'application/json';
68
71
  this.retryStrategy = options.retryStrategy ?? noopRestRetryStrategy;
69
72
  this.rateLimitStrategy = options.rateLimitStrategy ?? noopRestClientRateLimitStrategy;
73
+ this.strict = options.strict ?? false;
70
74
  }
71
75
  call = async (request: RestRequest): Promise<RestResponse> => {
72
76
  const req = _.merge(request);
@@ -74,6 +78,9 @@ export class DefaultRestClient {
74
78
  await this.rateLimitStrategy.reserve(req);
75
79
  this.logger.debug({req}); // noop logger by default
76
80
  const res = await this.ampAxios.request(req);
81
+ if (this.strict && res.status >= 400) {
82
+ throw new Error(`[${res.status}] ${res.statusText} (P8ZZJp-1)`);
83
+ }
77
84
  return {
78
85
  status: res.status,
79
86
  headers: convertHeaders(res),
@@ -83,8 +90,15 @@ export class DefaultRestClient {
83
90
  } catch (err: unknown) {
84
91
  if (err instanceof AxiosError) {
85
92
  const axiosError = err as AxiosError;
93
+ const status = axiosError.response?.status ?? 500;
94
+ // ! DO NOT print the entire error here because axiosError.request likely contains sensitive data
95
+ this.logger.error(`[${status}] ${axiosError} ${axiosError.message}`);
96
+ // ! DO NOT print the entire error here because axiosError.request likely contains sensitive data
97
+ if (this.strict && status >= 400) {
98
+ throw new Error(`[${status}] ${axiosError.message} (hGRxLB-2)`);
99
+ }
86
100
  return {
87
- status: axiosError.response?.status ?? 500,
101
+ status: status,
88
102
  headers: axiosError.response ? convertHeaders(axiosError.response) : {},
89
103
  data: null,
90
104
  error: err,