nylas 6.8.0 → 6.10.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/lib/config.d.ts CHANGED
@@ -3,10 +3,17 @@ export declare let apiServer: string | null;
3
3
  export declare function setApiServer(newApiServer: string | null): void;
4
4
  export declare let clientSecret: string;
5
5
  export declare function setClientSecret(newClientSecret: string): void;
6
+ export declare let timeout: number;
7
+ export declare function setTimeout(newTimeout: number): void;
6
8
  export declare type NylasConfig = {
9
+ /** Nylas application client ID */
7
10
  clientId: string;
11
+ /** Nylas application client secret */
8
12
  clientSecret: string;
13
+ /** API Server base URL */
9
14
  apiServer?: string;
15
+ /** Timeout for outgoing API calls, in milliseconds */
16
+ timeout?: number;
10
17
  };
11
18
  export declare enum ResponseType {
12
19
  CODE = "code",
package/lib/config.js CHANGED
@@ -12,6 +12,11 @@ function setClientSecret(newClientSecret) {
12
12
  exports.clientSecret = newClientSecret;
13
13
  }
14
14
  exports.setClientSecret = setClientSecret;
15
+ exports.timeout = 0;
16
+ function setTimeout(newTimeout) {
17
+ exports.timeout = newTimeout;
18
+ }
19
+ exports.setTimeout = setTimeout;
15
20
  var ResponseType;
16
21
  (function (ResponseType) {
17
22
  ResponseType["CODE"] = "code";
@@ -42,6 +42,7 @@ var NylasApiError = /** @class */ (function (_super) {
42
42
  403: 'Forbidden',
43
43
  404: 'Not Found',
44
44
  405: 'Method Not Allowed',
45
+ 409: 'Conflict',
45
46
  410: 'Gone',
46
47
  418: "I'm a Teapot",
47
48
  422: 'Sending Error',
@@ -2,7 +2,7 @@ import NylasConnection from '../nylas-connection';
2
2
  import RestfulModel from './restful-model';
3
3
  import ModelCollection from './model-collection';
4
4
  export declare type GetCallback = (error: Error | null, result?: RestfulModel) => void;
5
- export default class RestfulModelCollection<T extends RestfulModel> extends ModelCollection<any> {
5
+ export default class RestfulModelCollection<T extends RestfulModel> extends ModelCollection<T> {
6
6
  modelClass: typeof RestfulModel;
7
7
  constructor(modelClass: typeof RestfulModel, connection: NylasConnection);
8
8
  count(params?: Record<string, unknown>, callback?: (err: Error | null, num?: number) => void): Promise<number>;
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import Model from './model';
2
3
  import { Attribute } from './attributes';
3
4
  export declare type LinkClickProperties = {
@@ -121,4 +122,12 @@ export default class WebhookNotification extends Model implements WebhookNotific
121
122
  deltas: WebhookDelta[];
122
123
  static attributes: Record<string, Attribute>;
123
124
  constructor(props?: WebhookNotificationProperties);
125
+ /**
126
+ * Verify incoming webhook signature came from Nylas
127
+ * @param xNylasSignature The signature to verify
128
+ * @param rawBody The raw body from the payload
129
+ * @param clientSecret Overriding client secret of the app receiving the webhook
130
+ * @return true if the webhook signature was verified from Nylas
131
+ */
132
+ static verifyWebhookSignature(xNylasSignature: string, rawBody: Buffer, clientSecret?: string): boolean;
124
133
  }
@@ -15,9 +15,18 @@ var __extends = (this && this.__extends) || (function () {
15
15
  var __importDefault = (this && this.__importDefault) || function (mod) {
16
16
  return (mod && mod.__esModule) ? mod : { "default": mod };
17
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
+ };
18
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ var crypto_1 = __importDefault(require("crypto"));
19
27
  var model_1 = __importDefault(require("./model"));
20
28
  var attributes_1 = __importDefault(require("./attributes"));
29
+ var config = __importStar(require("../config"));
21
30
  var LinkClick = /** @class */ (function (_super) {
22
31
  __extends(LinkClick, _super);
23
32
  function LinkClick(props) {
@@ -276,6 +285,24 @@ var WebhookNotification = /** @class */ (function (_super) {
276
285
  _this.initAttributes(props);
277
286
  return _this;
278
287
  }
288
+ /**
289
+ * Verify incoming webhook signature came from Nylas
290
+ * @param xNylasSignature The signature to verify
291
+ * @param rawBody The raw body from the payload
292
+ * @param clientSecret Overriding client secret of the app receiving the webhook
293
+ * @return true if the webhook signature was verified from Nylas
294
+ */
295
+ WebhookNotification.verifyWebhookSignature = function (xNylasSignature, rawBody, clientSecret) {
296
+ var clientSecretToUse = clientSecret || config.clientSecret;
297
+ if (!clientSecretToUse) {
298
+ throw new Error('Client secret is required to verify webhook signature');
299
+ }
300
+ var digest = crypto_1.default
301
+ .createHmac('sha256', clientSecretToUse)
302
+ .update(rawBody)
303
+ .digest('hex');
304
+ return digest === xNylasSignature;
305
+ };
279
306
  WebhookNotification.attributes = {
280
307
  deltas: attributes_1.default.Collection({
281
308
  modelKey: 'deltas',
@@ -3,7 +3,10 @@ import { Attribute } from './attributes';
3
3
  export declare type WhenProperties = {
4
4
  startTime?: number;
5
5
  endTime?: number;
6
+ startTimezone?: string;
7
+ endTimezone?: string;
6
8
  time?: number;
9
+ timezone?: string;
7
10
  startDate?: string;
8
11
  endDate?: string;
9
12
  date?: string;
@@ -12,7 +15,10 @@ export declare type WhenProperties = {
12
15
  export default class When extends Model implements WhenProperties {
13
16
  startTime?: number;
14
17
  endTime?: number;
18
+ startTimezone?: string;
19
+ endTimezone?: string;
15
20
  time?: number;
21
+ timezone?: string;
16
22
  startDate?: string;
17
23
  endDate?: string;
18
24
  date?: string;
@@ -41,9 +41,20 @@ var When = /** @class */ (function (_super) {
41
41
  modelKey: 'endTime',
42
42
  jsonKey: 'end_time',
43
43
  }),
44
+ startTimezone: attributes_1.default.String({
45
+ modelKey: 'startTimezone',
46
+ jsonKey: 'start_timezone',
47
+ }),
48
+ endTimezone: attributes_1.default.String({
49
+ modelKey: 'endTimezone',
50
+ jsonKey: 'end_timezone',
51
+ }),
44
52
  time: attributes_1.default.Number({
45
53
  modelKey: 'time',
46
54
  }),
55
+ timezone: attributes_1.default.String({
56
+ modelKey: 'timezone',
57
+ }),
47
58
  startDate: attributes_1.default.String({
48
59
  modelKey: 'startDate',
49
60
  jsonKey: 'start_date',
@@ -10,6 +10,9 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
13
16
  var __importStar = (this && this.__importStar) || function (mod) {
14
17
  if (mod && mod.__esModule) return mod;
15
18
  var result = {};
@@ -17,12 +20,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
17
20
  result["default"] = mod;
18
21
  return result;
19
22
  };
20
- var __importDefault = (this && this.__importDefault) || function (mod) {
21
- return (mod && mod.__esModule) ? mod : { "default": mod };
22
- };
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  // TODO since node 10 URL is global
25
25
  var url_1 = require("url");
26
+ // TODO since node 15 AbortController is global
27
+ var abort_controller_1 = __importDefault(require("abort-controller"));
26
28
  var node_fetch_1 = __importStar(require("node-fetch"));
27
29
  var config = __importStar(require("./config"));
28
30
  var restful_model_collection_1 = __importDefault(require("./models/restful-model-collection"));
@@ -181,8 +183,16 @@ var NylasConnection = /** @class */ (function () {
181
183
  NylasConnection.prototype.request = function (options) {
182
184
  var _this = this;
183
185
  var req = this.newRequest(options);
186
+ var controller;
187
+ var timeout;
188
+ if (config.timeout && config.timeout !== 0) {
189
+ controller = new abort_controller_1.default();
190
+ timeout = setTimeout(function () {
191
+ controller.abort();
192
+ }, 150);
193
+ }
184
194
  return new Promise(function (resolve, reject) {
185
- return node_fetch_1.default(req)
195
+ return node_fetch_1.default(req, { signal: controller === null || controller === void 0 ? void 0 : controller.signal })
186
196
  .then(function (response) {
187
197
  if (typeof response === 'undefined') {
188
198
  return reject(new Error('No response'));
@@ -255,8 +265,15 @@ var NylasConnection = /** @class */ (function () {
255
265
  }
256
266
  })
257
267
  .catch(function (err) {
268
+ if (err && err.name && err.name === 'AbortError') {
269
+ console.warn('Request timed out');
270
+ return reject(err);
271
+ }
258
272
  console.error("Error encountered during request:\n" + err.stack);
259
273
  return reject(err);
274
+ })
275
+ .then(function () {
276
+ clearTimeout(timeout);
260
277
  });
261
278
  });
262
279
  };
package/lib/nylas.d.ts CHANGED
@@ -14,6 +14,8 @@ declare class Nylas {
14
14
  static set clientSecret(newClientSecret: string);
15
15
  static get apiServer(): string | null;
16
16
  static set apiServer(newApiServer: string | null);
17
+ static get timeout(): number;
18
+ static set timeout(timeout: number);
17
19
  static accounts: ManagementModelCollection<ManagementAccount> | RestfulModelCollection<Account>;
18
20
  static connect: Connect;
19
21
  static webhooks: ManagementModelCollection<Webhook>;
package/lib/nylas.js CHANGED
@@ -46,6 +46,17 @@ var Nylas = /** @class */ (function () {
46
46
  enumerable: true,
47
47
  configurable: true
48
48
  });
49
+ Object.defineProperty(Nylas, "timeout", {
50
+ get: function () {
51
+ return config.timeout;
52
+ },
53
+ // Timeout for outgoing API calls, in milliseconds
54
+ set: function (timeout) {
55
+ config.setTimeout(timeout);
56
+ },
57
+ enumerable: true,
58
+ configurable: true
59
+ });
49
60
  Nylas.config = function (config) {
50
61
  if (config.apiServer && config.apiServer.indexOf('://') === -1) {
51
62
  throw new Error('Please specify a fully qualified URL for the API Server.');
@@ -62,6 +73,7 @@ var Nylas = /** @class */ (function () {
62
73
  else {
63
74
  this.apiServer = 'https://api.nylas.com';
64
75
  }
76
+ this.timeout = config.timeout || 0;
65
77
  var conn = new nylas_connection_1.default(this.clientSecret, {
66
78
  clientId: this.clientId,
67
79
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nylas",
3
- "version": "6.8.0",
3
+ "version": "6.10.0",
4
4
  "description": "A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.",
5
5
  "main": "lib/nylas.js",
6
6
  "types": "lib/nylas.d.ts",