-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
191 lines (176 loc) · 4.87 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Dependencies
const queryValidation = require('./libs/query-validation.js');
const utils = require('./libs/utilities');
/**
* Create an instance of Behance
* @param {string} token - authentication for Behance API
* @public
*/
const Behance = function behance(token) {
this.clientId = token;
// Throw an error if Auth Key is not specified
if (this.clientId === undefined) {
throw Error('Please supply an authorization token for new Behance().');
}
};
// Endpoints that only support Options
const endpointWithOptionOnly = [{
name: 'projects',
path: 'projects',
queries: queryValidation.projects
}, {
name: 'creativesToFollow',
path: 'creativestofollow',
queries: queryValidation.creativesToFollow
}, {
name: 'users',
path: 'users',
queries: queryValidation.users
}, {
name: 'teams',
path: 'teams',
queries: queryValidation.teams
}, {
name: 'collections',
path: 'collections',
queries: queryValidation.collections
}];
endpointWithOptionOnly.forEach(function iterate(def) {
/**
* Get a list of projects/creatives/users/collections
* @param {object} opts - queries
* @param {function} cb - callback
* @return {object} - response from Behance API
* @public
*/
Behance.prototype[def.name] = function assign(opts, cb) {
if (Object.keys(opts).length === 0 || utils.compareKeys(opts, def.queries, def.name)) {
utils.requestHandler(utils.requestUrl(def.path, this.clientId, opts), cb);
}
};
});
/**
* Endpoints that require an ID with no Options
*/
const endpointWithOnlyAnId = [{
name: 'project',
pathprefix: 'projects/'
}, {
name: 'user',
pathprefix: 'users/'
}, {
name: 'team',
pathprefix: 'teams/'
}, {
name: 'userStats',
pathprefix: 'users/',
pathsuffix: '/stats'
}, {
name: 'userWorkExperience',
pathprefix: 'users/',
pathsuffix: '/work_experience'
}, {
name: 'collection',
path: 'collection'
}];
endpointWithOnlyAnId.forEach(function iterate(def) {
/**
* Get info about a project/user/collection
* @param {string} id - identifier
* @param {function} cb - callback
* @return {object} - response from Behance API
* @public
*/
Behance.prototype[def.name] = function assign(id, cb) {
const endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : '');
if (arguments.length !== 2) {
throw Error(`.${def.name} requires both an id and a callback function.`);
}
utils.requestHandler(utils.requestUrl(endpoint, this.clientId), cb);
};
});
/**
* Endpoints that require an ID and support Options
*/
const endpointWithIdAndOptions = [{
name: 'projectComments',
pathprefix: 'projects/',
pathsuffix: '/comments',
queries: queryValidation.projectComments
}, {
name: 'userProjects',
pathprefix: 'users/',
pathsuffix: '/projects',
queries: queryValidation.userProjects
}, {
name: 'userWips',
pathprefix: 'users/',
pathsuffix: '/wips',
queries: queryValidation.userWips
}, {
name: 'teamProjects',
pathprefix: 'teams/',
pathsuffix: '/projects',
queries: queryValidation.teamsProjects
}, {
name: 'userAppreciations',
pathprefix: 'users/',
pathsuffix: '/appreciations',
queries: queryValidation.userAppreciations
}, {
name: 'userCollections',
pathprefix: 'users/',
pathsuffix: '/collections',
queries: queryValidation.userCollections
}, {
name: 'userFollowers',
pathprefix: 'users/',
pathsuffix: '/followers',
queries: queryValidation.userFollowers
}, {
name: 'userFollowing',
pathprefix: 'users/',
pathsuffix: '/following',
queries: queryValidation.userFollowing
}, {
name: 'collectionProjects',
pathprefix: 'collections/',
pathsuffix: '/projects',
queries: queryValidation.collectionProjects
}];
endpointWithIdAndOptions.forEach(function iterate(def) {
/**
* Get a list of comments/projects/wips/appreciations/collections/followers
* @param {string} id - identifier
* @param {object} opts - queries
* @param {function} cb - callback
* @return {object} - response from Behance API
* @public
*/
Behance.prototype[def.name] = function assign(id, opts, cb) {
const endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : '');
let newCb;
let newOpts;
// Update Params order if options aren't supplied
if (arguments.length === 2) {
newCb = opts;
newOpts = {};
}
if (id === '' || typeof id === 'object') {
throw Error(`.${def.name} requires at least an id and a callback function.`);
}
if (Object.keys(opts).length === 0 || utils.compareKeys(opts, def.queries, def.name)) {
utils.requestHandler(utils.requestUrl(endpoint, this.clientId, newOpts || opts), newCb || cb);
}
};
});
/**
* Get Creative Fields
* @param {function} cb - callback
* @return {object} - response from Behance API
* @public
*/
Behance.prototype.fields = function assign(cb) {
utils.requestHandler(utils.requestUrl('fields', this.clientId), cb);
};
module.exports = Behance;