Source: client/filtering-request.js

  1. /*
  2. * Copyright 2023, TeamDev. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Redistribution and use in source and/or binary forms, with or without
  11. * modification, must retain the above copyright notice and the following
  12. * disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. import {ClientRequest} from "./client-request";
  27. /**
  28. * An abstract base for client requests that filter messages by certain criteria.
  29. *
  30. * @abstract
  31. *
  32. * @template <B> the type of the builder wrapped by this request
  33. * @template <T> the type of the messages that store the request data
  34. */
  35. export class FilteringRequest extends ClientRequest {
  36. /**
  37. * @param {!Class<Message>} targetType the target type of the request
  38. * @param {!Client} client the client which initiated the request
  39. * @param {!ActorRequestFactory} actorRequestFactory the request factory
  40. *
  41. * @protected
  42. */
  43. constructor(targetType, client, actorRequestFactory) {
  44. super(client, actorRequestFactory);
  45. this.targetType = targetType;
  46. }
  47. /**
  48. * Adds filtering by IDs to the built request.
  49. *
  50. * @param ids {!Message|Number|String|Message[]|Number[]|String[]}
  51. * the IDs of interest
  52. * @return {this} self for method chaining
  53. */
  54. byId(ids) {
  55. ids = FilteringRequest._ensureArray(ids);
  56. this._builder().byIds(ids);
  57. return this._self();
  58. }
  59. /**
  60. * Adds filtering by predicates to the built request.
  61. *
  62. * Filters specified in a list are considered to be joined using `AND` operator.
  63. *
  64. * @param {!Filter|CompositeFilter|Filter[]|CompositeFilter[]} predicates the filters
  65. * @return {this} self for method chaining
  66. */
  67. where(predicates) {
  68. predicates = FilteringRequest._ensureArray(predicates);
  69. this._builder().where(predicates);
  70. return this._self();
  71. }
  72. /**
  73. * Applies a field mask to the request results.
  74. *
  75. * The names of the fields must be formatted according to the `google.protobuf.FieldMask`
  76. * specification.
  77. *
  78. * @param {!String|String[]} fieldPaths the fields to include in the mask
  79. * @return {this} self for method chaining
  80. */
  81. withMask(fieldPaths) {
  82. fieldPaths = FilteringRequest._ensureArray(fieldPaths);
  83. this._builder().withMask(fieldPaths);
  84. return this._self();
  85. }
  86. /**
  87. * Returns the builder for messages that store request data.
  88. *
  89. * @return {AbstractTargetBuilder<Message>} the builder instance
  90. *
  91. * @protected
  92. */
  93. _builder() {
  94. if (!this._builderInstance) {
  95. const newBuilderFn = this._newBuilderFn();
  96. this._builderInstance = newBuilderFn(this._requestFactory);
  97. }
  98. return this._builderInstance;
  99. }
  100. /**
  101. * @callback _NewBuilderFn
  102. *
  103. * @param {ActorRequestFactory}
  104. * @return {AbstractTargetBuilder}
  105. */
  106. /**
  107. * Returns the function with which the {@link _builderInstance} can be created.
  108. *
  109. * @abstract
  110. * @return {_NewBuilderFn}
  111. *
  112. * @protected
  113. */
  114. _newBuilderFn() {
  115. throw new Error('Not implemented in abstract base.');
  116. }
  117. /**
  118. * @abstract
  119. * @return {this}
  120. *
  121. * @protected
  122. */
  123. _self() {
  124. throw new Error('Not implemented in abstract base.');
  125. }
  126. /**
  127. * Wraps the passed argument into array if it's not an array already.
  128. *
  129. * The `null` values are converted into an empty array.
  130. *
  131. * @return {Array} the passed argument as an array
  132. *
  133. * @private
  134. */
  135. static _ensureArray(values) {
  136. if (!values) {
  137. return [];
  138. }
  139. if (!(values instanceof Array)) {
  140. return [values]
  141. }
  142. return values;
  143. }
  144. }