@odata2ts/odata-service
Version:
Main runtime dependency of odata2ts for generated odata client services
90 lines • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntitySetServiceV4 = void 0;
const tslib_1 = require("tslib");
const odata_query_objects_1 = require("@odata2ts/odata-query-objects");
const ServiceStateHelperV4_js_1 = require("./ServiceStateHelperV4.js");
class EntitySetServiceV4 {
/**
* Overriding the constructor to support creation of EntityTypeService from within this service.
* Also support key spec.
*
* @param client the odata client responsible for data requests
* @param basePath the base URL path
* @param name name of the service
* @param qModel query object
* @param idFunction the id function
* @param options
* @protected
*/
constructor(client, basePath, name, qModel, idFunction, options) {
this.__base = new ServiceStateHelperV4_js_1.ServiceStateHelperV4(client, basePath, name, qModel, options);
this.__idFunction = idFunction;
}
getPath() {
return this.__base.path;
}
/**
* The key specification for the given entity type.
* Supports composite keys.
*/
getKeySpec() {
return this.__idFunction.getParams();
}
/**
* Create an OData path for an entity with a given id.
* Might be useful for routing.
*
* @example createKey(1234) => myEntity(1234)
* @example createKey({id: 1234, name: "Test"}) => myEntity(id=1234,name='Test')
* @param id either a primitive value (single key entities only) or an object
* @param notEncoded if set to {@code true}, special chars are not escaped
*/
createKey(id, notEncoded) {
const url = this.__idFunction.buildUrl(id, notEncoded !== null && notEncoded !== void 0 ? notEncoded : this.__base.isUrlNotEncoded());
return url.startsWith("/") ? url.substring(1) : url;
}
/**
* Parse an OData path representing the id of an entity.
* Might be useful for routing in combination with createKey.
*
* @example parseKey("myEntity(1234)") => 1234
* @example parseKey("myEntity(id=1234,name='Test')") => { id: 1234, name: "Test" }
* @param keyPath e.g. myEntity(id=1234,name='Test')
* @param notDecoded if set to {@code true}, encoded special chars are not decoded
*/
parseKey(keyPath, notDecoded) {
return this.__idFunction.parseUrl(keyPath, notDecoded !== null && notDecoded !== void 0 ? notDecoded : this.__base.isUrlNotEncoded());
}
/**
* Create a new model.
*
* @param model
* @param requestConfig
* @param createOptions
* @return
*/
create(model, requestConfig, createOptions) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { client, qModel, basePath, path, getDefaultHeaders, qResponseType } = this.__base;
const { dontUseCastPathSegment, useTypeCi } = this.__base.evaluateSubtypeOptions(createOptions);
const result = yield client.post(dontUseCastPathSegment ? basePath : path, qModel.convertToOData(useTypeCi ? this.__base.addTypeControlInfo(model) : model), requestConfig, getDefaultHeaders());
return (0, odata_query_objects_1.convertV4ModelResponse)(result, qResponseType);
});
}
/**
* Query the entity set.
*
* @param queryFn provide the query logic with the help of the builder and the query-object
* @param requestConfig any special configurations for this request
*/
query(queryFn, requestConfig) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { client, qResponseType, applyQueryBuilder, getDefaultHeaders } = this.__base;
const response = yield client.get(applyQueryBuilder(queryFn), requestConfig, getDefaultHeaders());
return (0, odata_query_objects_1.convertV4CollectionResponse)(response, qResponseType);
});
}
}
exports.EntitySetServiceV4 = EntitySetServiceV4;
//# sourceMappingURL=EntitySetServiceV4.js.map