@sap-cloud-sdk/odata-common
Version:
SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.
85 lines • 3.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateRequestBuilderBase = void 0;
const util_1 = require("@sap-cloud-sdk/util");
const uuid_1 = require("uuid");
const request_1 = require("../request");
const request_builder_base_1 = require("./request-builder-base");
/**
* Abstract create request class holding the parts shared in OData v2 and v4.
* @typeParam EntityT - Type of the entity to be created
*/
class CreateRequestBuilderBase extends request_builder_base_1.MethodRequestBuilder {
/**
* Creates an instance of CreateRequestBuilder.
* @param _entityApi - Entity API for building and executing the request.
* @param _entity - Entity to be created.
* @param oDataUri - URI conversion functions.
* @param serializer - Entity serializer.
* @param deserializer - Entity deserializer.
* @param responseDataAccessor - Object access functions for get requests.
*/
constructor(_entityApi, _entity, oDataUri, serializer, deserializer, responseDataAccessor) {
super(new request_1.ODataCreateRequestConfig(_entityApi, oDataUri));
this._entityApi = _entityApi;
this._entity = _entity;
this.oDataUri = oDataUri;
this.serializer = serializer;
this.deserializer = deserializer;
this.responseDataAccessor = responseDataAccessor;
this.requestConfig.payload = serializer.serializeEntity(this._entity, this._entityApi);
}
get entity() {
return this._entity;
}
/**
* 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 };
}
/**
* Specifies the parent of the entity to create.
* @param parentEntity - Parent of the entity to create.
* @param linkField - Static representation of the navigation property that navigates from the parent entity to the child entity.
* @returns The entity itself, to facilitate method chaining.
*/
asChildOf(parentEntity, linkField) {
this.requestConfig.parentKeys = this.oDataUri.getEntityKeys(parentEntity, linkField._entityApi);
this.requestConfig.childField = linkField;
return this;
}
/**
* Execute query.
* @param destination - Destination or DestinationFetchOptions to execute the request against.
* @returns A promise resolving to the created entity.
*/
async execute(destination) {
return this.executeRaw(destination)
.then(response => this.deserializer.deserializeEntity(this.responseDataAccessor.getSingleResult(response.data), this._entityApi, response.headers))
.catch(error => {
throw new util_1.ErrorWithCause('Create request failed!', error);
});
}
/**
* Execute request and return an {@link @sap-cloud-sdk/http-client!HttpResponse}.
* @param destination - Destination or DestinationFetchOptions to execute the request against.
* @returns A promise resolving to an {@link @sap-cloud-sdk/http-client!HttpResponse}.
*/
async executeRaw(destination) {
return this.build(destination).then(request => request.execute());
}
}
exports.CreateRequestBuilderBase = CreateRequestBuilderBase;
//# sourceMappingURL=create-request-builder-base.js.map