-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
99 lines (87 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
var assert = require('assert');
var setupMakeRequest = require('./lib/make-request');
var createArticleUploadFormData = require('./lib/create-article-upload-form-data');
var articleMetadataFromOpts = require('./lib/article-metadata-from-opts');
module.exports = function (config) {
assert(typeof config.apiId === 'string', 'config.apiId: API ID is required');
assert(typeof config.apiSecret === 'string', 'config.apiSecret: API secret is required');
var makeRequest = setupMakeRequest(config);
return {
readChannel: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.channelId === 'string', 'opts.channelId required');
var channelId = opts.channelId;
makeRequest('GET', '/channels/' + channelId, {}, cb);
},
listSections: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.channelId === 'string', 'opts.channelId required');
var channelId = opts.channelId;
makeRequest('GET', '/channels/' + channelId + '/sections', {}, cb);
},
readSection: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.sectionId === 'string', 'opts.sectionId required');
var sectionId = opts.sectionId;
makeRequest('GET', '/sections/' + sectionId, {}, cb);
},
createArticle: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.channelId === 'string', 'opts.channelId required');
assert(Object(opts.article) === opts.article, 'opts.article required');
var channelId = opts.channelId;
var bundleFiles = opts.bundleFiles || [];
var meta = articleMetadataFromOpts(opts);
var fd = createArticleUploadFormData(opts.article, bundleFiles, meta);
makeRequest('POST', '/channels/' + channelId + '/articles', {
formData: fd
}, cb);
},
readArticle: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.articleId === 'string', 'opts.articleId required');
var articleId = opts.articleId;
makeRequest('GET', '/articles/' + articleId, {}, cb);
},
updateArticle: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.articleId === 'string', 'opts.articleId required');
assert(typeof opts.revision === 'string', 'opts.revision required');
assert(Object(opts.article) === opts.article, 'opts.article required');
var articleId = opts.articleId;
var bundleFiles = opts.bundleFiles || [];
var meta = articleMetadataFromOpts(opts);
meta.revision = opts.revision;
var fd = createArticleUploadFormData(opts.article, bundleFiles, meta);
makeRequest('POST', '/articles/' + articleId, {
formData: fd
}, cb);
},
deleteArticle: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.articleId === 'string', 'opts.articleId required');
var articleId = opts.articleId;
makeRequest('DELETE', '/articles/' + articleId, {}, cb);
},
searchArticles: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
assert(typeof opts.channelId === 'string' || typeof opts.sectionId === 'string',
'opts.channelId or opts.sectionId required');
var channelId = opts.channelId;
var sectionId = opts.sectionId;
var endpoint = channelId
? '/channels/' + channelId + '/articles'
: '/sections/' + sectionId + '/articles';
makeRequest('GET', endpoint, {}, cb);
}
};
};