-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
194 lines (162 loc) · 5.06 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
192
193
194
const axios = require("axios");
// Helper to deal with pulling an inner object out of returned data
// and then assigning other data as "attrs"
const pullObject = (key) => (object) => {
let newObj = object[key];
delete object[key];
Object.defineProperty(newObj, "attrs", {
value: object,
});
return newObj;
};
// Helper to accept an array or multiple arguments
// and convert to array
const arrayOrList = (list) => {
if (typeof list[0] === "object" || typeof list[0] === "array") {
return list[0];
}
return list;
};
class Arena {
constructor(opts) {
opts = opts || {};
let headers = {
"Content-Type": "application/json",
};
if (opts.accessToken) {
headers["Authorization"] = `Bearer ${opts.accessToken}`;
}
if (opts.authToken) {
headers["X-AUTH-TOKEN"] = `${opts.authToken}`;
}
this.axios = axios.create({
baseURL: opts.baseURL || "https://api.are.na/v2/",
headers,
});
this.requestHandler =
opts.requestHandler ||
((method, url, data) => {
return method === "get"
? this.axios
.request({ method, url, params: data })
.then(({ data }) => data)
: this.axios.request({ method, url, data }).then(({ data }) => data);
});
}
_req(method, url, ...data) {
return this.requestHandler(
method.toLowerCase(),
url,
Object.assign({}, ...data)
);
}
group(slug) {
slug = slug || "";
return {
get: (opts) => this._req("GET", "groups/" + slug, opts),
channels: (opts) =>
this._req("GET", "groups/" + slug + "/channels", opts).then(
pullObject("channels")
),
};
}
channel(slug, data) {
slug = slug || "";
return {
get: (opts) => this._req("GET", "channels/" + slug, data, opts),
thumb: (opts) =>
this._req("GET", "channels/" + slug + "/thumb", data, opts),
connections: (opts) =>
this._req("GET", "channels/" + slug + "/connections", data, opts).then(
pullObject("channels")
),
channels: (opts) =>
this._req("GET", "channels/" + slug + "/channels", data, opts).then(
pullObject("channels")
),
contents: (opts) =>
this._req("GET", "channels/" + slug + "/contents", data, opts).then(
pullObject("contents")
),
collaborators: (opts) =>
this._req(
"GET",
"channels/" + slug + "/collaborators",
data,
opts
).then(pullObject("users")),
create: (title, status) =>
this._req("POST", "channels", {
// Allow it to be called as .channel(title).create(status) or
// .channel().create(title, status)
title: slug || title,
status: slug ? title : status,
}),
delete: (deleteSlug) =>
this._req("DELETE", "channels/" + (slug || deleteSlug)),
update: (opts) => this._req("PUT", "channels/" + slug, opts),
addCollaborators: (...ids) =>
this._req("POST", "channels/" + slug + "/collaborators", {
"ids[]": arrayOrList(ids),
}).then(pullObject("users")),
deleteCollaborators: (...ids) =>
this._req("DELETE", "channels/" + slug + "/collaborators", {
"ids[]": arrayOrList(ids),
}).then(pullObject("users")),
createBlock: (opts) => {
if (opts.content.match(/^https?:\/\//)) {
opts.source = opts.content;
}
return this._req("POST", "channels/" + slug + "/blocks", opts);
},
deleteBlock: (id) =>
this._req("DELETE", "channels/" + slug + "/blocks/" + id),
};
}
block(id, data) {
return {
get: (opts) => this._req("GET", "blocks/" + id, data, opts),
channels: (opts) =>
this._req("GET", "blocks/" + id + "/channels", data, opts).then(
pullObject("channels")
),
create: (channel, opts) => this.channel(channel).createBlock(opts),
update: (opts) => this._req("PUT", "blocks/" + id, data, opts),
};
}
user(id, data) {
return {
get: (opts) => this._req("GET", "users/" + id, data, opts),
channels: (opts) =>
this._req("GET", "users/" + id + "/channels", data, opts).then(
pullObject("channels")
),
following: (opts) =>
this._req("GET", "users/" + id + "/following", data, opts).then(
pullObject("following")
),
followers: (opts) =>
this._req("GET", "users/" + id + "/followers", data, opts).then(
pullObject("users")
),
};
}
search(q, data) {
return {
all: (opts) => this._req("GET", "search", { q }, data, opts),
users: (opts) =>
this._req("GET", "search/users", { q }, data, opts).then(
pullObject("users")
),
channels: (opts) =>
this._req("GET", "search/channels", { q }, data, opts).then(
pullObject("channels")
),
blocks: (opts) =>
this._req("GET", "search/blocks", { q }, data, opts).then(
pullObject("blocks")
),
};
}
}
module.exports = Arena;