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.
- package/LICENSE.txt +22 -0
- package/README.md +65 -0
- package/lib/config.d.ts +4 -0
- package/lib/config.js +13 -0
- package/lib/models/account.d.ts +10 -0
- package/lib/models/account.js +61 -0
- package/lib/models/attributes.d.ts +104 -0
- package/lib/models/attributes.js +266 -0
- package/lib/models/calendar-restful-model-collection.d.ts +76 -0
- package/lib/models/calendar-restful-model-collection.js +128 -0
- package/lib/models/calendar.d.ts +19 -0
- package/lib/models/calendar.js +81 -0
- package/lib/models/connect.d.ts +12 -0
- package/lib/models/connect.js +51 -0
- package/lib/models/contact-restful-model-collection.d.ts +11 -0
- package/lib/models/contact-restful-model-collection.js +54 -0
- package/lib/models/contact.d.ts +75 -0
- package/lib/models/contact.js +245 -0
- package/lib/models/delta-stream.d.ts +30 -0
- package/lib/models/delta-stream.js +225 -0
- package/lib/models/delta.d.ts +11 -0
- package/lib/models/delta.js +91 -0
- package/lib/models/draft.d.ts +24 -0
- package/lib/models/draft.js +145 -0
- package/lib/models/email-participant.d.ts +6 -0
- package/lib/models/email-participant.js +44 -0
- package/lib/models/event-conferencing.d.ts +24 -0
- package/lib/models/event-conferencing.js +84 -0
- package/lib/models/event-participant.d.ts +7 -0
- package/lib/models/event-participant.js +47 -0
- package/lib/models/event.d.ts +45 -0
- package/lib/models/event.js +206 -0
- package/lib/models/file.d.ts +18 -0
- package/lib/models/file.js +151 -0
- package/lib/models/folder.d.ts +14 -0
- package/lib/models/folder.js +70 -0
- package/lib/models/job-status.d.ts +7 -0
- package/lib/models/job-status.js +51 -0
- package/lib/models/management-account.d.ts +14 -0
- package/lib/models/management-account.js +101 -0
- package/lib/models/management-model-collection.d.ts +11 -0
- package/lib/models/management-model-collection.js +35 -0
- package/lib/models/management-model.d.ts +8 -0
- package/lib/models/management-model.js +29 -0
- package/lib/models/message.d.ts +32 -0
- package/lib/models/message.js +154 -0
- package/lib/models/neural-categorizer.d.ts +18 -0
- package/lib/models/neural-categorizer.js +140 -0
- package/lib/models/neural-clean-conversation.d.ts +7 -0
- package/lib/models/neural-clean-conversation.js +110 -0
- package/lib/models/neural-ocr.d.ts +5 -0
- package/lib/models/neural-ocr.js +46 -0
- package/lib/models/neural-sentiment-analysis.d.ts +8 -0
- package/lib/models/neural-sentiment-analysis.js +49 -0
- package/lib/models/neural-signature-contact.d.ts +37 -0
- package/lib/models/neural-signature-contact.js +141 -0
- package/lib/models/neural-signature-extraction.d.ts +7 -0
- package/lib/models/neural-signature-extraction.js +50 -0
- package/lib/models/neural.d.ts +22 -0
- package/lib/models/neural.js +124 -0
- package/lib/models/nylas-api-error.d.ts +28 -0
- package/lib/models/nylas-api-error.js +61 -0
- package/lib/models/resource.d.ts +9 -0
- package/lib/models/resource.js +59 -0
- package/lib/models/restful-model-collection.d.ts +59 -0
- package/lib/models/restful-model-collection.js +325 -0
- package/lib/models/restful-model-instance.d.ts +11 -0
- package/lib/models/restful-model-instance.js +37 -0
- package/lib/models/restful-model.d.ts +48 -0
- package/lib/models/restful-model.js +153 -0
- package/lib/models/thread.d.ts +30 -0
- package/lib/models/thread.js +112 -0
- package/lib/models/webhook.d.ts +15 -0
- package/lib/models/webhook.js +71 -0
- package/lib/nylas-connection.d.ts +67 -0
- package/lib/nylas-connection.js +235 -0
- package/lib/nylas.d.ts +38 -0
- package/lib/nylas.js +178 -0
- package/package.json +76 -0
|
@@ -0,0 +1,50 @@
|
|
|
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 __assign = (this && this.__assign) || function () {
|
|
16
|
+
__assign = Object.assign || function(t) {
|
|
17
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
18
|
+
s = arguments[i];
|
|
19
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
20
|
+
t[p] = s[p];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
return __assign.apply(this, arguments);
|
|
25
|
+
};
|
|
26
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
var neural_signature_contact_1 = __importDefault(require("./neural-signature-contact"));
|
|
31
|
+
var attributes_1 = __importDefault(require("./attributes"));
|
|
32
|
+
var message_1 = __importDefault(require("./message"));
|
|
33
|
+
var NeuralSignatureExtraction = /** @class */ (function (_super) {
|
|
34
|
+
__extends(NeuralSignatureExtraction, _super);
|
|
35
|
+
function NeuralSignatureExtraction() {
|
|
36
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
37
|
+
}
|
|
38
|
+
return NeuralSignatureExtraction;
|
|
39
|
+
}(message_1.default));
|
|
40
|
+
exports.default = NeuralSignatureExtraction;
|
|
41
|
+
NeuralSignatureExtraction.collectionName = 'signature';
|
|
42
|
+
NeuralSignatureExtraction.attributes = __assign(__assign({}, message_1.default.attributes), { signature: attributes_1.default.String({
|
|
43
|
+
modelKey: 'signature',
|
|
44
|
+
}), modelVersion: attributes_1.default.String({
|
|
45
|
+
modelKey: 'modelVersion',
|
|
46
|
+
jsonKey: 'model_version',
|
|
47
|
+
}), contacts: attributes_1.default.Object({
|
|
48
|
+
modelKey: 'contacts',
|
|
49
|
+
itemClass: neural_signature_contact_1.default,
|
|
50
|
+
}) });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import RestfulModel from './restful-model';
|
|
2
|
+
import NeuralSentimentAnalysis from './neural-sentiment-analysis';
|
|
3
|
+
import NeuralSignatureExtraction from './neural-signature-extraction';
|
|
4
|
+
import NeuralOcr from './neural-ocr';
|
|
5
|
+
import NeuralCategorizer from './neural-categorizer';
|
|
6
|
+
import NeuralCleanConversation from './neural-clean-conversation';
|
|
7
|
+
export declare type NeuralMessageOptions = {
|
|
8
|
+
ignore_links?: boolean;
|
|
9
|
+
ignore_images?: boolean;
|
|
10
|
+
ignore_tables?: boolean;
|
|
11
|
+
remove_conclusion_phrases?: boolean;
|
|
12
|
+
images_as_markdowns?: boolean;
|
|
13
|
+
};
|
|
14
|
+
export default class Neural extends RestfulModel {
|
|
15
|
+
sentimentAnalysisMessage(messageIds: string[]): Promise<NeuralSentimentAnalysis[]>;
|
|
16
|
+
sentimentAnalysisText(text: string): Promise<NeuralSentimentAnalysis>;
|
|
17
|
+
extractSignature(messageIds: string[], parseContact?: boolean, options?: NeuralMessageOptions): Promise<NeuralSignatureExtraction[]>;
|
|
18
|
+
ocrRequest(fileId: string, pages?: number[]): Promise<NeuralOcr>;
|
|
19
|
+
categorize(messageIds: string[]): Promise<NeuralCategorizer[]>;
|
|
20
|
+
cleanConversation(messageIds: string[], options?: NeuralMessageOptions): Promise<NeuralCleanConversation[]>;
|
|
21
|
+
_request(path?: string, body?: object): Promise<any>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
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 __assign = (this && this.__assign) || function () {
|
|
16
|
+
__assign = Object.assign || function(t) {
|
|
17
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
18
|
+
s = arguments[i];
|
|
19
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
20
|
+
t[p] = s[p];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
return __assign.apply(this, arguments);
|
|
25
|
+
};
|
|
26
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
var restful_model_1 = __importDefault(require("./restful-model"));
|
|
31
|
+
var neural_sentiment_analysis_1 = __importDefault(require("./neural-sentiment-analysis"));
|
|
32
|
+
var neural_signature_extraction_1 = __importDefault(require("./neural-signature-extraction"));
|
|
33
|
+
var neural_ocr_1 = __importDefault(require("./neural-ocr"));
|
|
34
|
+
var neural_categorizer_1 = __importDefault(require("./neural-categorizer"));
|
|
35
|
+
var neural_clean_conversation_1 = __importDefault(require("./neural-clean-conversation"));
|
|
36
|
+
var Neural = /** @class */ (function (_super) {
|
|
37
|
+
__extends(Neural, _super);
|
|
38
|
+
function Neural() {
|
|
39
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
40
|
+
}
|
|
41
|
+
Neural.prototype.sentimentAnalysisMessage = function (messageIds) {
|
|
42
|
+
var _this = this;
|
|
43
|
+
var body = { message_id: messageIds };
|
|
44
|
+
var path = 'sentiment';
|
|
45
|
+
return this._request(path, body).then(function (jsonArray) {
|
|
46
|
+
return jsonArray.map(function (json) {
|
|
47
|
+
return new neural_sentiment_analysis_1.default(_this.connection, json);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
Neural.prototype.sentimentAnalysisText = function (text) {
|
|
52
|
+
var _this = this;
|
|
53
|
+
var body = { text: text };
|
|
54
|
+
var path = 'sentiment';
|
|
55
|
+
return this._request(path, body).then(function (json) {
|
|
56
|
+
return new neural_sentiment_analysis_1.default(_this.connection, json);
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
Neural.prototype.extractSignature = function (messageIds, parseContact, options) {
|
|
60
|
+
var _this = this;
|
|
61
|
+
var body = { message_id: messageIds };
|
|
62
|
+
var path = 'signature';
|
|
63
|
+
if (options) {
|
|
64
|
+
body = __assign(__assign({}, body), options);
|
|
65
|
+
}
|
|
66
|
+
if (parseContact) {
|
|
67
|
+
body['parse_contact'] = parseContact;
|
|
68
|
+
}
|
|
69
|
+
return this._request(path, body).then(function (jsonArray) {
|
|
70
|
+
return jsonArray.map(function (json) {
|
|
71
|
+
return new neural_signature_extraction_1.default(_this.connection, json);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
Neural.prototype.ocrRequest = function (fileId, pages) {
|
|
76
|
+
var _this = this;
|
|
77
|
+
var body = { file_id: fileId };
|
|
78
|
+
var path = 'ocr';
|
|
79
|
+
if (pages) {
|
|
80
|
+
body['pages'] = pages;
|
|
81
|
+
}
|
|
82
|
+
return this._request(path, body).then(function (json) {
|
|
83
|
+
return new neural_ocr_1.default(_this.connection, json);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
Neural.prototype.categorize = function (messageIds) {
|
|
87
|
+
var _this = this;
|
|
88
|
+
var body = { message_id: messageIds };
|
|
89
|
+
var path = 'categorize';
|
|
90
|
+
return this._request(path, body).then(function (jsonArray) {
|
|
91
|
+
return jsonArray.map(function (json) {
|
|
92
|
+
return new neural_categorizer_1.default(_this.connection, json);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
Neural.prototype.cleanConversation = function (messageIds, options) {
|
|
97
|
+
var _this = this;
|
|
98
|
+
var body = { message_id: messageIds };
|
|
99
|
+
var path = 'conversation';
|
|
100
|
+
if (options) {
|
|
101
|
+
body = __assign(__assign({}, body), options);
|
|
102
|
+
}
|
|
103
|
+
return this._request(path, body).then(function (jsonArray) {
|
|
104
|
+
return jsonArray.map(function (json) {
|
|
105
|
+
return new neural_clean_conversation_1.default(_this.connection, json);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
Neural.prototype._request = function (path, body) {
|
|
110
|
+
if (!path) {
|
|
111
|
+
path = this.constructor.collectionName;
|
|
112
|
+
}
|
|
113
|
+
return this.connection.request({
|
|
114
|
+
method: 'PUT',
|
|
115
|
+
path: "/neural/" + path,
|
|
116
|
+
body: body,
|
|
117
|
+
headers: {
|
|
118
|
+
'Content-Type': 'application/json',
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
return Neural;
|
|
123
|
+
}(restful_model_1.default));
|
|
124
|
+
exports.default = Neural;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extended Error class for errors returned from the Nylas API
|
|
3
|
+
*
|
|
4
|
+
* Properties:
|
|
5
|
+
* name - The description of the HTTP status code
|
|
6
|
+
* message - The error message returned from the Nylas API payload
|
|
7
|
+
* statusCode - The status code returned from the API call
|
|
8
|
+
* type - The type of error returned from the Nylas API payload
|
|
9
|
+
* stack - The Error stacktrace
|
|
10
|
+
* missingFields (optional) - The fields that were missing in the call returned from the Nylas API payload
|
|
11
|
+
* serverError (optional) - The error returned by the provider returned from the Nylas API payload
|
|
12
|
+
*/
|
|
13
|
+
export default class NylasApiError extends Error {
|
|
14
|
+
statusCode: number;
|
|
15
|
+
type: string;
|
|
16
|
+
missingFields?: string[];
|
|
17
|
+
serverError?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Mapping of HTTP status codes to error descriptions
|
|
20
|
+
*
|
|
21
|
+
* For more details on what each status code means head to
|
|
22
|
+
* https://developer.nylas.com/docs/developer-tools/api/errors/
|
|
23
|
+
*/
|
|
24
|
+
errorMapping: {
|
|
25
|
+
[statusCode: number]: string;
|
|
26
|
+
};
|
|
27
|
+
constructor(statusCode: number, type: string, message: string);
|
|
28
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
/**
|
|
17
|
+
* Extended Error class for errors returned from the Nylas API
|
|
18
|
+
*
|
|
19
|
+
* Properties:
|
|
20
|
+
* name - The description of the HTTP status code
|
|
21
|
+
* message - The error message returned from the Nylas API payload
|
|
22
|
+
* statusCode - The status code returned from the API call
|
|
23
|
+
* type - The type of error returned from the Nylas API payload
|
|
24
|
+
* stack - The Error stacktrace
|
|
25
|
+
* missingFields (optional) - The fields that were missing in the call returned from the Nylas API payload
|
|
26
|
+
* serverError (optional) - The error returned by the provider returned from the Nylas API payload
|
|
27
|
+
*/
|
|
28
|
+
var NylasApiError = /** @class */ (function (_super) {
|
|
29
|
+
__extends(NylasApiError, _super);
|
|
30
|
+
function NylasApiError(statusCode, type, message) {
|
|
31
|
+
var _this = _super.call(this, message) || this;
|
|
32
|
+
/**
|
|
33
|
+
* Mapping of HTTP status codes to error descriptions
|
|
34
|
+
*
|
|
35
|
+
* For more details on what each status code means head to
|
|
36
|
+
* https://developer.nylas.com/docs/developer-tools/api/errors/
|
|
37
|
+
*/
|
|
38
|
+
_this.errorMapping = {
|
|
39
|
+
400: 'Bad Request',
|
|
40
|
+
401: 'Unauthorized',
|
|
41
|
+
402: 'Request Failed or Payment Required',
|
|
42
|
+
403: 'Forbidden',
|
|
43
|
+
404: 'Not Found',
|
|
44
|
+
405: 'Method Not Allowed',
|
|
45
|
+
410: 'Gone',
|
|
46
|
+
418: "I'm a Teapot",
|
|
47
|
+
422: 'Sending Error',
|
|
48
|
+
429: 'Too Many Requests',
|
|
49
|
+
500: 'Server Error',
|
|
50
|
+
502: 'Server Error',
|
|
51
|
+
503: 'Server Error',
|
|
52
|
+
504: 'Server Error',
|
|
53
|
+
};
|
|
54
|
+
_this.statusCode = statusCode;
|
|
55
|
+
_this.name = _this.errorMapping[statusCode];
|
|
56
|
+
_this.type = type;
|
|
57
|
+
return _this;
|
|
58
|
+
}
|
|
59
|
+
return NylasApiError;
|
|
60
|
+
}(Error));
|
|
61
|
+
exports.default = NylasApiError;
|
|
@@ -0,0 +1,59 @@
|
|
|
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 restful_model_1 = __importDefault(require("./restful-model"));
|
|
20
|
+
var attributes_1 = __importDefault(require("./attributes"));
|
|
21
|
+
var Resource = /** @class */ (function (_super) {
|
|
22
|
+
__extends(Resource, _super);
|
|
23
|
+
function Resource() {
|
|
24
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
25
|
+
}
|
|
26
|
+
return Resource;
|
|
27
|
+
}(restful_model_1.default));
|
|
28
|
+
exports.default = Resource;
|
|
29
|
+
Resource.collectionName = 'resources';
|
|
30
|
+
Resource.attributes = {
|
|
31
|
+
object: attributes_1.default.String({
|
|
32
|
+
modelKey: 'object',
|
|
33
|
+
readOnly: true,
|
|
34
|
+
}),
|
|
35
|
+
email: attributes_1.default.String({
|
|
36
|
+
modelKey: 'email',
|
|
37
|
+
}),
|
|
38
|
+
name: attributes_1.default.String({
|
|
39
|
+
modelKey: 'name',
|
|
40
|
+
}),
|
|
41
|
+
capacity: attributes_1.default.String({
|
|
42
|
+
modelKey: 'capacity',
|
|
43
|
+
readOnly: true,
|
|
44
|
+
}),
|
|
45
|
+
building: attributes_1.default.String({
|
|
46
|
+
modelKey: 'building',
|
|
47
|
+
readOnly: true,
|
|
48
|
+
}),
|
|
49
|
+
floorName: attributes_1.default.String({
|
|
50
|
+
modelKey: 'floorName',
|
|
51
|
+
jsonKey: 'floor_name',
|
|
52
|
+
readOnly: true,
|
|
53
|
+
}),
|
|
54
|
+
floorNumber: attributes_1.default.String({
|
|
55
|
+
modelKey: 'floorNumber',
|
|
56
|
+
jsonKey: 'floor_number',
|
|
57
|
+
readOnly: true,
|
|
58
|
+
}),
|
|
59
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import NylasConnection from '../nylas-connection';
|
|
2
|
+
import RestfulModel from './restful-model';
|
|
3
|
+
export declare type GetCallback = (error: Error | null, result?: RestfulModel) => void;
|
|
4
|
+
export default class RestfulModelCollection<T extends RestfulModel> {
|
|
5
|
+
connection: NylasConnection;
|
|
6
|
+
modelClass: typeof RestfulModel;
|
|
7
|
+
constructor(modelClass: typeof RestfulModel, connection: NylasConnection);
|
|
8
|
+
forEach(params: {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
} | undefined, eachCallback: (item: T) => void, completeCallback?: (err?: Error | null | undefined) => void): Promise<never> | undefined;
|
|
11
|
+
count(params?: {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}, callback?: (err: Error | null, num?: number) => void): Promise<any>;
|
|
14
|
+
first(params?: {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}, callback?: (error: Error | null, model?: T) => void): Promise<T>;
|
|
17
|
+
list(params?: {
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}, callback?: (error: Error | null, obj?: T[]) => void): Promise<void | T[]>;
|
|
20
|
+
find(id: string, paramsArg?: {
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
} | GetCallback | null, callbackArg?: GetCallback | {
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
} | null): Promise<T>;
|
|
25
|
+
search(query: string, params?: {
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}, callback?: (error: Error | null) => void): Promise<void | T[]>;
|
|
28
|
+
delete(itemOrId: T | string, params?: {
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
}, callback?: (error: Error | null) => void): Promise<any>;
|
|
31
|
+
deleteItem(options: {
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}, callbackArg?: (error: Error | null) => void): Promise<any>;
|
|
34
|
+
build(args: {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
}): T;
|
|
37
|
+
path(): string;
|
|
38
|
+
_range({ params, offset, limit, callback, path, }: {
|
|
39
|
+
params?: {
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
};
|
|
42
|
+
offset?: number;
|
|
43
|
+
limit?: number;
|
|
44
|
+
callback?: (error: Error | null, results?: T[]) => void;
|
|
45
|
+
path?: string;
|
|
46
|
+
}): Promise<void | T[]>;
|
|
47
|
+
_getItems(params: {
|
|
48
|
+
[key: string]: any;
|
|
49
|
+
}, offset: number, limit: number, path?: string): Promise<T[]>;
|
|
50
|
+
_createModel(json: {
|
|
51
|
+
[key: string]: any;
|
|
52
|
+
}): T;
|
|
53
|
+
_getModel(id: string, params?: {
|
|
54
|
+
[key: string]: any;
|
|
55
|
+
}): Promise<T>;
|
|
56
|
+
_getModelCollection(params: {
|
|
57
|
+
[key: string]: any;
|
|
58
|
+
}, offset: number, limit: number, path: string): Promise<T[]>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
var message_1 = __importDefault(require("./message"));
|
|
18
|
+
var nylas_connection_1 = __importDefault(require("../nylas-connection"));
|
|
19
|
+
var thread_1 = __importDefault(require("./thread"));
|
|
20
|
+
var REQUEST_CHUNK_SIZE = 100;
|
|
21
|
+
var RestfulModelCollection = /** @class */ (function () {
|
|
22
|
+
function RestfulModelCollection(modelClass, connection) {
|
|
23
|
+
this.modelClass = modelClass;
|
|
24
|
+
this.connection = connection;
|
|
25
|
+
if (!(this.connection instanceof nylas_connection_1.default)) {
|
|
26
|
+
throw new Error('Connection object not provided');
|
|
27
|
+
}
|
|
28
|
+
if (!this.modelClass) {
|
|
29
|
+
throw new Error('Model class not provided');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
RestfulModelCollection.prototype.forEach = function (params, eachCallback, completeCallback) {
|
|
33
|
+
var _this = this;
|
|
34
|
+
if (params === void 0) { params = {}; }
|
|
35
|
+
if (params.view == 'count') {
|
|
36
|
+
var err = new Error('forEach() cannot be called with the count view');
|
|
37
|
+
if (completeCallback) {
|
|
38
|
+
completeCallback(err);
|
|
39
|
+
}
|
|
40
|
+
return Promise.reject(err);
|
|
41
|
+
}
|
|
42
|
+
var offset = 0;
|
|
43
|
+
var iteratee = function () {
|
|
44
|
+
return _this._getItems(params, offset, REQUEST_CHUNK_SIZE).then(function (items) {
|
|
45
|
+
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
|
|
46
|
+
var item = items_1[_i];
|
|
47
|
+
eachCallback(item);
|
|
48
|
+
}
|
|
49
|
+
offset += items.length;
|
|
50
|
+
var finished = items.length < REQUEST_CHUNK_SIZE;
|
|
51
|
+
if (finished === false) {
|
|
52
|
+
return iteratee();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
iteratee().then(function () {
|
|
57
|
+
if (completeCallback) {
|
|
58
|
+
completeCallback();
|
|
59
|
+
}
|
|
60
|
+
}, function (err) {
|
|
61
|
+
if (completeCallback) {
|
|
62
|
+
return completeCallback(err);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
RestfulModelCollection.prototype.count = function (params, callback) {
|
|
67
|
+
if (params === void 0) { params = {}; }
|
|
68
|
+
return this.connection
|
|
69
|
+
.request({
|
|
70
|
+
method: 'GET',
|
|
71
|
+
path: this.path(),
|
|
72
|
+
qs: __assign({ view: 'count' }, params),
|
|
73
|
+
})
|
|
74
|
+
.then(function (json) {
|
|
75
|
+
if (callback) {
|
|
76
|
+
callback(null, json.count);
|
|
77
|
+
}
|
|
78
|
+
return Promise.resolve(json.count);
|
|
79
|
+
})
|
|
80
|
+
.catch(function (err) {
|
|
81
|
+
if (callback) {
|
|
82
|
+
callback(err);
|
|
83
|
+
}
|
|
84
|
+
return Promise.reject(err);
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
RestfulModelCollection.prototype.first = function (params, callback) {
|
|
88
|
+
if (params === void 0) { params = {}; }
|
|
89
|
+
if (params.view == 'count') {
|
|
90
|
+
var err = new Error('first() cannot be called with the count view');
|
|
91
|
+
if (callback) {
|
|
92
|
+
callback(err);
|
|
93
|
+
}
|
|
94
|
+
return Promise.reject(err);
|
|
95
|
+
}
|
|
96
|
+
return this._getItems(params, 0, 1)
|
|
97
|
+
.then(function (items) {
|
|
98
|
+
if (callback) {
|
|
99
|
+
callback(null, items[0]);
|
|
100
|
+
}
|
|
101
|
+
return Promise.resolve(items[0]);
|
|
102
|
+
})
|
|
103
|
+
.catch(function (err) {
|
|
104
|
+
if (callback) {
|
|
105
|
+
callback(err);
|
|
106
|
+
}
|
|
107
|
+
return Promise.reject(err);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
RestfulModelCollection.prototype.list = function (params, callback) {
|
|
111
|
+
if (params === void 0) { params = {}; }
|
|
112
|
+
if (params.view == 'count') {
|
|
113
|
+
var err = new Error('list() cannot be called with the count view');
|
|
114
|
+
if (callback) {
|
|
115
|
+
callback(err);
|
|
116
|
+
}
|
|
117
|
+
return Promise.reject(err);
|
|
118
|
+
}
|
|
119
|
+
var limit = params.limit || Infinity;
|
|
120
|
+
var offset = params.offset;
|
|
121
|
+
return this._range({ params: params, offset: offset, limit: limit, callback: callback });
|
|
122
|
+
};
|
|
123
|
+
RestfulModelCollection.prototype.find = function (id, paramsArg, callbackArg) {
|
|
124
|
+
// callback used to be the second argument, and params was the third
|
|
125
|
+
var callback;
|
|
126
|
+
if (typeof callbackArg === 'function') {
|
|
127
|
+
callback = callbackArg;
|
|
128
|
+
}
|
|
129
|
+
else if (typeof paramsArg === 'function') {
|
|
130
|
+
callback = paramsArg;
|
|
131
|
+
}
|
|
132
|
+
var params = {};
|
|
133
|
+
if (paramsArg && typeof paramsArg === 'object') {
|
|
134
|
+
params = paramsArg;
|
|
135
|
+
}
|
|
136
|
+
else if (callbackArg && typeof callbackArg === 'object') {
|
|
137
|
+
params = callbackArg;
|
|
138
|
+
}
|
|
139
|
+
if (!id) {
|
|
140
|
+
var err = new Error('find() must be called with an item id');
|
|
141
|
+
if (callback) {
|
|
142
|
+
callback(err);
|
|
143
|
+
}
|
|
144
|
+
return Promise.reject(err);
|
|
145
|
+
}
|
|
146
|
+
if (params.view == 'count' || params.view == 'ids') {
|
|
147
|
+
var err = new Error('find() cannot be called with the count or ids view');
|
|
148
|
+
if (callback) {
|
|
149
|
+
callback(err);
|
|
150
|
+
}
|
|
151
|
+
return Promise.reject(err);
|
|
152
|
+
}
|
|
153
|
+
return this._getModel(id, params)
|
|
154
|
+
.then(function (model) {
|
|
155
|
+
if (callback) {
|
|
156
|
+
callback(null, model);
|
|
157
|
+
}
|
|
158
|
+
return Promise.resolve(model);
|
|
159
|
+
})
|
|
160
|
+
.catch(function (err) {
|
|
161
|
+
if (callback) {
|
|
162
|
+
callback(err);
|
|
163
|
+
}
|
|
164
|
+
return Promise.reject(err);
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
RestfulModelCollection.prototype.search = function (query, params, callback) {
|
|
168
|
+
if (params === void 0) { params = {}; }
|
|
169
|
+
if (this.modelClass != message_1.default && this.modelClass != thread_1.default) {
|
|
170
|
+
var err = new Error('search() can only be called for messages and threads');
|
|
171
|
+
if (callback) {
|
|
172
|
+
callback(err);
|
|
173
|
+
}
|
|
174
|
+
return Promise.reject(err);
|
|
175
|
+
}
|
|
176
|
+
if (!query) {
|
|
177
|
+
var err = new Error('search() requires a query string');
|
|
178
|
+
if (callback) {
|
|
179
|
+
callback(err);
|
|
180
|
+
}
|
|
181
|
+
return Promise.reject(err);
|
|
182
|
+
}
|
|
183
|
+
params.q = query;
|
|
184
|
+
var limit = params.limit || 40;
|
|
185
|
+
var offset = params.offset;
|
|
186
|
+
var path = this.path() + "/search";
|
|
187
|
+
return this._range({ params: params, offset: offset, limit: limit, path: path });
|
|
188
|
+
};
|
|
189
|
+
RestfulModelCollection.prototype.delete = function (itemOrId, params, callback) {
|
|
190
|
+
if (params === void 0) { params = {}; }
|
|
191
|
+
if (!itemOrId) {
|
|
192
|
+
var err = new Error('delete() requires an item or an id');
|
|
193
|
+
if (callback) {
|
|
194
|
+
callback(err);
|
|
195
|
+
}
|
|
196
|
+
return Promise.reject(err);
|
|
197
|
+
}
|
|
198
|
+
if (typeof params === 'function') {
|
|
199
|
+
callback = params;
|
|
200
|
+
params = {};
|
|
201
|
+
}
|
|
202
|
+
var item = typeof itemOrId === 'string' ? this.build({ id: itemOrId }) : itemOrId;
|
|
203
|
+
var options = item.deleteRequestOptions(params);
|
|
204
|
+
options.item = item;
|
|
205
|
+
return this.deleteItem(options, callback);
|
|
206
|
+
};
|
|
207
|
+
RestfulModelCollection.prototype.deleteItem = function (options, callbackArg) {
|
|
208
|
+
var item = options.item;
|
|
209
|
+
// callback used to be in the options object
|
|
210
|
+
var callback = options.callback ? options.callback : callbackArg;
|
|
211
|
+
var body = options.hasOwnProperty('body')
|
|
212
|
+
? options.body
|
|
213
|
+
: item.deleteRequestBody({});
|
|
214
|
+
var qs = options.hasOwnProperty('qs')
|
|
215
|
+
? options.qs
|
|
216
|
+
: item.deleteRequestQueryString({});
|
|
217
|
+
return this.connection
|
|
218
|
+
.request({
|
|
219
|
+
method: 'DELETE',
|
|
220
|
+
qs: qs,
|
|
221
|
+
body: body,
|
|
222
|
+
path: this.path() + "/" + item.id,
|
|
223
|
+
})
|
|
224
|
+
.then(function (data) {
|
|
225
|
+
if (callback) {
|
|
226
|
+
callback(null, data);
|
|
227
|
+
}
|
|
228
|
+
return Promise.resolve(data);
|
|
229
|
+
})
|
|
230
|
+
.catch(function (err) {
|
|
231
|
+
if (callback) {
|
|
232
|
+
callback(err);
|
|
233
|
+
}
|
|
234
|
+
return Promise.reject(err);
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
RestfulModelCollection.prototype.build = function (args) {
|
|
238
|
+
var model = this._createModel({});
|
|
239
|
+
for (var key in args) {
|
|
240
|
+
var val = args[key];
|
|
241
|
+
model[key] = val;
|
|
242
|
+
}
|
|
243
|
+
return model;
|
|
244
|
+
};
|
|
245
|
+
RestfulModelCollection.prototype.path = function () {
|
|
246
|
+
return "/" + this.modelClass.collectionName;
|
|
247
|
+
};
|
|
248
|
+
RestfulModelCollection.prototype._range = function (_a) {
|
|
249
|
+
var _this = this;
|
|
250
|
+
var _b = _a.params, params = _b === void 0 ? {} : _b, _c = _a.offset, offset = _c === void 0 ? 0 : _c, _d = _a.limit, limit = _d === void 0 ? 100 : _d, callback = _a.callback, path = _a.path;
|
|
251
|
+
var accumulated = [];
|
|
252
|
+
var iteratee = function () {
|
|
253
|
+
var chunkOffset = offset + accumulated.length;
|
|
254
|
+
var chunkLimit = Math.min(REQUEST_CHUNK_SIZE, limit - accumulated.length);
|
|
255
|
+
return _this._getItems(params, chunkOffset, chunkLimit, path).then(function (items) {
|
|
256
|
+
accumulated = accumulated.concat(items);
|
|
257
|
+
var finished = items.length < REQUEST_CHUNK_SIZE || accumulated.length >= limit;
|
|
258
|
+
if (finished === false) {
|
|
259
|
+
return iteratee();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
// do not return rejected promise when callback is provided
|
|
264
|
+
// to prevent unhandled rejection warning
|
|
265
|
+
return iteratee().then(function () {
|
|
266
|
+
if (callback) {
|
|
267
|
+
return callback(null, accumulated);
|
|
268
|
+
}
|
|
269
|
+
return accumulated;
|
|
270
|
+
}, function (err) {
|
|
271
|
+
if (callback) {
|
|
272
|
+
return callback(err);
|
|
273
|
+
}
|
|
274
|
+
throw err;
|
|
275
|
+
});
|
|
276
|
+
};
|
|
277
|
+
RestfulModelCollection.prototype._getItems = function (params, offset, limit, path) {
|
|
278
|
+
// Items can be either models or ids
|
|
279
|
+
if (!path) {
|
|
280
|
+
path = this.path();
|
|
281
|
+
}
|
|
282
|
+
if (params.view == 'ids') {
|
|
283
|
+
return this.connection.request({
|
|
284
|
+
method: 'GET',
|
|
285
|
+
path: path,
|
|
286
|
+
qs: __assign(__assign({}, params), { offset: offset, limit: limit }),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return this._getModelCollection(params, offset, limit, path);
|
|
290
|
+
};
|
|
291
|
+
RestfulModelCollection.prototype._createModel = function (json) {
|
|
292
|
+
return new this.modelClass(this.connection, json);
|
|
293
|
+
};
|
|
294
|
+
RestfulModelCollection.prototype._getModel = function (id, params) {
|
|
295
|
+
var _this = this;
|
|
296
|
+
if (params === void 0) { params = {}; }
|
|
297
|
+
return this.connection
|
|
298
|
+
.request({
|
|
299
|
+
method: 'GET',
|
|
300
|
+
path: this.path() + "/" + id,
|
|
301
|
+
qs: params,
|
|
302
|
+
})
|
|
303
|
+
.then(function (json) {
|
|
304
|
+
var model = _this._createModel(json);
|
|
305
|
+
return Promise.resolve(model);
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
RestfulModelCollection.prototype._getModelCollection = function (params, offset, limit, path) {
|
|
309
|
+
var _this = this;
|
|
310
|
+
return this.connection
|
|
311
|
+
.request({
|
|
312
|
+
method: 'GET',
|
|
313
|
+
path: path,
|
|
314
|
+
qs: __assign(__assign({}, params), { offset: offset, limit: limit }),
|
|
315
|
+
})
|
|
316
|
+
.then(function (jsonArray) {
|
|
317
|
+
var models = jsonArray.map(function (json) {
|
|
318
|
+
return _this._createModel(json);
|
|
319
|
+
});
|
|
320
|
+
return Promise.resolve(models);
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
return RestfulModelCollection;
|
|
324
|
+
}());
|
|
325
|
+
exports.default = RestfulModelCollection;
|