This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
88 lines (80 loc) · 2.8 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
var Township = require('township-client')
var qs = require('querystring')
var nets = require('nets')
var xtend = Object.assign
module.exports = API
function API (opts) {
if (!(this instanceof API)) return new API(opts)
if (!opts) opts = {}
if (!opts.apiPath) opts.apiPath = ''
// datbase.org defaults, specify opts.server and opts.apiPath to override
var SERVER = 'https://datbase.org'
var API_PATH = '/api/v1'
var apiPath = !opts.server || (opts.server.indexOf('datbase.org') > -1) ? API_PATH : opts.apiPath // only add default path to datbase.org server
var townshipOpts = Object.assign({}, opts)
// set default township server & routes for datbase.org
if (!townshipOpts.config) townshipOpts.config = {}
if (!townshipOpts.config.filename) townshipOpts.config.filename = '.datrc'
if (!townshipOpts.server) townshipOpts.server = SERVER
if (!opts.routes && apiPath) {
townshipOpts.routes = {
register: apiPath + '/register',
login: apiPath + '/login',
updatePassword: apiPath + '/updatepassword'
}
}
var township = Township(townshipOpts)
var api = township.server + apiPath // let township parse server
return {
login: township.login.bind(township),
logout: township.logout.bind(township),
register: township.register.bind(township),
whoami: township.getLogin.bind(township),
secureRequest: township.secureRequest.bind(township),
dats: rest('/dats'),
users: xtend(rest('/users'), {
resetPassword: function (input, cb) {
nets({ method: 'POST', uri: api + '/password-reset', body: input, json: true }, cb)
},
resetPasswordConfirmation: function (input, cb) {
nets({ method: 'POST', uri: api + '/password-reset-confirm', body: input, json: true }, cb)
},
suspend: function (input, cb) {
nets({ method: 'PUT', uri: api + '/users/suspend', body: input, json: true }, cb)
}
})
}
function rest (path) {
var defaults = {
uri: api + path,
json: true
}
return {
get: function (input, cb) {
var params = qs.stringify(input)
var opts = Object.assign({}, defaults)
opts.uri = defaults.uri + '?' + params
opts.method = 'GET'
township.secureRequest(opts, cb)
},
create: function (input, cb) {
var opts = Object.assign({}, defaults)
opts.body = input
opts.method = 'POST'
township.secureRequest(opts, cb)
},
update: function (input, cb) {
var opts = Object.assign({}, defaults)
opts.body = input
opts.method = 'PUT'
township.secureRequest(opts, cb)
},
delete: function (input, cb) {
var opts = Object.assign({}, defaults)
opts.body = input
opts.method = 'DELETE'
township.secureRequest(opts, cb)
}
}
}
}