@trycourier/courier 3.16.0 → 4.0.1

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 CHANGED
@@ -672,6 +672,36 @@ async function run() {
672
672
  run();
673
673
  ```
674
674
 
675
+ ### Error Handling
676
+
677
+ This package tries to use the native `fetch` client to make requests or falls back to a polyfill if the client doesn't exist in the environment it's running in.
678
+
679
+ All network related promise rejections are not handled in any way. All successfully made requests that produce errors on the server side are resulting in promise rejections with custom `CourierHttpClientError` error type.
680
+
681
+ `CourierHttpClientError` extends native `Error` interface with two extra properties:
682
+
683
+ * `response`: this is the `fetch` response as is
684
+ * `data`: this is the parsed body of the response
685
+
686
+ ```javascript
687
+ // Error handling example
688
+ import { CourierClient, CourierHttpClientError } from '@trycourier/courier';
689
+
690
+ const courier = CourierClient();
691
+
692
+ try {
693
+ await courier.send(/* ... */);
694
+ } catch (error) {
695
+ if (error instanceof CourierHttpClientError) {
696
+ console.log("Failed to send with status code:", error.response.status);
697
+ console.log("The Courier response is:", error.data);
698
+ console.log("The error message is:", error.message);
699
+ } else {
700
+ console.log('There was a problem making that request. Make sure you are online.');
701
+ }
702
+ }
703
+ ```
704
+
675
705
  ### Idempotency
676
706
 
677
707
  For `POST` methods, you can supply an `idempotencyKey` in the config parameter to ensure the idempotency of the API Call. We recommend that you use a `V4 UUID` for the key. Keys are eligible to be removed from the system after they're at least 24 hours old, and a new request is generated if a key is reused after the original has been removed. For more info, see [Idempotent Requests](https://docs.courier.com/reference/idempotent-requests) in the Courier documentation.
@@ -91,7 +91,7 @@ var putAudience = function (options) {
91
91
  var response;
92
92
  return __generator(this, function (_a) {
93
93
  switch (_a.label) {
94
- case 0: return [4 /*yield*/, options.httpClient.get("/audiences/" + audience.id)];
94
+ case 0: return [4 /*yield*/, options.httpClient.put("/audiences/" + audience.id)];
95
95
  case 1:
96
96
  response = _a.sent();
97
97
  return [2 /*return*/, response.data.audience];
@@ -39,28 +39,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.automations = void 0;
40
40
  var invokeAdHocAutomation = function (options) {
41
41
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
42
- var axiosConfig, res;
42
+ var res;
43
43
  return __generator(this, function (_a) {
44
44
  switch (_a.label) {
45
- case 0:
46
- axiosConfig = {
47
- headers: {}
48
- };
49
- if (config && config.idempotencyKey) {
50
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
51
- }
52
- if (config && config.idempotencyExpiry) {
53
- axiosConfig.headers["x-idempotency-expiration"] =
54
- config.idempotencyExpiry;
55
- }
56
- return [4 /*yield*/, options.httpClient.post("/automations/invoke", {
57
- automation: params.automation,
58
- brand: params.brand,
59
- data: params.data,
60
- profile: params.profile,
61
- recipient: params.recipient,
62
- template: params.template
63
- }, axiosConfig)];
45
+ case 0: return [4 /*yield*/, options.httpClient.post("/automations/invoke", {
46
+ automation: params.automation,
47
+ brand: params.brand,
48
+ data: params.data,
49
+ profile: params.profile,
50
+ recipient: params.recipient,
51
+ template: params.template
52
+ }, {
53
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
54
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
55
+ })];
64
56
  case 1:
65
57
  res = _a.sent();
66
58
  return [2 /*return*/, res.data];
@@ -70,25 +62,19 @@ var invokeAdHocAutomation = function (options) {
70
62
  };
71
63
  var invokeAutomationTemplate = function (options) {
72
64
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
73
- var axiosConfig, res;
65
+ var res;
74
66
  return __generator(this, function (_a) {
75
67
  switch (_a.label) {
76
- case 0:
77
- axiosConfig = {
78
- headers: {}
79
- };
80
- if (config && config.idempotencyKey) {
81
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
82
- axiosConfig.headers["x-idempotency-expiration"] =
83
- config.idempotencyExpiry;
84
- }
85
- return [4 /*yield*/, options.httpClient.post("/automations/" + params.templateId + "/invoke", {
86
- brand: params.brand,
87
- data: params.data,
88
- profile: params.profile,
89
- recipient: params.recipient,
90
- template: params.template
91
- }, axiosConfig)];
68
+ case 0: return [4 /*yield*/, options.httpClient.post("/automations/" + params.templateId + "/invoke", {
69
+ brand: params.brand,
70
+ data: params.data,
71
+ profile: params.profile,
72
+ recipient: params.recipient,
73
+ template: params.template
74
+ }, {
75
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
76
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
77
+ })];
92
78
  case 1:
93
79
  res = _a.sent();
94
80
  return [2 /*return*/, res.data];
package/lib/brands.js CHANGED
@@ -76,21 +76,13 @@ exports.getBrand = function (options) {
76
76
  };
77
77
  exports.createBrand = function (options) {
78
78
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
79
- var axiosConfig, res;
79
+ var res;
80
80
  return __generator(this, function (_a) {
81
81
  switch (_a.label) {
82
- case 0:
83
- axiosConfig = {
84
- headers: {}
85
- };
86
- if (config && config.idempotencyKey) {
87
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
88
- }
89
- if (config && config.idempotencyExpiry) {
90
- axiosConfig.headers["x-idempotency-expiration"] =
91
- config.idempotencyExpiry;
92
- }
93
- return [4 /*yield*/, options.httpClient.post("/brands", params, axiosConfig)];
82
+ case 0: return [4 /*yield*/, options.httpClient.post("/brands", params, {
83
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
84
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
85
+ })];
94
86
  case 1:
95
87
  res = _a.sent();
96
88
  return [2 /*return*/, res.data];
package/lib/bulk/index.js CHANGED
@@ -39,23 +39,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.bulk = void 0;
40
40
  var createJob = function (options) {
41
41
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
42
- var axiosConfig, res;
42
+ var res;
43
43
  return __generator(this, function (_a) {
44
44
  switch (_a.label) {
45
- case 0:
46
- axiosConfig = {
47
- headers: {}
48
- };
49
- if (config && config.idempotencyKey) {
50
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
51
- }
52
- if (config && config.idempotencyExpiry) {
53
- axiosConfig.headers["x-idempotency-expiration"] =
54
- config.idempotencyExpiry;
55
- }
56
- return [4 /*yield*/, options.httpClient.post("/bulk", {
57
- message: params.message
58
- }, axiosConfig)];
45
+ case 0: return [4 /*yield*/, options.httpClient.post("/bulk", {
46
+ message: params.message
47
+ }, {
48
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
49
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
50
+ })];
59
51
  case 1:
60
52
  res = _a.sent();
61
53
  return [2 /*return*/, res.data];
@@ -65,23 +57,15 @@ var createJob = function (options) {
65
57
  };
66
58
  var ingestUsers = function (options) {
67
59
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
68
- var axiosConfig, res;
60
+ var res;
69
61
  return __generator(this, function (_a) {
70
62
  switch (_a.label) {
71
- case 0:
72
- axiosConfig = {
73
- headers: {}
74
- };
75
- if (config && config.idempotencyKey) {
76
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
77
- }
78
- if (config && config.idempotencyExpiry) {
79
- axiosConfig.headers["x-idempotency-expiration"] =
80
- config.idempotencyExpiry;
81
- }
82
- return [4 /*yield*/, options.httpClient.post("/bulk/" + params.jobId, {
83
- users: params.users
84
- }, axiosConfig)];
63
+ case 0: return [4 /*yield*/, options.httpClient.post("/bulk/" + params.jobId, {
64
+ users: params.users
65
+ }, {
66
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
67
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
68
+ })];
85
69
  case 1:
86
70
  res = _a.sent();
87
71
  return [2 /*return*/, res.data];
@@ -91,21 +75,12 @@ var ingestUsers = function (options) {
91
75
  };
92
76
  var runJob = function (options) {
93
77
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
94
- var axiosConfig;
95
78
  return __generator(this, function (_a) {
96
79
  switch (_a.label) {
97
- case 0:
98
- axiosConfig = {
99
- headers: {}
100
- };
101
- if (config && config.idempotencyKey) {
102
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
103
- }
104
- if (config && config.idempotencyExpiry) {
105
- axiosConfig.headers["x-idempotency-expiration"] =
106
- config.idempotencyExpiry;
107
- }
108
- return [4 /*yield*/, options.httpClient.post("/bulk/" + params.jobId + "/run", {}, axiosConfig)];
80
+ case 0: return [4 /*yield*/, options.httpClient.post("/bulk/" + params.jobId + "/run", {}, {
81
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
82
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
83
+ })];
109
84
  case 1:
110
85
  _a.sent();
111
86
  return [2 /*return*/];
@@ -0,0 +1,10 @@
1
+ import { IHttpClient, IInitHttpClientOptions } from "./types";
2
+ export declare class CourierHttpClientError extends Error {
3
+ response?: Response;
4
+ data?: any;
5
+ constructor(message: string, { response, data }: {
6
+ response?: Response;
7
+ data?: any;
8
+ });
9
+ }
10
+ export declare const initHttpClient: ({ baseUrl, version, authorizationToken }: IInitHttpClientOptions) => IHttpClient;
@@ -0,0 +1,131 @@
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ var __generator = (this && this.__generator) || function (thisArg, body) {
36
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
37
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
38
+ function verb(n) { return function (v) { return step([n, v]); }; }
39
+ function step(op) {
40
+ if (f) throw new TypeError("Generator is already executing.");
41
+ while (_) try {
42
+ 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;
43
+ if (y = 0, t) op = [op[0] & 2, t.value];
44
+ switch (op[0]) {
45
+ case 0: case 1: t = op; break;
46
+ case 4: _.label++; return { value: op[1], done: false };
47
+ case 5: _.label++; y = op[1]; op = [0]; continue;
48
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
49
+ default:
50
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
51
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
52
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
53
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
54
+ if (t[2]) _.ops.pop();
55
+ _.trys.pop(); continue;
56
+ }
57
+ op = body.call(thisArg, _);
58
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
59
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
60
+ }
61
+ };
62
+ var _a;
63
+ Object.defineProperty(exports, "__esModule", { value: true });
64
+ exports.initHttpClient = exports.CourierHttpClientError = void 0;
65
+ var cross_fetch_1 = require("cross-fetch");
66
+ var fetch = (_a = globalThis.fetch) !== null && _a !== void 0 ? _a : cross_fetch_1.default;
67
+ var CourierHttpClientError = /** @class */ (function (_super) {
68
+ __extends(CourierHttpClientError, _super);
69
+ function CourierHttpClientError(message, _a) {
70
+ var response = _a.response, data = _a.data;
71
+ var _this = _super.call(this, message) || this;
72
+ Object.setPrototypeOf(_this, CourierHttpClientError.prototype);
73
+ _this.response = response;
74
+ _this.data = data;
75
+ return _this;
76
+ }
77
+ return CourierHttpClientError;
78
+ }(Error));
79
+ exports.CourierHttpClientError = CourierHttpClientError;
80
+ exports.initHttpClient = function (_a) {
81
+ var baseUrl = _a.baseUrl, version = _a.version, authorizationToken = _a.authorizationToken;
82
+ var createHttpMethodClient = function (method) {
83
+ return function () {
84
+ var _a = [];
85
+ for (var _i = 0; _i < arguments.length; _i++) {
86
+ _a[_i] = arguments[_i];
87
+ }
88
+ var url = _a[0], body = _a[1], config = _a[2];
89
+ return __awaiter(void 0, void 0, void 0, function () {
90
+ var searchParams, searchQueryString, fullUrl, contentTypeHeader, idempotencyKeyHeader, idempotencyExpiryHeader, response, parseAsJson, data;
91
+ return __generator(this, function (_b) {
92
+ switch (_b.label) {
93
+ case 0:
94
+ searchParams = String(new URLSearchParams(config === null || config === void 0 ? void 0 : config.params));
95
+ searchQueryString = searchParams && "?" + searchParams;
96
+ fullUrl = String(new URL("" + url + searchQueryString, baseUrl));
97
+ contentTypeHeader = body == null ? null : { "Content-Type": "application/json" };
98
+ idempotencyKeyHeader = (config === null || config === void 0 ? void 0 : config.idempotencyKey) ? { "Idempotency-Key": config.idempotencyKey }
99
+ : null;
100
+ idempotencyExpiryHeader = (config === null || config === void 0 ? void 0 : config.idempotencyExpiry) == null
101
+ ? null
102
+ : { "x-idempotency-expiration": String(config.idempotencyExpiry) };
103
+ return [4 /*yield*/, fetch(fullUrl, {
104
+ body: body != null ? JSON.stringify(body) : undefined,
105
+ headers: __assign(__assign(__assign({ Authorization: "Bearer " + authorizationToken, "User-Agent": "courier-node/" + version }, contentTypeHeader), idempotencyKeyHeader), idempotencyExpiryHeader),
106
+ method: method
107
+ })];
108
+ case 1:
109
+ response = _b.sent();
110
+ parseAsJson = response.headers.get("content-length") !== "0" &&
111
+ response.headers.get("content-type") === "application/json";
112
+ return [4 /*yield*/, (parseAsJson ? response.json() : response.text())];
113
+ case 2:
114
+ data = _b.sent();
115
+ if (!response.ok) {
116
+ throw new CourierHttpClientError(data.message || "An unexpected error has occurred", { response: response, data: data });
117
+ }
118
+ return [2 /*return*/, { data: data }];
119
+ }
120
+ });
121
+ });
122
+ };
123
+ };
124
+ return {
125
+ delete: createHttpMethodClient("delete"),
126
+ get: function (url, config) { return createHttpMethodClient("get")(url, undefined, config); },
127
+ patch: createHttpMethodClient("patch"),
128
+ post: createHttpMethodClient("post"),
129
+ put: createHttpMethodClient("put")
130
+ };
131
+ };
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { ICourierClientOptions } from "./types";
2
2
  export { ICourierClient } from "./types";
3
+ export { CourierHttpClientError } from "./http-client";
3
4
  export declare const CourierClient: (options?: ICourierClientOptions) => import("./types").ICourierClient;
package/lib/index.js CHANGED
@@ -1,30 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CourierClient = void 0;
4
- var axios_1 = require("axios");
5
4
  var client_1 = require("./client");
5
+ var http_client_1 = require("./http-client");
6
+ var http_client_2 = require("./http-client");
7
+ Object.defineProperty(exports, "CourierHttpClientError", { enumerable: true, get: function () { return http_client_2.CourierHttpClientError; } });
6
8
  // cannot be `import` as it's not under TS root dir
7
9
  // tslint:disable-next-line:no-var-requires
8
10
  var version = require("../package.json").version;
9
11
  var DEFAULTS = {
10
12
  BASE_URL: "https://api.courier.com"
11
13
  };
14
+ var getEnvVariable = function (name) { var _a, _b; return (_b = (_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.process) === null || _a === void 0 ? void 0 : _a.env) === null || _b === void 0 ? void 0 : _b[name]; };
12
15
  exports.CourierClient = function (options) {
13
16
  if (options === void 0) { options = {}; }
14
- var authorizationToken = options.authorizationToken || process.env.COURIER_AUTH_TOKEN;
17
+ var authorizationToken = options.authorizationToken || getEnvVariable("COURIER_AUTH_TOKEN");
15
18
  if (!authorizationToken) {
16
19
  throw new Error("Courier Auth Token is required.");
17
20
  }
18
- var baseURL = options.baseUrl || process.env.COURIER_BASE_URL || DEFAULTS.BASE_URL;
19
- var axiosInstance = axios_1.default.create({
20
- baseURL: baseURL,
21
- headers: {
22
- Authorization: "Bearer " + authorizationToken,
23
- "User-Agent": "courier-node/" + version
24
- }
21
+ var baseUrl = options.baseUrl || getEnvVariable("COURIER_BASE_URL") || DEFAULTS.BASE_URL;
22
+ var httpClient = http_client_1.initHttpClient({
23
+ authorizationToken: authorizationToken,
24
+ baseUrl: baseUrl,
25
+ version: version
25
26
  });
26
27
  var courier = client_1.client({
27
- httpClient: axiosInstance
28
+ httpClient: httpClient
28
29
  });
29
30
  return courier;
30
31
  };
@@ -182,21 +182,13 @@ var findByRecipientId = function (options) {
182
182
  };
183
183
  var send = function (options) {
184
184
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
185
- var axiosConfig, res;
185
+ var res;
186
186
  return __generator(this, function (_a) {
187
187
  switch (_a.label) {
188
- case 0:
189
- axiosConfig = {
190
- headers: {}
191
- };
192
- if (config && config.idempotencyKey) {
193
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
194
- }
195
- if (config && config.idempotencyExpiry) {
196
- axiosConfig.headers["x-idempotency-expiration"] =
197
- config.idempotencyExpiry;
198
- }
199
- return [4 /*yield*/, options.httpClient.post("/send/list", params, axiosConfig)];
188
+ case 0: return [4 /*yield*/, options.httpClient.post("/send/list", params, {
189
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
190
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
191
+ })];
200
192
  case 1:
201
193
  res = _a.sent();
202
194
  return [2 /*return*/, res.data];
package/lib/profile.js CHANGED
@@ -54,23 +54,15 @@ exports.replaceProfile = function (options) {
54
54
  };
55
55
  exports.mergeProfile = function (options) {
56
56
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
57
- var axiosConfig, res;
57
+ var res;
58
58
  return __generator(this, function (_a) {
59
59
  switch (_a.label) {
60
- case 0:
61
- axiosConfig = {
62
- headers: {}
63
- };
64
- if (config && config.idempotencyKey) {
65
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
66
- }
67
- if (config && config.idempotencyExpiry) {
68
- axiosConfig.headers["x-idempotency-expiration"] =
69
- config.idempotencyExpiry;
70
- }
71
- return [4 /*yield*/, options.httpClient.post("/profiles/" + params.recipientId, {
72
- profile: params.profile
73
- }, axiosConfig)];
60
+ case 0: return [4 /*yield*/, options.httpClient.post("/profiles/" + params.recipientId, {
61
+ profile: params.profile
62
+ }, {
63
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
64
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
65
+ })];
74
66
  case 1:
75
67
  res = _a.sent();
76
68
  return [2 /*return*/, res.data];
package/lib/send/index.js CHANGED
@@ -37,7 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.send = void 0;
40
- var sendCall = function (options, config, params) { return __awaiter(void 0, void 0, void 0, function () {
40
+ var sendCall = function (options, params, config) { return __awaiter(void 0, void 0, void 0, function () {
41
41
  var res;
42
42
  return __generator(this, function (_a) {
43
43
  switch (_a.label) {
@@ -49,20 +49,26 @@ var sendCall = function (options, config, params) { return __awaiter(void 0, voi
49
49
  preferences: params.preferences,
50
50
  profile: params.profile,
51
51
  recipient: params.recipientId
52
- }, config)];
52
+ }, {
53
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
54
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
55
+ })];
53
56
  case 1:
54
57
  res = _a.sent();
55
58
  return [2 /*return*/, res.data];
56
59
  }
57
60
  });
58
61
  }); };
59
- var sendMessageCall = function (options, config, params) { return __awaiter(void 0, void 0, void 0, function () {
62
+ var sendMessageCall = function (options, params, config) { return __awaiter(void 0, void 0, void 0, function () {
60
63
  var res;
61
64
  return __generator(this, function (_a) {
62
65
  switch (_a.label) {
63
66
  case 0: return [4 /*yield*/, options.httpClient.post("/send", {
64
67
  message: params.message
65
- }, config)];
68
+ }, {
69
+ idempotencyExpiry: config === null || config === void 0 ? void 0 : config.idempotencyExpiry,
70
+ idempotencyKey: config === null || config === void 0 ? void 0 : config.idempotencyKey
71
+ })];
66
72
  case 1:
67
73
  res = _a.sent();
68
74
  return [2 /*return*/, res.data];
@@ -71,26 +77,16 @@ var sendMessageCall = function (options, config, params) { return __awaiter(void
71
77
  }); };
72
78
  exports.send = function (options) {
73
79
  return function (params, config) { return __awaiter(void 0, void 0, void 0, function () {
74
- var axiosConfig, v2Response, v1Response;
80
+ var v2Response, v1Response;
75
81
  return __generator(this, function (_a) {
76
82
  switch (_a.label) {
77
83
  case 0:
78
- axiosConfig = {
79
- headers: {}
80
- };
81
- if (config && config.idempotencyKey) {
82
- axiosConfig.headers["Idempotency-Key"] = config.idempotencyKey;
83
- }
84
- if (config && config.idempotencyExpiry) {
85
- axiosConfig.headers["x-idempotency-expiration"] =
86
- config.idempotencyExpiry;
87
- }
88
84
  if (!params.message) return [3 /*break*/, 2];
89
- return [4 /*yield*/, sendMessageCall(options, axiosConfig, params)];
85
+ return [4 /*yield*/, sendMessageCall(options, params, config)];
90
86
  case 1:
91
87
  v2Response = _a.sent();
92
88
  return [2 /*return*/, v2Response];
93
- case 2: return [4 /*yield*/, sendCall(options, axiosConfig, params)];
89
+ case 2: return [4 /*yield*/, sendCall(options, params, config)];
94
90
  case 3:
95
91
  v1Response = _a.sent();
96
92
  return [2 /*return*/, v1Response];
package/lib/types.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { AxiosRequestConfig } from "axios";
2
1
  import { ICourierClientAudiences } from "./audiences/types";
3
2
  import { auditEvents } from "./audit-events";
4
3
  import { ICourierClientAutomations } from "./automations/types";
@@ -8,7 +7,16 @@ import { ICourierClientNotifications } from "./notifications/types";
8
7
  import { ICourierClientPreferences, IRecipientPreferences } from "./preferences/types";
9
8
  import { Message } from "./send/types";
10
9
  import { tokenManagement } from "./token-management";
11
- export declare type HttpMethodClient = <T>(url: string, body?: object, config?: AxiosRequestConfig) => Promise<{
10
+ export interface IInitHttpClientOptions {
11
+ baseUrl: string;
12
+ version: string;
13
+ authorizationToken: string;
14
+ }
15
+ export declare type HttpMethodClient = <T>(url: string, body?: object, config?: {
16
+ params?: Record<string, string>;
17
+ idempotencyKey?: string;
18
+ idempotencyExpiry?: number;
19
+ }) => Promise<{
12
20
  data: T;
13
21
  }>;
14
22
  export interface IHttpClient {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/courier",
3
- "version": "3.16.0",
3
+ "version": "4.0.1",
4
4
  "description": "A node.js module for communicating with the Courier REST API.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -14,8 +14,8 @@
14
14
  "devDependencies": {
15
15
  "@types/jest": "^24.0.15",
16
16
  "@types/node": "^12.6.2",
17
- "axios-mock-adapter": "^1.17.0",
18
17
  "jest": "^24.8.0",
18
+ "jest-fetch-mock": "^3.0.3",
19
19
  "prettier": "^1.18.2",
20
20
  "ts-jest": "^24.0.2",
21
21
  "tslint": "^5.18.0",
@@ -31,6 +31,6 @@
31
31
  "prepublishOnly": "yarn lint && yarn test"
32
32
  },
33
33
  "dependencies": {
34
- "axios": "^0.21.1"
34
+ "cross-fetch": "^3.1.5"
35
35
  }
36
36
  }
package/CHANGELOG.md DELETED
@@ -1,296 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- This project adheres to [Semantic Versioning](http://semver.org/).
5
-
6
- ## [Unreleased][unreleased]
7
-
8
- ## [3.16.0]
9
-
10
- - adds support for idempotency expiry to be passed in profiles and brands POST
11
-
12
- ## [3.15.0]
13
-
14
- - adds support for audit events
15
-
16
- ## [3.14.0]
17
-
18
- - adds support for token management
19
-
20
- ## [3.13.1] - 2022-06-03
21
-
22
- - adds provider and channel timeout
23
-
24
- ## [3.12.0] - 2022-03-31
25
-
26
- - adds support for message trace id (`message.metadata.trace_id`)
27
-
28
- ## [3.11.0] - 2022-03-23
29
-
30
- - adds support for `audiences`
31
-
32
- ## [3.10.1] - 2022-03-20
33
-
34
- - adds support for messages timeout (`message.timeout`)
35
-
36
- ## [3.10.0] - 2020-03-24
37
-
38
- - adds support for messages brand_id (`message.brand_id`)
39
-
40
- ## [3.9.0] - 2022-03-17
41
-
42
- - adds support for bulk messaging API v2 support
43
-
44
- ## [3.8.0] - 2022-03-14
45
-
46
- - adds additional types for utm property (`message.metadata.utm`)
47
-
48
- ## [v3.7.0] - 2022-03-11
49
-
50
- - adds additional types for the tags property (`message.metadata.tags`)
51
- - adds support for searching message by tags
52
-
53
- ## [v3.6.0] - 2022-02-10
54
-
55
- - adds additional types for the recipient property (`message.to`)
56
-
57
- ## [v3.5.0] - 2022-02-10
58
-
59
- - adds type for unroutable status
60
-
61
- ## [v3.4.0] - 2022-01-25
62
-
63
- - adds support for the send message object in the request body of a `/send` call
64
-
65
- ## [v3.3.0] - 2022-01-25
66
-
67
- - adds support for bulk processing endpoints
68
-
69
- ## [v3.2.1] - 2022-01-13
70
-
71
- - Fixes `getMessages` query params
72
-
73
- ## [v3.2.0] - 2021-11-18
74
-
75
- - adds idempotency expiration support for send and send list endpoints
76
-
77
- ## [v3.1.0] - 2021-11-16
78
-
79
- - Expose additional type definitions for `getMessage`
80
-
81
- ## [v3.0.0] - 2021-11-02
82
-
83
- - fixes type definition for `getRecipientSubscriptions`
84
-
85
- ## [v2.8.0] - 2021-10-29
86
-
87
- - adds GET /messages/{messageId}/output API
88
-
89
- ## [v2.7.0] - 2021-10-21
90
-
91
- - adds GET /messages/{messageId}/history API
92
-
93
- ## [v2.6.0] - 2021-10-07
94
-
95
- - Add support for DELETE /profiles/{recipient_id} (#58)
96
- - adds GET /messages API
97
- - adds idempotencyKey support in automations client
98
-
99
- ## [v2.4.0] - 2021-08-23
100
-
101
- - adds notifications API
102
- - type fix `put` method of `ICourierClientLists`
103
-
104
- ## [v2.3.0] - 2021-04-28
105
-
106
- - adds support for update-profile step via `client.automations.invokeAdHocAutomation({...})`
107
-
108
- ## [v2.2.0] - 2021-04-07
109
-
110
- - adds automations API
111
-
112
- ## [v2.1.0] - 2021-03-15
113
-
114
- - adds support to add more recipients to a list subscription (#40)
115
- - adds support to delete all the lists subscriptions for a recipient (#39)
116
- - adds support to add recipient to multiple lists (#38)
117
- - updates preference interface to accept new preference options (#37)
118
-
119
- ## [v2.0.0] - 2021-03-03
120
-
121
- - supports adding subscription preferences (#35)
122
-
123
- ## [v1.7.3] - 2021-03-03
124
-
125
- ### Added
126
-
127
- - ICourierClient exported as a type [#33](https://github.com/trycourier/courier-node/pull/33)
128
-
129
- ## [v1.7.2] - 2021-02-16
130
-
131
- ### Fixed
132
-
133
- - Update PUT list subscription request params with appropriate type [#32](https://github.com/trycourier/courier-node/pull/32)
134
-
135
- ## [v1.7.1] - 2021-02-01
136
-
137
- ### Fixed
138
-
139
- - Fix the notification(s) typo [#28](https://github.com/trycourier/courier-node/pull/28)
140
-
141
- ## [v1.7.0] - 2021-01-25
142
-
143
- ### Added
144
-
145
- - Support for [Preferences API]() by @helenamerk and @aydrian
146
- - `GET /preferences` via `client.preferences.list()`
147
- - `GET /preferences/{recipient_id}` via `client.preferences.get(recipientId)`
148
- - `PUT /preferences/{recipient_id}` via `client.preferences.put(recipientId, {...})`
149
-
150
- ## [v1.6.2] - 2021-01-19
151
-
152
- ### Fixed
153
-
154
- - Bumped [axios](https://www.npmjs.com/package/axios) to version 0.21.1
155
- - Updated types for Send to List Parameters by @rileylnapier
156
-
157
- ## [v1.6.1] - 2020-09-23
158
-
159
- ### Fixed
160
-
161
- - Fixed return types for `PUT` methods that return 204
162
- - Fixed param types for `client.lists.send()`
163
- - Fixed return type for `client.lists.findByRecipientId()`
164
- - Fixed param types for `client.lists.put()`
165
-
166
- ## [v1.6.0] - 2020-09-22
167
-
168
- ### Added
169
-
170
- - Support for `idempotencyKey` for `POST` methods by @aydrian & @rileylnapier
171
- - Support for [Lists API](https://docs.courier.com/reference/lists-api) by @aydrian
172
- - `POST /send/list` via `client.lists.send(params, config)`
173
- - `GET /profiles/{recipient_id}/lists` via `client.lists.findByRecipientId(recipientId, params)`
174
- - `GET /lists` via `client.lists.list(params)`
175
- - `GET /lists/{list_id}` via `client.lists.get(listId)`
176
- - `PUT /lists/{list_id}` via `client.lists.put(listId, {...})`
177
- - `DELETE /lists/{list_id}` via `client.lists.delete(listId)`
178
- - `PUT /lists/{list_id}/restore` via `client.lists.restore(listId)`
179
- - `GET /lists/{list_id}/subscriptions` via `client.lists.getSubscriptions(listId)`
180
- - `PUT /lists/{list_id}/subscriptions` via `client.lists.putSubscriptions(listId, [recipientId], config)`
181
- - `PUT /lists/{list_id}/subscriptions/{recipient_id}` via `client.lists.subscribe(listId, recipientId)`
182
- - `DELETE /lists/{list_id}/subscriptions/{recipient_id}` via `client.lists.unsubscribe(listId, recipientId)`
183
-
184
- ### Changed
185
-
186
- - Default `base_url` is now `api.courier.com`
187
-
188
- ## [v1.5.0] - 2020-07-08
189
-
190
- ### Added
191
-
192
- - Support for [Brands API](https://docs.courier.com/reference/brands-api) by @aydrian
193
- - `GET /brands` via `client.getBrands(params)`
194
- - `GET /brands/:brand_id` via `client.getBrand(brandId)`
195
- - `POST /brands` via `client.createBrand({…})`
196
- - `PUT /brands/:brand_id` via `client.replaceBrand({…})`
197
- - `DELETE /brands/:brand_id` via `client.deleteBrand(brandId)`
198
- - Support for specifying notification brand during [send](https://docs.courier.com/reference/send-api#sendmessage) by @aydrian
199
-
200
- ## [v1.4.0] - 2020-06-29
201
-
202
- ### Added
203
-
204
- - Support `GET /messages/:messageId` via `client.getMessage(messageId)` @rileylnapier
205
-
206
- ## [v1.3.0] - 2020-03-10
207
-
208
- ### Added
209
-
210
- - Support credential storage using `COURIER_AUTH_TOKEN` environment variable by @aydrian
211
- - Support setting base url using `COURIER_BASE_URL` environment variable by @aydrian
212
- - Support preferences and override in send method by @aydrian
213
- - Create GitHub Issue and Pull Request templates by @rileylnapier
214
-
215
- ### Changed
216
-
217
- - Updated user agent string to match new standard by @aydrian
218
-
219
- ## [v1.2.1] - 2019-12-30
220
-
221
- ### Fixed
222
-
223
- - Convert package.json import to a require by @@scarney81
224
-
225
- ## [v1.2.0] - 2019-12-20
226
-
227
- ## Added
228
-
229
- - Custom user agent string by @aydrian
230
-
231
- ## [v1.1.6] - 2019-07-12
232
-
233
- ## [v1.1.5] - 2019-07-12
234
-
235
- ## [v1.1.4] - 2019-07-12
236
-
237
- ## [v1.1.3] - 2019-07-12
238
-
239
- ## [v1.1.2] - 2019-07-12
240
-
241
- ## [v1.1.1] - 2019-07-12
242
-
243
- ## [v1.1.0] - 2019-07-12
244
-
245
- ## [v1.0.4] - 2019-07-12
246
-
247
- ## [v1.0.3] - 2019-07-12
248
-
249
- ## [v1.0.2] - 2019-07-12
250
-
251
- ## v1.0.1 - 2019-07-12
252
-
253
- [unreleased]: https://github.com/trycourier/courier-node/compare/v3.11.0...HEAD
254
- [v3.11.0]: https://github.com/trycourier/courier-node/compare/v3.10.0...v3.11.0
255
- [v3.10.0]: https://github.com/trycourier/courier-node/compare/v3.9.0...v3.10.0
256
- [v3.9.0]: https://github.com/trycourier/courier-node/compare/v3.8.0...v3.9.0
257
- [v3.8.0]: https://github.com/trycourier/courier-node/compare/v3.7.0...v3.8.0
258
- [v3.7.0]: https://github.com/trycourier/courier-node/compare/v3.6.0...v3.7.0
259
- [v3.6.0]: https://github.com/trycourier/courier-node/compare/v3.5.0...v3.6.0
260
- [v3.5.0]: https://github.com/trycourier/courier-node/compare/v3.4.0...v3.5.0
261
- [v3.4.0]: https://github.com/trycourier/courier-node/compare/v3.3.0...v3.4.0
262
- [v3.3.0]: https://github.com/trycourier/courier-node/compare/v3.2.1...v3.3.0
263
- [v3.2.1]: https://github.com/trycourier/courier-node/compare/v3.2.0...v3.2.1
264
- [v3.2.0]: https://github.com/trycourier/courier-node/compare/v3.1.0...v3.2.0
265
- [v3.1.0]: https://github.com/trycourier/courier-node/compare/v3.0.0...v3.1.0
266
- [v3.0.0]: https://github.com/trycourier/courier-node/compare/v2.8.0...v3.0.0
267
- [v2.8.0]: https://github.com/trycourier/courier-node/compare/v2.7.0...v2.8.0
268
- [v2.7.0]: https://github.com/trycourier/courier-node/compare/v2.6.0...v2.7.0
269
- [v2.6.0]: https://github.com/trycourier/courier-node/compare/v2.4.0...v2.6.0
270
- [v2.4.0]: https://github.com/trycourier/courier-node/compare/v2.3.0...v2.4.0
271
- [v2.3.0]: https://github.com/trycourier/courier-node/compare/v2.2.0...v2.3.0
272
- [v2.2.0]: https://github.com/trycourier/courier-node/compare/v2.1.0...v2.2.0
273
- [v2.1.0]: https://github.com/trycourier/courier-node/compare/v2.0.0...v2.1.0
274
- [v2.0.0]: https://github.com/trycourier/courier-node/compare/v1.7.3...v2.0.0
275
- [v1.7.3]: https://github.com/trycourier/courier-node/compare/v1.7.2...v1.7.3
276
- [v1.7.2]: https://github.com/trycourier/courier-node/compare/v1.7.1...v1.7.2
277
- [v1.7.1]: https://github.com/trycourier/courier-node/compare/v1.7.0...v1.7.1
278
- [v1.7.0]: https://github.com/trycourier/courier-node/compare/v1.6.2...v1.7.0
279
- [v1.6.2]: https://github.com/trycourier/courier-node/compare/v1.6.1...v1.6.2
280
- [v1.6.1]: https://github.com/trycourier/courier-node/compare/v1.6.0...v1.6.1
281
- [v1.6.0]: https://github.com/trycourier/courier-node/compare/v1.5.0...v1.6.0
282
- [v1.5.0]: https://github.com/trycourier/courier-node/compare/v1.4.0...v1.5.0
283
- [v1.4.0]: https://github.com/trycourier/courier-node/compare/v1.3.0...v1.4.0
284
- [v1.3.0]: https://github.com/trycourier/courier-node/compare/v1.2.1...v1.3.0
285
- [v1.2.1]: https://github.com/trycourier/courier-node/compare/v1.2.0...v1.2.1
286
- [v1.2.0]: https://github.com/trycourier/courier-node/compare/v1.1.6...v1.2.0
287
- [v1.1.6]: https://github.com/trycourier/courier-node/compare/v1.1.5...v1.1.6
288
- [v1.1.5]: https://github.com/trycourier/courier-node/compare/v1.1.4...v1.1.5
289
- [v1.1.4]: https://github.com/trycourier/courier-node/compare/v1.1.3...v1.1.4
290
- [v1.1.3]: https://github.com/trycourier/courier-node/compare/v1.1.2...v1.1.3
291
- [v1.1.2]: https://github.com/trycourier/courier-node/compare/v1.1.1...v1.1.2
292
- [v1.1.1]: https://github.com/trycourier/courier-node/compare/v1.1.0...v1.1.1
293
- [v1.1.0]: https://github.com/trycourier/courier-node/compare/v1.0.4...v1.1.0
294
- [v1.0.4]: https://github.com/trycourier/courier-node/compare/v1.0.3...v1.0.4
295
- [v1.0.3]: https://github.com/trycourier/courier-node/compare/v1.0.2...v1.0.3
296
- [v1.0.2]: https://github.com/trycourier/courier-node/compare/v1.0.1...v1.0.2