nylas 5.8.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.
Files changed (79) hide show
  1. package/LICENSE.txt +22 -0
  2. package/README.md +65 -0
  3. package/lib/config.d.ts +4 -0
  4. package/lib/config.js +13 -0
  5. package/lib/models/account.d.ts +10 -0
  6. package/lib/models/account.js +61 -0
  7. package/lib/models/attributes.d.ts +104 -0
  8. package/lib/models/attributes.js +266 -0
  9. package/lib/models/calendar-restful-model-collection.d.ts +76 -0
  10. package/lib/models/calendar-restful-model-collection.js +128 -0
  11. package/lib/models/calendar.d.ts +19 -0
  12. package/lib/models/calendar.js +81 -0
  13. package/lib/models/connect.d.ts +12 -0
  14. package/lib/models/connect.js +51 -0
  15. package/lib/models/contact-restful-model-collection.d.ts +11 -0
  16. package/lib/models/contact-restful-model-collection.js +54 -0
  17. package/lib/models/contact.d.ts +75 -0
  18. package/lib/models/contact.js +245 -0
  19. package/lib/models/delta-stream.d.ts +30 -0
  20. package/lib/models/delta-stream.js +225 -0
  21. package/lib/models/delta.d.ts +11 -0
  22. package/lib/models/delta.js +91 -0
  23. package/lib/models/draft.d.ts +24 -0
  24. package/lib/models/draft.js +145 -0
  25. package/lib/models/email-participant.d.ts +6 -0
  26. package/lib/models/email-participant.js +44 -0
  27. package/lib/models/event-conferencing.d.ts +24 -0
  28. package/lib/models/event-conferencing.js +84 -0
  29. package/lib/models/event-participant.d.ts +7 -0
  30. package/lib/models/event-participant.js +47 -0
  31. package/lib/models/event.d.ts +45 -0
  32. package/lib/models/event.js +206 -0
  33. package/lib/models/file.d.ts +18 -0
  34. package/lib/models/file.js +151 -0
  35. package/lib/models/folder.d.ts +14 -0
  36. package/lib/models/folder.js +70 -0
  37. package/lib/models/job-status.d.ts +7 -0
  38. package/lib/models/job-status.js +51 -0
  39. package/lib/models/management-account.d.ts +14 -0
  40. package/lib/models/management-account.js +101 -0
  41. package/lib/models/management-model-collection.d.ts +11 -0
  42. package/lib/models/management-model-collection.js +35 -0
  43. package/lib/models/management-model.d.ts +8 -0
  44. package/lib/models/management-model.js +29 -0
  45. package/lib/models/message.d.ts +32 -0
  46. package/lib/models/message.js +154 -0
  47. package/lib/models/neural-categorizer.d.ts +18 -0
  48. package/lib/models/neural-categorizer.js +140 -0
  49. package/lib/models/neural-clean-conversation.d.ts +7 -0
  50. package/lib/models/neural-clean-conversation.js +110 -0
  51. package/lib/models/neural-ocr.d.ts +5 -0
  52. package/lib/models/neural-ocr.js +46 -0
  53. package/lib/models/neural-sentiment-analysis.d.ts +8 -0
  54. package/lib/models/neural-sentiment-analysis.js +49 -0
  55. package/lib/models/neural-signature-contact.d.ts +37 -0
  56. package/lib/models/neural-signature-contact.js +141 -0
  57. package/lib/models/neural-signature-extraction.d.ts +7 -0
  58. package/lib/models/neural-signature-extraction.js +50 -0
  59. package/lib/models/neural.d.ts +22 -0
  60. package/lib/models/neural.js +124 -0
  61. package/lib/models/nylas-api-error.d.ts +28 -0
  62. package/lib/models/nylas-api-error.js +61 -0
  63. package/lib/models/resource.d.ts +9 -0
  64. package/lib/models/resource.js +59 -0
  65. package/lib/models/restful-model-collection.d.ts +59 -0
  66. package/lib/models/restful-model-collection.js +325 -0
  67. package/lib/models/restful-model-instance.d.ts +11 -0
  68. package/lib/models/restful-model-instance.js +37 -0
  69. package/lib/models/restful-model.d.ts +48 -0
  70. package/lib/models/restful-model.js +153 -0
  71. package/lib/models/thread.d.ts +30 -0
  72. package/lib/models/thread.js +112 -0
  73. package/lib/models/webhook.d.ts +15 -0
  74. package/lib/models/webhook.js +71 -0
  75. package/lib/nylas-connection.d.ts +67 -0
  76. package/lib/nylas-connection.js +235 -0
  77. package/lib/nylas.d.ts +38 -0
  78. package/lib/nylas.js +178 -0
  79. package/package.json +76 -0
@@ -0,0 +1,245 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
27
+ return (mod && mod.__esModule) ? mod : { "default": mod };
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.Contact = exports.Group = exports.WebPage = exports.PhoneNumber = exports.EmailAddress = void 0;
31
+ var restful_model_1 = __importDefault(require("./restful-model"));
32
+ var attributes_1 = __importDefault(require("./attributes"));
33
+ var EmailAddress = /** @class */ (function (_super) {
34
+ __extends(EmailAddress, _super);
35
+ function EmailAddress() {
36
+ return _super !== null && _super.apply(this, arguments) || this;
37
+ }
38
+ EmailAddress.prototype.toJSON = function () {
39
+ return {
40
+ type: this.type,
41
+ email: this.email,
42
+ };
43
+ };
44
+ return EmailAddress;
45
+ }(restful_model_1.default));
46
+ exports.EmailAddress = EmailAddress;
47
+ EmailAddress.collectionName = 'email_addresses';
48
+ EmailAddress.attributes = __assign(__assign({}, restful_model_1.default.attributes), { type: attributes_1.default.String({
49
+ modelKey: 'type',
50
+ }), email: attributes_1.default.String({
51
+ modelKey: 'email',
52
+ }) });
53
+ var IMAddress = /** @class */ (function (_super) {
54
+ __extends(IMAddress, _super);
55
+ function IMAddress() {
56
+ return _super !== null && _super.apply(this, arguments) || this;
57
+ }
58
+ IMAddress.prototype.toJSON = function () {
59
+ return {
60
+ type: this.type,
61
+ im_address: this.imAddress,
62
+ };
63
+ };
64
+ return IMAddress;
65
+ }(restful_model_1.default));
66
+ IMAddress.collectionName = 'im_addresses';
67
+ IMAddress.attributes = __assign(__assign({}, restful_model_1.default.attributes), { type: attributes_1.default.String({
68
+ modelKey: 'type',
69
+ }), imAddress: attributes_1.default.String({
70
+ modelKey: 'imAddress',
71
+ jsonKey: 'im_address',
72
+ }) });
73
+ var PhysicalAddress = /** @class */ (function (_super) {
74
+ __extends(PhysicalAddress, _super);
75
+ function PhysicalAddress() {
76
+ return _super !== null && _super.apply(this, arguments) || this;
77
+ }
78
+ PhysicalAddress.prototype.toJSON = function () {
79
+ var json = {
80
+ type: this.type,
81
+ format: this.format,
82
+ };
83
+ if (this.format == 'unstructured') {
84
+ json.address = this.address;
85
+ }
86
+ else {
87
+ json.street_address = this.streetAddress;
88
+ json.postal_code = this.postalCode;
89
+ json.state = this.state;
90
+ json.city = this.city;
91
+ json.country = this.country;
92
+ }
93
+ return json;
94
+ };
95
+ return PhysicalAddress;
96
+ }(restful_model_1.default));
97
+ PhysicalAddress.collectionName = 'physical_addresses';
98
+ PhysicalAddress.attributes = __assign(__assign({}, restful_model_1.default.attributes), { type: attributes_1.default.String({
99
+ modelKey: 'type',
100
+ }), format: attributes_1.default.String({
101
+ modelKey: 'format',
102
+ }), address: attributes_1.default.String({
103
+ modelKey: 'address',
104
+ }), streetAddress: attributes_1.default.String({
105
+ modelKey: 'streetAddress',
106
+ jsonKey: 'street_address',
107
+ }), city: attributes_1.default.String({
108
+ modelKey: 'city',
109
+ }), postalCode: attributes_1.default.String({
110
+ modelKey: 'postalCode',
111
+ jsonKey: 'postal_code',
112
+ }), state: attributes_1.default.String({
113
+ modelKey: 'state',
114
+ }), country: attributes_1.default.String({
115
+ modelKey: 'country',
116
+ }) });
117
+ var PhoneNumber = /** @class */ (function (_super) {
118
+ __extends(PhoneNumber, _super);
119
+ function PhoneNumber() {
120
+ return _super !== null && _super.apply(this, arguments) || this;
121
+ }
122
+ PhoneNumber.prototype.toJSON = function () {
123
+ return {
124
+ type: this.type,
125
+ number: this.number,
126
+ };
127
+ };
128
+ return PhoneNumber;
129
+ }(restful_model_1.default));
130
+ exports.PhoneNumber = PhoneNumber;
131
+ PhoneNumber.collectionName = 'phone_numbers';
132
+ PhoneNumber.attributes = __assign(__assign({}, restful_model_1.default.attributes), { type: attributes_1.default.String({
133
+ modelKey: 'type',
134
+ }), number: attributes_1.default.String({
135
+ modelKey: 'number',
136
+ }) });
137
+ var WebPage = /** @class */ (function (_super) {
138
+ __extends(WebPage, _super);
139
+ function WebPage() {
140
+ return _super !== null && _super.apply(this, arguments) || this;
141
+ }
142
+ WebPage.prototype.toJSON = function () {
143
+ var json = {
144
+ type: this.type,
145
+ url: this.url,
146
+ };
147
+ return json;
148
+ };
149
+ return WebPage;
150
+ }(restful_model_1.default));
151
+ exports.WebPage = WebPage;
152
+ WebPage.collectionName = 'web_pages';
153
+ WebPage.attributes = __assign(__assign({}, restful_model_1.default.attributes), { type: attributes_1.default.String({
154
+ modelKey: 'type',
155
+ }), url: attributes_1.default.String({
156
+ modelKey: 'url',
157
+ }) });
158
+ var Group = /** @class */ (function (_super) {
159
+ __extends(Group, _super);
160
+ function Group() {
161
+ return _super !== null && _super.apply(this, arguments) || this;
162
+ }
163
+ return Group;
164
+ }(restful_model_1.default));
165
+ exports.Group = Group;
166
+ Group.collectionName = 'groups';
167
+ Group.attributes = __assign(__assign({}, restful_model_1.default.attributes), { name: attributes_1.default.String({
168
+ modelKey: 'name',
169
+ }), path: attributes_1.default.String({
170
+ modelKey: 'path',
171
+ }) });
172
+ var Contact = /** @class */ (function (_super) {
173
+ __extends(Contact, _super);
174
+ function Contact() {
175
+ return _super !== null && _super.apply(this, arguments) || this;
176
+ }
177
+ Contact.prototype.save = function (params, callback) {
178
+ if (params === void 0) { params = {}; }
179
+ return this._save(params, callback);
180
+ };
181
+ Contact.prototype.getPicture = function (params, callback) {
182
+ if (params === void 0) { params = {}; }
183
+ return this._get(params, callback, '/picture');
184
+ };
185
+ return Contact;
186
+ }(restful_model_1.default));
187
+ exports.Contact = Contact;
188
+ Contact.collectionName = 'contacts';
189
+ Contact.attributes = __assign(__assign({}, restful_model_1.default.attributes), { givenName: attributes_1.default.String({
190
+ modelKey: 'givenName',
191
+ jsonKey: 'given_name',
192
+ }), middleName: attributes_1.default.String({
193
+ modelKey: 'middleName',
194
+ jsonKey: 'middle_name',
195
+ }), surname: attributes_1.default.String({
196
+ modelKey: 'surname',
197
+ }), suffix: attributes_1.default.String({
198
+ modelKey: 'suffix',
199
+ }), nickname: attributes_1.default.String({
200
+ modelKey: 'nickname',
201
+ }), birthday: attributes_1.default.String({
202
+ modelKey: 'birthday',
203
+ }), companyName: attributes_1.default.String({
204
+ modelKey: 'companyName',
205
+ jsonKey: 'company_name',
206
+ }), jobTitle: attributes_1.default.String({
207
+ modelKey: 'jobTitle',
208
+ jsonKey: 'job_title',
209
+ }), managerName: attributes_1.default.String({
210
+ modelKey: 'managerName',
211
+ jsonKey: 'manager_name',
212
+ }), officeLocation: attributes_1.default.String({
213
+ modelKey: 'officeLocation',
214
+ jsonKey: 'office_location',
215
+ }), notes: attributes_1.default.String({
216
+ modelKey: 'notes',
217
+ }), pictureUrl: attributes_1.default.String({
218
+ modelKey: 'pictureUrl',
219
+ jsonKey: 'picture_url',
220
+ }), emailAddresses: attributes_1.default.Collection({
221
+ modelKey: 'emailAddresses',
222
+ jsonKey: 'emails',
223
+ itemClass: EmailAddress,
224
+ }), imAddresses: attributes_1.default.Collection({
225
+ modelKey: 'imAddresses',
226
+ jsonKey: 'im_addresses',
227
+ itemClass: IMAddress,
228
+ }), physicalAddresses: attributes_1.default.Collection({
229
+ modelKey: 'physicalAddresses',
230
+ jsonKey: 'physical_addresses',
231
+ itemClass: PhysicalAddress,
232
+ }), phoneNumbers: attributes_1.default.Collection({
233
+ modelKey: 'phoneNumbers',
234
+ jsonKey: 'phone_numbers',
235
+ itemClass: PhoneNumber,
236
+ }), webPages: attributes_1.default.Collection({
237
+ modelKey: 'webPages',
238
+ jsonKey: 'web_pages',
239
+ itemClass: WebPage,
240
+ }), groups: attributes_1.default.Collection({
241
+ modelKey: 'groups',
242
+ itemClass: Group,
243
+ }), source: attributes_1.default.String({
244
+ modelKey: 'source',
245
+ }) });
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import NylasConnection from '../nylas-connection';
4
+ import { Request } from 'node-fetch';
5
+ import AbortController from 'abort-controller';
6
+ import backoff from 'backoff';
7
+ export default class DeltaStream extends EventEmitter {
8
+ static MAX_RESTART_RETRIES: number;
9
+ connection: NylasConnection;
10
+ cursor?: string;
11
+ params: {
12
+ includeTypes?: string[];
13
+ excludeTypes?: string[];
14
+ expanded?: boolean;
15
+ };
16
+ requestInfo?: {
17
+ request: Request;
18
+ controller: AbortController;
19
+ };
20
+ restartBackoff: backoff.Backoff;
21
+ timeoutId?: number;
22
+ constructor(connection: NylasConnection, cursor: string, params?: {
23
+ [key: string]: any;
24
+ });
25
+ close(): void;
26
+ open(): Promise<any>;
27
+ _onDataReceived(): void;
28
+ _onError(err: Error): void;
29
+ _restartConnection(n: number): Promise<any>;
30
+ }
@@ -0,0 +1,225 @@
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 __rest = (this && this.__rest) || function (s, e) {
63
+ var t = {};
64
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
65
+ t[p] = s[p];
66
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
67
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
68
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
69
+ t[p[i]] = s[p[i]];
70
+ }
71
+ return t;
72
+ };
73
+ var __importDefault = (this && this.__importDefault) || function (mod) {
74
+ return (mod && mod.__esModule) ? mod : { "default": mod };
75
+ };
76
+ Object.defineProperty(exports, "__esModule", { value: true });
77
+ /*
78
+ A connection to the Nylas delta streaming API.
79
+
80
+ Emits the following events:
81
+ - `response` when the connection is established, with one argument, a `http.IncomingMessage`
82
+ - `delta` for each delta received
83
+ - `error` when an error occurs in the connection
84
+ - `info` when the connection status changes
85
+ */
86
+ var events_1 = require("events");
87
+ var nylas_connection_1 = __importDefault(require("../nylas-connection"));
88
+ var node_fetch_1 = __importDefault(require("node-fetch"));
89
+ var abort_controller_1 = __importDefault(require("abort-controller"));
90
+ var backoff_1 = __importDefault(require("backoff"));
91
+ var JSONStream_1 = __importDefault(require("JSONStream"));
92
+ var delta_1 = __importDefault(require("./delta"));
93
+ var DeltaStream = /** @class */ (function (_super) {
94
+ __extends(DeltaStream, _super);
95
+ // @param {string} cursor Nylas delta API cursor
96
+ // @param {Object} params object contianing query string params to be passed to the request
97
+ // @param {Array<string>} params.excludeTypes object types to not return deltas for (e.g., {excludeTypes: ['thread']})
98
+ // @param {Array<string>} params.includeTypes object types to exclusively return deltas for (e.g., {includeTypes: ['thread']})
99
+ // @param {boolean} params.expanded boolean to specify wether to request the expanded view
100
+ function DeltaStream(connection, cursor, params) {
101
+ if (params === void 0) { params = {}; }
102
+ var _this = _super.call(this) || this;
103
+ _this.restartBackoff = backoff_1.default.exponential({
104
+ randomisationFactor: 0.5,
105
+ initialDelay: 250,
106
+ maxDelay: 30000,
107
+ factor: 4,
108
+ });
109
+ _this.connection = connection;
110
+ _this.cursor = cursor;
111
+ _this.params = params;
112
+ if (!(_this.connection instanceof nylas_connection_1.default)) {
113
+ throw new Error('Connection object not provided');
114
+ }
115
+ _this.restartBackoff.failAfter(DeltaStream.MAX_RESTART_RETRIES);
116
+ _this.restartBackoff
117
+ .on('backoff', _this._restartConnection.bind(_this))
118
+ .on('fail', function () {
119
+ return _this.emit('error', "Nylas DeltaStream failed to reconnect after\n " + DeltaStream.MAX_RESTART_RETRIES + "\n retries.");
120
+ });
121
+ return _this;
122
+ }
123
+ DeltaStream.prototype.close = function () {
124
+ clearTimeout(this.timeoutId);
125
+ delete this.timeoutId;
126
+ this.restartBackoff.reset();
127
+ if (this.requestInfo) {
128
+ this.requestInfo.controller.abort();
129
+ }
130
+ delete this.requestInfo;
131
+ };
132
+ DeltaStream.prototype.open = function () {
133
+ return __awaiter(this, void 0, void 0, function () {
134
+ var path, _a, _b, excludeTypes, _c, includeTypes, params, queryObj, request, controller, response, error_1;
135
+ var _this = this;
136
+ return __generator(this, function (_d) {
137
+ switch (_d.label) {
138
+ case 0:
139
+ this.close();
140
+ path = '/delta/streaming';
141
+ _a = this.params, _b = _a.excludeTypes, excludeTypes = _b === void 0 ? [] : _b, _c = _a.includeTypes, includeTypes = _c === void 0 ? [] : _c, params = __rest(_a, ["excludeTypes", "includeTypes"]);
142
+ queryObj = __assign(__assign({}, params), { cursor: this.cursor });
143
+ if (excludeTypes.length > 0) {
144
+ queryObj.exclude_types = excludeTypes.join(',');
145
+ }
146
+ if (includeTypes.length > 0) {
147
+ queryObj.include_types = includeTypes.join(',');
148
+ }
149
+ request = this.connection.newRequest({
150
+ method: 'GET',
151
+ path: path,
152
+ qs: queryObj,
153
+ });
154
+ _d.label = 1;
155
+ case 1:
156
+ _d.trys.push([1, 3, , 4]);
157
+ controller = new abort_controller_1.default();
158
+ this.requestInfo = {
159
+ request: request,
160
+ controller: controller,
161
+ };
162
+ return [4 /*yield*/, node_fetch_1.default(request, { signal: controller.signal })];
163
+ case 2:
164
+ response = _d.sent();
165
+ if (response.status !== 200) {
166
+ response.body.on('data', function (data) {
167
+ var err = data;
168
+ try {
169
+ err = JSON.parse(err);
170
+ }
171
+ catch (e) {
172
+ // Do nothing
173
+ }
174
+ // Do nothing, keep err as string.
175
+ return _this._onError(err);
176
+ });
177
+ return [2 /*return*/];
178
+ }
179
+ // Successfully established connection
180
+ this.emit('response', response);
181
+ this._onDataReceived();
182
+ return [2 /*return*/, (response.body
183
+ .on('data', this._onDataReceived.bind(this))
184
+ // Each data block received may not be a complete JSON object. Pipe through
185
+ // JSONStream.parse(), which handles converting data blocks to JSON objects.
186
+ .pipe(JSONStream_1.default.parse())
187
+ .on('data', function (obj) {
188
+ if (obj.cursor) {
189
+ _this.cursor = obj.cursor;
190
+ }
191
+ return _this.emit('delta', obj);
192
+ }))];
193
+ case 3:
194
+ error_1 = _d.sent();
195
+ this._onError(error_1);
196
+ return [3 /*break*/, 4];
197
+ case 4: return [2 /*return*/];
198
+ }
199
+ });
200
+ });
201
+ };
202
+ DeltaStream.prototype._onDataReceived = function () {
203
+ // Nylas sends a newline heartbeat in the raw data stream once every 5 seconds.
204
+ // Automatically restart the connection if we haven't gotten any data in
205
+ // Delta.streamingTimeoutMs. The connection will restart with the last
206
+ // received cursor.
207
+ clearTimeout(this.timeoutId);
208
+ this.restartBackoff.reset();
209
+ this.timeoutId = setTimeout(this.restartBackoff.backoff.bind(this.restartBackoff), delta_1.default.streamingTimeoutMs);
210
+ };
211
+ DeltaStream.prototype._onError = function (err) {
212
+ this.emit('error', err);
213
+ return this.restartBackoff.reset();
214
+ };
215
+ DeltaStream.prototype._restartConnection = function (n) {
216
+ this.emit('info', "Restarting Nylas DeltaStream connection (attempt " + (n + 1) + "): " + (this.requestInfo != null ? this.requestInfo.request.url : undefined));
217
+ this.close();
218
+ return this.open();
219
+ };
220
+ // Max number of times to retry a connection if we receive no data heartbeats
221
+ // from the Nylas server.
222
+ DeltaStream.MAX_RESTART_RETRIES = 5;
223
+ return DeltaStream;
224
+ }(events_1.EventEmitter));
225
+ exports.default = DeltaStream;