nylas 6.1.0 → 6.2.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.
@@ -1,5 +1,5 @@
1
1
  import Model from './model';
2
- import RestfulModel from './restful-model';
2
+ declare type AnyModel = new (...args: any[]) => Model;
3
3
  export declare abstract class Attribute {
4
4
  modelKey: string;
5
5
  jsonKey: string;
@@ -18,7 +18,7 @@ declare class AttributeObject extends Attribute {
18
18
  constructor({ modelKey, jsonKey, itemClass, readOnly, }: {
19
19
  modelKey: string;
20
20
  jsonKey?: string;
21
- itemClass?: typeof Model | typeof RestfulModel;
21
+ itemClass?: AnyModel;
22
22
  readOnly?: boolean;
23
23
  });
24
24
  toJSON(val: any, saveRequestBody?: boolean): unknown;
@@ -58,12 +58,12 @@ declare class AttributeCollection extends Attribute {
58
58
  constructor({ modelKey, jsonKey, itemClass, readOnly, }: {
59
59
  modelKey: string;
60
60
  jsonKey?: string;
61
- itemClass: typeof Model | typeof RestfulModel;
61
+ itemClass: AnyModel;
62
62
  readOnly?: boolean;
63
63
  });
64
- toJSON(vals: any, saveRequestBody?: boolean): unknown[];
65
- fromJSON(json: unknown[], _parent: any): typeof Model[] | typeof RestfulModel[] | unknown[];
66
- saveRequestBody(val: any): unknown;
64
+ toJSON(vals: any, saveRequestBody?: boolean): AnyModel[];
65
+ fromJSON(json: unknown[], _parent: any): AnyModel[];
66
+ saveRequestBody(val: any): AnyModel[] | undefined;
67
67
  }
68
68
  declare class AttributeEnum extends Attribute {
69
69
  itemClass: any;
@@ -121,7 +121,7 @@ declare const Attributes: {
121
121
  Collection(__0: {
122
122
  modelKey: string;
123
123
  jsonKey?: string | undefined;
124
- itemClass: typeof Model | typeof RestfulModel;
124
+ itemClass: AnyModel;
125
125
  readOnly?: boolean | undefined;
126
126
  }): AttributeCollection;
127
127
  Boolean(__0: {
@@ -132,7 +132,7 @@ declare const Attributes: {
132
132
  Object(__0: {
133
133
  modelKey: string;
134
134
  jsonKey?: string | undefined;
135
- itemClass?: typeof Model | typeof RestfulModel | undefined;
135
+ itemClass?: AnyModel | undefined;
136
136
  readOnly?: boolean | undefined;
137
137
  }): AttributeObject;
138
138
  Enum(__0: {
@@ -19,15 +19,11 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
19
19
  r[k] = a[j];
20
20
  return r;
21
21
  };
22
- var __importDefault = (this && this.__importDefault) || function (mod) {
23
- return (mod && mod.__esModule) ? mod : { "default": mod };
24
- };
25
22
  Object.defineProperty(exports, "__esModule", { value: true });
26
- var restful_model_1 = __importDefault(require("./restful-model"));
27
- // The Attribute class represents a single model attribute, like 'namespace_id'
28
- // Subclasses of Attribute like AttributeDateTime know how to covert between
29
- // the JSON representation of that type and the javascript representation.
30
- // The Attribute class also exposes convenience methods for generating Matchers.
23
+ function isRestfulModel(cls) {
24
+ // A 'RestfulModel' has 'endpointName' and 'collectionName' unlike 'Model'
25
+ return cls.endpointName !== undefined && cls.collectionName !== undefined;
26
+ }
31
27
  var Attribute = /** @class */ (function () {
32
28
  function Attribute(_a) {
33
29
  var modelKey = _a.modelKey, jsonKey = _a.jsonKey, readOnly = _a.readOnly;
@@ -68,8 +64,8 @@ var AttributeObject = /** @class */ (function (_super) {
68
64
  if (!val || !this.itemClass) {
69
65
  return val;
70
66
  }
71
- if (this.itemClass.prototype instanceof restful_model_1.default) {
72
- return new this.itemClass(_parent.connection).fromJSON(val);
67
+ if (isRestfulModel(this.itemClass)) {
68
+ return new this.itemClass(_parent.connection, val).fromJSON(val);
73
69
  }
74
70
  return new this.itemClass(val).fromJSON(val);
75
71
  };
@@ -239,8 +235,8 @@ var AttributeCollection = /** @class */ (function (_super) {
239
235
  for (var _i = 0, json_1 = json; _i < json_1.length; _i++) {
240
236
  var objJSON = json_1[_i];
241
237
  var obj = void 0;
242
- if (this.itemClass.prototype instanceof restful_model_1.default) {
243
- obj = new this.itemClass(_parent.connection).fromJSON(objJSON);
238
+ if (isRestfulModel(this.itemClass)) {
239
+ obj = new this.itemClass(_parent.connection, objJSON).fromJSON(objJSON);
244
240
  }
245
241
  else {
246
242
  obj = new this.itemClass(objJSON).fromJSON(objJSON);
@@ -286,16 +282,18 @@ var AttributeEnumList = /** @class */ (function (_super) {
286
282
  }
287
283
  AttributeEnumList.prototype.toJSON = function (val) {
288
284
  var enumList = [];
289
- for (var v in val) {
285
+ for (var _i = 0, val_1 = val; _i < val_1.length; _i++) {
286
+ var v = val_1[_i];
290
287
  enumList.push(v.toString());
291
288
  }
292
289
  return enumList;
293
290
  };
294
291
  AttributeEnumList.prototype.fromJSON = function (val, _parent) {
295
292
  var enumList = [];
296
- for (var v in val) {
297
- if (Object.values(this.itemClass).includes(val[v])) {
298
- enumList.push(val[v]);
293
+ for (var _i = 0, val_2 = val; _i < val_2.length; _i++) {
294
+ var v = val_2[_i];
295
+ if (Object.values(this.itemClass).includes(v)) {
296
+ enumList.push(v);
299
297
  }
300
298
  }
301
299
  return enumList;
@@ -19,9 +19,7 @@ export declare enum Scope {
19
19
  export declare type VirtualCalendarProperties = {
20
20
  name: string;
21
21
  emailAddress: string;
22
- scopes: Scope[];
23
22
  clientId?: string;
24
- settings?: Record<string, any>;
25
23
  };
26
24
  export declare class VirtualCalendar extends Model implements VirtualCalendarProperties {
27
25
  provider: string;
@@ -45,6 +43,7 @@ export declare enum NativeAuthenticationProvider {
45
43
  Office365 = "office365"
46
44
  }
47
45
  export declare type NativeAuthenticationProperties = VirtualCalendarProperties & {
46
+ scopes: Scope[];
48
47
  settings: Record<string, any>;
49
48
  provider: NativeAuthenticationProvider;
50
49
  };
@@ -41,9 +41,9 @@ var VirtualCalendar = /** @class */ (function (_super) {
41
41
  _this.provider = 'nylas';
42
42
  _this.name = '';
43
43
  _this.emailAddress = '';
44
- _this.scopes = [];
45
44
  _this.settings = {};
46
45
  _this.initAttributes(props);
46
+ _this.scopes = [Scope.Calendar];
47
47
  return _this;
48
48
  }
49
49
  VirtualCalendar.attributes = {
@@ -149,7 +149,7 @@ var Connect = /** @class */ (function () {
149
149
  if (!auth.clientId) {
150
150
  auth.clientId = this.clientId;
151
151
  }
152
- if (auth.hasOwnProperty('scopes')) {
152
+ if (auth.hasOwnProperty('provider') && auth.provider != 'nylas') {
153
153
  authClass = new NativeAuthentication(auth);
154
154
  }
155
155
  else {
@@ -0,0 +1,17 @@
1
+ import NylasConnection from '../nylas-connection';
2
+ import DeltaStream, { DeltaLongPoll } from './delta-stream';
3
+ import { DeltaParams } from './delta';
4
+ import { Deltas } from './deltas';
5
+ export declare type LatestCursor = {
6
+ cursor: string;
7
+ };
8
+ export default class DeltaCollection {
9
+ connection: NylasConnection;
10
+ private path;
11
+ constructor(connection: NylasConnection);
12
+ latestCursor(callback: (error: Error | null, cursor: string | null) => void): Promise<string>;
13
+ since(cursor: string, params?: DeltaParams): Promise<Deltas>;
14
+ longPoll(cursor: string, timeout: number, params?: DeltaParams): Promise<DeltaLongPoll>;
15
+ startStream(cursor: string, params?: Record<string, unknown>): Promise<DeltaStream>;
16
+ private buildDeltaParams;
17
+ }
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importStar = (this && this.__importStar) || function (mod) {
50
+ if (mod && mod.__esModule) return mod;
51
+ var result = {};
52
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
53
+ result["default"] = mod;
54
+ return result;
55
+ };
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ var delta_stream_1 = __importStar(require("./delta-stream"));
58
+ var deltas_1 = require("./deltas");
59
+ var DeltaCollection = /** @class */ (function () {
60
+ function DeltaCollection(connection) {
61
+ this.path = '/delta';
62
+ this.connection = connection;
63
+ }
64
+ DeltaCollection.prototype.latestCursor = function (callback) {
65
+ var reqOpts = {
66
+ method: 'POST',
67
+ path: this.path + "/latest_cursor",
68
+ };
69
+ return this.connection
70
+ .request(reqOpts)
71
+ .then(function (response) {
72
+ if (callback) {
73
+ callback(null, response.cursor);
74
+ }
75
+ return Promise.resolve(response.cursor);
76
+ })
77
+ .catch(function (err) {
78
+ if (callback) {
79
+ callback(err, null);
80
+ }
81
+ return Promise.reject(err);
82
+ });
83
+ };
84
+ DeltaCollection.prototype.since = function (cursor, params) {
85
+ var _this = this;
86
+ var queryParams = this.buildDeltaParams(params);
87
+ return this.connection
88
+ .request({
89
+ method: 'GET',
90
+ path: "" + this.path,
91
+ qs: __assign({ cursor: cursor }, queryParams),
92
+ })
93
+ .then(function (response) {
94
+ return Promise.resolve(new deltas_1.Deltas(_this.connection).fromJSON(response));
95
+ })
96
+ .catch(function (err) {
97
+ return Promise.reject(err);
98
+ });
99
+ };
100
+ DeltaCollection.prototype.longPoll = function (cursor, timeout, params) {
101
+ return __awaiter(this, void 0, void 0, function () {
102
+ var queryParams, stream;
103
+ return __generator(this, function (_a) {
104
+ switch (_a.label) {
105
+ case 0:
106
+ queryParams = this.buildDeltaParams(params);
107
+ stream = new delta_stream_1.DeltaLongPoll(this.connection, cursor, timeout, queryParams);
108
+ return [4 /*yield*/, stream.open(true)];
109
+ case 1:
110
+ _a.sent();
111
+ return [2 /*return*/, stream];
112
+ }
113
+ });
114
+ });
115
+ };
116
+ DeltaCollection.prototype.startStream = function (cursor, params) {
117
+ if (params === void 0) { params = {}; }
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var stream;
120
+ return __generator(this, function (_a) {
121
+ switch (_a.label) {
122
+ case 0:
123
+ stream = new delta_stream_1.default(this.connection, cursor, params);
124
+ return [4 /*yield*/, stream.open()];
125
+ case 1:
126
+ _a.sent();
127
+ return [2 /*return*/, stream];
128
+ }
129
+ });
130
+ });
131
+ };
132
+ DeltaCollection.prototype.buildDeltaParams = function (params) {
133
+ return __assign(__assign(__assign({}, ((params === null || params === void 0 ? void 0 : params.view) && { view: params.view })), ((params === null || params === void 0 ? void 0 : params.excludeTypes) && {
134
+ exclude_types: params.excludeTypes.join(),
135
+ })), ((params === null || params === void 0 ? void 0 : params.includeTypes) && { includeTypes: params.includeTypes.join() }));
136
+ };
137
+ return DeltaCollection;
138
+ }());
139
+ exports.default = DeltaCollection;
@@ -4,15 +4,15 @@ import NylasConnection from '../nylas-connection';
4
4
  import { Request } from 'node-fetch';
5
5
  import AbortController from 'abort-controller';
6
6
  import backoff from 'backoff';
7
+ import Delta, { DeltaParams } from './delta';
8
+ import { Deltas } from './deltas';
7
9
  export default class DeltaStream extends EventEmitter {
8
10
  static MAX_RESTART_RETRIES: number;
9
11
  connection: NylasConnection;
12
+ path: string;
13
+ modelClass: typeof Deltas | typeof Delta;
10
14
  cursor?: string;
11
- params: {
12
- includeTypes?: string[];
13
- excludeTypes?: string[];
14
- expanded?: boolean;
15
- };
15
+ params: DeltaParams;
16
16
  requestInfo?: {
17
17
  request: Request;
18
18
  controller: AbortController;
@@ -21,8 +21,12 @@ export default class DeltaStream extends EventEmitter {
21
21
  timeoutId?: number;
22
22
  constructor(connection: NylasConnection, cursor: string, params?: Record<string, unknown>);
23
23
  close(): void;
24
- open(): Promise<void>;
25
- private onDataReceived;
24
+ open(emitAsModel?: boolean): Promise<void>;
25
+ protected onDataReceived(): void;
26
26
  private onError;
27
27
  private restartConnection;
28
28
  }
29
+ export declare class DeltaLongPoll extends DeltaStream {
30
+ constructor(connection: NylasConnection, cursor: string, timeout: number, params?: Record<string, unknown>);
31
+ protected onDataReceived(): void;
32
+ }
@@ -90,6 +90,7 @@ var abort_controller_1 = __importDefault(require("abort-controller"));
90
90
  var backoff_1 = __importDefault(require("backoff"));
91
91
  var JSONStream_1 = __importDefault(require("JSONStream"));
92
92
  var delta_1 = __importDefault(require("./delta"));
93
+ var deltas_1 = require("./deltas");
93
94
  var DeltaStream = /** @class */ (function (_super) {
94
95
  __extends(DeltaStream, _super);
95
96
  // @param {string} cursor Nylas delta API cursor
@@ -109,6 +110,8 @@ var DeltaStream = /** @class */ (function (_super) {
109
110
  _this.connection = connection;
110
111
  _this.cursor = cursor;
111
112
  _this.params = params;
113
+ _this.path = '/delta/streaming';
114
+ _this.modelClass = delta_1.default;
112
115
  if (!(_this.connection instanceof nylas_connection_1.default)) {
113
116
  throw new Error('Connection object not provided');
114
117
  }
@@ -129,15 +132,14 @@ var DeltaStream = /** @class */ (function (_super) {
129
132
  }
130
133
  delete this.requestInfo;
131
134
  };
132
- DeltaStream.prototype.open = function () {
135
+ DeltaStream.prototype.open = function (emitAsModel) {
133
136
  return __awaiter(this, void 0, void 0, function () {
134
- var path, _a, _b, excludeTypes, _c, includeTypes, params, queryObj, request, controller, response, error_1;
137
+ var _a, _b, excludeTypes, _c, includeTypes, params, queryObj, request, controller, response, error_1;
135
138
  var _this = this;
136
139
  return __generator(this, function (_d) {
137
140
  switch (_d.label) {
138
141
  case 0:
139
142
  this.close();
140
- path = '/delta/streaming';
141
143
  _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
144
  queryObj = __assign(__assign({}, params), { cursor: this.cursor });
143
145
  if (excludeTypes.length > 0) {
@@ -148,7 +150,7 @@ var DeltaStream = /** @class */ (function (_super) {
148
150
  }
149
151
  request = this.connection.newRequest({
150
152
  method: 'GET',
151
- path: path,
153
+ path: this.path,
152
154
  qs: queryObj,
153
155
  });
154
156
  _d.label = 1;
@@ -185,6 +187,9 @@ var DeltaStream = /** @class */ (function (_super) {
185
187
  // JSONStream.parse(), which handles converting data blocks to JSON objects.
186
188
  .pipe(JSONStream_1.default.parse())
187
189
  .on('data', function (obj) {
190
+ if (emitAsModel === true) {
191
+ obj = new _this.modelClass(_this.connection).fromJSON(obj);
192
+ }
188
193
  if (obj.cursor) {
189
194
  _this.cursor = obj.cursor;
190
195
  }
@@ -223,3 +228,23 @@ var DeltaStream = /** @class */ (function (_super) {
223
228
  return DeltaStream;
224
229
  }(events_1.EventEmitter));
225
230
  exports.default = DeltaStream;
231
+ var DeltaLongPoll = /** @class */ (function (_super) {
232
+ __extends(DeltaLongPoll, _super);
233
+ function DeltaLongPoll(connection, cursor, timeout, params) {
234
+ if (params === void 0) { params = {}; }
235
+ var _this = _super.call(this, connection, cursor, params) || this;
236
+ params['timeout'] = timeout;
237
+ _this.params = params;
238
+ _this.path = '/delta/longpoll';
239
+ _this.modelClass = deltas_1.Deltas;
240
+ return _this;
241
+ }
242
+ DeltaLongPoll.prototype.onDataReceived = function () {
243
+ // For streaming we restart the connection on every data received in order
244
+ // to keep the connection alive. For long polling this is not needed as the
245
+ // server terminates the connection when data is sent
246
+ return;
247
+ };
248
+ return DeltaLongPoll;
249
+ }(DeltaStream));
250
+ exports.DeltaLongPoll = DeltaLongPoll;
@@ -1,12 +1,28 @@
1
1
  import NylasConnection from '../nylas-connection';
2
- import DeltaStream from './delta-stream';
3
- export declare type LatestCursor = {
2
+ import { Attribute } from './attributes';
3
+ import RestfulModel from './restful-model';
4
+ export declare type DeltaParams = {
5
+ view?: string;
6
+ includeTypes?: string[];
7
+ excludeTypes?: string[];
8
+ expanded?: boolean;
9
+ };
10
+ export declare type DeltaProperties = {
11
+ id: string;
4
12
  cursor: string;
13
+ event: string;
14
+ object: string;
15
+ objectAttributes?: unknown;
5
16
  };
6
- export default class Delta {
17
+ export default class Delta extends RestfulModel implements DeltaProperties {
18
+ id: string;
19
+ cursor: string;
20
+ event: string;
21
+ object: string;
7
22
  connection: NylasConnection;
23
+ objectAttributes?: RestfulModel;
8
24
  static streamingTimeoutMs: number;
9
- constructor(connection: NylasConnection);
10
- latestCursor(callback: (error: Error | null, cursor: string | null) => void): Promise<string>;
11
- startStream(cursor: string, params?: Record<string, unknown>): Promise<DeltaStream>;
25
+ static attributes: Record<string, Attribute>;
26
+ constructor(connection: NylasConnection, props?: DeltaProperties);
27
+ fromJSON(json: Record<string, unknown>): this;
12
28
  }
@@ -1,90 +1,88 @@
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 (_) 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
- };
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
+ })();
38
15
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
16
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
17
  };
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
22
+ result["default"] = mod;
23
+ return result;
24
+ };
41
25
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var nylas_connection_1 = __importDefault(require("../nylas-connection"));
43
- var delta_stream_1 = __importDefault(require("./delta-stream"));
44
- var Delta = /** @class */ (function () {
45
- function Delta(connection) {
46
- this.connection = connection;
47
- if (!(this.connection instanceof nylas_connection_1.default)) {
48
- throw new Error('Connection object not provided');
26
+ var attributes_1 = __importDefault(require("./attributes"));
27
+ var restful_model_1 = __importDefault(require("./restful-model"));
28
+ var contact_1 = __importDefault(require("./contact"));
29
+ var file_1 = __importDefault(require("./file"));
30
+ var message_1 = __importDefault(require("./message"));
31
+ var draft_1 = __importDefault(require("./draft"));
32
+ var thread_1 = __importDefault(require("./thread"));
33
+ var event_1 = __importDefault(require("./event"));
34
+ var folder_1 = __importStar(require("./folder"));
35
+ var DeltaClassMap = Object.freeze({
36
+ contact: contact_1.default,
37
+ file: file_1.default,
38
+ message: message_1.default,
39
+ draft: draft_1.default,
40
+ thread: thread_1.default,
41
+ event: event_1.default,
42
+ folder: folder_1.default,
43
+ label: folder_1.Label,
44
+ });
45
+ var Delta = /** @class */ (function (_super) {
46
+ __extends(Delta, _super);
47
+ function Delta(connection, props) {
48
+ var _this = _super.call(this, connection) || this;
49
+ _this.id = '';
50
+ _this.cursor = '';
51
+ _this.event = '';
52
+ _this.object = '';
53
+ _this.connection = connection;
54
+ _this.initAttributes(props);
55
+ if (_this.objectAttributes && DeltaClassMap[_this.object]) {
56
+ _this.objectAttributes = new DeltaClassMap[_this.object](connection, _this.objectAttributes);
49
57
  }
58
+ return _this;
50
59
  }
51
- Delta.prototype.latestCursor = function (callback) {
52
- var reqOpts = {
53
- method: 'POST',
54
- path: '/delta/latest_cursor',
55
- };
56
- return this.connection
57
- .request(reqOpts)
58
- .then(function (response) {
59
- if (callback) {
60
- callback(null, response.cursor);
61
- }
62
- return Promise.resolve(response.cursor);
63
- })
64
- .catch(function (err) {
65
- if (callback) {
66
- callback(err, null);
67
- }
68
- return Promise.reject(err);
69
- });
70
- };
71
- Delta.prototype.startStream = function (cursor, params) {
72
- if (params === void 0) { params = {}; }
73
- return __awaiter(this, void 0, void 0, function () {
74
- var stream;
75
- return __generator(this, function (_a) {
76
- switch (_a.label) {
77
- case 0:
78
- stream = new delta_stream_1.default(this.connection, cursor, params);
79
- return [4 /*yield*/, stream.open()];
80
- case 1:
81
- _a.sent();
82
- return [2 /*return*/, stream];
83
- }
84
- });
85
- });
60
+ Delta.prototype.fromJSON = function (json) {
61
+ _super.prototype.fromJSON.call(this, json);
62
+ if (this.objectAttributes && DeltaClassMap[this.object]) {
63
+ this.objectAttributes = new DeltaClassMap[this.object](this.connection).fromJSON(this.objectAttributes);
64
+ }
65
+ return this;
86
66
  };
87
67
  Delta.streamingTimeoutMs = 15000;
68
+ Delta.attributes = {
69
+ id: attributes_1.default.String({
70
+ modelKey: 'id',
71
+ }),
72
+ cursor: attributes_1.default.String({
73
+ modelKey: 'cursor',
74
+ }),
75
+ event: attributes_1.default.String({
76
+ modelKey: 'event',
77
+ }),
78
+ object: attributes_1.default.String({
79
+ modelKey: 'object',
80
+ }),
81
+ objectAttributes: attributes_1.default.Object({
82
+ modelKey: 'objectAttributes',
83
+ jsonKey: 'attributes',
84
+ }),
85
+ };
88
86
  return Delta;
89
- }());
87
+ }(restful_model_1.default));
90
88
  exports.default = Delta;
@@ -0,0 +1,17 @@
1
+ import Model from './model';
2
+ import NylasConnection from '../nylas-connection';
3
+ import { Attribute } from './attributes';
4
+ import Delta, { DeltaProperties } from './delta';
5
+ export declare type DeltasProperties = {
6
+ cursorStart: string;
7
+ cursorEnd: string;
8
+ deltas: DeltaProperties[];
9
+ };
10
+ export declare class Deltas extends Model implements DeltasProperties {
11
+ cursorStart: string;
12
+ cursorEnd: string;
13
+ deltas: Delta[];
14
+ connection: NylasConnection;
15
+ static attributes: Record<string, Attribute>;
16
+ constructor(connection: NylasConnection, props?: DeltasProperties);
17
+ }