@dynatrace-sdk/http-client 1.5.2 → 1.6.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.
- package/CHANGELOG.md +6 -0
- package/cjs/index.js +302 -5
- package/docs/DOCS.md +2 -2
- package/esm/index.js +304 -7
- package/package.json +1 -1
- package/types/index.d.ts +318 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
@dynatrace-sdk/http-client
|
|
4
4
|
|
|
5
|
+
## 1.6.0
|
|
6
|
+
|
|
7
|
+
### Minor Changes
|
|
8
|
+
|
|
9
|
+
- Added an internal `executeRequest` function, along with internal `ApiClientError`, `ApiGatewayError`, `ClientRequestError` and `InvalidResponseError` classes mirroring the ones in `@dynatrace-sdk/shared-errors`, so generated SDK clients can share the request, response and error-handling logic instead of inlining it in every operation. `@dynatrace-sdk/shared-errors` will switch to re-exporting these classes from here in a follow-up release. (APPDEV-17291)
|
|
10
|
+
|
|
5
11
|
## 1.5.2
|
|
6
12
|
|
|
7
13
|
### Patch Changes
|
package/cjs/index.js
CHANGED
|
@@ -43,10 +43,20 @@ __export(index_exports, {
|
|
|
43
43
|
PlatformHttpClient: () => HttpClient,
|
|
44
44
|
PlatformHttpClientAbortError: () => HttpClientAbortError,
|
|
45
45
|
PlatformHttpClientRequestError: () => HttpClientRequestError,
|
|
46
|
-
PlatformHttpClientResponse: () =>
|
|
46
|
+
PlatformHttpClientResponse: () => HttpClientResponse2,
|
|
47
47
|
PlatformHttpClientResponseError: () => HttpClientResponseError,
|
|
48
48
|
PlatformUnsupportedOperationError: () => UnsupportedOperationError,
|
|
49
|
+
_ApiClientError: () => ApiClientError,
|
|
50
|
+
_ApiGatewayError: () => ApiGatewayError,
|
|
51
|
+
_ClientRequestError: () => ClientRequestError,
|
|
52
|
+
_ErrorType: () => ErrorType,
|
|
53
|
+
_InvalidResponseError: () => InvalidResponseError,
|
|
49
54
|
_OAuthHttpClient: () => OAuthHttpClient,
|
|
55
|
+
_executeRequest: () => executeRequest,
|
|
56
|
+
_isApiClientError: () => isApiClientError,
|
|
57
|
+
_isApiGatewayError: () => isApiGatewayError,
|
|
58
|
+
_isClientRequestError: () => isClientRequestError,
|
|
59
|
+
_isInvalidResponseError: () => isInvalidResponseError,
|
|
50
60
|
httpClient: () => httpClient,
|
|
51
61
|
isHttpClientAbortError: () => isHttpClientAbortError,
|
|
52
62
|
isHttpClientNetworkError: () => isHttpClientNetworkError,
|
|
@@ -75,12 +85,65 @@ function isHttpClientNetworkError(e) {
|
|
|
75
85
|
return e?.name === "NetworkError";
|
|
76
86
|
}
|
|
77
87
|
|
|
88
|
+
// packages/http-client/src/lib/types/error-types.ts
|
|
89
|
+
var ErrorType = /* @__PURE__ */ ((ErrorType3) => {
|
|
90
|
+
ErrorType3["COMMON"] = "JS Error";
|
|
91
|
+
ErrorType3["HTTP"] = "Http Error";
|
|
92
|
+
return ErrorType3;
|
|
93
|
+
})(ErrorType || {});
|
|
94
|
+
|
|
78
95
|
// packages/http-client/src/lib/platform/abort-controller.ts
|
|
79
96
|
var AbortController = globalThis.AbortController;
|
|
80
97
|
|
|
81
98
|
// packages/http-client/src/lib/platform/abort-signal.ts
|
|
82
99
|
var AbortSignal = globalThis.AbortSignal;
|
|
83
100
|
|
|
101
|
+
// packages/http-client/src/lib/platform/api-client-error.ts
|
|
102
|
+
var ApiClientError = class extends Error {
|
|
103
|
+
constructor(name, message, cause) {
|
|
104
|
+
super(message ?? "ApiClientError: an unexpected error occurred.");
|
|
105
|
+
this.cause = cause;
|
|
106
|
+
this.name = name;
|
|
107
|
+
}
|
|
108
|
+
cause;
|
|
109
|
+
isApiClientError = true;
|
|
110
|
+
errorType = "JS Error" /* COMMON */;
|
|
111
|
+
};
|
|
112
|
+
function isApiClientError(e) {
|
|
113
|
+
return e?.isApiClientError === true && e instanceof Error;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// packages/http-client/src/lib/platform/client-request-error.ts
|
|
117
|
+
var ClientRequestError = class extends ApiClientError {
|
|
118
|
+
isClientRequestError = true;
|
|
119
|
+
body;
|
|
120
|
+
response;
|
|
121
|
+
constructor(name, response, body, message, cause) {
|
|
122
|
+
super(name, message, cause);
|
|
123
|
+
this.errorType = "Http Error" /* HTTP */;
|
|
124
|
+
this.body = body;
|
|
125
|
+
this.response = response;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
function isClientRequestError(e) {
|
|
129
|
+
return e?.isClientRequestError === true && isApiClientError(e);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// packages/http-client/src/lib/platform/api-gateway-error.ts
|
|
133
|
+
var ApiGatewayError = class extends ClientRequestError {
|
|
134
|
+
isApiGatewayError = true;
|
|
135
|
+
code;
|
|
136
|
+
retryAfterSeconds;
|
|
137
|
+
constructor(response, body) {
|
|
138
|
+
super(body.error.details?.errorCode || "ApiGatewayError", response, body, body.error.message);
|
|
139
|
+
this.code = body.error.code;
|
|
140
|
+
this.retryAfterSeconds = body.error.retryAfterSeconds;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
function isApiGatewayError(e) {
|
|
144
|
+
return e?.isApiGatewayError === true && isClientRequestError(e);
|
|
145
|
+
}
|
|
146
|
+
|
|
84
147
|
// packages/http-client/src/lib/platform/base-error.ts
|
|
85
148
|
var BaseError = class extends Error {
|
|
86
149
|
constructor(message, cause) {
|
|
@@ -152,6 +215,36 @@ var DataTypeError = class extends BaseError {
|
|
|
152
215
|
// packages/http-client/src/lib/platform/default-status-validator.ts
|
|
153
216
|
var defaultStatusValidator = (status) => 200 <= status && status < 300;
|
|
154
217
|
|
|
218
|
+
// packages/http-client/src/lib/platform/get-error-message.ts
|
|
219
|
+
function serializeData(data) {
|
|
220
|
+
try {
|
|
221
|
+
return JSON.stringify(data);
|
|
222
|
+
} catch {
|
|
223
|
+
return String(data);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function getMessagesFromErrorDetails(details) {
|
|
227
|
+
const messages = [];
|
|
228
|
+
Object.entries(details).forEach(([name, data]) => {
|
|
229
|
+
if (!data) return;
|
|
230
|
+
const serializedData = serializeData(data);
|
|
231
|
+
switch (name) {
|
|
232
|
+
case "missingScopes":
|
|
233
|
+
messages.push(`Missing scopes: ${serializedData}`);
|
|
234
|
+
break;
|
|
235
|
+
default:
|
|
236
|
+
messages.push(`${name}: ${serializedData}`);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
return messages;
|
|
240
|
+
}
|
|
241
|
+
function getErrorMessage(errorBody, defaultMessage) {
|
|
242
|
+
const error = errorBody;
|
|
243
|
+
const msg = error?.error?.message || defaultMessage;
|
|
244
|
+
const details = error?.error?.details || {};
|
|
245
|
+
return [msg, ...getMessagesFromErrorDetails(details)].join(". ");
|
|
246
|
+
}
|
|
247
|
+
|
|
155
248
|
// packages/http-client/src/lib/platform/encode-request-body.ts
|
|
156
249
|
function encodeTextRequestBody(body) {
|
|
157
250
|
return String(body);
|
|
@@ -639,7 +732,7 @@ function decodeResponseBody(response, responseBodyType) {
|
|
|
639
732
|
}
|
|
640
733
|
|
|
641
734
|
// packages/http-client/src/lib/platform/http-client-response.ts
|
|
642
|
-
var
|
|
735
|
+
var HttpClientResponse2 = class {
|
|
643
736
|
constructor(response, data) {
|
|
644
737
|
this.response = response;
|
|
645
738
|
this.requestUrl = data?.requestUrl ?? response.url;
|
|
@@ -758,7 +851,7 @@ var HttpClientResponseError = class extends BaseError {
|
|
|
758
851
|
constructor(response, data) {
|
|
759
852
|
addHttpClientResponseErrorSerializer();
|
|
760
853
|
super(`HTTP Error ${response.status}: ${response.statusText}`);
|
|
761
|
-
this.response = new
|
|
854
|
+
this.response = new HttpClientResponse2(response, {
|
|
762
855
|
requestUrl: data?.requestUrl ?? response.url
|
|
763
856
|
});
|
|
764
857
|
this.requestMethod = data?.requestMethod;
|
|
@@ -809,7 +902,7 @@ var HttpClient = class {
|
|
|
809
902
|
});
|
|
810
903
|
const ok = statusValidator(response.status);
|
|
811
904
|
if (ok) {
|
|
812
|
-
return new
|
|
905
|
+
return new HttpClientResponse2(response, { requestUrl: url });
|
|
813
906
|
} else {
|
|
814
907
|
throw new HttpClientResponseError(response, {
|
|
815
908
|
requestUrl: url,
|
|
@@ -820,6 +913,210 @@ var HttpClient = class {
|
|
|
820
913
|
};
|
|
821
914
|
var httpClient = new HttpClient();
|
|
822
915
|
|
|
916
|
+
// packages/http-client/src/lib/platform/invalid-response-error.ts
|
|
917
|
+
var InvalidResponseError = class extends ApiClientError {
|
|
918
|
+
isInvalidResponseError = true;
|
|
919
|
+
responseBody;
|
|
920
|
+
expectedType;
|
|
921
|
+
nestedError;
|
|
922
|
+
response;
|
|
923
|
+
constructor(name, nestedError, body, expectedType, message, response) {
|
|
924
|
+
super(
|
|
925
|
+
name,
|
|
926
|
+
message ?? `${name}: Response does not match expected datatype${expectedType ? " " + expectedType : ""}: ${nestedError?.toString() ?? "unable to deserialize"}`
|
|
927
|
+
);
|
|
928
|
+
this.nestedError = nestedError;
|
|
929
|
+
this.responseBody = body;
|
|
930
|
+
this.expectedType = expectedType;
|
|
931
|
+
this.response = response;
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
function isHttpClientDataTypeError(e) {
|
|
935
|
+
return e instanceof Error && e?.name === "DataTypeError";
|
|
936
|
+
}
|
|
937
|
+
function isInvalidResponseError(e) {
|
|
938
|
+
if (e?.isInvalidResponseError === true && isApiClientError(e)) {
|
|
939
|
+
return true;
|
|
940
|
+
}
|
|
941
|
+
return isHttpClientDataTypeError(e);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// packages/http-client/src/lib/platform/execute-request.ts
|
|
945
|
+
var API_GATEWAY_RESPONSE_SOURCE_HEADER = "dynatrace-response-source";
|
|
946
|
+
var API_GATEWAY_RESPONSE_SOURCE = "API Gateway";
|
|
947
|
+
function buildStatusValidator(acceptedStatusCodes, statusValidator) {
|
|
948
|
+
if (acceptedStatusCodes !== void 0) {
|
|
949
|
+
return (status) => acceptedStatusCodes.includes(status);
|
|
950
|
+
}
|
|
951
|
+
return statusValidator ?? defaultStatusValidator;
|
|
952
|
+
}
|
|
953
|
+
function resolveBodyType(resolver, response) {
|
|
954
|
+
return typeof resolver === "function" ? resolver(response) : resolver;
|
|
955
|
+
}
|
|
956
|
+
function readBody(response, responseBodyType) {
|
|
957
|
+
return response.body(responseBodyType);
|
|
958
|
+
}
|
|
959
|
+
async function readTextBody(response) {
|
|
960
|
+
try {
|
|
961
|
+
return await response.body("text");
|
|
962
|
+
} catch {
|
|
963
|
+
return "";
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
function resolveErrorFactory(status, factories) {
|
|
967
|
+
const exact = factories?.[status];
|
|
968
|
+
if (exact) {
|
|
969
|
+
return { factory: exact, label: `${status}` };
|
|
970
|
+
}
|
|
971
|
+
const range = factories?.[`${Math.floor(status / 100)}XX`];
|
|
972
|
+
if (range) {
|
|
973
|
+
return { factory: range, label: `${status}` };
|
|
974
|
+
}
|
|
975
|
+
if (factories?.default) {
|
|
976
|
+
return { factory: factories.default, label: "err" };
|
|
977
|
+
}
|
|
978
|
+
return void 0;
|
|
979
|
+
}
|
|
980
|
+
async function buildApiGatewayError(response, operationId) {
|
|
981
|
+
if (response.headers?.[API_GATEWAY_RESPONSE_SOURCE_HEADER] !== API_GATEWAY_RESPONSE_SOURCE) {
|
|
982
|
+
return void 0;
|
|
983
|
+
}
|
|
984
|
+
let body;
|
|
985
|
+
try {
|
|
986
|
+
body = await response.body("json");
|
|
987
|
+
} catch (decodeErr) {
|
|
988
|
+
throw new InvalidResponseError(
|
|
989
|
+
`${operationId}:${response.status}`,
|
|
990
|
+
decodeErr,
|
|
991
|
+
await readTextBody(response),
|
|
992
|
+
void 0,
|
|
993
|
+
void 0,
|
|
994
|
+
response
|
|
995
|
+
);
|
|
996
|
+
}
|
|
997
|
+
return new ApiGatewayError(response, body);
|
|
998
|
+
}
|
|
999
|
+
async function buildFactoryError(e, { factory, label }, options) {
|
|
1000
|
+
const { response } = e;
|
|
1001
|
+
const name = `${options.operationId}:${label}`;
|
|
1002
|
+
const expectedType = factory.errorEnvelope ?? options.errorEnvelope;
|
|
1003
|
+
const bodyType = resolveBodyType(factory.responseBodyType ?? "json", response);
|
|
1004
|
+
let body;
|
|
1005
|
+
if (bodyType !== "none") {
|
|
1006
|
+
try {
|
|
1007
|
+
body = await readBody(response, bodyType);
|
|
1008
|
+
} catch (decodeErr) {
|
|
1009
|
+
throw new InvalidResponseError(
|
|
1010
|
+
name,
|
|
1011
|
+
decodeErr,
|
|
1012
|
+
await readTextBody(response),
|
|
1013
|
+
expectedType,
|
|
1014
|
+
void 0,
|
|
1015
|
+
response
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
if (factory.transformBody) {
|
|
1020
|
+
try {
|
|
1021
|
+
body = factory.transformBody(body);
|
|
1022
|
+
} catch (transformErr) {
|
|
1023
|
+
throw new InvalidResponseError(name, transformErr, body, expectedType, void 0, response);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
return factory.build({ response, body, cause: e });
|
|
1027
|
+
}
|
|
1028
|
+
async function buildFallbackError(e) {
|
|
1029
|
+
const { response } = e;
|
|
1030
|
+
let responseValue;
|
|
1031
|
+
try {
|
|
1032
|
+
responseValue = await response.body("json");
|
|
1033
|
+
} catch (decodeErr) {
|
|
1034
|
+
throw new InvalidResponseError(
|
|
1035
|
+
`${response.status}`,
|
|
1036
|
+
decodeErr,
|
|
1037
|
+
await readTextBody(response),
|
|
1038
|
+
"json",
|
|
1039
|
+
"Unexpected api response",
|
|
1040
|
+
response
|
|
1041
|
+
);
|
|
1042
|
+
}
|
|
1043
|
+
return new ClientRequestError(
|
|
1044
|
+
`${response.status}`,
|
|
1045
|
+
response,
|
|
1046
|
+
responseValue,
|
|
1047
|
+
getErrorMessage(
|
|
1048
|
+
responseValue,
|
|
1049
|
+
`Unexpected api response: code=${response.status} body="${responseValue}"`
|
|
1050
|
+
),
|
|
1051
|
+
e
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
async function dispatchResponseError(e, options) {
|
|
1055
|
+
const gatewayError = await buildApiGatewayError(e.response, options.operationId);
|
|
1056
|
+
if (gatewayError) {
|
|
1057
|
+
throw gatewayError;
|
|
1058
|
+
}
|
|
1059
|
+
const resolved = resolveErrorFactory(e.response.status, options.errorFactories);
|
|
1060
|
+
throw resolved ? await buildFactoryError(e, resolved, options) : await buildFallbackError(e);
|
|
1061
|
+
}
|
|
1062
|
+
async function decodeSuccessBody(response, options) {
|
|
1063
|
+
const resolvedBodyType = resolveBodyType(options.responseBodyType ?? "json", response);
|
|
1064
|
+
if (resolvedBodyType === "none") {
|
|
1065
|
+
return void 0;
|
|
1066
|
+
}
|
|
1067
|
+
const name = `${options.operationId}:${response.status}`;
|
|
1068
|
+
let body;
|
|
1069
|
+
try {
|
|
1070
|
+
body = await readBody(response, resolvedBodyType);
|
|
1071
|
+
} catch (e) {
|
|
1072
|
+
throw new InvalidResponseError(
|
|
1073
|
+
name,
|
|
1074
|
+
e,
|
|
1075
|
+
await readTextBody(response),
|
|
1076
|
+
void 0,
|
|
1077
|
+
void 0,
|
|
1078
|
+
response
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
if (!options.transformResponse) {
|
|
1082
|
+
return body;
|
|
1083
|
+
}
|
|
1084
|
+
try {
|
|
1085
|
+
return options.transformResponse(body, response);
|
|
1086
|
+
} catch (e) {
|
|
1087
|
+
throw new InvalidResponseError(name, e, body, void 0, void 0, response);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
async function executeRequest(options) {
|
|
1091
|
+
const { acceptedStatusCodes, statusValidator } = options;
|
|
1092
|
+
if (acceptedStatusCodes !== void 0 && statusValidator !== void 0) {
|
|
1093
|
+
throw new TypeError("acceptedStatusCodes and statusValidator are mutually exclusive.");
|
|
1094
|
+
}
|
|
1095
|
+
const validator = buildStatusValidator(acceptedStatusCodes, statusValidator);
|
|
1096
|
+
const client = options.httpClient ?? httpClient;
|
|
1097
|
+
let response;
|
|
1098
|
+
try {
|
|
1099
|
+
response = await client.send({
|
|
1100
|
+
url: options.url,
|
|
1101
|
+
method: options.method,
|
|
1102
|
+
headers: options.headers,
|
|
1103
|
+
body: options.body,
|
|
1104
|
+
requestBodyType: options.requestBodyType,
|
|
1105
|
+
abortSignal: options.abortSignal,
|
|
1106
|
+
statusValidator: validator
|
|
1107
|
+
});
|
|
1108
|
+
} catch (e) {
|
|
1109
|
+
if (isHttpClientAbortError(e) || isInvalidResponseError(e) || isClientRequestError(e)) {
|
|
1110
|
+
throw e;
|
|
1111
|
+
}
|
|
1112
|
+
if (!isHttpClientResponseError(e)) {
|
|
1113
|
+
throw new ApiClientError("UnexpectedError", "Unexpected error", e);
|
|
1114
|
+
}
|
|
1115
|
+
return await dispatchResponseError(e, options);
|
|
1116
|
+
}
|
|
1117
|
+
return decodeSuccessBody(response, options);
|
|
1118
|
+
}
|
|
1119
|
+
|
|
823
1120
|
// packages/http-client/src/lib/platform/oauth-http-client.ts
|
|
824
1121
|
var TOKEN_EXPIRATION_SAFETY_MARGIN_SECONDS = 1;
|
|
825
1122
|
var OAuthToken = class {
|
|
@@ -885,7 +1182,7 @@ var OAuthHttpClient = class {
|
|
|
885
1182
|
});
|
|
886
1183
|
const ok = statusValidator(response.status);
|
|
887
1184
|
if (ok) {
|
|
888
|
-
return new
|
|
1185
|
+
return new HttpClientResponse2(response, { requestUrl });
|
|
889
1186
|
} else {
|
|
890
1187
|
throw new HttpClientResponseError(response, {
|
|
891
1188
|
requestUrl,
|
package/docs/DOCS.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "@dynatrace-sdk/http-client"
|
|
3
|
-
description: An environment agnostic (browser/deno vs nodejs) abstraction layer for making HTTP calls.
|
|
3
|
+
description: "An environment agnostic (browser/deno vs nodejs) abstraction layer for making HTTP calls."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
|
|
@@ -13,7 +13,7 @@ import NpmLogo from '@site/static/img/npm-logo.png';
|
|
|
13
13
|
<a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client" target="_blank" rel="noopener noreferrer"> <span class="margin-right--xs">@dynatrace-sdk/http-client</span> <img className="no-zoom" width="20px" src={NpmLogo} /> </a>
|
|
14
14
|
</div>
|
|
15
15
|
<div class="col" style={{textAlign: 'right'}}>
|
|
16
|
-
<a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client/v/1.
|
|
16
|
+
<a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client/v/1.6.0" target="_blank" rel="noopener noreferrer">v1.6.0</a>
|
|
17
17
|
</div>
|
|
18
18
|
</div>
|
|
19
19
|
|
package/esm/index.js
CHANGED
|
@@ -34,12 +34,65 @@ function isHttpClientNetworkError(e) {
|
|
|
34
34
|
return e?.name === "NetworkError";
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// packages/http-client/src/lib/types/error-types.ts
|
|
38
|
+
var ErrorType = /* @__PURE__ */ ((ErrorType3) => {
|
|
39
|
+
ErrorType3["COMMON"] = "JS Error";
|
|
40
|
+
ErrorType3["HTTP"] = "Http Error";
|
|
41
|
+
return ErrorType3;
|
|
42
|
+
})(ErrorType || {});
|
|
43
|
+
|
|
37
44
|
// packages/http-client/src/lib/platform/abort-controller.ts
|
|
38
45
|
var AbortController = globalThis.AbortController;
|
|
39
46
|
|
|
40
47
|
// packages/http-client/src/lib/platform/abort-signal.ts
|
|
41
48
|
var AbortSignal = globalThis.AbortSignal;
|
|
42
49
|
|
|
50
|
+
// packages/http-client/src/lib/platform/api-client-error.ts
|
|
51
|
+
var ApiClientError = class extends Error {
|
|
52
|
+
constructor(name, message, cause) {
|
|
53
|
+
super(message ?? "ApiClientError: an unexpected error occurred.");
|
|
54
|
+
this.cause = cause;
|
|
55
|
+
this.name = name;
|
|
56
|
+
}
|
|
57
|
+
cause;
|
|
58
|
+
isApiClientError = true;
|
|
59
|
+
errorType = "JS Error" /* COMMON */;
|
|
60
|
+
};
|
|
61
|
+
function isApiClientError(e) {
|
|
62
|
+
return e?.isApiClientError === true && e instanceof Error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// packages/http-client/src/lib/platform/client-request-error.ts
|
|
66
|
+
var ClientRequestError = class extends ApiClientError {
|
|
67
|
+
isClientRequestError = true;
|
|
68
|
+
body;
|
|
69
|
+
response;
|
|
70
|
+
constructor(name, response, body, message, cause) {
|
|
71
|
+
super(name, message, cause);
|
|
72
|
+
this.errorType = "Http Error" /* HTTP */;
|
|
73
|
+
this.body = body;
|
|
74
|
+
this.response = response;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function isClientRequestError(e) {
|
|
78
|
+
return e?.isClientRequestError === true && isApiClientError(e);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// packages/http-client/src/lib/platform/api-gateway-error.ts
|
|
82
|
+
var ApiGatewayError = class extends ClientRequestError {
|
|
83
|
+
isApiGatewayError = true;
|
|
84
|
+
code;
|
|
85
|
+
retryAfterSeconds;
|
|
86
|
+
constructor(response, body) {
|
|
87
|
+
super(body.error.details?.errorCode || "ApiGatewayError", response, body, body.error.message);
|
|
88
|
+
this.code = body.error.code;
|
|
89
|
+
this.retryAfterSeconds = body.error.retryAfterSeconds;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
function isApiGatewayError(e) {
|
|
93
|
+
return e?.isApiGatewayError === true && isClientRequestError(e);
|
|
94
|
+
}
|
|
95
|
+
|
|
43
96
|
// packages/http-client/src/lib/platform/base-error.ts
|
|
44
97
|
var BaseError = class extends Error {
|
|
45
98
|
constructor(message, cause) {
|
|
@@ -111,6 +164,36 @@ var DataTypeError = class extends BaseError {
|
|
|
111
164
|
// packages/http-client/src/lib/platform/default-status-validator.ts
|
|
112
165
|
var defaultStatusValidator = (status) => 200 <= status && status < 300;
|
|
113
166
|
|
|
167
|
+
// packages/http-client/src/lib/platform/get-error-message.ts
|
|
168
|
+
function serializeData(data) {
|
|
169
|
+
try {
|
|
170
|
+
return JSON.stringify(data);
|
|
171
|
+
} catch {
|
|
172
|
+
return String(data);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function getMessagesFromErrorDetails(details) {
|
|
176
|
+
const messages = [];
|
|
177
|
+
Object.entries(details).forEach(([name, data]) => {
|
|
178
|
+
if (!data) return;
|
|
179
|
+
const serializedData = serializeData(data);
|
|
180
|
+
switch (name) {
|
|
181
|
+
case "missingScopes":
|
|
182
|
+
messages.push(`Missing scopes: ${serializedData}`);
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
messages.push(`${name}: ${serializedData}`);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
return messages;
|
|
189
|
+
}
|
|
190
|
+
function getErrorMessage(errorBody, defaultMessage) {
|
|
191
|
+
const error = errorBody;
|
|
192
|
+
const msg = error?.error?.message || defaultMessage;
|
|
193
|
+
const details = error?.error?.details || {};
|
|
194
|
+
return [msg, ...getMessagesFromErrorDetails(details)].join(". ");
|
|
195
|
+
}
|
|
196
|
+
|
|
114
197
|
// packages/http-client/src/lib/platform/encode-request-body.ts
|
|
115
198
|
function encodeTextRequestBody(body) {
|
|
116
199
|
return String(body);
|
|
@@ -598,7 +681,7 @@ function decodeResponseBody(response, responseBodyType) {
|
|
|
598
681
|
}
|
|
599
682
|
|
|
600
683
|
// packages/http-client/src/lib/platform/http-client-response.ts
|
|
601
|
-
var
|
|
684
|
+
var HttpClientResponse2 = class {
|
|
602
685
|
constructor(response, data) {
|
|
603
686
|
this.response = response;
|
|
604
687
|
this.requestUrl = data?.requestUrl ?? response.url;
|
|
@@ -629,7 +712,7 @@ var HttpClientResponse = class {
|
|
|
629
712
|
// packages/http-client/src/lib/platform/http-client-response-error-serializer.ts
|
|
630
713
|
import {
|
|
631
714
|
addGlobalErrorSerializer,
|
|
632
|
-
ErrorType,
|
|
715
|
+
ErrorType as ErrorType2,
|
|
633
716
|
isGlobalErrorSerializerSupported
|
|
634
717
|
} from "@dynatrace-sdk/error-handlers";
|
|
635
718
|
function getMessageFromObject(obj) {
|
|
@@ -684,7 +767,7 @@ var httpClientResponseErrorSerializer = async (error) => {
|
|
|
684
767
|
status,
|
|
685
768
|
message,
|
|
686
769
|
stack: error.stack,
|
|
687
|
-
type:
|
|
770
|
+
type: ErrorType2.HTTP,
|
|
688
771
|
details: {
|
|
689
772
|
url: error.response.url,
|
|
690
773
|
requestUrl: error.response.requestUrl,
|
|
@@ -721,7 +804,7 @@ var HttpClientResponseError = class extends BaseError {
|
|
|
721
804
|
constructor(response, data) {
|
|
722
805
|
addHttpClientResponseErrorSerializer();
|
|
723
806
|
super(`HTTP Error ${response.status}: ${response.statusText}`);
|
|
724
|
-
this.response = new
|
|
807
|
+
this.response = new HttpClientResponse2(response, {
|
|
725
808
|
requestUrl: data?.requestUrl ?? response.url
|
|
726
809
|
});
|
|
727
810
|
this.requestMethod = data?.requestMethod;
|
|
@@ -772,7 +855,7 @@ var HttpClient = class {
|
|
|
772
855
|
});
|
|
773
856
|
const ok = statusValidator(response.status);
|
|
774
857
|
if (ok) {
|
|
775
|
-
return new
|
|
858
|
+
return new HttpClientResponse2(response, { requestUrl: url });
|
|
776
859
|
} else {
|
|
777
860
|
throw new HttpClientResponseError(response, {
|
|
778
861
|
requestUrl: url,
|
|
@@ -783,6 +866,210 @@ var HttpClient = class {
|
|
|
783
866
|
};
|
|
784
867
|
var httpClient = new HttpClient();
|
|
785
868
|
|
|
869
|
+
// packages/http-client/src/lib/platform/invalid-response-error.ts
|
|
870
|
+
var InvalidResponseError = class extends ApiClientError {
|
|
871
|
+
isInvalidResponseError = true;
|
|
872
|
+
responseBody;
|
|
873
|
+
expectedType;
|
|
874
|
+
nestedError;
|
|
875
|
+
response;
|
|
876
|
+
constructor(name, nestedError, body, expectedType, message, response) {
|
|
877
|
+
super(
|
|
878
|
+
name,
|
|
879
|
+
message ?? `${name}: Response does not match expected datatype${expectedType ? " " + expectedType : ""}: ${nestedError?.toString() ?? "unable to deserialize"}`
|
|
880
|
+
);
|
|
881
|
+
this.nestedError = nestedError;
|
|
882
|
+
this.responseBody = body;
|
|
883
|
+
this.expectedType = expectedType;
|
|
884
|
+
this.response = response;
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
function isHttpClientDataTypeError(e) {
|
|
888
|
+
return e instanceof Error && e?.name === "DataTypeError";
|
|
889
|
+
}
|
|
890
|
+
function isInvalidResponseError(e) {
|
|
891
|
+
if (e?.isInvalidResponseError === true && isApiClientError(e)) {
|
|
892
|
+
return true;
|
|
893
|
+
}
|
|
894
|
+
return isHttpClientDataTypeError(e);
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// packages/http-client/src/lib/platform/execute-request.ts
|
|
898
|
+
var API_GATEWAY_RESPONSE_SOURCE_HEADER = "dynatrace-response-source";
|
|
899
|
+
var API_GATEWAY_RESPONSE_SOURCE = "API Gateway";
|
|
900
|
+
function buildStatusValidator(acceptedStatusCodes, statusValidator) {
|
|
901
|
+
if (acceptedStatusCodes !== void 0) {
|
|
902
|
+
return (status) => acceptedStatusCodes.includes(status);
|
|
903
|
+
}
|
|
904
|
+
return statusValidator ?? defaultStatusValidator;
|
|
905
|
+
}
|
|
906
|
+
function resolveBodyType(resolver, response) {
|
|
907
|
+
return typeof resolver === "function" ? resolver(response) : resolver;
|
|
908
|
+
}
|
|
909
|
+
function readBody(response, responseBodyType) {
|
|
910
|
+
return response.body(responseBodyType);
|
|
911
|
+
}
|
|
912
|
+
async function readTextBody(response) {
|
|
913
|
+
try {
|
|
914
|
+
return await response.body("text");
|
|
915
|
+
} catch {
|
|
916
|
+
return "";
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
function resolveErrorFactory(status, factories) {
|
|
920
|
+
const exact = factories?.[status];
|
|
921
|
+
if (exact) {
|
|
922
|
+
return { factory: exact, label: `${status}` };
|
|
923
|
+
}
|
|
924
|
+
const range = factories?.[`${Math.floor(status / 100)}XX`];
|
|
925
|
+
if (range) {
|
|
926
|
+
return { factory: range, label: `${status}` };
|
|
927
|
+
}
|
|
928
|
+
if (factories?.default) {
|
|
929
|
+
return { factory: factories.default, label: "err" };
|
|
930
|
+
}
|
|
931
|
+
return void 0;
|
|
932
|
+
}
|
|
933
|
+
async function buildApiGatewayError(response, operationId) {
|
|
934
|
+
if (response.headers?.[API_GATEWAY_RESPONSE_SOURCE_HEADER] !== API_GATEWAY_RESPONSE_SOURCE) {
|
|
935
|
+
return void 0;
|
|
936
|
+
}
|
|
937
|
+
let body;
|
|
938
|
+
try {
|
|
939
|
+
body = await response.body("json");
|
|
940
|
+
} catch (decodeErr) {
|
|
941
|
+
throw new InvalidResponseError(
|
|
942
|
+
`${operationId}:${response.status}`,
|
|
943
|
+
decodeErr,
|
|
944
|
+
await readTextBody(response),
|
|
945
|
+
void 0,
|
|
946
|
+
void 0,
|
|
947
|
+
response
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
return new ApiGatewayError(response, body);
|
|
951
|
+
}
|
|
952
|
+
async function buildFactoryError(e, { factory, label }, options) {
|
|
953
|
+
const { response } = e;
|
|
954
|
+
const name = `${options.operationId}:${label}`;
|
|
955
|
+
const expectedType = factory.errorEnvelope ?? options.errorEnvelope;
|
|
956
|
+
const bodyType = resolveBodyType(factory.responseBodyType ?? "json", response);
|
|
957
|
+
let body;
|
|
958
|
+
if (bodyType !== "none") {
|
|
959
|
+
try {
|
|
960
|
+
body = await readBody(response, bodyType);
|
|
961
|
+
} catch (decodeErr) {
|
|
962
|
+
throw new InvalidResponseError(
|
|
963
|
+
name,
|
|
964
|
+
decodeErr,
|
|
965
|
+
await readTextBody(response),
|
|
966
|
+
expectedType,
|
|
967
|
+
void 0,
|
|
968
|
+
response
|
|
969
|
+
);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
if (factory.transformBody) {
|
|
973
|
+
try {
|
|
974
|
+
body = factory.transformBody(body);
|
|
975
|
+
} catch (transformErr) {
|
|
976
|
+
throw new InvalidResponseError(name, transformErr, body, expectedType, void 0, response);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
return factory.build({ response, body, cause: e });
|
|
980
|
+
}
|
|
981
|
+
async function buildFallbackError(e) {
|
|
982
|
+
const { response } = e;
|
|
983
|
+
let responseValue;
|
|
984
|
+
try {
|
|
985
|
+
responseValue = await response.body("json");
|
|
986
|
+
} catch (decodeErr) {
|
|
987
|
+
throw new InvalidResponseError(
|
|
988
|
+
`${response.status}`,
|
|
989
|
+
decodeErr,
|
|
990
|
+
await readTextBody(response),
|
|
991
|
+
"json",
|
|
992
|
+
"Unexpected api response",
|
|
993
|
+
response
|
|
994
|
+
);
|
|
995
|
+
}
|
|
996
|
+
return new ClientRequestError(
|
|
997
|
+
`${response.status}`,
|
|
998
|
+
response,
|
|
999
|
+
responseValue,
|
|
1000
|
+
getErrorMessage(
|
|
1001
|
+
responseValue,
|
|
1002
|
+
`Unexpected api response: code=${response.status} body="${responseValue}"`
|
|
1003
|
+
),
|
|
1004
|
+
e
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
async function dispatchResponseError(e, options) {
|
|
1008
|
+
const gatewayError = await buildApiGatewayError(e.response, options.operationId);
|
|
1009
|
+
if (gatewayError) {
|
|
1010
|
+
throw gatewayError;
|
|
1011
|
+
}
|
|
1012
|
+
const resolved = resolveErrorFactory(e.response.status, options.errorFactories);
|
|
1013
|
+
throw resolved ? await buildFactoryError(e, resolved, options) : await buildFallbackError(e);
|
|
1014
|
+
}
|
|
1015
|
+
async function decodeSuccessBody(response, options) {
|
|
1016
|
+
const resolvedBodyType = resolveBodyType(options.responseBodyType ?? "json", response);
|
|
1017
|
+
if (resolvedBodyType === "none") {
|
|
1018
|
+
return void 0;
|
|
1019
|
+
}
|
|
1020
|
+
const name = `${options.operationId}:${response.status}`;
|
|
1021
|
+
let body;
|
|
1022
|
+
try {
|
|
1023
|
+
body = await readBody(response, resolvedBodyType);
|
|
1024
|
+
} catch (e) {
|
|
1025
|
+
throw new InvalidResponseError(
|
|
1026
|
+
name,
|
|
1027
|
+
e,
|
|
1028
|
+
await readTextBody(response),
|
|
1029
|
+
void 0,
|
|
1030
|
+
void 0,
|
|
1031
|
+
response
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
if (!options.transformResponse) {
|
|
1035
|
+
return body;
|
|
1036
|
+
}
|
|
1037
|
+
try {
|
|
1038
|
+
return options.transformResponse(body, response);
|
|
1039
|
+
} catch (e) {
|
|
1040
|
+
throw new InvalidResponseError(name, e, body, void 0, void 0, response);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
async function executeRequest(options) {
|
|
1044
|
+
const { acceptedStatusCodes, statusValidator } = options;
|
|
1045
|
+
if (acceptedStatusCodes !== void 0 && statusValidator !== void 0) {
|
|
1046
|
+
throw new TypeError("acceptedStatusCodes and statusValidator are mutually exclusive.");
|
|
1047
|
+
}
|
|
1048
|
+
const validator = buildStatusValidator(acceptedStatusCodes, statusValidator);
|
|
1049
|
+
const client = options.httpClient ?? httpClient;
|
|
1050
|
+
let response;
|
|
1051
|
+
try {
|
|
1052
|
+
response = await client.send({
|
|
1053
|
+
url: options.url,
|
|
1054
|
+
method: options.method,
|
|
1055
|
+
headers: options.headers,
|
|
1056
|
+
body: options.body,
|
|
1057
|
+
requestBodyType: options.requestBodyType,
|
|
1058
|
+
abortSignal: options.abortSignal,
|
|
1059
|
+
statusValidator: validator
|
|
1060
|
+
});
|
|
1061
|
+
} catch (e) {
|
|
1062
|
+
if (isHttpClientAbortError(e) || isInvalidResponseError(e) || isClientRequestError(e)) {
|
|
1063
|
+
throw e;
|
|
1064
|
+
}
|
|
1065
|
+
if (!isHttpClientResponseError(e)) {
|
|
1066
|
+
throw new ApiClientError("UnexpectedError", "Unexpected error", e);
|
|
1067
|
+
}
|
|
1068
|
+
return await dispatchResponseError(e, options);
|
|
1069
|
+
}
|
|
1070
|
+
return decodeSuccessBody(response, options);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
786
1073
|
// packages/http-client/src/lib/platform/oauth-http-client.ts
|
|
787
1074
|
var TOKEN_EXPIRATION_SAFETY_MARGIN_SECONDS = 1;
|
|
788
1075
|
var OAuthToken = class {
|
|
@@ -848,7 +1135,7 @@ var OAuthHttpClient = class {
|
|
|
848
1135
|
});
|
|
849
1136
|
const ok = statusValidator(response.status);
|
|
850
1137
|
if (ok) {
|
|
851
|
-
return new
|
|
1138
|
+
return new HttpClientResponse2(response, { requestUrl });
|
|
852
1139
|
} else {
|
|
853
1140
|
throw new HttpClientResponseError(response, {
|
|
854
1141
|
requestUrl,
|
|
@@ -882,10 +1169,20 @@ export {
|
|
|
882
1169
|
HttpClient as PlatformHttpClient,
|
|
883
1170
|
HttpClientAbortError as PlatformHttpClientAbortError,
|
|
884
1171
|
HttpClientRequestError as PlatformHttpClientRequestError,
|
|
885
|
-
|
|
1172
|
+
HttpClientResponse2 as PlatformHttpClientResponse,
|
|
886
1173
|
HttpClientResponseError as PlatformHttpClientResponseError,
|
|
887
1174
|
UnsupportedOperationError as PlatformUnsupportedOperationError,
|
|
1175
|
+
ApiClientError as _ApiClientError,
|
|
1176
|
+
ApiGatewayError as _ApiGatewayError,
|
|
1177
|
+
ClientRequestError as _ClientRequestError,
|
|
1178
|
+
ErrorType as _ErrorType,
|
|
1179
|
+
InvalidResponseError as _InvalidResponseError,
|
|
888
1180
|
OAuthHttpClient as _OAuthHttpClient,
|
|
1181
|
+
executeRequest as _executeRequest,
|
|
1182
|
+
isApiClientError as _isApiClientError,
|
|
1183
|
+
isApiGatewayError as _isApiGatewayError,
|
|
1184
|
+
isClientRequestError as _isClientRequestError,
|
|
1185
|
+
isInvalidResponseError as _isInvalidResponseError,
|
|
889
1186
|
httpClient,
|
|
890
1187
|
isHttpClientAbortError,
|
|
891
1188
|
isHttpClientNetworkError,
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -20,6 +20,40 @@ export { AbortController_2 as AbortController }
|
|
|
20
20
|
declare type AbortSignal_2 = HttpClientAbortSignal | globalThis.AbortSignal;
|
|
21
21
|
export { AbortSignal_2 as AbortSignal }
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Base error for all client SDKs. All other errors extend this class.
|
|
25
|
+
*/
|
|
26
|
+
export declare class _ApiClientError extends Error {
|
|
27
|
+
readonly cause?: any;
|
|
28
|
+
readonly isApiClientError = true;
|
|
29
|
+
errorType: _ErrorType;
|
|
30
|
+
constructor(name: string, message?: string, cause?: any);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Dedicated error response class for errors thrown by API Gateway.
|
|
35
|
+
* Autogenerated SDK Clients have built-in handler for API Gateway errors that throws this error.
|
|
36
|
+
*/
|
|
37
|
+
export declare class _ApiGatewayError extends _ClientRequestError<_ApiGatewayErrorResponseBody> {
|
|
38
|
+
readonly isApiGatewayError = true;
|
|
39
|
+
readonly code: number;
|
|
40
|
+
readonly retryAfterSeconds: number | undefined;
|
|
41
|
+
constructor(response: HttpClientResponse, body: _ApiGatewayErrorResponseBody);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Reference for *ApiGatewayError* details object. Contains information about received API Getaway error.
|
|
46
|
+
* */
|
|
47
|
+
export declare interface _ApiGatewayErrorDetails extends _CommonErrorDetails {
|
|
48
|
+
errorCodeProperties?: {
|
|
49
|
+
[key: string]: string;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export declare type _ApiGatewayErrorResponseBody = _ErrorResponseBody<_CommonApiError<_ApiGatewayErrorDetails> & {
|
|
54
|
+
retryAfterSeconds?: number;
|
|
55
|
+
}>;
|
|
56
|
+
|
|
23
57
|
export declare interface BaseError extends Error {
|
|
24
58
|
readonly name: string;
|
|
25
59
|
readonly cause?: any;
|
|
@@ -76,6 +110,106 @@ export declare interface BinaryFormDataResponseField {
|
|
|
76
110
|
value: Binary;
|
|
77
111
|
}
|
|
78
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Generic error class for service errors, used to handle both expected and unexpected service-level errors.
|
|
115
|
+
* @example Handling unexpected error response, received by the SDK Client
|
|
116
|
+
* const responseValue = await response.body('json');
|
|
117
|
+
*
|
|
118
|
+
* throw new ClientRequestError(
|
|
119
|
+
* `${response.status}`,
|
|
120
|
+
* response,
|
|
121
|
+
* responseValue,
|
|
122
|
+
* getErrorMessage(
|
|
123
|
+
* responseValue,
|
|
124
|
+
* `Unexpected api response: code=${response.status} body="${responseValue}"`,
|
|
125
|
+
* ),
|
|
126
|
+
* e,
|
|
127
|
+
* );
|
|
128
|
+
*
|
|
129
|
+
*/
|
|
130
|
+
export declare class _ClientRequestError<DTO = _ErrorResponseBody> extends _ApiClientError {
|
|
131
|
+
readonly isClientRequestError = true;
|
|
132
|
+
body: DTO;
|
|
133
|
+
response: HttpClientResponse;
|
|
134
|
+
constructor(name: string, response: HttpClientResponse, body: DTO, message?: string, cause?: any);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Common API error structure, established by Dynatrace API guidelines.
|
|
139
|
+
* */
|
|
140
|
+
export declare interface _CommonApiError<T extends _CommonErrorDetails & Record<string, any> = _CommonErrorDetails & Record<string, any>> {
|
|
141
|
+
/** The error code should be set to the HTTP error code by default.<br>
|
|
142
|
+
* The error code may be set to an API-specific error code which must be properly documented.*/
|
|
143
|
+
code: number;
|
|
144
|
+
/** The error message should be short and precise, it shouldn't contain details.*/
|
|
145
|
+
message: string;
|
|
146
|
+
/**
|
|
147
|
+
* An additional help field may be added which must be a URL to further information on how to deal with the error.<br>
|
|
148
|
+
* This may be some detailed error documentation page or a link to the Dynatrace support system, etc.
|
|
149
|
+
* */
|
|
150
|
+
help?: string;
|
|
151
|
+
/**
|
|
152
|
+
* Additional details about the error may be added in a details field.
|
|
153
|
+
* Refer to *CommonErrorDetails*
|
|
154
|
+
* */
|
|
155
|
+
details?: T;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* *CommonApiError* details object reference. Contains common API error information.
|
|
160
|
+
* This object is used to convey additional information about the error like e.g., which query parameter exactly violated a precondition. <br>
|
|
161
|
+
* Details may contain any fields to further describe the error
|
|
162
|
+
* */
|
|
163
|
+
export declare interface _CommonErrorDetails {
|
|
164
|
+
/**
|
|
165
|
+
* UUID string that represents a reference of the error into e.g., the log file of the service.
|
|
166
|
+
* */
|
|
167
|
+
errorRef?: string;
|
|
168
|
+
/**
|
|
169
|
+
* String containing a 32-character hex integer value that's used for tracing.
|
|
170
|
+
* */
|
|
171
|
+
traceId?: string;
|
|
172
|
+
/**
|
|
173
|
+
* String value representing a more detailed error information than the http response code alone.
|
|
174
|
+
* Must be a single word in CamelCase, and all possible values must be documented.
|
|
175
|
+
* */
|
|
176
|
+
errorCode?: string;
|
|
177
|
+
/**
|
|
178
|
+
* An array of *ConstraintViolation* object, refer to ConstraintViolation object reference for more details.
|
|
179
|
+
* */
|
|
180
|
+
constraintViolations?: _ConstraintViolation[];
|
|
181
|
+
/**
|
|
182
|
+
* Must be an array of strings containing a complete list of missing IAM scopes necessary to successfully execute the request.
|
|
183
|
+
* Should be set if the API returns a 403 - Forbidden response in case of missing OAuth scopes.
|
|
184
|
+
* */
|
|
185
|
+
missingScopes?: string[];
|
|
186
|
+
/**
|
|
187
|
+
* Must be an array of strings containing a complete list of missing IAM permissions necessary to successfully execute the request.
|
|
188
|
+
* Should be set if the API returns a 403 - Forbidden response in case of missing OAuth user permissions.
|
|
189
|
+
* */
|
|
190
|
+
missingPermissions?: string[];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Contains information about an input parameter (path, query or request body)
|
|
195
|
+
* that violated some validation rule of the service API and caused the warning.<br>
|
|
196
|
+
* May contain additional fields further describing the warning.
|
|
197
|
+
* */
|
|
198
|
+
export declare interface _ConstraintViolation extends Record<string, string | undefined> {
|
|
199
|
+
/**
|
|
200
|
+
* Mandatory field message describing the warning.
|
|
201
|
+
* */
|
|
202
|
+
message: string;
|
|
203
|
+
/**
|
|
204
|
+
* Describes the general location of the violating parameter (query parameter, request body, etc.)
|
|
205
|
+
*/
|
|
206
|
+
parameterLocation?: string;
|
|
207
|
+
/**
|
|
208
|
+
* Refers to the violating parameter within the parameterLocation.
|
|
209
|
+
*/
|
|
210
|
+
path?: string;
|
|
211
|
+
}
|
|
212
|
+
|
|
79
213
|
export declare interface DataTypeError extends BaseError {
|
|
80
214
|
/**
|
|
81
215
|
* The error name.
|
|
@@ -121,6 +255,99 @@ export declare type DataTypes = {
|
|
|
121
255
|
stream: stream.Readable;
|
|
122
256
|
};
|
|
123
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Basic error envelope, that enforces all error to have mandatory "error" property.
|
|
260
|
+
* */
|
|
261
|
+
export declare interface _ErrorResponseBody<T = _CommonApiError> {
|
|
262
|
+
/**
|
|
263
|
+
* *error* object must have two mandatory fields (*code, message*) and may have two additional fields (*help, details*):
|
|
264
|
+
* refer to the *CommonApiError* for more details.
|
|
265
|
+
* */
|
|
266
|
+
error: T;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Builds the typed error thrown for a response status, a status range or the `default` response.
|
|
271
|
+
*/
|
|
272
|
+
export declare interface _ErrorResponseFactory {
|
|
273
|
+
/** Body type used to decode the error payload. Defaults to 'json'. */
|
|
274
|
+
responseBodyType?: _ResponseBodyTypeResolver;
|
|
275
|
+
/** Transform applied to the decoded error body before `build` is called. */
|
|
276
|
+
transformBody?: (body: unknown) => unknown;
|
|
277
|
+
/**
|
|
278
|
+
* Name of this response's error schema, reported as `expectedType` when its body can't be
|
|
279
|
+
* decoded or transformed. Falls back to `ExecuteRequestOptions.errorEnvelope`.
|
|
280
|
+
*/
|
|
281
|
+
errorEnvelope?: string;
|
|
282
|
+
/** Constructs the final error thrown for this response. */
|
|
283
|
+
build: (ctx: {
|
|
284
|
+
response: HttpClientResponse;
|
|
285
|
+
body: unknown;
|
|
286
|
+
cause: unknown;
|
|
287
|
+
}) => Error;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** @ignore*/
|
|
291
|
+
export declare enum _ErrorType {
|
|
292
|
+
COMMON = "JS Error",
|
|
293
|
+
HTTP = "Http Error"
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Sends a request and dispatches the response the way generated SDK clients do:
|
|
298
|
+
* - The success body is decoded and optionally transformed.
|
|
299
|
+
* - Aborts, request-side `DataTypeError`s and `ClientRequestError`s are rethrown as-is; any other
|
|
300
|
+
* request failure is wrapped in `ApiClientError`.
|
|
301
|
+
* - API Gateway error responses throw `ApiGatewayError`.
|
|
302
|
+
* - Other error responses throw the error built by `errorFactories`, matched by exact status,
|
|
303
|
+
* then status range, then `default`. Without a match, the body is decoded as JSON and a
|
|
304
|
+
* generic `ClientRequestError` is thrown.
|
|
305
|
+
* - A body that can't be decoded or transformed throws `InvalidResponseError`, named
|
|
306
|
+
* `<operationId>:<status>` (`<operationId>:err` for the `default` response, `<status>` for the
|
|
307
|
+
* generic fallback).
|
|
308
|
+
*
|
|
309
|
+
* Unlike the inlined code it replaces, a body that fails to decode on the success, typed-error or
|
|
310
|
+
* API Gateway path throws `InvalidResponseError` (carrying the decode error as `nestedError` and
|
|
311
|
+
* the raw text body as `responseBody`) instead of the raw `DataTypeError`. `isInvalidResponseError`
|
|
312
|
+
* matches both.
|
|
313
|
+
*/
|
|
314
|
+
export declare function _executeRequest<Req extends keyof RequestBodyTypes = 'json', TResponse = unknown>(options: _ExecuteRequestOptions<Req, TResponse>): Promise<TResponse>;
|
|
315
|
+
|
|
316
|
+
export declare interface _ExecuteRequestOptions<Req extends keyof RequestBodyTypes = 'json', TResponse = unknown> {
|
|
317
|
+
url: string;
|
|
318
|
+
method?: string;
|
|
319
|
+
headers?: Headers_2;
|
|
320
|
+
body?: RequestBodyTypes[Req];
|
|
321
|
+
requestBodyType?: Req;
|
|
322
|
+
abortSignal?: AbortSignal_2;
|
|
323
|
+
/** Defaults to the exported `httpClient` singleton. */
|
|
324
|
+
httpClient?: HttpClient;
|
|
325
|
+
/** Shorthand: builds a StatusValidator from a list. Mutually exclusive with `statusValidator`. */
|
|
326
|
+
acceptedStatusCodes?: ReadonlyArray<number>;
|
|
327
|
+
statusValidator?: StatusValidator;
|
|
328
|
+
/** Fixed body type, resolver callback, or 'none' to skip decoding. Defaults to 'json'. */
|
|
329
|
+
responseBodyType?: _ResponseBodyTypeResolver;
|
|
330
|
+
/** Post-decode transform on the success body. */
|
|
331
|
+
transformResponse?: (body: unknown, response: HttpClientResponse) => TResponse;
|
|
332
|
+
/** Identifies the calling operation in thrown errors, e.g. `MyServiceClient.myMethod`. */
|
|
333
|
+
operationId: string;
|
|
334
|
+
/** Default name of the expected error schema, for factories that don't set their own `errorEnvelope`. */
|
|
335
|
+
errorEnvelope?: string;
|
|
336
|
+
/**
|
|
337
|
+
* Error builders, looked up by exact status first, then by status range (for example `4XX`), then
|
|
338
|
+
* `default`. A status none of them match throws a generic `ClientRequestError`.
|
|
339
|
+
*/
|
|
340
|
+
errorFactories?: {
|
|
341
|
+
[status: number]: _ErrorResponseFactory;
|
|
342
|
+
'1XX'?: _ErrorResponseFactory;
|
|
343
|
+
'2XX'?: _ErrorResponseFactory;
|
|
344
|
+
'3XX'?: _ErrorResponseFactory;
|
|
345
|
+
'4XX'?: _ErrorResponseFactory;
|
|
346
|
+
'5XX'?: _ErrorResponseFactory;
|
|
347
|
+
default?: _ErrorResponseFactory;
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
124
351
|
/**
|
|
125
352
|
* Represents a multipart/form-data body.
|
|
126
353
|
*/
|
|
@@ -325,6 +552,61 @@ export declare interface HttpClientResponseError extends BaseError {
|
|
|
325
552
|
readonly response: HttpClientResponse;
|
|
326
553
|
}
|
|
327
554
|
|
|
555
|
+
/**
|
|
556
|
+
* Dedicated error class for errors related to response serialization.
|
|
557
|
+
* Thrown when received service response can't be deserialized.
|
|
558
|
+
**/
|
|
559
|
+
export declare class _InvalidResponseError extends _ApiClientError {
|
|
560
|
+
readonly isInvalidResponseError = true;
|
|
561
|
+
responseBody: any;
|
|
562
|
+
expectedType?: string;
|
|
563
|
+
nestedError?: Error;
|
|
564
|
+
response: any;
|
|
565
|
+
constructor(name: string, nestedError: Error | any | undefined, body: any, expectedType?: string, message?: string, response?: any);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* ApiClientError type guard function.
|
|
570
|
+
* Does a structural check of the passed object.
|
|
571
|
+
* @example
|
|
572
|
+
* try {
|
|
573
|
+
* doSomething();
|
|
574
|
+
* } catch (e: unknown) {
|
|
575
|
+
* if(isApiClientError(e)) {
|
|
576
|
+
* handleTheError();
|
|
577
|
+
* }
|
|
578
|
+
* }
|
|
579
|
+
*/
|
|
580
|
+
export declare function _isApiClientError(e: any): e is _ApiClientError;
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* APIGatewayError type guard function.
|
|
584
|
+
* Does a structural check of the passed object.
|
|
585
|
+
* @example
|
|
586
|
+
* try {
|
|
587
|
+
* doSomething();
|
|
588
|
+
* } catch (e: unknown) {
|
|
589
|
+
* if(isApiClientError(e)) {
|
|
590
|
+
* handleTheError();
|
|
591
|
+
* }
|
|
592
|
+
* }
|
|
593
|
+
*/
|
|
594
|
+
export declare function _isApiGatewayError(e: any): e is _ApiGatewayError;
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* ClientRequestError type guard function.
|
|
598
|
+
* Does a structural check of the passed object.
|
|
599
|
+
* @example
|
|
600
|
+
* try {
|
|
601
|
+
* doSomething();
|
|
602
|
+
* } catch (e: unknown) {
|
|
603
|
+
* if(isClientRequestError(e)) {
|
|
604
|
+
* handleTheError();
|
|
605
|
+
* }
|
|
606
|
+
* }
|
|
607
|
+
*/
|
|
608
|
+
export declare function _isClientRequestError<T = _ErrorResponseBody>(e: any): e is _ClientRequestError<T>;
|
|
609
|
+
|
|
328
610
|
export declare function isHttpClientAbortError(e: any): e is HttpClientAbortError;
|
|
329
611
|
|
|
330
612
|
export declare function isHttpClientNetworkError(e: any): e is HttpClientNetworkError;
|
|
@@ -333,6 +615,26 @@ export declare function isHttpClientRequestError(e: any): e is HttpClientRequest
|
|
|
333
615
|
|
|
334
616
|
export declare function isHttpClientResponseError(e: any): e is HttpClientResponseError;
|
|
335
617
|
|
|
618
|
+
/**
|
|
619
|
+
* InvalidResponseError type guard function.
|
|
620
|
+
* Does a structural check of the passed object.
|
|
621
|
+
*
|
|
622
|
+
* Also returns `true` for `DataTypeError` instances thrown when a response body can't be
|
|
623
|
+
* decoded as the expected type (e.g. the server returned `text/html` but the SDK requested
|
|
624
|
+
* `application/json`). Treating those as `InvalidResponseError` lets app code handle "the
|
|
625
|
+
* response didn't match the expected schema" with a single type guard.
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* try {
|
|
629
|
+
* doSomething();
|
|
630
|
+
* } catch (e: unknown) {
|
|
631
|
+
* if (isInvalidResponseError(e)) {
|
|
632
|
+
* throw e;
|
|
633
|
+
* }
|
|
634
|
+
* }
|
|
635
|
+
*/
|
|
636
|
+
export declare function _isInvalidResponseError(e: any): e is _InvalidResponseError;
|
|
637
|
+
|
|
336
638
|
/**
|
|
337
639
|
* Represents a JSON field.
|
|
338
640
|
*/
|
|
@@ -493,6 +795,13 @@ export declare type RequestBodyTypes = {
|
|
|
493
795
|
binary: Binary | ArrayBuffer | Blob | ReadableStream | Buffer | stream.Readable;
|
|
494
796
|
};
|
|
495
797
|
|
|
798
|
+
/**
|
|
799
|
+
* Resolves the body type used to decode a response. A fixed type, `'none'` to skip decoding
|
|
800
|
+
* entirely, or a callback invoked with the response to resolve it dynamically, for example from
|
|
801
|
+
* its content type.
|
|
802
|
+
*/
|
|
803
|
+
export declare type _ResponseBodyTypeResolver = keyof ResponseBodyTypes | 'none' | ((response: HttpClientResponse) => keyof ResponseBodyTypes | 'none');
|
|
804
|
+
|
|
496
805
|
/**
|
|
497
806
|
* Mapping between the response body type names and types.
|
|
498
807
|
*/
|
|
@@ -599,7 +908,15 @@ declare namespace types {
|
|
|
599
908
|
RequestBodyTypes,
|
|
600
909
|
DataTypeError,
|
|
601
910
|
isHttpClientNetworkError,
|
|
602
|
-
HttpClientNetworkError
|
|
911
|
+
HttpClientNetworkError,
|
|
912
|
+
_ErrorType as ErrorType,
|
|
913
|
+
_ErrorResponseBody as ErrorResponseBody,
|
|
914
|
+
_CommonApiError as CommonApiError,
|
|
915
|
+
_CommonErrorDetails as CommonErrorDetails,
|
|
916
|
+
_ConstraintViolation as ConstraintViolation,
|
|
917
|
+
_ResponseBodyTypeResolver as ResponseBodyTypeResolver,
|
|
918
|
+
_ErrorResponseFactory as ErrorResponseFactory,
|
|
919
|
+
_ExecuteRequestOptions as ExecuteRequestOptions
|
|
603
920
|
}
|
|
604
921
|
}
|
|
605
922
|
|