forked from marklogic/node-client-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-lib.js
More file actions
208 lines (177 loc) · 6.04 KB
/
Copy pathtest-lib.js
File metadata and controls
208 lines (177 loc) · 6.04 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
/*
* Copyright © 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
'use strict';
var valcheck = require('core-util-is');
var mlutil = require('../lib/mlutil.js');
var Operation = require('../lib/operation.js');
var requester = require('../lib/requester.js');
const marklogic = require("../lib/marklogic");
const testconfig = require("./test-config");
//CAUTION: the functions in this module are not part of the supported API and
//may change or may be removed at any time.
function responseOutputTransform(headers, data) {
/*jshint validthis:true */
var operation = this;
var response = {
statusCode: operation.responseStatusCode,
headers: headers
};
if (!valcheck.isNullOrUndefined(data)) {
response.data = data;
}
return response;
}
function Manager(adminClient) {
if (!(this instanceof Manager)) {
return new Manager(adminClient);
}
this.client = adminClient;
}
// TODO: configure acceptable errors
Manager.prototype.get = function manageGet(paramsObj) {
var endpoint = paramsObj.endpoint;
var params = paramsObj.params;
var headers = paramsObj.headers;
var hasResponse = paramsObj.hasResponse;
var path = makePath(endpoint, params);
var requestOptions = mlutil.copyProperties(this.client.connectionParams);
requestOptions.method = 'GET';
requestOptions.headers = valcheck.isNullOrUndefined(headers) ? {
'Accept': 'application/json'
} : headers;
requestOptions.path = path;
var operation = new Operation(
'GET ' + path, this.client, requestOptions, 'empty',
((hasResponse === 'false') ? 'empty' : 'single')
);
operation.validStatusCodes = [200, 201, 204, 404];
operation.outputTransform = responseOutputTransform;
return requester.startRequest(operation);
};
Manager.prototype.post = function managePost(paramsObj) {
var endpoint = paramsObj.endpoint;
var params = paramsObj.params;
var headers = paramsObj.headers;
var body = paramsObj.body;
var hasResponse = paramsObj.hasResponse;
var path = makePath(endpoint, params);
var requestOptions = mlutil.copyProperties(this.client.connectionParams);
requestOptions.method = 'POST';
requestOptions.headers = valcheck.isNullOrUndefined(headers) ? {
'Content-Type': 'application/json',
'Accept': 'application/json'
} : headers;
requestOptions.path = path;
var hasBody = !valcheck.isNullOrUndefined(body);
var operation = new Operation(
'POST ' + path,
this.client,
requestOptions,
hasBody ? 'single' : 'empty',
((hasResponse === 'false') ? 'empty' : 'single')
);
operation.outputTransform = responseOutputTransform;
if (hasBody) {
operation.requestBody = body;
}
return requester.startRequest(operation);
};
Manager.prototype.put = function managePut(paramsObj) {
var endpoint = paramsObj.endpoint;
var params = paramsObj.params;
var headers = paramsObj.headers;
var body = paramsObj.body;
var hasResponse = paramsObj.hasResponse;
var path = makePath(endpoint, params);
var requestOptions = mlutil.copyProperties(this.client.connectionParams);
requestOptions.method = 'PUT';
requestOptions.headers = valcheck.isNullOrUndefined(headers) ? {
'Content-Type': 'application/json'
} : headers;
requestOptions.path = path;
var hasBody = !valcheck.isNullOrUndefined(body);
var operation = new Operation(
'PUT ' + path,
this.client,
requestOptions,
hasBody ? 'single' : 'empty',
((hasResponse === 'true') ? 'single' : 'empty')
);
operation.outputTransform = responseOutputTransform;
if (hasBody) {
operation.requestBody = body;
}
return requester.startRequest(operation);
};
Manager.prototype.remove = function manageRemove(paramsObj) {
var endpoint = paramsObj.endpoint;
var params = paramsObj.params;
var headers = paramsObj.headers;
var hasResponse = paramsObj.hasResponse;
var path = makePath(endpoint, params);
var requestOptions = mlutil.copyProperties(this.client.connectionParams);
requestOptions.method = 'DELETE';
requestOptions.headers = valcheck.isNullOrUndefined(headers) ? {
'Accept': 'application/json'
} : headers;
requestOptions.path = path;
var operation = new Operation(
'DELETE ' + path,
this.client,
requestOptions,
'empty',
((hasResponse === 'true') ? 'single' : 'empty')
);
operation.outputTransform = responseOutputTransform;
return requester.startRequest(operation);
};
function makePath(endpoint, params) {
var path = encodeURI(endpoint);
if (!valcheck.isNullOrUndefined(params)) {
var paramKeys = Object.keys(params);
var sep = '?';
for (var i = 0; i < paramKeys.length; i++) {
var paramKey = paramKeys[i];
var value = params[paramKey];
if (valcheck.isArray(value)) {
for (var j = 0; j < value.length; j++) {
path += sep + paramKey + '=' + encodeURIComponent(value[j]);
if (i === 0 && j === 0) {
sep = '&';
}
}
} else {
path += sep + paramKey + '=' + encodeURIComponent(value);
if (i === 0) {
sep = '&';
}
}
}
}
return path;
}
function createManager(adminClient) {
return new Manager(adminClient);
}
function findServerConfiguration(serverConfiguration) {
const manageClient = marklogic.createDatabaseClient(testconfig.manageAdminConnection);
new Manager(manageClient).get({
endpoint: '/manage/v2?format=json',
}).result(function (response) {
serverConfiguration.serverVersion = parseFloat(response.data['local-cluster-default'].version).toFixed(2);
});
}
function findServerConfigurationPromise(serverConfiguration) {
const manageClient = marklogic.createDatabaseClient(testconfig.manageAdminConnection);
return new Manager(manageClient).get({
endpoint: '/manage/v2?format=json',
}).result(function (response) {
serverConfiguration.serverVersion = parseFloat(response.data['local-cluster-default'].version).toFixed(2);
});
}
module.exports = {
createManager: createManager,
findServerConfiguration: findServerConfiguration,
findServerConfigurationPromise: findServerConfigurationPromise
};