nylas 6.6.1 → 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 +28 -0
- package/lib/config.js +30 -0
- package/lib/models/event.d.ts +2 -0
- package/lib/models/event.js +3 -1
- package/lib/models/rate-limit-error.d.ts +26 -0
- package/lib/models/rate-limit-error.js +47 -0
- package/lib/models/webhook-notification.d.ts +5 -2
- package/lib/models/webhook-notification.js +20 -1
- package/lib/models/webhook.d.ts +1 -0
- package/lib/models/webhook.js +1 -0
- package/lib/nylas-connection.js +8 -1
- package/lib/nylas.js +2 -1
- package/lib/services/tunnel.d.ts +28 -0
- package/lib/services/tunnel.js +89 -0
- package/package.json +6 -2
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;
|
|
@@ -7,6 +8,10 @@ export declare type NylasConfig = {
|
|
|
7
8
|
clientSecret: string;
|
|
8
9
|
apiServer?: string;
|
|
9
10
|
};
|
|
11
|
+
export declare enum ResponseType {
|
|
12
|
+
CODE = "code",
|
|
13
|
+
TOKEN = "token"
|
|
14
|
+
}
|
|
10
15
|
export declare type AuthenticateUrlConfig = {
|
|
11
16
|
redirectURI: string;
|
|
12
17
|
redirectOnError?: boolean;
|
|
@@ -14,4 +19,27 @@ export declare type AuthenticateUrlConfig = {
|
|
|
14
19
|
state?: string;
|
|
15
20
|
provider?: string;
|
|
16
21
|
scopes?: string[];
|
|
22
|
+
responseType?: ResponseType;
|
|
17
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;
|
|
@@ -10,3 +12,31 @@ function setClientSecret(newClientSecret) {
|
|
|
10
12
|
exports.clientSecret = newClientSecret;
|
|
11
13
|
}
|
|
12
14
|
exports.setClientSecret = setClientSecret;
|
|
15
|
+
var ResponseType;
|
|
16
|
+
(function (ResponseType) {
|
|
17
|
+
ResponseType["CODE"] = "code";
|
|
18
|
+
ResponseType["TOKEN"] = "token";
|
|
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);
|
package/lib/models/event.d.ts
CHANGED
|
@@ -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;
|
package/lib/models/event.js
CHANGED
|
@@ -294,9 +294,11 @@ 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
|
-
readOnly: true,
|
|
300
302
|
}), customerEventId: attributes_1.default.String({
|
|
301
303
|
modelKey: 'customerEventId',
|
|
302
304
|
jsonKey: 'customer_event_id',
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import NylasApiError from './nylas-api-error';
|
|
2
|
+
import { Headers } from 'node-fetch';
|
|
3
|
+
/**
|
|
4
|
+
* This error class represents a 429 error response, with details on the rate limit
|
|
5
|
+
*/
|
|
6
|
+
export default class RateLimitError extends NylasApiError {
|
|
7
|
+
static RATE_LIMIT_STATUS_CODE: number;
|
|
8
|
+
static RATE_LIMIT_LIMIT_HEADER: string;
|
|
9
|
+
static RATE_LIMIT_RESET_HEADER: string;
|
|
10
|
+
/**
|
|
11
|
+
* The rate limit
|
|
12
|
+
*/
|
|
13
|
+
rateLimit?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The rate limit expiration time, in seconds
|
|
16
|
+
*/
|
|
17
|
+
rateLimitReset?: number;
|
|
18
|
+
constructor(type: string, message: string, rateLimit?: number, rateLimitReset?: number);
|
|
19
|
+
/**
|
|
20
|
+
* Parses an API response and generates a 429 error with details filled in
|
|
21
|
+
* @param parsedApiError The response parsed as a JSON
|
|
22
|
+
* @param headers The response headers
|
|
23
|
+
* @return The error with the rate limit details filled in
|
|
24
|
+
*/
|
|
25
|
+
static parseErrorResponse(parsedApiError: Record<string, string>, headers: Headers): RateLimitError;
|
|
26
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
16
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
var nylas_api_error_1 = __importDefault(require("./nylas-api-error"));
|
|
20
|
+
/**
|
|
21
|
+
* This error class represents a 429 error response, with details on the rate limit
|
|
22
|
+
*/
|
|
23
|
+
var RateLimitError = /** @class */ (function (_super) {
|
|
24
|
+
__extends(RateLimitError, _super);
|
|
25
|
+
function RateLimitError(type, message, rateLimit, rateLimitReset) {
|
|
26
|
+
var _this = _super.call(this, RateLimitError.RATE_LIMIT_STATUS_CODE, type, message) || this;
|
|
27
|
+
_this.rateLimit = rateLimit;
|
|
28
|
+
_this.rateLimitReset = rateLimitReset;
|
|
29
|
+
return _this;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Parses an API response and generates a 429 error with details filled in
|
|
33
|
+
* @param parsedApiError The response parsed as a JSON
|
|
34
|
+
* @param headers The response headers
|
|
35
|
+
* @return The error with the rate limit details filled in
|
|
36
|
+
*/
|
|
37
|
+
RateLimitError.parseErrorResponse = function (parsedApiError, headers) {
|
|
38
|
+
var rateLimit = Number(headers.get(this.RATE_LIMIT_LIMIT_HEADER)) || undefined;
|
|
39
|
+
var rateLimitReset = Number(headers.get(this.RATE_LIMIT_RESET_HEADER)) || undefined;
|
|
40
|
+
return new RateLimitError(parsedApiError.type, parsedApiError.message, rateLimit, rateLimitReset);
|
|
41
|
+
};
|
|
42
|
+
RateLimitError.RATE_LIMIT_STATUS_CODE = 429;
|
|
43
|
+
RateLimitError.RATE_LIMIT_LIMIT_HEADER = 'X-RateLimit-Limit';
|
|
44
|
+
RateLimitError.RATE_LIMIT_RESET_HEADER = 'X-RateLimit-Reset';
|
|
45
|
+
return RateLimitError;
|
|
46
|
+
}(nylas_api_error_1.default));
|
|
47
|
+
exports.default = RateLimitError;
|
|
@@ -76,6 +76,8 @@ export declare class WebhookObjectAttributes extends Model implements WebhookObj
|
|
|
76
76
|
extras?: WebhookObjectExtrasProperties;
|
|
77
77
|
threadId?: string;
|
|
78
78
|
receivedDate?: Date;
|
|
79
|
+
calendarId?: string;
|
|
80
|
+
createdBeforeAccountConnection?: boolean;
|
|
79
81
|
static attributes: Record<string, Attribute>;
|
|
80
82
|
constructor(props?: WebhookObjectAttributesProperties);
|
|
81
83
|
}
|
|
@@ -84,7 +86,7 @@ export declare type WebhookObjectDataProperties = {
|
|
|
84
86
|
accountId: string;
|
|
85
87
|
namespaceId: string;
|
|
86
88
|
object: string;
|
|
87
|
-
metadata?: MessageTrackingDataProperties
|
|
89
|
+
metadata?: MessageTrackingDataProperties | Record<string, unknown>;
|
|
88
90
|
objectAttributes?: WebhookObjectAttributesProperties;
|
|
89
91
|
};
|
|
90
92
|
export declare class WebhookObjectData extends Model implements WebhookObjectDataProperties {
|
|
@@ -92,10 +94,11 @@ export declare class WebhookObjectData extends Model implements WebhookObjectDat
|
|
|
92
94
|
accountId: string;
|
|
93
95
|
namespaceId: string;
|
|
94
96
|
object: string;
|
|
95
|
-
metadata?: MessageTrackingData
|
|
97
|
+
metadata?: MessageTrackingData | Record<string, unknown>;
|
|
96
98
|
objectAttributes?: WebhookObjectAttributes;
|
|
97
99
|
static attributes: Record<string, Attribute>;
|
|
98
100
|
constructor(props?: WebhookObjectDataProperties);
|
|
101
|
+
fromJSON(json: Record<string, unknown>): this;
|
|
99
102
|
}
|
|
100
103
|
export declare type WebhookDeltaProperties = {
|
|
101
104
|
object: string;
|
|
@@ -168,10 +168,18 @@ var WebhookObjectAttributes = /** @class */ (function (_super) {
|
|
|
168
168
|
modelKey: 'threadId',
|
|
169
169
|
jsonKey: 'thread_id',
|
|
170
170
|
}),
|
|
171
|
+
calendarId: attributes_1.default.String({
|
|
172
|
+
modelKey: 'calendarId',
|
|
173
|
+
jsonKey: 'calendar_id',
|
|
174
|
+
}),
|
|
171
175
|
receivedDate: attributes_1.default.DateTime({
|
|
172
176
|
modelKey: 'receivedDate',
|
|
173
177
|
jsonKey: 'received_date',
|
|
174
178
|
}),
|
|
179
|
+
createdBeforeAccountConnection: attributes_1.default.Boolean({
|
|
180
|
+
modelKey: 'createdBeforeAccountConnection',
|
|
181
|
+
jsonKey: 'created_before_account_connection',
|
|
182
|
+
}),
|
|
175
183
|
extras: attributes_1.default.Object({
|
|
176
184
|
modelKey: 'extras',
|
|
177
185
|
itemClass: WebhookObjectExtras,
|
|
@@ -189,8 +197,20 @@ var WebhookObjectData = /** @class */ (function (_super) {
|
|
|
189
197
|
_this.namespaceId = '';
|
|
190
198
|
_this.object = '';
|
|
191
199
|
_this.initAttributes(props);
|
|
200
|
+
if (_this.metadata &&
|
|
201
|
+
(_this.object === 'message' || _this.object === 'thread')) {
|
|
202
|
+
_this.metadata = new MessageTrackingData(_this.metadata);
|
|
203
|
+
}
|
|
192
204
|
return _this;
|
|
193
205
|
}
|
|
206
|
+
WebhookObjectData.prototype.fromJSON = function (json) {
|
|
207
|
+
var notification = _super.prototype.fromJSON.call(this, json);
|
|
208
|
+
if (notification.metadata &&
|
|
209
|
+
(notification.object === 'message' || notification.object === 'thread')) {
|
|
210
|
+
notification.metadata = new MessageTrackingData().fromJSON(json['metadata']);
|
|
211
|
+
}
|
|
212
|
+
return notification;
|
|
213
|
+
};
|
|
194
214
|
WebhookObjectData.attributes = {
|
|
195
215
|
id: attributes_1.default.String({
|
|
196
216
|
modelKey: 'id',
|
|
@@ -208,7 +228,6 @@ var WebhookObjectData = /** @class */ (function (_super) {
|
|
|
208
228
|
}),
|
|
209
229
|
metadata: attributes_1.default.Object({
|
|
210
230
|
modelKey: 'metadata',
|
|
211
|
-
itemClass: MessageTrackingData,
|
|
212
231
|
}),
|
|
213
232
|
objectAttributes: attributes_1.default.Object({
|
|
214
233
|
modelKey: 'objectAttributes',
|
package/lib/models/webhook.d.ts
CHANGED
|
@@ -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",
|
package/lib/models/webhook.js
CHANGED
|
@@ -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";
|
package/lib/nylas-connection.js
CHANGED
|
@@ -45,6 +45,7 @@ var message_restful_model_collection_1 = __importDefault(require("./models/messa
|
|
|
45
45
|
var delta_collection_1 = __importDefault(require("./models/delta-collection"));
|
|
46
46
|
var outbox_1 = __importDefault(require("./models/outbox"));
|
|
47
47
|
var job_status_restful_model_collection_1 = __importDefault(require("./models/job-status-restful-model-collection"));
|
|
48
|
+
var rate_limit_error_1 = __importDefault(require("./models/rate-limit-error"));
|
|
48
49
|
var PACKAGE_JSON = require('../package.json');
|
|
49
50
|
var SDK_VERSION = PACKAGE_JSON.version;
|
|
50
51
|
var SUPPORTED_API_VERSION = '2.5';
|
|
@@ -196,7 +197,13 @@ var NylasConnection = /** @class */ (function () {
|
|
|
196
197
|
return response.text().then(function (body) {
|
|
197
198
|
try {
|
|
198
199
|
var parsedApiError = JSON.parse(body);
|
|
199
|
-
var error =
|
|
200
|
+
var error = void 0;
|
|
201
|
+
if (response.status === rate_limit_error_1.default.RATE_LIMIT_STATUS_CODE) {
|
|
202
|
+
error = rate_limit_error_1.default.parseErrorResponse(parsedApiError, response.headers);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
error = new nylas_api_error_1.default(response.status, parsedApiError.type, parsedApiError.message);
|
|
206
|
+
}
|
|
200
207
|
if (parsedApiError.missing_fields) {
|
|
201
208
|
error.missingFields = parsedApiError.missing_fields;
|
|
202
209
|
}
|
package/lib/nylas.js
CHANGED
|
@@ -20,6 +20,7 @@ var connect_1 = __importDefault(require("./models/connect"));
|
|
|
20
20
|
var restful_model_collection_1 = __importDefault(require("./models/restful-model-collection"));
|
|
21
21
|
var management_model_collection_1 = __importDefault(require("./models/management-model-collection"));
|
|
22
22
|
var webhook_1 = __importDefault(require("./models/webhook"));
|
|
23
|
+
var config_1 = require("./config");
|
|
23
24
|
var access_token_1 = __importDefault(require("./models/access-token"));
|
|
24
25
|
var application_details_1 = __importDefault(require("./models/application-details"));
|
|
25
26
|
var Nylas = /** @class */ (function () {
|
|
@@ -153,7 +154,7 @@ var Nylas = /** @class */ (function () {
|
|
|
153
154
|
if (!options.loginHint) {
|
|
154
155
|
options.loginHint = '';
|
|
155
156
|
}
|
|
156
|
-
var url = this.apiServer + "/oauth/authorize?client_id=" + this.clientId + "&response_type=
|
|
157
|
+
var url = this.apiServer + "/oauth/authorize?client_id=" + this.clientId + "&response_type=" + (options.responseType || config_1.ResponseType.CODE) + "&login_hint=" + options.loginHint + "&redirect_uri=" + options.redirectURI;
|
|
157
158
|
if (options.state != null) {
|
|
158
159
|
url += "&state=" + options.state;
|
|
159
160
|
}
|
|
@@ -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.
|
|
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",
|