nylas 6.7.0 → 6.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.
package/lib/config.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { WebhookTriggers } from './models/webhook';
1
2
  export declare let apiServer: string | null;
2
3
  export declare function setApiServer(newApiServer: string | null): void;
3
4
  export declare let clientSecret: string;
@@ -20,3 +21,25 @@ export declare type AuthenticateUrlConfig = {
20
21
  scopes?: string[];
21
22
  responseType?: ResponseType;
22
23
  };
24
+ export declare enum Region {
25
+ Us = "us",
26
+ Ireland = "ireland"
27
+ }
28
+ export declare const DEFAULT_REGION = Region.Us;
29
+ export declare const regionConfig: {
30
+ us: {
31
+ nylasAPIUrl: string;
32
+ dashboardApiUrl: string;
33
+ callbackDomain: string;
34
+ websocketDomain: string;
35
+ telemetryApiUrl: string;
36
+ };
37
+ ireland: {
38
+ nylasAPIUrl: string;
39
+ dashboardApiUrl: string;
40
+ callbackDomain: string;
41
+ websocketDomain: string;
42
+ telemetryApiUrl: string;
43
+ };
44
+ };
45
+ export declare const DEFAULT_WEBHOOK_TRIGGERS: WebhookTriggers[];
package/lib/config.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ var webhook_1 = require("./models/webhook");
3
5
  exports.apiServer = null;
4
6
  function setApiServer(newApiServer) {
5
7
  exports.apiServer = newApiServer;
@@ -15,3 +17,26 @@ var ResponseType;
15
17
  ResponseType["CODE"] = "code";
16
18
  ResponseType["TOKEN"] = "token";
17
19
  })(ResponseType = exports.ResponseType || (exports.ResponseType = {}));
20
+ var Region;
21
+ (function (Region) {
22
+ Region["Us"] = "us";
23
+ Region["Ireland"] = "ireland";
24
+ })(Region = exports.Region || (exports.Region = {}));
25
+ exports.DEFAULT_REGION = Region.Us;
26
+ exports.regionConfig = (_a = {},
27
+ _a[Region.Us] = {
28
+ nylasAPIUrl: 'https://api.nylas.com',
29
+ dashboardApiUrl: 'https://dashboard-api.nylas.com',
30
+ callbackDomain: 'cb.nylas.com',
31
+ websocketDomain: 'tunnel.nylas.com',
32
+ telemetryApiUrl: 'https://cli.nylas.com',
33
+ },
34
+ _a[Region.Ireland] = {
35
+ nylasAPIUrl: 'https://ireland.api.nylas.com',
36
+ dashboardApiUrl: 'https://ireland.dashboard.nylas.com',
37
+ callbackDomain: 'cb.nylas.com',
38
+ websocketDomain: 'tunnel.nylas.com',
39
+ telemetryApiUrl: 'https://cli.nylas.com',
40
+ },
41
+ _a);
42
+ exports.DEFAULT_WEBHOOK_TRIGGERS = Object.values(webhook_1.WebhookTriggers);
@@ -47,6 +47,7 @@ export declare type EventProperties = {
47
47
  jobStatusId?: string;
48
48
  organizerEmail?: string;
49
49
  organizerName?: string;
50
+ hideParticipants?: boolean;
50
51
  visibility?: string;
51
52
  customerEventId?: string;
52
53
  reminderMinutes?: string;
@@ -84,6 +85,7 @@ export default class Event extends RestfulModel {
84
85
  jobStatusId?: string;
85
86
  organizerEmail?: string;
86
87
  organizerName?: string;
88
+ hideParticipants?: boolean;
87
89
  visibility?: string;
88
90
  customerEventId?: string;
89
91
  static collectionName: string;
@@ -294,6 +294,9 @@ var Event = /** @class */ (function (_super) {
294
294
  modelKey: 'organizerName',
295
295
  jsonKey: 'organizer_name',
296
296
  readOnly: true,
297
+ }), hideParticipants: attributes_1.default.Boolean({
298
+ modelKey: 'hideParticipants',
299
+ jsonKey: 'hide_participants',
297
300
  }), visibility: attributes_1.default.String({
298
301
  modelKey: 'visibility',
299
302
  }), customerEventId: attributes_1.default.String({
@@ -8,6 +8,7 @@ export declare enum WebhookTriggers {
8
8
  AccountStopped = "account.stopped",
9
9
  AccountInvalid = "account.invalid",
10
10
  AccountSyncError = "account.sync_error",
11
+ MessageBounced = "message.bounced",
11
12
  MessageCreated = "message.created",
12
13
  MessageOpened = "message.opened",
13
14
  MessageUpdated = "message.updated",
@@ -25,6 +25,7 @@ var WebhookTriggers;
25
25
  WebhookTriggers["AccountStopped"] = "account.stopped";
26
26
  WebhookTriggers["AccountInvalid"] = "account.invalid";
27
27
  WebhookTriggers["AccountSyncError"] = "account.sync_error";
28
+ WebhookTriggers["MessageBounced"] = "message.bounced";
28
29
  WebhookTriggers["MessageCreated"] = "message.created";
29
30
  WebhookTriggers["MessageOpened"] = "message.opened";
30
31
  WebhookTriggers["MessageUpdated"] = "message.updated";
@@ -0,0 +1,28 @@
1
+ import { client as WebSocketClient } from 'websocket';
2
+ import { Region } from '../config';
3
+ import Webhook, { WebhookTriggers } from '../models/webhook';
4
+ import { WebhookDelta } from '../models/webhook-notification';
5
+ export interface OpenWebhookTunnelOptions {
6
+ onMessage: (msg: WebhookDelta) => void;
7
+ onConnectFail?: (error: Error) => void;
8
+ onError?: (error: Error) => void;
9
+ onClose?: (wsClient: WebSocketClient) => void;
10
+ onConnect?: (wsClient: WebSocketClient) => void;
11
+ region?: Region;
12
+ triggers?: WebhookTriggers[];
13
+ }
14
+ /**
15
+ * Open a webhook tunnel and register it with the Nylas API
16
+ * 1. Creates a UUID
17
+ * 2. Opens a websocket connection to Nylas' webhook forwarding service,
18
+ * with the UUID as a header
19
+ * 3. Creates a new webhook pointed at the forwarding service with the UUID as the path
20
+ *
21
+ * When an event is received by the forwarding service, it will push directly to this websocket
22
+ * connection
23
+ *
24
+ * @param config Configuration for the webhook tunnel, including callback functions,
25
+ * region, and events to subscribe to
26
+ * @return The webhook details response from the API
27
+ */
28
+ export declare const openWebhookTunnel: (config: OpenWebhookTunnelOptions) => Promise<Webhook>;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var websocket_1 = require("websocket");
7
+ var uuid_1 = require("uuid");
8
+ var config_1 = require("../config");
9
+ var nylas_1 = __importDefault(require("../nylas"));
10
+ var webhook_notification_1 = require("../models/webhook-notification");
11
+ /**
12
+ * Create a webhook to the Nylas forwarding service which will pass messages to our websocket
13
+ * @param callbackDomain The domain name of the callback
14
+ * @param tunnelPath The path to the tunnel
15
+ * @param triggers The list of triggers to subscribe to
16
+ * @return The webhook details response from the API
17
+ */
18
+ var buildTunnelWebhook = function (callbackDomain, tunnelPath, triggers) {
19
+ var callbackUrl = "https://" + callbackDomain + "/" + tunnelPath;
20
+ return nylas_1.default.webhooks
21
+ .build({
22
+ callbackUrl: callbackUrl,
23
+ state: 'active',
24
+ test: true,
25
+ triggers: triggers,
26
+ })
27
+ .save();
28
+ };
29
+ /**
30
+ * Open a webhook tunnel and register it with the Nylas API
31
+ * 1. Creates a UUID
32
+ * 2. Opens a websocket connection to Nylas' webhook forwarding service,
33
+ * with the UUID as a header
34
+ * 3. Creates a new webhook pointed at the forwarding service with the UUID as the path
35
+ *
36
+ * When an event is received by the forwarding service, it will push directly to this websocket
37
+ * connection
38
+ *
39
+ * @param config Configuration for the webhook tunnel, including callback functions,
40
+ * region, and events to subscribe to
41
+ * @return The webhook details response from the API
42
+ */
43
+ exports.openWebhookTunnel = function (config) {
44
+ var triggers = config.triggers || config_1.DEFAULT_WEBHOOK_TRIGGERS;
45
+ var region = config.region || config_1.DEFAULT_REGION;
46
+ var _a = config_1.regionConfig[region], websocketDomain = _a.websocketDomain, callbackDomain = _a.callbackDomain;
47
+ // This UUID will map our websocket to a webhook in the forwarding server
48
+ var tunnelId = uuid_1.v4();
49
+ var client = new websocket_1.client({ closeTimeout: 60000 });
50
+ client.on('connectFailed', function (error) {
51
+ config.onConnectFail && config.onConnectFail(error);
52
+ });
53
+ client.on('connect', function (connection) {
54
+ config.onConnect && config.onConnect(client);
55
+ connection.on('error', function (error) {
56
+ config.onError && config.onError(error);
57
+ });
58
+ connection.on('close', function () {
59
+ config.onClose && config.onClose(client);
60
+ });
61
+ connection.on('message', function (message) {
62
+ // This shouldn't happen. If any of these are seen, open an issue
63
+ if (message.type === 'binary') {
64
+ config.onError &&
65
+ config.onError(new Error('Unknown binary message received'));
66
+ return;
67
+ }
68
+ try {
69
+ var req = JSON.parse(message.utf8Data);
70
+ var deltas = JSON.parse(req.body).deltas;
71
+ deltas.forEach(function (delta) {
72
+ return config.onMessage(new webhook_notification_1.WebhookDelta().fromJSON(delta));
73
+ });
74
+ }
75
+ catch (e) {
76
+ config.onError &&
77
+ config.onError(new Error("Error converting Nylas websocket event to JSON: " + (e &&
78
+ e.message)));
79
+ }
80
+ });
81
+ });
82
+ client.connect("wss://" + websocketDomain, undefined, undefined, {
83
+ 'Client-Id': nylas_1.default.clientId,
84
+ 'Client-Secret': nylas_1.default.clientSecret,
85
+ 'Tunnel-Id': tunnelId,
86
+ Region: region,
87
+ });
88
+ return buildTunnelWebhook(callbackDomain, tunnelId, triggers);
89
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nylas",
3
- "version": "6.7.0",
3
+ "version": "6.8.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",
@@ -47,7 +47,9 @@
47
47
  "backoff": "^2.5.0",
48
48
  "form-data": "^4.0.0",
49
49
  "JSONStream": "^1.3.5",
50
- "node-fetch": "^2.6.1"
50
+ "node-fetch": "^2.6.1",
51
+ "uuid": "^8.3.2",
52
+ "websocket": "^1.0.34"
51
53
  },
52
54
  "devDependencies": {
53
55
  "@babel/cli": "^7.13.16",
@@ -57,6 +59,8 @@
57
59
  "@types/backoff": "^2.5.1",
58
60
  "@types/jest": "^25.1.4",
59
61
  "@types/node-fetch": "^2.5.8",
62
+ "@types/uuid": "^8.3.4",
63
+ "@types/websocket": "^1.0.5",
60
64
  "@typescript-eslint/eslint-plugin": "^2.25.0",
61
65
  "@typescript-eslint/parser": "^2.25.0",
62
66
  "babel-eslint": "^10.0.1",