@sap-cloud-sdk/odata-common
Version:
SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.
62 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetByKeyRequestBuilderBase = void 0;
const util_1 = require("@sap-cloud-sdk/util");
const uuid_1 = require("uuid");
const request_1 = require("../request");
const get_request_builder_base_1 = require("./get-request-builder-base");
/**
* Abstract class to create a get by key request containing the shared functionality for OData v2 and v4.
* @typeParam EntityT - Type of the entity to be requested
*/
class GetByKeyRequestBuilderBase extends get_request_builder_base_1.GetRequestBuilderBase {
/**
* Creates an instance of GetByKeyRequestBuilder.
* @param entityApi - Entity API for building and executing the request.
* @param keys - Key-value pairs where the key is the name of a key property of the given entity and the value is the respective value.
* @param oDataUri - URI conversion functions.
* @param entityDeserializer - Entity deserializer.
* @param dataAccessor - Object access functions for get requests.
*/
constructor(entityApi, keys, oDataUri, entityDeserializer, dataAccessor) {
super(entityApi, new request_1.ODataGetByKeyRequestConfig(entityApi, oDataUri));
this.entityDeserializer = entityDeserializer;
this.dataAccessor = dataAccessor;
this.requestConfig.keys = keys;
}
/**
* Gets identifier for the batch request.
* @returns Batch request identifier.
*/
getBatchReference() {
if (!this._batchReference) {
this.setBatchId((0, uuid_1.v4)());
}
return this._batchReference;
}
/**
* Sets user-defined identifier for the batch reference.
* @param id - User-defined batch request identifier.
*/
setBatchId(id) {
this._batchReference = { id };
}
select(first, ...rest) {
this.requestConfig.selects = (0, util_1.transformVariadicArgumentToArray)(first, rest);
return this;
}
/**
* Execute request.
* @param destination - Destination or DestinationFetchOptions to execute the request against.
* @returns A promise resolving to the requested entity.
*/
async execute(destination) {
return this.executeRaw(destination)
.then(response => this.entityDeserializer.deserializeEntity(this.dataAccessor.getSingleResult(response.data), this._entityApi, response.headers))
.catch(error => {
throw new util_1.ErrorWithCause('OData get by key request failed!', error);
});
}
}
exports.GetByKeyRequestBuilderBase = GetByKeyRequestBuilderBase;
//# sourceMappingURL=get-by-key-request-builder-base.js.map