unione 2.2.4 → 2.3.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/AGENTS.md +10 -10
- package/LICENSE +1 -1
- package/README.md +27 -7
- package/package.json +5 -2
- package/src/api/confirmed-email/ConfirmedEmailApi.d.ts +3 -3
- package/src/api/confirmed-email/models.d.ts +23 -2
- package/src/api/domain/DomainApi.d.ts +6 -6
- package/src/api/domain/models.d.ts +52 -3
- package/src/api/email/EmailApi.d.ts +3 -3
- package/src/api/email/models.d.ts +54 -8
- package/src/api/email-validation/EmailValidationApi.d.ts +2 -2
- package/src/api/email-validation/models.d.ts +6 -0
- package/src/api/event-dump/EventDumpApi.d.ts +4 -4
- package/src/api/event-dump/models.d.ts +51 -4
- package/src/api/obsolete/ObsoleteApi.d.ts +4 -4
- package/src/api/obsolete/models.d.ts +25 -1
- package/src/api/project/ProjectApi.d.ts +5 -5
- package/src/api/project/models.d.ts +42 -3
- package/src/api/suppression/SuppressionApi.d.ts +5 -5
- package/src/api/suppression/models.d.ts +34 -2
- package/src/api/system/models.d.ts +22 -2
- package/src/api/tag/TagApi.d.ts +2 -2
- package/src/api/tag/models.d.ts +13 -1
- package/src/api/template/TemplateApi.d.ts +5 -5
- package/src/api/template/models.d.ts +91 -13
- package/src/api/webhook/WebhookApi.d.ts +5 -5
- package/src/api/webhook/models.d.ts +102 -9
- package/src/client/ApiClient.js +47 -16
- package/src/client/ApiError.d.ts +34 -1
- package/src/client/ApiError.js +42 -3
- package/src/client/Configuration.js +1 -1
- package/src/client/errors.js +195 -0
- package/src/client/index.d.ts +1 -1
- package/src/client/index.js +1 -1
- package/src/common/models.d.ts +15 -0
- package/src/index.d.ts +1 -1
- package/src/index.js +1 -1
|
@@ -120,6 +120,17 @@ export interface WebhookSetRequestJSON {
|
|
|
120
120
|
"events"?: WebhookEventsObjectJSON;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
/** WebhookSetRequest in the API format as an argument: a nested model may also be given as a model. */
|
|
124
|
+
export interface WebhookSetRequestInput {
|
|
125
|
+
"url": string;
|
|
126
|
+
"status"?: WebhookStatus;
|
|
127
|
+
"event_format"?: WebhookEventFormat;
|
|
128
|
+
"delivery_info"?: number;
|
|
129
|
+
"single_event"?: number;
|
|
130
|
+
"max_parallel"?: number;
|
|
131
|
+
"events"?: WebhookEventsObject | WebhookEventsObjectInput;
|
|
132
|
+
}
|
|
133
|
+
|
|
123
134
|
/** Fields of new WebhookSetRequest(): a nested model may be a plain object in the API format. */
|
|
124
135
|
export interface WebhookSetRequestInit {
|
|
125
136
|
/**
|
|
@@ -166,7 +177,7 @@ export interface WebhookSetRequestInit {
|
|
|
166
177
|
*/
|
|
167
178
|
maxParallel?: number;
|
|
168
179
|
/** Object containing events to notify of. */
|
|
169
|
-
events?: WebhookEventsObject |
|
|
180
|
+
events?: WebhookEventsObject | WebhookEventsObjectInput;
|
|
170
181
|
}
|
|
171
182
|
|
|
172
183
|
/** WebhookSetRequest */
|
|
@@ -229,6 +240,9 @@ export interface WebhookEventsObjectJSON {
|
|
|
229
240
|
"email_status"?: Array<EmailStatus>;
|
|
230
241
|
}
|
|
231
242
|
|
|
243
|
+
/** WebhookEventsObject in the API format as an argument: a nested model may also be given as a model. */
|
|
244
|
+
export type WebhookEventsObjectInput = WebhookEventsObjectJSON;
|
|
245
|
+
|
|
232
246
|
/** Fields of new WebhookEventsObject(): a nested model may be a plain object in the API format. */
|
|
233
247
|
export interface WebhookEventsObjectInit {
|
|
234
248
|
/**
|
|
@@ -268,6 +282,12 @@ export interface WebhookSetResponseJSON {
|
|
|
268
282
|
"object"?: WebhookObjectJSON;
|
|
269
283
|
}
|
|
270
284
|
|
|
285
|
+
/** WebhookSetResponse in the API format as an argument: a nested model may also be given as a model. */
|
|
286
|
+
export interface WebhookSetResponseInput {
|
|
287
|
+
"status"?: string;
|
|
288
|
+
"object"?: WebhookObject | WebhookObjectInput;
|
|
289
|
+
}
|
|
290
|
+
|
|
271
291
|
/** Fields of new WebhookSetResponse(): a nested model may be a plain object in the API format. */
|
|
272
292
|
export interface WebhookSetResponseInit {
|
|
273
293
|
/**
|
|
@@ -276,7 +296,7 @@ export interface WebhookSetResponseInit {
|
|
|
276
296
|
*/
|
|
277
297
|
status?: string;
|
|
278
298
|
/** Object with all the webhook properties. */
|
|
279
|
-
object?: WebhookObject |
|
|
299
|
+
object?: WebhookObject | WebhookObjectInput;
|
|
280
300
|
}
|
|
281
301
|
|
|
282
302
|
/** WebhookSetResponse */
|
|
@@ -308,6 +328,19 @@ export interface WebhookObjectJSON {
|
|
|
308
328
|
"events"?: WebhookEventsObjectJSON;
|
|
309
329
|
}
|
|
310
330
|
|
|
331
|
+
/** WebhookObject in the API format as an argument: a nested model may also be given as a model. */
|
|
332
|
+
export interface WebhookObjectInput {
|
|
333
|
+
"id"?: number;
|
|
334
|
+
"url"?: string;
|
|
335
|
+
"status"?: WebhookStatus;
|
|
336
|
+
"event_format"?: WebhookEventFormat;
|
|
337
|
+
"delivery_info"?: number;
|
|
338
|
+
"single_event"?: number;
|
|
339
|
+
"max_parallel"?: number;
|
|
340
|
+
"updated_at"?: string;
|
|
341
|
+
"events"?: WebhookEventsObject | WebhookEventsObjectInput;
|
|
342
|
+
}
|
|
343
|
+
|
|
311
344
|
/** Fields of new WebhookObject(): a nested model may be a plain object in the API format. */
|
|
312
345
|
export interface WebhookObjectInit {
|
|
313
346
|
/** Webhook unique identifier. */
|
|
@@ -354,7 +387,7 @@ export interface WebhookObjectInit {
|
|
|
354
387
|
/** Webhook properties last update date and time in UTC timezone in "YYYY-MM-DD hh:mm:ss" format. */
|
|
355
388
|
updatedAt?: string;
|
|
356
389
|
/** Object containing events to notify of. */
|
|
357
|
-
events?: WebhookEventsObject |
|
|
390
|
+
events?: WebhookEventsObject | WebhookEventsObjectInput;
|
|
358
391
|
}
|
|
359
392
|
|
|
360
393
|
/** Object with all the webhook properties. */
|
|
@@ -417,6 +450,12 @@ export interface WebhookCallbackJSON {
|
|
|
417
450
|
"events_by_user"?: Array<WebhookCallbackEventsByUserItemJSON>;
|
|
418
451
|
}
|
|
419
452
|
|
|
453
|
+
/** WebhookCallback in the API format as an argument: a nested model may also be given as a model. */
|
|
454
|
+
export interface WebhookCallbackInput {
|
|
455
|
+
"auth"?: string;
|
|
456
|
+
"events_by_user"?: Array<WebhookCallbackEventsByUserItem | WebhookCallbackEventsByUserItemInput>;
|
|
457
|
+
}
|
|
458
|
+
|
|
420
459
|
/** Fields of new WebhookCallback(): a nested model may be a plain object in the API format. */
|
|
421
460
|
export interface WebhookCallbackInit {
|
|
422
461
|
/**
|
|
@@ -426,7 +465,7 @@ export interface WebhookCallbackInit {
|
|
|
426
465
|
*/
|
|
427
466
|
auth?: string;
|
|
428
467
|
/** Array with only one element, containing events of a user/project. */
|
|
429
|
-
eventsByUser?: Array<WebhookCallbackEventsByUserItem |
|
|
468
|
+
eventsByUser?: Array<WebhookCallbackEventsByUserItem | WebhookCallbackEventsByUserItemInput>;
|
|
430
469
|
}
|
|
431
470
|
|
|
432
471
|
/** Webhook callback format description. */
|
|
@@ -454,6 +493,14 @@ export interface WebhookCallbackEventsByUserItemJSON {
|
|
|
454
493
|
"events"?: Array<WebhookCallbackEventsByUserItemEventsItemJSON>;
|
|
455
494
|
}
|
|
456
495
|
|
|
496
|
+
/** WebhookCallbackEventsByUserItem in the API format as an argument: a nested model may also be given as a model. */
|
|
497
|
+
export interface WebhookCallbackEventsByUserItemInput {
|
|
498
|
+
"user_id"?: number;
|
|
499
|
+
"project_id"?: string;
|
|
500
|
+
"project_name"?: string;
|
|
501
|
+
"events"?: Array<WebhookCallbackEventsByUserItemEventsItem | WebhookCallbackEventsByUserItemEventsItemInput>;
|
|
502
|
+
}
|
|
503
|
+
|
|
457
504
|
/** Fields of new WebhookCallbackEventsByUserItem(): a nested model may be a plain object in the API format. */
|
|
458
505
|
export interface WebhookCallbackEventsByUserItemInit {
|
|
459
506
|
/** Unique user identifier. */
|
|
@@ -463,7 +510,7 @@ export interface WebhookCallbackEventsByUserItemInit {
|
|
|
463
510
|
/** Project name, present only if webhook was registered for the project using project API key. */
|
|
464
511
|
projectName?: string;
|
|
465
512
|
/** Array of events reported by webhook. */
|
|
466
|
-
events?: Array<WebhookCallbackEventsByUserItemEventsItem |
|
|
513
|
+
events?: Array<WebhookCallbackEventsByUserItemEventsItem | WebhookCallbackEventsByUserItemEventsItemInput>;
|
|
467
514
|
}
|
|
468
515
|
|
|
469
516
|
/** WebhookCallbackEventsByUserItem */
|
|
@@ -489,6 +536,12 @@ export interface WebhookCallbackEventsByUserItemEventsItemJSON {
|
|
|
489
536
|
"event_data"?: WebhookCallbackEventsByUserItemEventsItemEventDataJSON;
|
|
490
537
|
}
|
|
491
538
|
|
|
539
|
+
/** WebhookCallbackEventsByUserItemEventsItem in the API format as an argument: a nested model may also be given as a model. */
|
|
540
|
+
export interface WebhookCallbackEventsByUserItemEventsItemInput {
|
|
541
|
+
"event_name"?: EventName;
|
|
542
|
+
"event_data"?: WebhookCallbackEventsByUserItemEventsItemEventData | WebhookCallbackEventsByUserItemEventsItemEventDataInput;
|
|
543
|
+
}
|
|
544
|
+
|
|
492
545
|
/** Fields of new WebhookCallbackEventsByUserItemEventsItem(): a nested model may be a plain object in the API format. */
|
|
493
546
|
export interface WebhookCallbackEventsByUserItemEventsItemInit {
|
|
494
547
|
/**
|
|
@@ -500,7 +553,7 @@ export interface WebhookCallbackEventsByUserItemEventsItemInit {
|
|
|
500
553
|
* Object with different event properties depending on "event_name". Below you can see all the
|
|
501
554
|
* properties, "transactional_email_status"-related first and then "transactional_spam_block"-related.
|
|
502
555
|
*/
|
|
503
|
-
eventData?: WebhookCallbackEventsByUserItemEventsItemEventData |
|
|
556
|
+
eventData?: WebhookCallbackEventsByUserItemEventsItemEventData | WebhookCallbackEventsByUserItemEventsItemEventDataInput;
|
|
504
557
|
}
|
|
505
558
|
|
|
506
559
|
/** WebhookCallbackEventsByUserItemEventsItem */
|
|
@@ -538,6 +591,22 @@ export interface WebhookCallbackEventsByUserItemEventsItemEventDataJSON {
|
|
|
538
591
|
"domain_status"?: DomainStatus;
|
|
539
592
|
}
|
|
540
593
|
|
|
594
|
+
/** WebhookCallbackEventsByUserItemEventsItemEventData in the API format as an argument: a nested model may also be given as a model. */
|
|
595
|
+
export interface WebhookCallbackEventsByUserItemEventsItemEventDataInput {
|
|
596
|
+
"job_id"?: string;
|
|
597
|
+
"metadata"?: Record<string, string>;
|
|
598
|
+
"email"?: string;
|
|
599
|
+
"status"?: EmailStatus;
|
|
600
|
+
"event_time"?: string;
|
|
601
|
+
"url"?: string;
|
|
602
|
+
"delivery_info"?: WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfo | WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfoInput;
|
|
603
|
+
"block_time"?: string;
|
|
604
|
+
"block_type"?: BlockType;
|
|
605
|
+
"domain"?: string;
|
|
606
|
+
"SMTP_blocks_count"?: number;
|
|
607
|
+
"domain_status"?: DomainStatus;
|
|
608
|
+
}
|
|
609
|
+
|
|
541
610
|
/** Fields of new WebhookCallbackEventsByUserItemEventsItemEventData(): a nested model may be a plain object in the API format. */
|
|
542
611
|
export interface WebhookCallbackEventsByUserItemEventsItemEventDataInit {
|
|
543
612
|
/**
|
|
@@ -597,7 +666,7 @@ export interface WebhookCallbackEventsByUserItemEventsItemEventDataInit {
|
|
|
597
666
|
* format. This property exists only if webhook has delivery_info proeprty set to 1 and
|
|
598
667
|
* event_name="transactional_email_status".
|
|
599
668
|
*/
|
|
600
|
-
deliveryInfo?: WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfo |
|
|
669
|
+
deliveryInfo?: WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfo | WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfoInput;
|
|
601
670
|
/**
|
|
602
671
|
* Spam block date & time in UTC time zone in "YYYY-MM-DD hh:mm:ss" format. This property exists only
|
|
603
672
|
* if event_name="transactional_spam_block".
|
|
@@ -728,6 +797,9 @@ export interface WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfoJ
|
|
|
728
797
|
"sender_ip"?: string;
|
|
729
798
|
}
|
|
730
799
|
|
|
800
|
+
/** WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfo in the API format as an argument: a nested model may also be given as a model. */
|
|
801
|
+
export type WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfoInput = WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfoJSON;
|
|
802
|
+
|
|
731
803
|
/** Fields of new WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfo(): a nested model may be a plain object in the API format. */
|
|
732
804
|
export interface WebhookCallbackEventsByUserItemEventsItemEventDataDeliveryInfoInit {
|
|
733
805
|
/**
|
|
@@ -930,6 +1002,9 @@ export interface WebhookGetRequestJSON {
|
|
|
930
1002
|
"url": string;
|
|
931
1003
|
}
|
|
932
1004
|
|
|
1005
|
+
/** WebhookGetRequest in the API format as an argument: a nested model may also be given as a model. */
|
|
1006
|
+
export type WebhookGetRequestInput = WebhookGetRequestJSON;
|
|
1007
|
+
|
|
933
1008
|
/** Fields of new WebhookGetRequest(): a nested model may be a plain object in the API format. */
|
|
934
1009
|
export interface WebhookGetRequestInit {
|
|
935
1010
|
/** Webhook URL. */
|
|
@@ -953,6 +1028,12 @@ export interface WebhookGetResponseJSON {
|
|
|
953
1028
|
"object"?: WebhookObjectJSON;
|
|
954
1029
|
}
|
|
955
1030
|
|
|
1031
|
+
/** WebhookGetResponse in the API format as an argument: a nested model may also be given as a model. */
|
|
1032
|
+
export interface WebhookGetResponseInput {
|
|
1033
|
+
"status"?: string;
|
|
1034
|
+
"object"?: WebhookObject | WebhookObjectInput;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
956
1037
|
/** Fields of new WebhookGetResponse(): a nested model may be a plain object in the API format. */
|
|
957
1038
|
export interface WebhookGetResponseInit {
|
|
958
1039
|
/**
|
|
@@ -961,7 +1042,7 @@ export interface WebhookGetResponseInit {
|
|
|
961
1042
|
*/
|
|
962
1043
|
status?: string;
|
|
963
1044
|
/** Object with all the webhook properties. */
|
|
964
|
-
object?: WebhookObject |
|
|
1045
|
+
object?: WebhookObject | WebhookObjectInput;
|
|
965
1046
|
}
|
|
966
1047
|
|
|
967
1048
|
/** WebhookGetResponse */
|
|
@@ -986,6 +1067,9 @@ export interface WebhookListRequestJSON {
|
|
|
986
1067
|
"offset"?: number;
|
|
987
1068
|
}
|
|
988
1069
|
|
|
1070
|
+
/** WebhookListRequest in the API format as an argument: a nested model may also be given as a model. */
|
|
1071
|
+
export type WebhookListRequestInput = WebhookListRequestJSON;
|
|
1072
|
+
|
|
989
1073
|
/** Fields of new WebhookListRequest(): a nested model may be a plain object in the API format. */
|
|
990
1074
|
export interface WebhookListRequestInit {
|
|
991
1075
|
/** Limits the number of webhooks returned. 50 is recommended. */
|
|
@@ -1019,6 +1103,12 @@ export interface WebhookListResponseJSON {
|
|
|
1019
1103
|
"objects"?: Array<WebhookObjectJSON>;
|
|
1020
1104
|
}
|
|
1021
1105
|
|
|
1106
|
+
/** WebhookListResponse in the API format as an argument: a nested model may also be given as a model. */
|
|
1107
|
+
export interface WebhookListResponseInput {
|
|
1108
|
+
"status"?: string;
|
|
1109
|
+
"objects"?: Array<WebhookObject | WebhookObjectInput>;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1022
1112
|
/** Fields of new WebhookListResponse(): a nested model may be a plain object in the API format. */
|
|
1023
1113
|
export interface WebhookListResponseInit {
|
|
1024
1114
|
/**
|
|
@@ -1027,7 +1117,7 @@ export interface WebhookListResponseInit {
|
|
|
1027
1117
|
*/
|
|
1028
1118
|
status?: string;
|
|
1029
1119
|
/** Array of webhooks objects. */
|
|
1030
|
-
objects?: Array<WebhookObject |
|
|
1120
|
+
objects?: Array<WebhookObject | WebhookObjectInput>;
|
|
1031
1121
|
}
|
|
1032
1122
|
|
|
1033
1123
|
/** WebhookListResponse */
|
|
@@ -1051,6 +1141,9 @@ export interface WebhookDeleteRequestJSON {
|
|
|
1051
1141
|
"url": string;
|
|
1052
1142
|
}
|
|
1053
1143
|
|
|
1144
|
+
/** WebhookDeleteRequest in the API format as an argument: a nested model may also be given as a model. */
|
|
1145
|
+
export type WebhookDeleteRequestInput = WebhookDeleteRequestJSON;
|
|
1146
|
+
|
|
1054
1147
|
/** Fields of new WebhookDeleteRequest(): a nested model may be a plain object in the API format. */
|
|
1055
1148
|
export interface WebhookDeleteRequestInit {
|
|
1056
1149
|
/** Webhook URL. */
|
package/src/client/ApiClient.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ApiError } from "./ApiError.js";
|
|
1
|
+
import { ApiError, ErrorKind } from "./ApiError.js";
|
|
2
|
+
import { apiMessage, codeOf, hintOf, invalidRequest, methodOf, missingField, quote, transportError } from "./errors.js";
|
|
2
3
|
|
|
3
4
|
/** Removes undefined/null fields, used by the models' toJSON(). */
|
|
4
5
|
export function compact(object) {
|
|
@@ -34,7 +35,7 @@ export function extraFields(data, names) {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/** The SDK and its version in the "platform" query parameter of every request. */
|
|
37
|
-
export const PLATFORM = "node-sdk.2.
|
|
38
|
+
export const PLATFORM = "node-sdk.2.3.0";
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
41
|
* Sets a header, replacing one of the same name in another case.
|
|
@@ -51,9 +52,14 @@ function setHeader(headers, name, value) {
|
|
|
51
52
|
headers[name] = String(value);
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
/**
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
/**
|
|
56
|
+
* The error of the time limit of a request (name "TimeoutError", like the one of AbortSignal.timeout()).
|
|
57
|
+
* @param {number} timeout
|
|
58
|
+
*/
|
|
59
|
+
function timeoutError(timeout) {
|
|
60
|
+
const error = new Error(`timeout of ${timeout} ms exceeded`);
|
|
61
|
+
error.name = "TimeoutError";
|
|
62
|
+
return error;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
/**
|
|
@@ -70,7 +76,7 @@ async function fetchText(send, url, init, timeout, result) {
|
|
|
70
76
|
let timer;
|
|
71
77
|
const expired = new Promise((resolve, reject) => {
|
|
72
78
|
timer = setTimeout(() => {
|
|
73
|
-
reject(
|
|
79
|
+
reject(timeoutError(timeout));
|
|
74
80
|
controller.abort();
|
|
75
81
|
}, timeout);
|
|
76
82
|
});
|
|
@@ -129,16 +135,31 @@ export class ApiClient {
|
|
|
129
135
|
* @throws {ApiError}
|
|
130
136
|
*/
|
|
131
137
|
async call(path, body = {}) {
|
|
138
|
+
const method = methodOf(path);
|
|
139
|
+
if (!this.config.apiKey) {
|
|
140
|
+
throw invalidRequest(method, "the API key is empty: pass the API key of the user or of the project to the client");
|
|
141
|
+
}
|
|
142
|
+
let json;
|
|
143
|
+
try {
|
|
144
|
+
json = JSON.stringify(body ?? {});
|
|
145
|
+
} catch (error) {
|
|
146
|
+
throw invalidRequest(method, `can not encode the request: ${error instanceof Error ? error.message : String(error)}`,
|
|
147
|
+
error);
|
|
148
|
+
}
|
|
149
|
+
const missing = missingField(method, JSON.parse(json));
|
|
150
|
+
if (missing !== null) {
|
|
151
|
+
throw invalidRequest(method, `${missing} is required`);
|
|
152
|
+
}
|
|
132
153
|
const send = this.config.fetch || globalThis.fetch;
|
|
154
|
+
if (typeof send !== "function") {
|
|
155
|
+
throw invalidRequest(method, "fetch is not available: pass the fetch option of the configuration");
|
|
156
|
+
}
|
|
133
157
|
const result = { status: 0, text: "" };
|
|
134
158
|
try {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
await fetchText(send, this.url(path), { method: "POST", headers: this.headers(), body: JSON.stringify(body ?? {}) },
|
|
139
|
-
this.config.timeout, result);
|
|
159
|
+
await fetchText(send, this.url(path), { method: "POST", headers: this.headers(), body: json }, this.config.timeout,
|
|
160
|
+
result);
|
|
140
161
|
} catch (error) {
|
|
141
|
-
throw
|
|
162
|
+
throw transportError(method, error, this.config.timeout, result.status);
|
|
142
163
|
}
|
|
143
164
|
const { status, text: raw } = result;
|
|
144
165
|
|
|
@@ -146,21 +167,31 @@ export class ApiClient {
|
|
|
146
167
|
try {
|
|
147
168
|
data = JSON.parse(raw);
|
|
148
169
|
} catch (error) {
|
|
149
|
-
throw new ApiError(
|
|
170
|
+
throw new ApiError(`${method}: invalid JSON in response (HTTP ${status}): ${quote(raw)}`, {
|
|
150
171
|
httpStatus: status,
|
|
151
172
|
cause: error,
|
|
173
|
+
method,
|
|
174
|
+
kind: ErrorKind.INVALID_RESPONSE,
|
|
152
175
|
});
|
|
153
176
|
}
|
|
154
177
|
if (data === null || typeof data !== "object" || Array.isArray(data)) {
|
|
155
|
-
throw new ApiError(
|
|
178
|
+
throw new ApiError(`${method}: unexpected response (HTTP ${status}): ${quote(raw)}`, {
|
|
156
179
|
httpStatus: status,
|
|
180
|
+
method,
|
|
181
|
+
kind: ErrorKind.INVALID_RESPONSE,
|
|
157
182
|
});
|
|
158
183
|
}
|
|
159
184
|
if (status >= 400 || data.status === "error") {
|
|
160
|
-
|
|
185
|
+
const code = codeOf(data.code);
|
|
186
|
+
const message = data.message === undefined || data.message === null ? null : String(data.message);
|
|
187
|
+
throw new ApiError(apiMessage(method, message ?? "", status, code), {
|
|
161
188
|
httpStatus: status,
|
|
162
|
-
errorCode:
|
|
189
|
+
errorCode: code,
|
|
163
190
|
responseBody: data,
|
|
191
|
+
method,
|
|
192
|
+
hint: hintOf(code),
|
|
193
|
+
kind: ErrorKind.API,
|
|
194
|
+
apiMessage: message,
|
|
164
195
|
});
|
|
165
196
|
}
|
|
166
197
|
return data;
|
package/src/client/ApiError.d.ts
CHANGED
|
@@ -1,12 +1,35 @@
|
|
|
1
|
+
/** What failed: ApiError.kind. */
|
|
2
|
+
export declare const ErrorKind: Readonly<{
|
|
3
|
+
/** The API returned an error (errorCode, hint). */
|
|
4
|
+
API: "api";
|
|
5
|
+
/** The request could not be sent or the response could not be received. */
|
|
6
|
+
NETWORK: "network";
|
|
7
|
+
/** The request timed out. */
|
|
8
|
+
TIMEOUT: "timeout";
|
|
9
|
+
/** The response is not the JSON of the API (a proxy page, a broken body). */
|
|
10
|
+
INVALID_RESPONSE: "invalid_response";
|
|
11
|
+
/** Found before sending (an empty API key, a required field missing): no request was sent. */
|
|
12
|
+
INVALID_REQUEST: "invalid_request";
|
|
13
|
+
}>;
|
|
14
|
+
export type ErrorKind = (typeof ErrorKind)[keyof typeof ErrorKind];
|
|
15
|
+
|
|
1
16
|
/** Options of ApiError. */
|
|
2
17
|
export interface ApiErrorOptions {
|
|
3
18
|
httpStatus?: number;
|
|
4
19
|
errorCode?: number | null;
|
|
5
20
|
responseBody?: Record<string, any> | null;
|
|
6
21
|
cause?: unknown;
|
|
22
|
+
method?: string;
|
|
23
|
+
hint?: string | null;
|
|
24
|
+
kind?: ErrorKind;
|
|
25
|
+
apiMessage?: string | null;
|
|
7
26
|
}
|
|
8
27
|
|
|
9
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Error thrown when the API returns an error, the request fails or is invalid, or the response can not be processed.
|
|
30
|
+
* The message names the API method and, for an API error, the code, the HTTP status and how to fix it:
|
|
31
|
+
* "email/send: Unknown recipient (API error 204, HTTP 400). Fix: ... See ...#api-errors".
|
|
32
|
+
*/
|
|
10
33
|
export declare class ApiError extends Error {
|
|
11
34
|
constructor(message: string, options?: ApiErrorOptions);
|
|
12
35
|
/** HTTP status of the response, 0 when the request was not sent. */
|
|
@@ -17,4 +40,14 @@ export declare class ApiError extends Error {
|
|
|
17
40
|
responseBody: Record<string, any> | null;
|
|
18
41
|
/** The error that caused this one (network error, invalid JSON, ...). */
|
|
19
42
|
cause?: unknown;
|
|
43
|
+
/** API method of the request, e.g. "email/send"; empty when unknown. */
|
|
44
|
+
method: string;
|
|
45
|
+
/** How to fix the API error (the errors table of the docs), null when unknown. */
|
|
46
|
+
hint: string | null;
|
|
47
|
+
/** What failed. */
|
|
48
|
+
kind: ErrorKind;
|
|
49
|
+
/** The API's own error message (the "message" field of the error response, in full), null without one. */
|
|
50
|
+
apiMessage: string | null;
|
|
51
|
+
/** A repeat of the request may succeed: a network error, a timeout, HTTP 429 or 5xx. */
|
|
52
|
+
readonly retryable: boolean;
|
|
20
53
|
}
|
package/src/client/ApiError.js
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** What failed: ApiError.kind. */
|
|
2
|
+
export const ErrorKind = Object.freeze({
|
|
3
|
+
/** The API returned an error (errorCode, hint). */
|
|
4
|
+
API: "api",
|
|
5
|
+
/** The request could not be sent or the response could not be received. */
|
|
6
|
+
NETWORK: "network",
|
|
7
|
+
/** The request timed out. */
|
|
8
|
+
TIMEOUT: "timeout",
|
|
9
|
+
/** The response is not the JSON of the API (a proxy page, a broken body). */
|
|
10
|
+
INVALID_RESPONSE: "invalid_response",
|
|
11
|
+
/** Found before sending (an empty API key, a required field missing): no request was sent. */
|
|
12
|
+
INVALID_REQUEST: "invalid_request",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Error thrown when the API returns an error, the request fails or is invalid, or the response can not be processed.
|
|
17
|
+
* The message names the API method and, for an API error, the code, the HTTP status and how to fix it:
|
|
18
|
+
* "email/send: Unknown recipient (API error 204, HTTP 400). Fix: ... See ...#api-errors".
|
|
19
|
+
*/
|
|
2
20
|
export class ApiError extends Error {
|
|
3
21
|
/**
|
|
4
22
|
* @param {string} message
|
|
5
|
-
* @param {{httpStatus?: number, errorCode?: number|null, responseBody?: object|null, cause?: unknown
|
|
23
|
+
* @param {{httpStatus?: number, errorCode?: number|null, responseBody?: object|null, cause?: unknown,
|
|
24
|
+
* method?: string, hint?: string|null, kind?: string, apiMessage?: string|null}} [options]
|
|
6
25
|
*/
|
|
7
|
-
constructor(message, { httpStatus = 0, errorCode = null, responseBody = null, cause
|
|
26
|
+
constructor(message, { httpStatus = 0, errorCode = null, responseBody = null, cause, method = "", hint = null,
|
|
27
|
+
kind = ErrorKind.API, apiMessage = null } = {}) {
|
|
8
28
|
super(message, cause === undefined ? undefined : { cause });
|
|
9
29
|
this.name = "ApiError";
|
|
10
30
|
/** HTTP status of the response, 0 when the request was not sent. */
|
|
@@ -13,5 +33,24 @@ export class ApiError extends Error {
|
|
|
13
33
|
this.errorCode = errorCode;
|
|
14
34
|
/** Decoded error response. */
|
|
15
35
|
this.responseBody = responseBody;
|
|
36
|
+
/** API method of the request, e.g. "email/send"; empty when unknown. */
|
|
37
|
+
this.method = method;
|
|
38
|
+
/** How to fix the API error (the errors table of the docs), null when unknown. */
|
|
39
|
+
this.hint = hint;
|
|
40
|
+
/** What failed: one of ErrorKind. */
|
|
41
|
+
this.kind = kind;
|
|
42
|
+
/** The API's own error message (the "message" field of the error response, in full), null without one. */
|
|
43
|
+
this.apiMessage = apiMessage;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A repeat of the request may succeed: a network error, a timeout, HTTP 429 or 5xx. */
|
|
47
|
+
get retryable() {
|
|
48
|
+
if (this.kind === ErrorKind.NETWORK || this.kind === ErrorKind.TIMEOUT) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
if (this.kind === ErrorKind.API || this.kind === ErrorKind.INVALID_RESPONSE) {
|
|
52
|
+
return this.httpStatus === 429 || this.httpStatus >= 500;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
16
55
|
}
|
|
17
56
|
}
|
|
@@ -36,7 +36,7 @@ export class Configuration {
|
|
|
36
36
|
* that uses the SDK and its version (e.g. "express.1.0.0"), sent in the "platform" query parameter before the SDK
|
|
37
37
|
* one (PLATFORM); headers: extra headers of every request; fetch: a fetch-compatible function, default: globalThis.fetch.
|
|
38
38
|
*/
|
|
39
|
-
constructor(apiKey, { baseUrl = DEFAULT_BASE_URL, timeout = 30000, userAgent = "unione-node-sdk/2.
|
|
39
|
+
constructor(apiKey, { baseUrl = DEFAULT_BASE_URL, timeout = 30000, userAgent = "unione-node-sdk/2.3.0", platform, headers = {}, fetch } = {}) {
|
|
40
40
|
this.apiKey = apiKey;
|
|
41
41
|
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
42
42
|
this.timeout = timeout;
|