@onesignal/node-onesignal 5.7.0 → 5.9.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/README.md +9 -0
- package/dist/apis/DefaultApi.js +2282 -4372
- package/dist/apis/baseapi.js +11 -30
- package/dist/apis/exception.d.ts +1 -0
- package/dist/apis/exception.js +52 -26
- package/dist/auth/auth.js +17 -83
- package/dist/configuration.js +9 -9
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +11 -0
- package/dist/helpers.d.ts +12 -0
- package/dist/helpers.js +79 -0
- package/dist/http/http.js +53 -132
- package/dist/http/isomorphic-fetch.js +15 -18
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/middleware.js +8 -9
- package/dist/models/ApiKeyToken.js +45 -46
- package/dist/models/ApiKeyTokensListResponse.js +15 -16
- package/dist/models/App.js +213 -214
- package/dist/models/BasicNotification.js +711 -712
- package/dist/models/BasicNotificationAllOf.js +627 -628
- package/dist/models/BasicNotificationAllOfAndroidBackgroundLayout.js +27 -28
- package/dist/models/Button.js +27 -28
- package/dist/models/CopyTemplateRequest.js +15 -16
- package/dist/models/CreateApiKeyRequest.js +27 -28
- package/dist/models/CreateApiKeyResponse.js +21 -22
- package/dist/models/CreateNotificationSuccessResponse.js +27 -28
- package/dist/models/CreateSegmentConflictResponse.js +21 -22
- package/dist/models/CreateSegmentSuccessResponse.js +21 -22
- package/dist/models/CreateTemplateRequest.js +75 -76
- package/dist/models/CreateUserConflictResponse.js +15 -16
- package/dist/models/CreateUserConflictResponseErrorsInner.js +27 -28
- package/dist/models/CreateUserConflictResponseErrorsItemsMeta.js +15 -16
- package/dist/models/CustomEvent.js +39 -40
- package/dist/models/CustomEventsRequest.js +15 -16
- package/dist/models/DeliveryData.js +39 -40
- package/dist/models/ExportEventsSuccessResponse.js +15 -16
- package/dist/models/ExportSubscriptionsRequestBody.js +27 -28
- package/dist/models/ExportSubscriptionsSuccessResponse.js +15 -16
- package/dist/models/Filter.js +57 -58
- package/dist/models/FilterExpression.js +63 -64
- package/dist/models/GenericError.js +27 -28
- package/dist/models/GenericSuccessBoolResponse.js +15 -16
- package/dist/models/GetNotificationHistoryRequestBody.js +27 -28
- package/dist/models/GetSegmentsSuccessResponse.js +33 -34
- package/dist/models/LanguageStringMap.js +267 -268
- package/dist/models/Notification.js +717 -718
- package/dist/models/NotificationAllOf.js +15 -16
- package/dist/models/NotificationHistorySuccessResponse.js +21 -22
- package/dist/models/NotificationSlice.js +33 -34
- package/dist/models/NotificationTarget.js +93 -94
- package/dist/models/NotificationWithMeta.js +789 -790
- package/dist/models/NotificationWithMetaAllOf.js +93 -94
- package/dist/models/ObjectSerializer.js +111 -115
- package/dist/models/Operator.js +15 -16
- package/dist/models/OutcomeData.js +27 -28
- package/dist/models/OutcomesData.js +15 -16
- package/dist/models/PlatformDeliveryData.js +57 -58
- package/dist/models/PlatformDeliveryDataEmailAllOf.js +51 -52
- package/dist/models/PlatformDeliveryDataSmsAllOf.js +27 -28
- package/dist/models/PropertiesBody.js +15 -16
- package/dist/models/PropertiesDeltas.js +27 -28
- package/dist/models/PropertiesObject.js +75 -76
- package/dist/models/Purchase.js +33 -34
- package/dist/models/RateLimitError.js +21 -22
- package/dist/models/Segment.js +27 -28
- package/dist/models/SegmentData.js +51 -52
- package/dist/models/SegmentNotificationTarget.js +21 -22
- package/dist/models/StartLiveActivityRequest.js +105 -106
- package/dist/models/StartLiveActivitySuccessResponse.js +15 -16
- package/dist/models/Subscription.js +111 -112
- package/dist/models/SubscriptionBody.js +15 -16
- package/dist/models/SubscriptionNotificationTarget.js +81 -82
- package/dist/models/TemplateResource.js +45 -46
- package/dist/models/TemplatesListResponse.js +15 -16
- package/dist/models/TransferSubscriptionRequestBody.js +15 -16
- package/dist/models/UpdateApiKeyRequest.js +27 -28
- package/dist/models/UpdateLiveActivityRequest.js +63 -64
- package/dist/models/UpdateLiveActivitySuccessResponse.js +15 -16
- package/dist/models/UpdateTemplateRequest.js +69 -70
- package/dist/models/UpdateUserRequest.js +27 -28
- package/dist/models/User.js +27 -28
- package/dist/models/UserIdentityBody.js +15 -16
- package/dist/models/WebButton.js +33 -34
- package/dist/rxjsStub.js +8 -9
- package/dist/servers.js +14 -15
- package/dist/types/ObjectParamAPI.js +92 -94
- package/dist/types/ObservableAPI.js +534 -931
- package/dist/types/PromiseAPI.d.ts +4 -0
- package/dist/types/PromiseAPI.js +141 -137
- package/dist/util.js +2 -2
- package/package.json +4 -2
package/dist/apis/baseapi.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.RequiredError = exports.BaseAPIRequestFactory = exports.COLLECTION_FORMATS = void 0;
|
|
19
4
|
exports.COLLECTION_FORMATS = {
|
|
@@ -22,25 +7,21 @@ exports.COLLECTION_FORMATS = {
|
|
|
22
7
|
tsv: "\t",
|
|
23
8
|
pipes: "|",
|
|
24
9
|
};
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
class BaseAPIRequestFactory {
|
|
11
|
+
constructor(configuration) {
|
|
27
12
|
this.configuration = configuration;
|
|
28
13
|
}
|
|
29
|
-
|
|
30
|
-
}());
|
|
14
|
+
}
|
|
31
15
|
exports.BaseAPIRequestFactory = BaseAPIRequestFactory;
|
|
32
16
|
;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
_this.name = "RequiredError";
|
|
41
|
-
return _this;
|
|
17
|
+
class RequiredError extends Error {
|
|
18
|
+
constructor(api, method, field) {
|
|
19
|
+
super("Required parameter " + field + " was null or undefined when calling " + api + "." + method + ".");
|
|
20
|
+
this.api = api;
|
|
21
|
+
this.method = method;
|
|
22
|
+
this.field = field;
|
|
23
|
+
this.name = "RequiredError";
|
|
42
24
|
}
|
|
43
|
-
|
|
44
|
-
}(Error));
|
|
25
|
+
}
|
|
45
26
|
exports.RequiredError = RequiredError;
|
|
46
27
|
//# sourceMappingURL=baseapi.js.map
|
package/dist/apis/exception.d.ts
CHANGED
package/dist/apis/exception.js
CHANGED
|
@@ -1,32 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.ApiException = void 0;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return _this;
|
|
4
|
+
class ApiException extends Error {
|
|
5
|
+
constructor(code, message, body, headers) {
|
|
6
|
+
super("HTTP-Code: " + code + "\nMessage: " + message + "\nBody: " + JSON.stringify(body) + "\nHeaders: " +
|
|
7
|
+
JSON.stringify(headers));
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.body = body;
|
|
10
|
+
this.headers = headers;
|
|
11
|
+
Object.setPrototypeOf(this, ApiException.prototype);
|
|
28
12
|
}
|
|
29
|
-
|
|
30
|
-
|
|
13
|
+
get errorMessages() {
|
|
14
|
+
let parsed = this.body;
|
|
15
|
+
if (typeof parsed === "string") {
|
|
16
|
+
try {
|
|
17
|
+
parsed = JSON.parse(parsed);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (parsed === null || typeof parsed !== "object") {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const errors = parsed.errors;
|
|
27
|
+
if (typeof errors === "string") {
|
|
28
|
+
return [errors];
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(errors)) {
|
|
31
|
+
return errors
|
|
32
|
+
.map((e) => {
|
|
33
|
+
if (typeof e === "string") {
|
|
34
|
+
return e;
|
|
35
|
+
}
|
|
36
|
+
if (e !== null && typeof e === "object") {
|
|
37
|
+
const title = typeof e.title === "string" ? e.title : undefined;
|
|
38
|
+
const code = typeof e.code === "string" ? e.code : (e.code != null ? String(e.code) : undefined);
|
|
39
|
+
return title || code;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
})
|
|
43
|
+
.filter((m) => typeof m === "string");
|
|
44
|
+
}
|
|
45
|
+
if (errors !== null && typeof errors === "object") {
|
|
46
|
+
return Object.keys(errors)
|
|
47
|
+
.map((key) => {
|
|
48
|
+
const value = errors[key];
|
|
49
|
+
const rendered = typeof value === "string" ? value : JSON.stringify(value);
|
|
50
|
+
return key + ": " + rendered;
|
|
51
|
+
})
|
|
52
|
+
.sort();
|
|
53
|
+
}
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
31
57
|
exports.ApiException = ApiException;
|
|
32
58
|
//# sourceMappingURL=exception.js.map
|
package/dist/auth/auth.js
CHANGED
|
@@ -1,98 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.configureAuthMethods = exports.RestApiKeyAuthentication = exports.OrganizationApiKeyAuthentication = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
class OrganizationApiKeyAuthentication {
|
|
5
|
+
constructor(tokenProvider) {
|
|
42
6
|
this.tokenProvider = tokenProvider;
|
|
43
7
|
}
|
|
44
|
-
|
|
8
|
+
getName() {
|
|
45
9
|
return "organization_api_key";
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
switch (_e.label) {
|
|
52
|
-
case 0:
|
|
53
|
-
_b = (_a = context).setHeaderParam;
|
|
54
|
-
_c = ["Authorization"];
|
|
55
|
-
_d = "Key ";
|
|
56
|
-
return [4, this.tokenProvider.getToken()];
|
|
57
|
-
case 1:
|
|
58
|
-
_b.apply(_a, _c.concat([_d + (_e.sent())]));
|
|
59
|
-
return [2];
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
return OrganizationApiKeyAuthentication;
|
|
65
|
-
}());
|
|
10
|
+
}
|
|
11
|
+
async applySecurityAuthentication(context) {
|
|
12
|
+
context.setHeaderParam("Authorization", "Key " + await this.tokenProvider.getToken());
|
|
13
|
+
}
|
|
14
|
+
}
|
|
66
15
|
exports.OrganizationApiKeyAuthentication = OrganizationApiKeyAuthentication;
|
|
67
|
-
|
|
68
|
-
|
|
16
|
+
class RestApiKeyAuthentication {
|
|
17
|
+
constructor(tokenProvider) {
|
|
69
18
|
this.tokenProvider = tokenProvider;
|
|
70
19
|
}
|
|
71
|
-
|
|
20
|
+
getName() {
|
|
72
21
|
return "rest_api_key";
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
switch (_e.label) {
|
|
79
|
-
case 0:
|
|
80
|
-
_b = (_a = context).setHeaderParam;
|
|
81
|
-
_c = ["Authorization"];
|
|
82
|
-
_d = "Key ";
|
|
83
|
-
return [4, this.tokenProvider.getToken()];
|
|
84
|
-
case 1:
|
|
85
|
-
_b.apply(_a, _c.concat([_d + (_e.sent())]));
|
|
86
|
-
return [2];
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
return RestApiKeyAuthentication;
|
|
92
|
-
}());
|
|
22
|
+
}
|
|
23
|
+
async applySecurityAuthentication(context) {
|
|
24
|
+
context.setHeaderParam("Authorization", "Key " + await this.tokenProvider.getToken());
|
|
25
|
+
}
|
|
26
|
+
}
|
|
93
27
|
exports.RestApiKeyAuthentication = RestApiKeyAuthentication;
|
|
94
28
|
function configureAuthMethods(config) {
|
|
95
|
-
|
|
29
|
+
let authMethods = {};
|
|
96
30
|
if (!config) {
|
|
97
31
|
return authMethods;
|
|
98
32
|
}
|
package/dist/configuration.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createConfiguration = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const middleware_1 = require("./middleware");
|
|
5
|
+
const isomorphic_fetch_1 = require("./http/isomorphic-fetch");
|
|
6
|
+
const servers_1 = require("./servers");
|
|
7
|
+
const auth_1 = require("./auth/auth");
|
|
8
8
|
function createConfiguration(conf) {
|
|
9
|
-
|
|
9
|
+
const authMethods = {
|
|
10
10
|
'organization_api_key': {
|
|
11
11
|
tokenProvider: {
|
|
12
|
-
getToken:
|
|
12
|
+
getToken: () => conf.organizationApiKey || ''
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
'rest_api_key': {
|
|
16
16
|
tokenProvider: {
|
|
17
|
-
getToken:
|
|
17
|
+
getToken: () => conf.restApiKey || ''
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
const configuration = {
|
|
22
22
|
baseServer: conf.baseServer !== undefined ? conf.baseServer : servers_1.server1,
|
|
23
23
|
httpApi: conf.httpApi || new isomorphic_fetch_1.IsomorphicFetchHttpLibrary(),
|
|
24
24
|
middleware: conf.middleware || [],
|
|
25
25
|
authMethods: (0, auth_1.configureAuthMethods)(Object.assign(authMethods, conf.authMethods || {}))
|
|
26
26
|
};
|
|
27
27
|
if (conf.promiseMiddleware) {
|
|
28
|
-
conf.promiseMiddleware.forEach(
|
|
28
|
+
conf.promiseMiddleware.forEach(m => configuration.middleware.push(new middleware_1.PromiseMiddlewareWrapper(m)));
|
|
29
29
|
}
|
|
30
30
|
return configuration;
|
|
31
31
|
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const OneSignalErrors: {
|
|
2
|
+
readonly INVALID_API_KEY: "Access denied. Please include an 'Authorization: ...' header with a valid API key (https://documentation.onesignal.com/docs/en/keys-and-ids#api-keys).";
|
|
3
|
+
readonly NOTIFICATION_NOT_FOUND: "Notification not found";
|
|
4
|
+
readonly NO_SUBSCRIBERS: "All included players are not subscribed";
|
|
5
|
+
readonly NO_TARGETING_SPECIFIED: "You must include which players, segments, or tags you wish to send this notification to.";
|
|
6
|
+
readonly SERVICE_UNAVAILABLE: "Service temporarily unavailable";
|
|
7
|
+
};
|
|
8
|
+
export type OneSignalErrorMessage = typeof OneSignalErrors[keyof typeof OneSignalErrors];
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OneSignalErrors = void 0;
|
|
4
|
+
exports.OneSignalErrors = {
|
|
5
|
+
INVALID_API_KEY: "Access denied. Please include an 'Authorization: ...' header with a valid API key (https://documentation.onesignal.com/docs/en/keys-and-ids#api-keys).",
|
|
6
|
+
NOTIFICATION_NOT_FOUND: "Notification not found",
|
|
7
|
+
NO_SUBSCRIBERS: "All included players are not subscribed",
|
|
8
|
+
NO_TARGETING_SPECIFIED: "You must include which players, segments, or tags you wish to send this notification to.",
|
|
9
|
+
SERVICE_UNAVAILABLE: "Service temporarily unavailable",
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Configuration } from './configuration';
|
|
2
|
+
import { Notification } from './models/Notification';
|
|
3
|
+
import { CreateNotificationSuccessResponse } from './models/CreateNotificationSuccessResponse';
|
|
4
|
+
export interface CreateNotificationWithRetryOptions {
|
|
5
|
+
maxRetries?: number;
|
|
6
|
+
baseDelayMs?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface CreateNotificationWithRetryResult {
|
|
9
|
+
response: CreateNotificationSuccessResponse;
|
|
10
|
+
wasReplayed: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function createNotificationWithRetry(configuration: Configuration, notification: Notification, options?: CreateNotificationWithRetryOptions): Promise<CreateNotificationWithRetryResult>;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createNotificationWithRetry = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const DefaultApi_1 = require("./apis/DefaultApi");
|
|
6
|
+
const RETRYABLE_STATUSES = [429, 503];
|
|
7
|
+
const MIN_BASE_DELAY_MS = 1000;
|
|
8
|
+
const MAX_BASE_DELAY_MS = 60000;
|
|
9
|
+
async function createNotificationWithRetry(configuration, notification, options) {
|
|
10
|
+
const maxRetries = options && options.maxRetries !== undefined ? options.maxRetries : 3;
|
|
11
|
+
const requestedBaseDelayMs = options && typeof options.baseDelayMs === 'number' && isFinite(options.baseDelayMs)
|
|
12
|
+
? options.baseDelayMs
|
|
13
|
+
: MIN_BASE_DELAY_MS;
|
|
14
|
+
const baseDelayMs = Math.min(MAX_BASE_DELAY_MS, Math.max(MIN_BASE_DELAY_MS, requestedBaseDelayMs));
|
|
15
|
+
if (!notification.idempotency_key) {
|
|
16
|
+
notification.idempotency_key = generateUuidV4();
|
|
17
|
+
}
|
|
18
|
+
const requestFactory = new DefaultApi_1.DefaultApiRequestFactory(configuration);
|
|
19
|
+
const responseProcessor = new DefaultApi_1.DefaultApiResponseProcessor();
|
|
20
|
+
let attempt = 0;
|
|
21
|
+
while (true) {
|
|
22
|
+
let response;
|
|
23
|
+
try {
|
|
24
|
+
const requestContext = await requestFactory.createNotification(notification, configuration);
|
|
25
|
+
response = await configuration.httpApi.send(requestContext).toPromise();
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
if (attempt >= maxRetries) {
|
|
29
|
+
throw e;
|
|
30
|
+
}
|
|
31
|
+
await sleep(baseDelayMs * Math.pow(2, attempt));
|
|
32
|
+
attempt++;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (RETRYABLE_STATUSES.indexOf(response.httpStatusCode) !== -1 && attempt < maxRetries) {
|
|
36
|
+
await sleep(retryDelayMs(response.headers, attempt, baseDelayMs));
|
|
37
|
+
attempt++;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const body = await responseProcessor.createNotification(response);
|
|
41
|
+
return { response: body, wasReplayed: isReplayed(response.headers) };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.createNotificationWithRetry = createNotificationWithRetry;
|
|
45
|
+
function headerValue(headers, name) {
|
|
46
|
+
const target = name.toLowerCase();
|
|
47
|
+
for (const key in headers) {
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(headers, key) && key.toLowerCase() === target) {
|
|
49
|
+
return headers[key];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
function isReplayed(headers) {
|
|
55
|
+
const value = headerValue(headers, 'idempotent-replayed');
|
|
56
|
+
return value !== undefined && value.trim().toLowerCase() === 'true';
|
|
57
|
+
}
|
|
58
|
+
function retryDelayMs(headers, attempt, baseDelayMs) {
|
|
59
|
+
const retryAfter = headerValue(headers, 'retry-after');
|
|
60
|
+
if (retryAfter !== undefined && /^\d+$/.test(retryAfter.trim())) {
|
|
61
|
+
return parseInt(retryAfter.trim(), 10) * 1000;
|
|
62
|
+
}
|
|
63
|
+
return baseDelayMs * Math.pow(2, attempt);
|
|
64
|
+
}
|
|
65
|
+
function generateUuidV4() {
|
|
66
|
+
const bytes = (0, crypto_1.randomBytes)(16);
|
|
67
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
68
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
69
|
+
const hex = bytes.toString('hex');
|
|
70
|
+
return (hex.substring(0, 8) + '-' +
|
|
71
|
+
hex.substring(8, 12) + '-' +
|
|
72
|
+
hex.substring(12, 16) + '-' +
|
|
73
|
+
hex.substring(16, 20) + '-' +
|
|
74
|
+
hex.substring(20));
|
|
75
|
+
}
|
|
76
|
+
function sleep(ms) {
|
|
77
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=helpers.js.map
|