-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
216 lines (197 loc) · 7.33 KB
/
Copy pathindex.js
File metadata and controls
216 lines (197 loc) · 7.33 KB
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
/*jslint node: true*/
'use strict';
var requestBase = require('request-promise-native'),
util = require('util'),
FindStream = require('./lib/FindStream'),
convertSDataError = require('./lib/convertSDataError'),
debug = require('debug')('sdata:debug')
function SDataService(sdataUri, username, password) {
var _request = requestBase;
var service = {
setAuthenticationParameters: function (newUsername, newPassword) {
_request = _request.defaults({
auth: {
'user': newUsername,
'pass': newPassword
},
headers: {
'Content-Type': 'application/json'
},
json: true,
resolveWithFullResponse: true
});
},
get: function(resourceKind, id, queryArgs, callback) {
// summary:
// Retrieve a single record matching the specified id.
// Throws an error if not found.
// parameters:
// resourceKind
// id
// queryArgs (optional): eg select, include
// callback: if specified this will be called with the result.
// If callback is not specified, return a promise instead.
var url = sdataUri + resourceKind + '(\'' + id + '\')?format=json';
if(queryArgs) {
if (typeof (queryArgs) == 'function' && !callback) {
callback = queryArgs;
} else {
for (var k in queryArgs) {
if (queryArgs.hasOwnProperty(k))
url += '&' + k + '=' + encodeURIComponent(queryArgs[k]);
}
}
}
return handleSdataResponse(_request.get(url), 200, callback)
},
read: function (resourceKind, where, queryArgs, callback) {
// summary:
// Retrieve SData resources matching the specified criteria
// parameters:
// resourceKind
// where
// queryArgs (optional) object with properties to be added to the request (e.g. {select: 'AccountName'})
// callback: function(error, data):
// - if the call is successful, will be called with data
// - if there is an error, will be called with an error object with the properties:
// message: description of the error (first error message returned from sdata if available)
// errors: populated with errors returned from sdata, if any
// statusCode: http status code, if available
// returns:
// Promise
var url = sdataUri + resourceKind + '?format=json'
if (where && !(queryArgs && 'where' in queryArgs)) {
// this must be encoded explicitly because Angular will encode a space to a + (conforming to RFC)
// which sdata cannot parse
url += '&where=' + encodeURIComponent(where)
}
if (queryArgs) {
if (typeof (queryArgs) == 'function' && !callback) {
callback = queryArgs;
} else {
for (var k in queryArgs) {
if (queryArgs.hasOwnProperty(k))
url += '&' + k + '=' + encodeURIComponent(queryArgs[k]);
}
}
}
debug('read: using URL: ' + url)
return handleSdataResponse(_request.get(url), 200, callback)
},
readPaged: function(resourceKind, where, queryArgs, limit) {
// summary:
// Retrieve SData resources matching the specified criteria, and automatically
// requests multiple pages of data
// parameters:
// resourceKind
// where
// queryArgs (optional) object with properties to be added to the request (e.g. {select: 'AccountName'})
// limit: can be used to limit the max # of records returned (default is to just read everything matching)
// returns:
// stream of records
let url = sdataUri + resourceKind + '?format=json'
if (where && !(queryArgs && 'where' in queryArgs)) {
url += '&where=' + encodeURIComponent(where);
}
if (queryArgs) {
Object.keys(queryArgs).forEach(k => {
url += '&' + k + '=' + encodeURIComponent(queryArgs[k]);
})
}
debug('readPaged: using URL: ' + url)
return new FindStream(_request, url, limit)
},
create: function (resourceKind, data, callback) {
// summary:
// Create resource
// parameters:
// resourceKind: string (e.g. accounts)
// data: object (content of the record to create)
// callback: function(data,error): handler for returned data (see callback documentation under read)
var url = sdataUri + resourceKind + '?format=json';
return handleSdataResponse(_request({
method: 'POST',
uri: url,
body: data
}), 201, callback);
},
update: function (resourceKind, data, callback) {
// summary:
// Update designated resource. The id ($key) must be provided as part of the data.
var url = sdataUri + resourceKind + '("' + data.$key + '")?format=json';
return handleSdataResponse(_request({
method: 'PUT',
uri: url,
body: data
}), 200, callback);
},
upsert: function(resourceKind, data, callback) {
// summary:
// Convenience method combining insert + update.
// If the data has a $key property, update will be called, otherwise insert.
(data.$key ? service.update : service.create)(resourceKind, data, callback);
},
delete: function (resourceKind, key, callback) {
// summary:
// delete designated resource.
// Note that when invoked successfully the callback will not be passed any data.
var url = sdataUri + resourceKind + '("' + key + '")?format=json';
return handleSdataResponse(_request({
method: 'DELETE',
uri: url
}), 200, callback);
},
callBusinessRule: function (resourceKind, operationName, recordId, parameters, callback) {
var payload = {
$name: operationName,
request: {
entity: {
$key: recordId
}
}
};
if (parameters){
util._extend(payload.request, parameters);
}
var url = sdataUri + resourceKind + '/$service/' + operationName + '?format=json';
var p = handleSdataResponse(_request({
method: 'POST',
uri: url,
body: payload
}), 200)
.then(function(body) {
// get the inside response, for business rule calls
return body.response ? body.response : body
})
if(callback) {
p = p.then(function(r) { callback(null, r) }, callback)
}
return p;
}
};
if (username)
service.setAuthenticationParameters(username, password);
return service;
///////////////
function handleSdataResponse(requestPromise, expectedStatusCode, callback) {
var p = requestPromise.then(function(response) {
var body = response.body
if(response.statusCode !== expectedStatusCode) {
return Promise.reject(convertSDataError({response}))
} else {
return body
}
}, function(err) {
return Promise.reject(convertSDataError(err))
})
if(callback) {
p = p.then(function(body) {
callback(null, body)
}, function(error) {
callback(error)
})
}
return p
}
}
module.exports = SDataService;