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,67 @@
1
+ /// <reference types="node" />
2
+ import { URL } from 'url';
3
+ import { Request } from 'node-fetch';
4
+ import RestfulModelCollection from './models/restful-model-collection';
5
+ import CalendarRestfulModelCollection from './models/calendar-restful-model-collection';
6
+ import ContactRestfulModelCollection from './models/contact-restful-model-collection';
7
+ import RestfulModelInstance from './models/restful-model-instance';
8
+ import Account from './models/account';
9
+ import Thread from './models/thread';
10
+ import Message from './models/message';
11
+ import Draft from './models/draft';
12
+ import File from './models/file';
13
+ import Event from './models/event';
14
+ import JobStatus from './models/job-status';
15
+ import Resource from './models/resource';
16
+ import Delta from './models/delta';
17
+ import { Folder, Label } from './models/folder';
18
+ import { AppendOptions } from 'form-data';
19
+ import Neural from './models/neural';
20
+ export declare type RequestOptions = {
21
+ path: string;
22
+ method?: string;
23
+ headers?: {
24
+ [key: string]: string;
25
+ };
26
+ qs?: {
27
+ [key: string]: any;
28
+ };
29
+ downloadRequest?: boolean;
30
+ json?: boolean;
31
+ formData?: {
32
+ [key: string]: FormDataType;
33
+ };
34
+ body?: any;
35
+ url?: URL;
36
+ };
37
+ export declare type FormDataType = {
38
+ value: any;
39
+ options?: {
40
+ [key: string]: any;
41
+ } | AppendOptions;
42
+ };
43
+ export default class NylasConnection {
44
+ accessToken: string | null | undefined;
45
+ clientId: string | null | undefined;
46
+ threads: RestfulModelCollection<Thread>;
47
+ contacts: ContactRestfulModelCollection;
48
+ messages: RestfulModelCollection<Message>;
49
+ drafts: RestfulModelCollection<Draft>;
50
+ files: RestfulModelCollection<File>;
51
+ calendars: CalendarRestfulModelCollection;
52
+ jobStatuses: RestfulModelCollection<JobStatus>;
53
+ events: RestfulModelCollection<Event>;
54
+ resources: RestfulModelCollection<Resource>;
55
+ deltas: Delta;
56
+ labels: RestfulModelCollection<Label>;
57
+ folders: RestfulModelCollection<Folder>;
58
+ account: RestfulModelInstance<Account>;
59
+ neural: Neural;
60
+ constructor(accessToken: string | null | undefined, { clientId }: {
61
+ clientId: string | null | undefined;
62
+ });
63
+ requestOptions(options: RequestOptions): RequestOptions;
64
+ newRequest(options: RequestOptions): Request;
65
+ _getWarningForVersion(sdkApiVersion?: string, apiVersion?: string): string;
66
+ request(options: RequestOptions): Promise<any>;
67
+ }
@@ -0,0 +1,235 @@
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || function (mod) {
26
+ if (mod && mod.__esModule) return mod;
27
+ var result = {};
28
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29
+ __setModuleDefault(result, mod);
30
+ return result;
31
+ };
32
+ var __importDefault = (this && this.__importDefault) || function (mod) {
33
+ return (mod && mod.__esModule) ? mod : { "default": mod };
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ // TODO since node 10 URL is global
37
+ var url_1 = require("url");
38
+ var node_fetch_1 = __importStar(require("node-fetch"));
39
+ var config = __importStar(require("./config"));
40
+ var restful_model_collection_1 = __importDefault(require("./models/restful-model-collection"));
41
+ var calendar_restful_model_collection_1 = __importDefault(require("./models/calendar-restful-model-collection"));
42
+ var contact_restful_model_collection_1 = __importDefault(require("./models/contact-restful-model-collection"));
43
+ var restful_model_instance_1 = __importDefault(require("./models/restful-model-instance"));
44
+ var account_1 = __importDefault(require("./models/account"));
45
+ var thread_1 = __importDefault(require("./models/thread"));
46
+ var message_1 = __importDefault(require("./models/message"));
47
+ var draft_1 = __importDefault(require("./models/draft"));
48
+ var file_1 = __importDefault(require("./models/file"));
49
+ var event_1 = __importDefault(require("./models/event"));
50
+ var job_status_1 = __importDefault(require("./models/job-status"));
51
+ var resource_1 = __importDefault(require("./models/resource"));
52
+ var delta_1 = __importDefault(require("./models/delta"));
53
+ var folder_1 = require("./models/folder");
54
+ var form_data_1 = __importDefault(require("form-data"));
55
+ var neural_1 = __importDefault(require("./models/neural"));
56
+ var nylas_api_error_1 = __importDefault(require("./models/nylas-api-error"));
57
+ var PACKAGE_JSON = require('../package.json');
58
+ var SDK_VERSION = PACKAGE_JSON.version;
59
+ var SUPPORTED_API_VERSION = '2.3';
60
+ var NylasConnection = /** @class */ (function () {
61
+ function NylasConnection(accessToken, _a) {
62
+ var clientId = _a.clientId;
63
+ this.threads = new restful_model_collection_1.default(thread_1.default, this);
64
+ this.contacts = new contact_restful_model_collection_1.default(this);
65
+ this.messages = new restful_model_collection_1.default(message_1.default, this);
66
+ this.drafts = new restful_model_collection_1.default(draft_1.default, this);
67
+ this.files = new restful_model_collection_1.default(file_1.default, this);
68
+ this.calendars = new calendar_restful_model_collection_1.default(this);
69
+ this.jobStatuses = new restful_model_collection_1.default(job_status_1.default, this);
70
+ this.events = new restful_model_collection_1.default(event_1.default, this);
71
+ this.resources = new restful_model_collection_1.default(resource_1.default, this);
72
+ this.deltas = new delta_1.default(this);
73
+ this.labels = new restful_model_collection_1.default(folder_1.Label, this);
74
+ this.folders = new restful_model_collection_1.default(folder_1.Folder, this);
75
+ this.account = new restful_model_instance_1.default(account_1.default, this);
76
+ this.neural = new neural_1.default(this);
77
+ this.accessToken = accessToken;
78
+ this.clientId = clientId;
79
+ }
80
+ NylasConnection.prototype.requestOptions = function (options) {
81
+ var url = new url_1.URL("" + config.apiServer + options.path);
82
+ // map querystring to search params
83
+ if (options.qs) {
84
+ for (var _i = 0, _a = Object.entries(options.qs); _i < _a.length; _i++) {
85
+ var _b = _a[_i], key = _b[0], value = _b[1];
86
+ // For convenience, If `expanded` param is provided, convert to view:
87
+ // 'expanded' api option
88
+ if (key === 'expanded') {
89
+ if (value === true) {
90
+ url.searchParams.set('view', 'expanded');
91
+ }
92
+ }
93
+ else if (key == 'metadata_pair') {
94
+ // The API understands a metadata_pair filter in the form of:
95
+ // <key>:<value>
96
+ for (var item in value) {
97
+ url.searchParams.set('metadata_pair', item + ":" + value[item]);
98
+ }
99
+ }
100
+ else if (Array.isArray(value)) {
101
+ for (var _c = 0, value_1 = value; _c < value_1.length; _c++) {
102
+ var item = value_1[_c];
103
+ url.searchParams.append(key, item);
104
+ }
105
+ }
106
+ else {
107
+ url.searchParams.set(key, value);
108
+ }
109
+ }
110
+ }
111
+ options.url = url;
112
+ var headers = __assign({}, options.headers);
113
+ var user = options.path.substr(0, 3) === '/a/'
114
+ ? config.clientSecret
115
+ : this.accessToken;
116
+ if (user) {
117
+ headers['authorization'] =
118
+ 'Basic ' + Buffer.from(user + ":", 'utf8').toString('base64');
119
+ }
120
+ if (!headers['User-Agent']) {
121
+ headers['User-Agent'] = "Nylas Node SDK v" + SDK_VERSION;
122
+ }
123
+ headers['Nylas-API-Version'] = SUPPORTED_API_VERSION;
124
+ headers['Nylas-SDK-API-Version'] = SUPPORTED_API_VERSION;
125
+ if (this.clientId != null) {
126
+ headers['X-Nylas-Client-Id'] = this.clientId;
127
+ }
128
+ options.headers = headers;
129
+ if (options.formData) {
130
+ var fd = new form_data_1.default();
131
+ for (var _d = 0, _e = Object.entries(options.formData); _d < _e.length; _d++) {
132
+ var _f = _e[_d], key = _f[0], obj = _f[1];
133
+ if (obj.options) {
134
+ fd.append(key, obj.value, obj.options);
135
+ }
136
+ else {
137
+ fd.append(key, obj.value);
138
+ }
139
+ }
140
+ options.body = fd;
141
+ }
142
+ else if (options.body && options.json !== false) {
143
+ options.body = JSON.stringify(options.body);
144
+ }
145
+ return options;
146
+ };
147
+ NylasConnection.prototype.newRequest = function (options) {
148
+ var newOptions = this.requestOptions(options);
149
+ return new node_fetch_1.Request(newOptions.url || '', {
150
+ method: newOptions.method || 'GET',
151
+ headers: newOptions.headers,
152
+ body: newOptions.body,
153
+ });
154
+ };
155
+ NylasConnection.prototype._getWarningForVersion = function (sdkApiVersion, apiVersion) {
156
+ var warning = '';
157
+ if (sdkApiVersion != apiVersion) {
158
+ if (sdkApiVersion && apiVersion) {
159
+ warning +=
160
+ "WARNING: SDK version may not support your Nylas API version." +
161
+ (" SDK supports version " + sdkApiVersion + " of the API and your application") +
162
+ (" is currently running on version " + apiVersion + " of the API.");
163
+ var apiNum = parseInt(apiVersion.split('-')[0]);
164
+ var sdkNum = parseInt(sdkApiVersion.split('-')[0]);
165
+ if (sdkNum > apiNum) {
166
+ warning += " Please update the version of the API that your application is using through the developer dashboard.";
167
+ }
168
+ else if (apiNum > sdkNum) {
169
+ warning += " Please update the sdk to ensure it works properly.";
170
+ }
171
+ }
172
+ }
173
+ return warning;
174
+ };
175
+ NylasConnection.prototype.request = function (options) {
176
+ var _this = this;
177
+ var req = this.newRequest(options);
178
+ return new Promise(function (resolve, reject) {
179
+ return node_fetch_1.default(req)
180
+ .then(function (response) {
181
+ if (typeof response === 'undefined') {
182
+ return reject(new Error('No response'));
183
+ }
184
+ // node headers are lowercaser so this refers to `Nylas-Api-Version`
185
+ var apiVersion = response.headers.get('nylas-api-version');
186
+ var warning = _this._getWarningForVersion(SUPPORTED_API_VERSION, apiVersion);
187
+ if (warning) {
188
+ console.warn(warning);
189
+ }
190
+ if (response.status > 299) {
191
+ return response.json().then(function (body) {
192
+ var error = new nylas_api_error_1.default(response.status, body.type, body.message);
193
+ if (body.missing_fields) {
194
+ error.missingFields = body.missing_fields;
195
+ }
196
+ if (body.server_error) {
197
+ error.serverError = body.server_error;
198
+ }
199
+ return reject(error);
200
+ });
201
+ }
202
+ else {
203
+ if (options.downloadRequest) {
204
+ response
205
+ .buffer()
206
+ .then(function (buffer) {
207
+ // Return an object with the headers and the body as a buffer
208
+ var fileDetails = {};
209
+ response.headers.forEach(function (v, k) {
210
+ fileDetails[k] = v;
211
+ });
212
+ fileDetails['body'] = buffer;
213
+ return resolve(fileDetails);
214
+ })
215
+ .catch(function (e) {
216
+ return reject(e);
217
+ });
218
+ }
219
+ else if (response.headers.get('Content-Type') === 'message/rfc822') {
220
+ return resolve(response.text());
221
+ }
222
+ else {
223
+ return resolve(response.json());
224
+ }
225
+ }
226
+ })
227
+ .catch(function (err) {
228
+ console.error("Error encountered during request:\n" + err.stack);
229
+ return reject(err);
230
+ });
231
+ });
232
+ };
233
+ return NylasConnection;
234
+ }());
235
+ exports.default = NylasConnection;
package/lib/nylas.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import NylasConnection from './nylas-connection';
2
+ import ManagementAccount from './models/management-account';
3
+ import Account from './models/account';
4
+ import Connect from './models/connect';
5
+ import RestfulModelCollection from './models/restful-model-collection';
6
+ import ManagementModelCollection from './models/management-model-collection';
7
+ import Webhook from './models/webhook';
8
+ declare class Nylas {
9
+ static clientId: string;
10
+ static get clientSecret(): string;
11
+ static set clientSecret(newClientSecret: string);
12
+ static get apiServer(): string | null;
13
+ static set apiServer(newApiServer: string | null);
14
+ static accounts?: ManagementModelCollection<ManagementAccount> | RestfulModelCollection<Account>;
15
+ static connect?: Connect;
16
+ static webhooks?: ManagementModelCollection<Webhook>;
17
+ static config({ clientId, clientSecret, apiServer, }: {
18
+ clientId: string;
19
+ clientSecret: string;
20
+ apiServer?: string;
21
+ }): typeof Nylas;
22
+ static clientCredentials(): boolean;
23
+ static with(accessToken: string): NylasConnection;
24
+ static application(options?: {
25
+ application_name?: string;
26
+ redirect_uris?: string[];
27
+ applicationName?: string;
28
+ redirectUris?: string[];
29
+ }): Promise<any>;
30
+ static exchangeCodeForToken(code: string, callback?: (error: Error | null, accessToken?: string) => void): Promise<any>;
31
+ static urlForAuthentication(options?: {
32
+ redirectURI?: string;
33
+ loginHint?: string;
34
+ state?: string;
35
+ scopes?: string[];
36
+ }): string;
37
+ }
38
+ export = Nylas;
package/lib/nylas.js ADDED
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ // TODO since node 10 URL is global
25
+ var url_1 = require("url");
26
+ var node_fetch_1 = __importDefault(require("node-fetch"));
27
+ var config = __importStar(require("./config"));
28
+ var nylas_connection_1 = __importDefault(require("./nylas-connection"));
29
+ var management_account_1 = __importDefault(require("./models/management-account"));
30
+ var account_1 = __importDefault(require("./models/account"));
31
+ var connect_1 = __importDefault(require("./models/connect"));
32
+ var restful_model_collection_1 = __importDefault(require("./models/restful-model-collection"));
33
+ var management_model_collection_1 = __importDefault(require("./models/management-model-collection"));
34
+ var webhook_1 = __importDefault(require("./models/webhook"));
35
+ var Nylas = /** @class */ (function () {
36
+ function Nylas() {
37
+ }
38
+ Object.defineProperty(Nylas, "clientSecret", {
39
+ get: function () {
40
+ return config.clientSecret;
41
+ },
42
+ set: function (newClientSecret) {
43
+ config.setClientSecret(newClientSecret);
44
+ },
45
+ enumerable: false,
46
+ configurable: true
47
+ });
48
+ Object.defineProperty(Nylas, "apiServer", {
49
+ get: function () {
50
+ return config.apiServer;
51
+ },
52
+ set: function (newApiServer) {
53
+ config.setApiServer(newApiServer);
54
+ },
55
+ enumerable: false,
56
+ configurable: true
57
+ });
58
+ Nylas.config = function (_a) {
59
+ var clientId = _a.clientId, clientSecret = _a.clientSecret, apiServer = _a.apiServer;
60
+ if (apiServer && apiServer.indexOf('://') === -1) {
61
+ throw new Error('Please specify a fully qualified URL for the API Server.');
62
+ }
63
+ if (clientId) {
64
+ this.clientId = clientId;
65
+ }
66
+ if (clientSecret) {
67
+ this.clientSecret = clientSecret;
68
+ }
69
+ if (apiServer) {
70
+ this.apiServer = apiServer;
71
+ }
72
+ else {
73
+ this.apiServer = 'https://api.nylas.com';
74
+ }
75
+ var conn = new nylas_connection_1.default(this.clientSecret, {
76
+ clientId: this.clientId,
77
+ });
78
+ this.connect = new connect_1.default(conn, this.clientId, this.clientSecret);
79
+ this.webhooks = new management_model_collection_1.default(webhook_1.default, conn, this.clientId);
80
+ if (this.clientCredentials()) {
81
+ this.accounts = new management_model_collection_1.default(management_account_1.default, conn, this.clientId);
82
+ }
83
+ else {
84
+ this.accounts = new restful_model_collection_1.default(account_1.default, conn);
85
+ }
86
+ return this;
87
+ };
88
+ Nylas.clientCredentials = function () {
89
+ return this.clientId != null && this.clientSecret != null;
90
+ };
91
+ Nylas.with = function (accessToken) {
92
+ if (!accessToken) {
93
+ throw new Error('This function requires an access token');
94
+ }
95
+ return new nylas_connection_1.default(accessToken, { clientId: this.clientId });
96
+ };
97
+ //TODO::Deprecate the camel case property in the next major release
98
+ Nylas.application = function (options) {
99
+ if (!this.clientId) {
100
+ throw new Error('This function requires a clientId');
101
+ }
102
+ if (!this.clientSecret) {
103
+ throw new Error('This function requires a clientSecret');
104
+ }
105
+ var connection = new nylas_connection_1.default(null, { clientId: this.clientId });
106
+ var requestOptions = {
107
+ path: "/a/" + this.clientId,
108
+ };
109
+ if (options) {
110
+ requestOptions.body = {
111
+ application_name: options.applicationName || options.application_name,
112
+ redirect_uris: options.redirectUris || options.redirect_uris,
113
+ };
114
+ requestOptions.method = 'PUT';
115
+ }
116
+ return connection.request(requestOptions);
117
+ };
118
+ Nylas.exchangeCodeForToken = function (code, callback) {
119
+ if (!this.clientId || !this.clientSecret) {
120
+ throw new Error('exchangeCodeForToken() cannot be called until you provide a clientId and secret via config()');
121
+ }
122
+ if (!code) {
123
+ throw new Error('exchangeCodeForToken() must be called with a code');
124
+ }
125
+ var url = new url_1.URL(this.apiServer + "/oauth/token");
126
+ url.searchParams.set('client_id', this.clientId);
127
+ url.searchParams.set('client_secret', this.clientSecret);
128
+ url.searchParams.set('grant_type', 'authorization_code');
129
+ url.searchParams.set('code', code);
130
+ return node_fetch_1.default(url)
131
+ .then(function (response) {
132
+ if (response.ok) {
133
+ return response.json();
134
+ }
135
+ })
136
+ .then(function (body) {
137
+ if (!body || !body['access_token']) {
138
+ var errorMessage = 'No access token in response';
139
+ if (body && body.message)
140
+ errorMessage = body.message;
141
+ throw new Error(errorMessage);
142
+ }
143
+ if (callback) {
144
+ callback(null, body['access_token']);
145
+ }
146
+ return body['access_token'];
147
+ }, function (error) {
148
+ var newError = new Error(error.message);
149
+ if (callback) {
150
+ callback(newError);
151
+ }
152
+ throw newError;
153
+ });
154
+ };
155
+ Nylas.urlForAuthentication = function (options) {
156
+ if (options === void 0) { options = {}; }
157
+ if (!this.clientId) {
158
+ throw new Error('urlForAuthentication() cannot be called until you provide a clientId via config()');
159
+ }
160
+ if (!options.redirectURI) {
161
+ throw new Error('urlForAuthentication() requires options.redirectURI');
162
+ }
163
+ if (!options.loginHint) {
164
+ options.loginHint = '';
165
+ }
166
+ var url = this.apiServer + "/oauth/authorize?client_id=" + this.clientId + "&response_type=code&login_hint=" + options.loginHint + "&redirect_uri=" + options.redirectURI;
167
+ if (options.state != null) {
168
+ url += "&state=" + options.state;
169
+ }
170
+ if (options.scopes != null) {
171
+ url += "&scopes=" + options.scopes.join(',');
172
+ }
173
+ return url;
174
+ };
175
+ Nylas.clientId = '';
176
+ return Nylas;
177
+ }());
178
+ module.exports = Nylas;