This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
311 lines (295 loc) · 9.38 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'use strict'
var Connection = require('./lib/connection')
var errors = require('./lib/errors')
if (!global.Promise) {
throw new Error('Promise could not be found in this context. You must polyfill it to use this module.')
}
/**
* @constructor
* @description Your client to interact with the [Habitica API](https://habitica.com/apidoc/).
*
* @param {Object} options - The properties to configure the Habitica client
* @param {String} [options.id] - The id of the user
* @param {String} [options.apiToken] - The API token of the user
* @param {String} [options.endpoint=https://habitica.com] - The endpoint to use
* @param {String} [options.platform=Habitica-Node] - The name of your integration
* @param {Function} [options.errorHandler] - A function to run when a request errors. The result of this function will be the argument passed to the `catch` in the request `Promise`
*
* @example
* var Habitica = require('habitica')
* var api = new Habitica({
* id: 'your-habitica.com-user-id',
* apiToken: 'your-habitica.com-api-token',
* endpoint: 'http://custom-url.com',
* platform: 'The Name of Your Integration'
* errorHandler: function (err) {
* // handle all errors from failed requests
* }
* })
* @example <caption>The id and apiToken parameters are not required and can be set later. The credentials will be automatically set when using the register and localLogin methods.</caption>
* var Habitica = require('habitica')
* var api = new Habitica()
* @example <caption>A sample error handler</caption>
* var Habitica = require('habitica')
*
* function sendNotification (style, message) {
* // logic for sending a notification to user
* }
*
* var api = new Habitica({
* id: 'your-habitica.com-user-id',
* apiToken: 'your-habitica.com-api-token',
* errorHandler: function (err) {
* if (err instanceof Habitica.ApiError) {
* // likely a validation error from
* // the API request
* sendNotification('warning', err.messsage)
* } else if (err instanceof Habitica.UnknownConnectionError) {
* // either the Habitica API is down
* // or there is no internet connection
* sendNotification('danger', err.originalError.message)
* } else {
* // there is something wrong with your integration
* // such as a syntax error or other problem
* console.error(err)
* }
* }
* })
*
* api.get('/tasks/id-that-does-not-exist').then(() => {
* // will never get here
* return api.get('/something-else')
* }).then(() => {
* // will never get here
* }).catch((err) => {
* // before this happens, the errorHandler gets run
* err // undefined because the errorHandler did not return anything
* // you could leave the catch off entirely since the
* // configured errorHandler does all the necessary work
* // to message back to the user
* })
*/
function Habitica (options) {
options = options || {}
this._connection = new Connection(options)
}
/** @public
*
* @returns {Object} The options used to make the requests. The same as the values used {@link Habitica#setOptions|to set the options}
*/
Habitica.prototype.getOptions = function () {
return this._connection.getOptions()
}
/** @public
*
* @param {Object} options - The properties to configure the Habitica client. If a property is not passed in, it will default to the value passed in on instantiation
* @param {String} [options.id] - The id of the user
* @param {String} [options.apiToken] - The API apiToken of the user
* @param {String} [options.endpoint] - The endpoint to use
* @param {String} [options.platform] - The name of your integration
* @param {Function} [options.errorHandler] - A function to run when a request errors
*
* @example
* api.setOptions({
* id: 'new-user-id',
* apiToken: 'new-api-token',
* endpoint: 'http://localhost:3000/',
* platform: 'Great-Habitica-Integration',
* errorHandler: yourErrorHandlerFunction
* })
*/
Habitica.prototype.setOptions = function (creds) {
this._connection.setOptions(creds)
}
/** @public
*
* @param {String} username - The username to register with
* @param {String} email - The email to register with
* @param {String} password - The password to register with
*
* @returns {Promise} A Promise that resolves the response from the register request
*
* @example <caption>The id and api token will be set automatically after a sucessful registration request</caption>
* api.register('username', 'email', 'password').then((res) => {
* var user = res.data
* }).catch((err) => {
* // handle registration errors
* })
*/
Habitica.prototype.register = function (username, email, password) {
return this.post('/user/auth/local/register', {
username: username,
email: email,
password: password,
confirmPassword: password
}).then(function (res) {
this.setOptions({
id: res.data._id,
apiToken: res.data.apiToken
})
return res
}.bind(this))
}
/** @public
*
* @param {String} usernameOrEmail - The username or email to login with
* @param {String} password - The password to login with
*
* @returns {Promise} A Promise that resolves the response from the login request
* @example <caption>The id and api token will be set automatically after a sucessful login request</caption>
* api.login('username or email','password').then((res) => {
* var creds = res.data
*
* creds.id // the user's id
* creds.apiToken // the user's api token
* }).catch((err) => {
* // handle login errors
* })
*/
Habitica.prototype.localLogin = function (usernameEmail, password) {
return this.post('/user/auth/local/login', {
username: usernameEmail,
password: password
}).then(function (res) {
this._connection.setOptions({
id: res.data.id,
apiToken: res.data.apiToken
})
return res
}.bind(this))
}
/** @public
*
* @param {String} route - The Habitica API route to use
* @param {Object} [query] - Query params to send along with the request
*
* @returns {Promise} A Promise that resolves the response from the GET request
* @example <caption>Making a basic request</caption>
* api.get('/user').then((res) => {
* var user = res.data
*
* user.profile.name // the user's display name
* })
* @example <caption>A request with a query Object</caption>
* api.get('/groups', {
* type: 'publicGuilds,privateGuilds'
* }).then((res) => {
* var guilds = res.data
* var guild = guilds[0]
*
* guild.name // the name of the group
* })
*
* @example <caption>Handling errors</caption>
* api.get('/tasks/non-existant-id').then((res) => {
* // will never get here
* }).catch((err) => {
* err.message // 'Task not found'
* })
*/
Habitica.prototype.get = function (path, query) {
return this._connection.get(path, {
query: query
})
}
/** @public
*
* @param {String} route - The Habitica API route to use
* @param {Object} [body] - The body to send along with the request
* @param {Object} [query] - Query params to send along with the request
*
* @returns {Promise} A Promise that resolves the response from the POST request
*
* @example <caption>A request with a body</caption>
* api.post('/tasks/user', {
* text: 'Task Name',
* notes: 'Task Notes',
* type: 'todo'
* }).then((res) => {
* var task = res.data
*
* task.text // 'Task Name'
* })
*
* @example <caption>Handling errors</caption>
* api.post('/groups', {
* type: 'party',
* name: 'My Party'
* }).then((res) => {
* var party = res.data
*
* party.name // 'My Party'
* }).catch((err) => {
* // handle errors
* })
*/
Habitica.prototype.post = function (path, body, query) {
return this._connection.post(path, {
send: body,
query: query
})
}
/** @public
*
* @param {String} route - The Habitica API route to use
* @param {Object} [body] - The body to send along with the request
* @param {Object} [query] - Query params to send along with the request
*
* @returns {Promise} A Promise that resolves the response from the PUT request
*
* @example <caption>A request with a body</caption>
* api.put('/tasks/the-task-id', {
* text: 'New Task Name',
* notes: 'New Text Notes'
* }).then((res) => {
* var task = res.data
*
* task.text // 'New Task Name'
* })
*
* @example <caption>Handling errors</caption>
* api.put('/groups/the-group-id', {
* name: 'New Group Name'
* }).then((res) => {
* var group = res.data
*
* group.name // 'New Group Name'
* }).catch((err) => {
* // handle errors
* })
*/
Habitica.prototype.put = function (path, body, query) {
return this._connection.put(path, {
send: body,
query: query
})
}
/** @public
*
* @param {String} route - The Habitica API route to use
* @param {Object} [body] - The body to send along with the request
* @param {Object} [query] - Query params to send along with the request
*
* @returns {Promise} A Promise that resolves the response from the DELETE request
*
* @example <caption>A basic request</caption>
* api.del('/tasks/the-task-id').then(() => {
* // The task has been deleted
* })
*
* @example <caption>Handling errors</caption>
* api.del('/groups/the-group-id').then(() => {
* // The group has been deleted
* }).catch((err) => {
* // handle errors
* })
*/
Habitica.prototype.del = function (path, body, query) {
return this._connection.del(path, {
send: body,
query: query
})
}
Habitica.ApiError = errors.HabiticaApiError
Habitica.UnknownConnectionError = errors.UnknownConnectionError
module.exports = Habitica