-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
52 lines (42 loc) · 1.29 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
var request = require('request')
var _ = require('lodash')
var TPQuery = require('./lib/tpquery')
var TPEntity= require('./lib/tpentity')
function configure(opts) {
// Catch folks using the `new` keyword when invoking our configurator
if (this instanceof configure) { return configure(opts) }
if (!opts.token && opts.username && opts.password) {
opts.token = new Buffer(opts.username+':'+opts.password).toString('base64')
} else if (!opts.token) {
throw new Error('A TargetProcess username and password is required')
}
if (!opts.domain) {
throw new Error('A TargetProcess domain is required')
}
var version = opts.version || 1
var domain = opts.domain
var token = opts.token
var protocol = opts.protocol || 'https'
var urlRoot = protocol + '://'+domain+'/api/v'+version
return function(entity, id) {
var collection = new TPQuery(urlRoot, token)
, model
if (entity) { collection.get(entity) }
if (entity && id) {
model = new TPEntity({Id: id}, collection.opts)
}
return (model) ? model : collection
}
}
// Usage
// ---------
// tp.create({}, function(err, entity) {
//
// })
//
// tp.get('Entity', 1234).fetch(err, entity) { })
//
// tp.get('Entity', 1234).destroy()
//
// tp.get('Entity', 1234).update({a: 'b'}, cb)
module.exports = configure