Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 1x 2x 2x 2x 1x 1x 2x 1x 1x 3x 3x 1x 2x 6x 6x 6x 5x 4x 21x 3x 3x 1x 2x 1x 1x 1x 14x | "use strict";
const _ = require("lodash");
const Metadata = require("./model/Metadata");
const EntitySet = require("./engine/EntitySet");
const FunctionImport = require("./engine/FunctionImport");
const Agent = require("./agent/Agent");
const normalizeSettings = require("./agent/settings");
const Batch = require("./agent/batch/Batch");
const ChangeSet = require("./agent/batch/ChangeSet");
/**
* OData service client implementation.
*
* See https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-odata/2b686a1a-9e1f-456f-80ff-072a010fc278 for detailed info on OData.
*
* <h3>Properties</h3>
*
* <ul>
* <li>agent - instance of superagent which contains current authorization</li>
* <li>init - promise which is resolved when Metadata are loaded and processed</li>
* <li>metadata - instance of the Metadata object, which contains metadata infromations and methods for finding out them</li>
* <li>entitySets - object which contains instance EntitySets objects for work with them</li>
* <li>functionImports - object which contains instance FunctionImport objects for work with them</li>
* </ul>
*
* @class Service
*/
class Service {
/**
* Creates an instance of <code>Service</code>.
* @param {String|Object} [args] Settings which defines URL, authrization and additional parameters of the OData service
* @memberof Service
*/
constructor(args) {
this.initializeProperties(normalizeSettings(args));
}
/**
* Creates OData client properties
*
* @param {Object} settings - structure which contains service endpoint definition
* @see {@link parseConnection}
*
* @memberof Service
*/
initializeProperties(settings) {
let entitySets;
let metadata;
let functionImports;
Object.defineProperty(this, "agent", {
value: new Agent(settings),
writable: false,
});
Object.defineProperty(this, "init", {
value: new Promise((resolve, reject) => {
this.agent
.metadata()
.then((responses) => {
metadata = new Metadata(responses, {
strict: settings.strict,
logger: this.agent.logger,
});
this.agent.setServiceVersion(metadata.model.version);
entitySets = this.buildEntitySets(this.agent, metadata);
functionImports = this.buildFunctionImports(this.agent, metadata);
resolve();
})
.catch(reject);
}),
writable: false,
});
Object.defineProperty(this, "metadata", {
get: () => metadata,
});
Object.defineProperty(this, "entitySets", {
get: () => entitySets,
});
Object.defineProperty(this, "functionImports", {
get: () => functionImports,
});
Object.defineProperty(this, "defaultSchema", {
get: () => metadata.model.getSchema(),
});
}
/**
* Creates Object with OData EntitySet wrappers
*
* @param {Agent} agent - instance of the agent which handler HTTP requests
* @param {Metadata} metadata - instance of the metadata object which keep serviice metadata
*
* @return {Object} returns map which contains EntitySets instances
*
* @memberof Service
*/
buildEntitySets(agent, metadata) {
return metadata.model
.getSchema()
.getEntityContainer()
.entitySets.reduce((acc, entitySet) => {
let name = entitySet.name;
acc[name] = new EntitySet(agent, metadata, entitySet);
if (!this[name]) {
this[name] = acc[name];
} else {
agent.logger.warn(
`EntitySet ${name} is not accessible as shorthand.`
);
}
return acc;
}, {});
}
/**
* Creates Object with OData FunctionImport wrappers
*
* @param {Agent} agent - instance of the agent which handler HTTP requests
* @param {Metadata} metadata - instance of the metadata object which keep serviice metadata
*
* @return {Object} returns map which contains EntitySets instances
*
* @memberof Service
*/
buildFunctionImports(agent, metadata) {
return metadata.model
.getSchema()
.getEntityContainer()
.functionImports.reduce((acc, metaFunction) => {
let functionImport = new FunctionImport(agent, metaFunction);
acc[metaFunction.name] = functionImport;
if (!this[metaFunction.name]) {
this[metaFunction.name] = functionImport.createDirectCaller();
} else {
agent.logger.warn(
`FunctionImport ${metaFunction.name} is not accessible as shorthand.`
);
}
return acc;
}, {});
}
/**
* Create batch and register it to the agent's batch manager
*
* @returns {Object} batch object which represents batch request
*
* @memberof Service
*/
createBatch() {
return this.agent.batchManager.add();
}
/**
* Sends batch passed as parameter or default batch from agents batch manager
*
* @param {Object} batch object which represents batch request
* @param {Boolean} raw if the parameter is true just return batch response object
*
* @returns {Promise} returns promise which is resolved when batch requests is received
*
* @memberof Service
*/
sendBatch(...args) {
return this.agent.batch(...args);
}
/**
* Create batch for default or explicit batch
*
* @param {Batch} batch object which represents explicit batch
*
* @returns {ChangeSet} returns new changeset object
*
* @public
* @memberof Service
*/
createChangeSet(batch) {
let batchNormalized =
batch || _.get(this.agent, "batchManager.defaultBatch");
if (!batchNormalized) {
throw new Error("Batch not found in the managed batches.");
}
return batchNormalized.createChangeSet();
}
/**
* Commit requests to changesets (close changeset for adding new changesets)
* The changeset could by commited explicitly or by default changeset of the
* passed chatch or by default changes of default batch (without passed parameters)
*
* @param {Batch|ChangeSet} [batchItem] changeset to be commited or batch which
* default changeset to be commited
*
* @public
* @memberof Service
*/
commitChangeSet(batchItem) {
_.each(
[
[
() => {
return batchItem instanceof Batch && !batchItem.defaultChangeSet;
},
"ChangeSet not found in the batch",
],
[
() => {
return (
arguments.length === 0 &&
!_.get(this.agent, "batchManager.defaultBatch")
);
},
"Batch not found in the managed batches.",
],
[
() => {
return (
arguments.length === 0 &&
!_.get(this.agent, "batchManager.defaultBatch.defaultChangeSet")
);
},
"ChangeSet not found in the managed batches.",
],
[
() => {
return (
arguments.length > 0 &&
!(batchItem instanceof Batch) &&
!(batchItem instanceof ChangeSet)
);
},
"Invalid object passed to commit changeset.",
],
],
(check) => {
if (check[0]()) {
throw new Error(check[1]);
}
}
);
if (batchItem instanceof ChangeSet) {
batchItem.commit();
} else if (batchItem instanceof Batch) {
batchItem.defaultChangeSet.commit();
} else Eif (arguments.length === 0) {
this.agent.batchManager.defaultBatch.defaultChangeSet.commit();
}
}
}
module.exports = Service;
|