@intellegens/cornerstone-client 0.0.0-experimental-upgrade-20260302-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. package/README.md +256 -0
  2. package/demo/index.ts +29 -0
  3. package/demo/public_html/favicon.ico +0 -0
  4. package/demo/public_html/index.html +106 -0
  5. package/demo/public_html/websettings.json +3 -0
  6. package/dist/adapters/CollectionViewAdapter/index.d.ts +198 -0
  7. package/dist/adapters/CollectionViewAdapter/index.integration.test.d.ts +1 -0
  8. package/dist/adapters/CollectionViewAdapter/index.integration.test.js +163 -0
  9. package/dist/adapters/CollectionViewAdapter/index.js +381 -0
  10. package/dist/adapters/SearchAdapter/index.d.ts +55 -0
  11. package/dist/adapters/SearchAdapter/index.js +233 -0
  12. package/dist/adapters/index.d.ts +2 -0
  13. package/dist/adapters/index.js +2 -0
  14. package/dist/data/api/dto/PropertyPathDto.d.ts +4 -0
  15. package/dist/data/api/dto/PropertyPathDto.js +1 -0
  16. package/dist/data/api/dto/ReadOptionsDto.d.ts +8 -0
  17. package/dist/data/api/dto/ReadOptionsDto.js +1 -0
  18. package/dist/data/api/dto/ReadResultDto.d.ts +12 -0
  19. package/dist/data/api/dto/ReadResultDto.js +1 -0
  20. package/dist/data/api/dto/ReadResultMetadataDto.d.ts +8 -0
  21. package/dist/data/api/dto/ReadResultMetadataDto.js +1 -0
  22. package/dist/data/api/dto/crud/CrudMetadataDto.d.ts +4 -0
  23. package/dist/data/api/dto/crud/CrudMetadataDto.js +1 -0
  24. package/dist/data/api/dto/crud/index.d.ts +1 -0
  25. package/dist/data/api/dto/crud/index.js +1 -0
  26. package/dist/data/api/dto/index.d.ts +4 -0
  27. package/dist/data/api/dto/index.js +4 -0
  28. package/dist/data/api/dto/read/ReadMetadataDto.d.ts +8 -0
  29. package/dist/data/api/dto/read/ReadMetadataDto.js +1 -0
  30. package/dist/data/api/dto/read/ReadSelectedDefinitionDto.d.ts +18 -0
  31. package/dist/data/api/dto/read/ReadSelectedDefinitionDto.js +1 -0
  32. package/dist/data/api/dto/read/ReadSelectedNestedCollectionCriteriaDto.d.ts +22 -0
  33. package/dist/data/api/dto/read/ReadSelectedNestedCollectionCriteriaDto.js +1 -0
  34. package/dist/data/api/dto/read/ReadSelectedNestedCriteriaDto.d.ts +18 -0
  35. package/dist/data/api/dto/read/ReadSelectedNestedCriteriaDto.js +1 -0
  36. package/dist/data/api/dto/read/ReadSelectedOrderingDefinitionDto.d.ts +7 -0
  37. package/dist/data/api/dto/read/ReadSelectedOrderingDefinitionDto.js +1 -0
  38. package/dist/data/api/dto/read/ReadSelectedOrderingPropertyDefinitionDto.d.ts +14 -0
  39. package/dist/data/api/dto/read/ReadSelectedOrderingPropertyDefinitionDto.js +1 -0
  40. package/dist/data/api/dto/read/ReadSelectedPaginationDefinitionDto.d.ts +13 -0
  41. package/dist/data/api/dto/read/ReadSelectedPaginationDefinitionDto.js +1 -0
  42. package/dist/data/api/dto/read/ReadSelectedSearchDefinitionBuilder.d.ts +167 -0
  43. package/dist/data/api/dto/read/ReadSelectedSearchDefinitionBuilder.js +267 -0
  44. package/dist/data/api/dto/read/ReadSelectedSearchDefinitionDto.d.ts +33 -0
  45. package/dist/data/api/dto/read/ReadSelectedSearchDefinitionDto.js +1 -0
  46. package/dist/data/api/dto/read/ReadSelectedSearchPropertyDefinitionDto.d.ts +114 -0
  47. package/dist/data/api/dto/read/ReadSelectedSearchPropertyDefinitionDto.js +1 -0
  48. package/dist/data/api/dto/read/index.d.ts +10 -0
  49. package/dist/data/api/dto/read/index.js +10 -0
  50. package/dist/data/api/dto/response/ApiErrorDto.d.ts +17 -0
  51. package/dist/data/api/dto/response/ApiErrorDto.js +1 -0
  52. package/dist/data/api/dto/response/ApiErrorResponseDto.d.ts +11 -0
  53. package/dist/data/api/dto/response/ApiErrorResponseDto.js +1 -0
  54. package/dist/data/api/dto/response/ApiResponseDto.d.ts +3 -0
  55. package/dist/data/api/dto/response/ApiResponseDto.js +1 -0
  56. package/dist/data/api/dto/response/ApiSuccessResponseDto.d.ts +13 -0
  57. package/dist/data/api/dto/response/ApiSuccessResponseDto.js +1 -0
  58. package/dist/data/api/dto/response/EmptyMetadataDto.d.ts +4 -0
  59. package/dist/data/api/dto/response/EmptyMetadataDto.js +1 -0
  60. package/dist/data/api/dto/response/MetadataDto.d.ts +25 -0
  61. package/dist/data/api/dto/response/MetadataDto.js +1 -0
  62. package/dist/data/api/dto/response/index.d.ts +5 -0
  63. package/dist/data/api/dto/response/index.js +5 -0
  64. package/dist/data/api/enum/index.d.ts +2 -0
  65. package/dist/data/api/enum/index.js +2 -0
  66. package/dist/data/api/enum/read/ReadSelectedCollectionOperator.d.ts +16 -0
  67. package/dist/data/api/enum/read/ReadSelectedCollectionOperator.js +17 -0
  68. package/dist/data/api/enum/read/ReadSelectedComparisonOperator.d.ts +69 -0
  69. package/dist/data/api/enum/read/ReadSelectedComparisonOperator.js +76 -0
  70. package/dist/data/api/enum/read/ReadSelectedLogicalOperator.d.ts +15 -0
  71. package/dist/data/api/enum/read/ReadSelectedLogicalOperator.js +16 -0
  72. package/dist/data/api/enum/read/ReadSelectedOrderingDirection.d.ts +13 -0
  73. package/dist/data/api/enum/read/ReadSelectedOrderingDirection.js +14 -0
  74. package/dist/data/api/enum/read/ReadSelectedPropertyType.d.ts +66 -0
  75. package/dist/data/api/enum/read/ReadSelectedPropertyType.js +75 -0
  76. package/dist/data/api/enum/read/index.d.ts +5 -0
  77. package/dist/data/api/enum/read/index.js +5 -0
  78. package/dist/data/api/enum/response/ApiErrorCodes.d.ts +7 -0
  79. package/dist/data/api/enum/response/ApiErrorCodes.js +8 -0
  80. package/dist/data/api/enum/response/ErrorCode.d.ts +13 -0
  81. package/dist/data/api/enum/response/ErrorCode.js +14 -0
  82. package/dist/data/api/enum/response/index.d.ts +1 -0
  83. package/dist/data/api/enum/response/index.js +1 -0
  84. package/dist/data/api/index.d.ts +3 -0
  85. package/dist/data/api/index.js +3 -0
  86. package/dist/data/api/interface/ICommonIdentifiable.d.ts +7 -0
  87. package/dist/data/api/interface/ICommonIdentifiable.js +1 -0
  88. package/dist/data/api/interface/IConcurrencySafe.d.ts +9 -0
  89. package/dist/data/api/interface/IConcurrencySafe.js +2 -0
  90. package/dist/data/api/interface/IIdentifiable.d.ts +11 -0
  91. package/dist/data/api/interface/IIdentifiable.js +1 -0
  92. package/dist/data/api/interface/IIdentifiableSecondary.d.ts +9 -0
  93. package/dist/data/api/interface/IIdentifiableSecondary.js +1 -0
  94. package/dist/data/api/interface/index.d.ts +3 -0
  95. package/dist/data/api/interface/index.js +3 -0
  96. package/dist/data/auth/dto/ClaimDto.d.ts +4 -0
  97. package/dist/data/auth/dto/ClaimDto.js +1 -0
  98. package/dist/data/auth/dto/RegisterRequestDto.d.ts +4 -0
  99. package/dist/data/auth/dto/RegisterRequestDto.js +1 -0
  100. package/dist/data/auth/dto/RoleDto.d.ts +5 -0
  101. package/dist/data/auth/dto/RoleDto.js +1 -0
  102. package/dist/data/auth/dto/SignInRequestDto.d.ts +4 -0
  103. package/dist/data/auth/dto/SignInRequestDto.js +1 -0
  104. package/dist/data/auth/dto/TokensDto.d.ts +4 -0
  105. package/dist/data/auth/dto/TokensDto.js +1 -0
  106. package/dist/data/auth/dto/UserDto.d.ts +17 -0
  107. package/dist/data/auth/dto/UserDto.js +1 -0
  108. package/dist/data/auth/dto/UserInfoDto.d.ts +14 -0
  109. package/dist/data/auth/dto/UserInfoDto.js +1 -0
  110. package/dist/data/auth/dto/index.d.ts +5 -0
  111. package/dist/data/auth/dto/index.js +5 -0
  112. package/dist/data/auth/index.d.ts +2 -0
  113. package/dist/data/auth/index.js +2 -0
  114. package/dist/data/auth/policy.d.ts +52 -0
  115. package/dist/data/auth/policy.js +44 -0
  116. package/dist/data/index.d.ts +2 -0
  117. package/dist/data/index.js +2 -0
  118. package/dist/index.d.ts +4 -0
  119. package/dist/index.js +4 -0
  120. package/dist/services/api/ApiCrudControllerClient/index.d.ts +41 -0
  121. package/dist/services/api/ApiCrudControllerClient/index.integration.test.d.ts +1 -0
  122. package/dist/services/api/ApiCrudControllerClient/index.integration.test.js +34 -0
  123. package/dist/services/api/ApiCrudControllerClient/index.js +116 -0
  124. package/dist/services/api/ApiInitializationService/index.d.ts +106 -0
  125. package/dist/services/api/ApiInitializationService/index.js +208 -0
  126. package/dist/services/api/ApiReadControllerClient/index.d.ts +40 -0
  127. package/dist/services/api/ApiReadControllerClient/index.integration.test.d.ts +1 -0
  128. package/dist/services/api/ApiReadControllerClient/index.integration.test.js +59 -0
  129. package/dist/services/api/ApiReadControllerClient/index.js +111 -0
  130. package/dist/services/api/HttpService/FetchHttpService.d.ts +7 -0
  131. package/dist/services/api/HttpService/FetchHttpService.integration.test.d.ts +1 -0
  132. package/dist/services/api/HttpService/FetchHttpService.integration.test.js +52 -0
  133. package/dist/services/api/HttpService/FetchHttpService.js +29 -0
  134. package/dist/services/api/HttpService/HttpRequestConfig.d.ts +10 -0
  135. package/dist/services/api/HttpService/HttpRequestConfig.js +1 -0
  136. package/dist/services/api/HttpService/HttpResponse.d.ts +11 -0
  137. package/dist/services/api/HttpService/HttpResponse.js +3 -0
  138. package/dist/services/api/HttpService/IHttpService.d.ts +13 -0
  139. package/dist/services/api/HttpService/IHttpService.js +3 -0
  140. package/dist/services/api/HttpService/index.d.ts +9 -0
  141. package/dist/services/api/HttpService/index.js +10 -0
  142. package/dist/services/api/UserManagementControllerClient/index.d.ts +41 -0
  143. package/dist/services/api/UserManagementControllerClient/index.integration.test.d.ts +1 -0
  144. package/dist/services/api/UserManagementControllerClient/index.integration.test.js +60 -0
  145. package/dist/services/api/UserManagementControllerClient/index.js +117 -0
  146. package/dist/services/api/index.d.ts +5 -0
  147. package/dist/services/api/index.js +5 -0
  148. package/dist/services/auth/client/AuthService/index.d.ts +75 -0
  149. package/dist/services/auth/client/AuthService/index.js +200 -0
  150. package/dist/services/auth/client/AuthorizationManagementControllerClient/index.d.ts +48 -0
  151. package/dist/services/auth/client/AuthorizationManagementControllerClient/index.integration.test.d.ts +1 -0
  152. package/dist/services/auth/client/AuthorizationManagementControllerClient/index.integration.test.js +89 -0
  153. package/dist/services/auth/client/AuthorizationManagementControllerClient/index.js +148 -0
  154. package/dist/services/auth/client/index.d.ts +2 -0
  155. package/dist/services/auth/client/index.js +2 -0
  156. package/dist/services/auth/index.d.ts +1 -0
  157. package/dist/services/auth/index.js +1 -0
  158. package/dist/services/index.d.ts +2 -0
  159. package/dist/services/index.js +2 -0
  160. package/dist/utils/authorization/index.d.ts +17 -0
  161. package/dist/utils/authorization/index.js +45 -0
  162. package/dist/utils/index.d.ts +2 -0
  163. package/dist/utils/index.js +2 -0
  164. package/dist/utils/result/index.d.ts +21 -0
  165. package/dist/utils/result/index.js +16 -0
  166. package/dist/utils/search/index.d.ts +34 -0
  167. package/dist/utils/search/index.js +106 -0
  168. package/package.json +45 -0
  169. package/src/adapters/CollectionViewAdapter/index.integration.test.ts +197 -0
  170. package/src/adapters/CollectionViewAdapter/index.ts +477 -0
  171. package/src/adapters/SearchAdapter/index.ts +302 -0
  172. package/src/adapters/index.ts +2 -0
  173. package/src/data/api/dto/PropertyPathDto.ts +4 -0
  174. package/src/data/api/dto/ReadOptionsDto.ts +8 -0
  175. package/src/data/api/dto/ReadResultDto.ts +13 -0
  176. package/src/data/api/dto/ReadResultMetadataDto.ts +8 -0
  177. package/src/data/api/dto/crud/CrudMetadataDto.ts +4 -0
  178. package/src/data/api/dto/crud/index.ts +1 -0
  179. package/src/data/api/dto/index.ts +4 -0
  180. package/src/data/api/dto/read/ReadMetadataDto.ts +8 -0
  181. package/src/data/api/dto/read/ReadSelectedDefinitionDto.ts +21 -0
  182. package/src/data/api/dto/read/ReadSelectedNestedCollectionCriteriaDto.ts +25 -0
  183. package/src/data/api/dto/read/ReadSelectedNestedCriteriaDto.ts +20 -0
  184. package/src/data/api/dto/read/ReadSelectedOrderingDefinitionDto.ts +8 -0
  185. package/src/data/api/dto/read/ReadSelectedOrderingPropertyDefinitionDto.ts +16 -0
  186. package/src/data/api/dto/read/ReadSelectedPaginationDefinitionDto.ts +13 -0
  187. package/src/data/api/dto/read/ReadSelectedSearchDefinitionBuilder.ts +348 -0
  188. package/src/data/api/dto/read/ReadSelectedSearchDefinitionDto.ts +43 -0
  189. package/src/data/api/dto/read/ReadSelectedSearchPropertyDefinitionDto.ts +186 -0
  190. package/src/data/api/dto/read/index.ts +10 -0
  191. package/src/data/api/dto/response/ApiErrorDto.ts +21 -0
  192. package/src/data/api/dto/response/ApiErrorResponseDto.ts +13 -0
  193. package/src/data/api/dto/response/ApiResponseDto.ts +7 -0
  194. package/src/data/api/dto/response/ApiSuccessResponseDto.ts +13 -0
  195. package/src/data/api/dto/response/MetadataDto.ts +24 -0
  196. package/src/data/api/dto/response/index.ts +5 -0
  197. package/src/data/api/enum/index.ts +2 -0
  198. package/src/data/api/enum/read/ReadSelectedCollectionOperator.ts +17 -0
  199. package/src/data/api/enum/read/ReadSelectedComparisonOperator.ts +96 -0
  200. package/src/data/api/enum/read/ReadSelectedLogicalOperator.ts +16 -0
  201. package/src/data/api/enum/read/ReadSelectedOrderingDirection.ts +13 -0
  202. package/src/data/api/enum/read/ReadSelectedPropertyType.ts +96 -0
  203. package/src/data/api/enum/read/index.ts +5 -0
  204. package/src/data/api/enum/response/ErrorCode.ts +13 -0
  205. package/src/data/api/enum/response/index.ts +1 -0
  206. package/src/data/api/index.ts +3 -0
  207. package/src/data/api/interface/ICommonIdentifiable.ts +9 -0
  208. package/src/data/api/interface/IConcurrencySafe.ts +9 -0
  209. package/src/data/api/interface/IIdentifiable.ts +12 -0
  210. package/src/data/api/interface/IIdentifiableSecondary.ts +9 -0
  211. package/src/data/api/interface/index.ts +3 -0
  212. package/src/data/auth/dto/ClaimDto.ts +4 -0
  213. package/src/data/auth/dto/RegisterRequestDto.ts +4 -0
  214. package/src/data/auth/dto/RoleDto.ts +6 -0
  215. package/src/data/auth/dto/SignInRequestDto.ts +4 -0
  216. package/src/data/auth/dto/TokensDto.ts +4 -0
  217. package/src/data/auth/dto/UserDto.ts +18 -0
  218. package/src/data/auth/dto/UserInfoDto.ts +15 -0
  219. package/src/data/auth/dto/index.ts +5 -0
  220. package/src/data/auth/index.ts +2 -0
  221. package/src/data/auth/policy.ts +63 -0
  222. package/src/data/index.ts +2 -0
  223. package/src/index.ts +4 -0
  224. package/src/services/api/ApiCrudControllerClient/index.integration.test.ts +46 -0
  225. package/src/services/api/ApiCrudControllerClient/index.ts +135 -0
  226. package/src/services/api/ApiInitializationService/index.ts +254 -0
  227. package/src/services/api/ApiReadControllerClient/index.integration.test.ts +71 -0
  228. package/src/services/api/ApiReadControllerClient/index.ts +137 -0
  229. package/src/services/api/HttpService/FetchHttpService.integration.test.ts +65 -0
  230. package/src/services/api/HttpService/FetchHttpService.ts +34 -0
  231. package/src/services/api/HttpService/HttpRequestConfig.ts +10 -0
  232. package/src/services/api/HttpService/HttpResponse.ts +14 -0
  233. package/src/services/api/HttpService/IHttpService.ts +17 -0
  234. package/src/services/api/HttpService/README.md +106 -0
  235. package/src/services/api/HttpService/index.ts +12 -0
  236. package/src/services/api/UserManagementControllerClient/index.integration.test.ts +69 -0
  237. package/src/services/api/UserManagementControllerClient/index.ts +134 -0
  238. package/src/services/api/index.ts +5 -0
  239. package/src/services/auth/client/AuthService/index.ts +233 -0
  240. package/src/services/auth/client/AuthorizationManagementControllerClient/index.integration.test.ts +110 -0
  241. package/src/services/auth/client/AuthorizationManagementControllerClient/index.ts +165 -0
  242. package/src/services/auth/client/index.ts +2 -0
  243. package/src/services/auth/index.ts +1 -0
  244. package/src/services/index.ts +2 -0
  245. package/src/utils/authorization/index.ts +47 -0
  246. package/src/utils/index.ts +2 -0
  247. package/src/utils/result/index.ts +25 -0
  248. package/src/utils/search/index.ts +150 -0
  249. package/tsconfig.json +19 -0
  250. package/vitest-setup.ts +43 -0
  251. package/vitest.config.ts +59 -0
@@ -0,0 +1,477 @@
1
+ import { IIdentifiable, PropertyPathDto, ReadSelectedDefinitionDto, ReadSelectedOrderingDirection, ReadSelectedSearchDefinitionDto } from '@data';
2
+ import { toPascalCase } from '@utils';
3
+ import { ApiReadControllerClient } from '@services';
4
+
5
+ /**
6
+ * Used with CollectionViewAdapter, a simplified configuration object for handling reading/writing from/to ReadSelectedDefinitionDto.
7
+ *
8
+ * @template TDto The type of the collection being sorted/filtered/paginated.
9
+ *
10
+ * @property {object} pagination - Pagination configuration
11
+ * @property {boolean} pagination.useTotalItemCount - Used for last page related logic of the paginator.
12
+ * Set to true if the total count for the collection is available; false assumes the last page is not known
13
+ * and adapts the pagination display accordingly.
14
+ * @property {number} [pagination.pageSize] - Number of items/rows to be fetched for the current page.
15
+ * @property {number} [pagination.pageNumber] - Current page index (skips fetching `(n-1) * pageSize` items/rows).
16
+ *
17
+ * @property {object} ordering - Ordering configuration
18
+ * @property {string[]} [ordering.orderByPath] - Array of column/property names that are sortable.
19
+ * @property {ReadSelectedOrderingDirection} [ordering.orderDirection] - Enum for ascending/descending sort order.
20
+ *
21
+ * @property {object} search - Search configuration
22
+ * @property {(keyof TDto)[]} [search.textSearchableProperties] - Array of column/property names that are searchable where the value type is string.
23
+ * @property {(keyof TDto)[]} [search.numericSearchableProperties] - Array of column/property names that are searchable where the value type is number.
24
+ * @property {ReadSelectedSearchDefinitionDto<TDto>} [search.searchDefinition] - The search definition by which the collection will be filtered.
25
+ */
26
+ export type CollectionViewAdapterOptions<TDto> = {
27
+ pagination: {
28
+ useTotalItemCount: boolean;
29
+ pageSize?: number;
30
+ pageNumber?: number;
31
+ };
32
+ ordering: {
33
+ maxActiveOrderingColumns: number;
34
+ orderByPaths: {
35
+ orderByPath: PropertyPathDto;
36
+ orderDirection: ReadSelectedOrderingDirection;
37
+ }[];
38
+ };
39
+ search: {
40
+ textSearchableProperties?: (keyof TDto)[];
41
+ numericSearchableProperties?: (keyof TDto)[];
42
+ searchDefinition?: ReadSelectedSearchDefinitionDto<TDto>;
43
+ };
44
+ };
45
+
46
+ /**
47
+ * TS class for handling ordering, filtering and paginating collections
48
+ *
49
+ * @param controllerName - specify the target endpoint controller name.
50
+ * @param dataChangedCallback - Function invoked whenever the data changes.
51
+ * Receives a loading state flag and the updated data collection.
52
+ * @param options - Optional settings of type CollectionViewAdapter to customize the behavior of the adapter.
53
+ */
54
+ export class CollectionViewAdapter<TKey, TDto extends IIdentifiable<TKey>, TDetailsDto extends IIdentifiable<TKey>> {
55
+ private _readClient: ApiReadControllerClient<TKey, TDto, TDetailsDto>;
56
+
57
+ private _isLoading = false;
58
+ private _pageData: TDto[] = [];
59
+ private _selectedItems: TDto[] = [];
60
+
61
+ private _defaultOptions: CollectionViewAdapterOptions<TDto> = {
62
+ pagination: {
63
+ useTotalItemCount: false,
64
+ pageSize: 10,
65
+ pageNumber: 1,
66
+ },
67
+ ordering: {
68
+ maxActiveOrderingColumns: 1,
69
+ orderByPaths: [],
70
+ },
71
+ search: {
72
+ textSearchableProperties: [],
73
+ numericSearchableProperties: [],
74
+ },
75
+ };
76
+
77
+ private _initialOptions: CollectionViewAdapterOptions<TDto>;
78
+ private _currentOptions: CollectionViewAdapterOptions<TDto>;
79
+
80
+ constructor(
81
+ controllerName: string,
82
+ dataChangedCallback: (isLoading: boolean, data: TDto[] | undefined, error: string | undefined) => void,
83
+ options?: CollectionViewAdapterOptions<TDto>,
84
+ ) {
85
+ this._readClient = new ApiReadControllerClient<TKey, TDto, TDetailsDto>(controllerName);
86
+
87
+ this._initialOptions = {
88
+ pagination: {
89
+ ...this._defaultOptions.pagination,
90
+ ...options?.pagination,
91
+ },
92
+ ordering: {
93
+ ...this._defaultOptions.ordering,
94
+ ...options?.ordering,
95
+ },
96
+ search: {
97
+ ...this._defaultOptions.search,
98
+ ...options?.search,
99
+ },
100
+ };
101
+
102
+ this._currentOptions = {
103
+ pagination: { ...this._initialOptions.pagination },
104
+ ordering: { ...this._initialOptions.ordering },
105
+ search: { ...this._initialOptions.search },
106
+ };
107
+
108
+ this._dataChangedCallback = dataChangedCallback;
109
+ this._fetchCurrentPageData();
110
+ }
111
+
112
+ //#region SEARCH
113
+
114
+ /**
115
+ * Allows for adding any filtering criteria. Label is required to distinguish custom search categories (e.g. searchTerm, dateRange, anyCustom ...)
116
+ */
117
+ public setSearchDefinition(searchDefinition: ReadSelectedSearchDefinitionDto<TDto>) {
118
+ this._currentOptions.search.searchDefinition = searchDefinition;
119
+ this._currentOptions.pagination.pageNumber = 1; // Reset to first page
120
+ return this._fetchCurrentPageData();
121
+ }
122
+
123
+ //#endregion
124
+
125
+ //#region ORDERING
126
+
127
+ /**
128
+ * Compares two PropertyPathDto arrays for equality
129
+ */
130
+ private _arePropertyPathsEqual(path1: PropertyPathDto, path2: PropertyPathDto): boolean {
131
+ if (path1.length !== path2.length) return false;
132
+ return path1.every((segment, index) => segment === path2[index]);
133
+ }
134
+
135
+ /**
136
+ * Public method for ordering the collection by property path / column name.
137
+ *
138
+ * @param propertyPath - must match camel-cased property/column name being sorted
139
+ * @param orderDirection - use `ReadSelectedOrderingDirection` enum choices
140
+ */
141
+ public setOrdering(propertyPath: PropertyPathDto, orderDirection: ReadSelectedOrderingDirection) {
142
+ // Remove any existing ordering for this property path
143
+ this._currentOptions.ordering.orderByPaths = this._currentOptions.ordering.orderByPaths.filter(
144
+ p => !this._arePropertyPathsEqual(p.orderByPath, propertyPath),
145
+ );
146
+
147
+ // Add the new ordering at the beginning
148
+ this._currentOptions.ordering.orderByPaths.unshift({
149
+ orderByPath: propertyPath,
150
+ orderDirection: orderDirection,
151
+ });
152
+
153
+ // Limit to maxActiveOrderingColumns
154
+ if (this._currentOptions.ordering.orderByPaths.length > this._currentOptions.ordering.maxActiveOrderingColumns) {
155
+ this._currentOptions.ordering.orderByPaths = this._currentOptions.ordering.orderByPaths.slice(0, this._currentOptions.ordering.maxActiveOrderingColumns);
156
+ }
157
+
158
+ this._currentOptions.pagination.pageNumber = 1; // Reset to first page
159
+ return this._fetchCurrentPageData();
160
+ }
161
+
162
+ /**
163
+ * Returns the property name of the current columns/properties on which ordering is applied, along with the `ReadSelectedOrderingDirection` direction
164
+ */
165
+ public getCurrentOrdering() {
166
+ return this._currentOptions.ordering.orderByPaths;
167
+ }
168
+ //#endregion
169
+
170
+ //#region PAGINATION
171
+
172
+ /**
173
+ * Returns the current page's page number
174
+ */
175
+ public get currentPage() {
176
+ return this._currentOptions.pagination.pageNumber;
177
+ }
178
+
179
+ /**
180
+ * Sets the current page to `pageNumber` and triggers data re-fetch
181
+ *
182
+ * @param pageNumber
183
+ */
184
+ public jumpToPage(pageNumber: number) {
185
+ this._currentOptions.pagination.pageNumber = pageNumber;
186
+ return this._fetchCurrentPageData();
187
+ }
188
+
189
+ /**
190
+ * Returns the currently set page size value
191
+ */
192
+ public get pageSize() {
193
+ return this._currentOptions.pagination.pageSize;
194
+ }
195
+
196
+ /**
197
+ * Page size dictates the number of items or rows being fetched in a single API call
198
+ *
199
+ * @param pageSize
200
+ */
201
+ public setPageSize(pageSize: number) {
202
+ this._currentOptions.pagination.pageSize = pageSize;
203
+ this._currentOptions.pagination.pageNumber = 1; // Reset to first page
204
+ return this._fetchCurrentPageData();
205
+ }
206
+
207
+ /**
208
+ * Refreshes the data by re-fetching the current page
209
+ */
210
+ public refreshData() {
211
+ return this._fetchCurrentPageData();
212
+ }
213
+
214
+ private _totalItemCount = -1;
215
+
216
+ /**
217
+ * Returns the total count value of the collection.
218
+ *
219
+ * If `CollectionViewAdapterOptions` has set `pagination.useTotalItemCount` to `true`, the API will return this value along with the collection and it will be set automatically.
220
+ * Else this adapter will attempt to dynamically calculate this value via `_calculateTotal()`.
221
+ */
222
+ public get totalItemCount() {
223
+ return this._totalItemCount;
224
+ }
225
+
226
+ //#endregion
227
+
228
+ /**
229
+ * Callback function invoked whenever data changes.
230
+ * Receives a loading state flag and the updated data collection.
231
+ */
232
+ private readonly _dataChangedCallback!: (isLoading: boolean, data: TDto[] | undefined, errorMessage: string | undefined) => void;
233
+
234
+ /**
235
+ * Timeout reference used to debounce calls to `_fetchCurrentPageData`.
236
+ */
237
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
238
+ private _fetchCurrentPageDataTimeout?: any;
239
+
240
+ /**
241
+ * Queue of pending promises waiting for `_fetchCurrentPageData` to resolve or reject.
242
+ */
243
+ private _fetchCurrentPageDataPromises: { resolve: (value: TDto[] | PromiseLike<TDto[]>) => void; reject: (reason?: unknown) => void }[] = [];
244
+
245
+ /**
246
+ * Tracks the current in-flight request controller to allow cancellation when a new fetch starts.
247
+ */
248
+ private _currentAbortController?: AbortController;
249
+
250
+ /**
251
+ * Fetches the current page of data with debouncing support.
252
+ * Consolidates multiple concurrent calls into a single execution,
253
+ * resolving all queued promises with the same result.
254
+ *
255
+ * @returns A promise resolving to the fetched page of data.
256
+ */
257
+ private async _fetchCurrentPageData(): Promise<TDto[]> {
258
+ return new Promise((resolve, reject) => {
259
+ this._fetchCurrentPageDataPromises.push({ resolve, reject });
260
+ if (this._fetchCurrentPageDataTimeout !== undefined) {
261
+ clearTimeout(this._fetchCurrentPageDataTimeout);
262
+ }
263
+ this._fetchCurrentPageDataTimeout = setTimeout(async () => {
264
+ try {
265
+ const result = await this._fetchCurrentPageDataDebounced();
266
+ const promises = this._fetchCurrentPageDataPromises.splice(0, this._fetchCurrentPageDataPromises.length);
267
+ for (const p of promises) p.resolve(result);
268
+ } catch (err) {
269
+ const promises = this._fetchCurrentPageDataPromises.splice(0, this._fetchCurrentPageDataPromises.length);
270
+ for (const p of promises) p.reject(err);
271
+ }
272
+ }, 50);
273
+ });
274
+ }
275
+
276
+ /**
277
+ * Executes the actual data fetch operation after debounce delay.
278
+ * Updates the loading state, triggers data change callbacks,
279
+ * and handles total item count calculation if pagination is enabled.
280
+ *
281
+ * @returns A promise resolving to the fetched page of data.
282
+ */
283
+ private async _fetchCurrentPageDataDebounced(): Promise<TDto[]> {
284
+ this._isLoading = true;
285
+ this._dataChangedCallback(this._isLoading, undefined, undefined);
286
+ let abortController: AbortController | undefined;
287
+ let caughtError: unknown;
288
+
289
+ try {
290
+ // Cancel any in-flight request before starting a new one
291
+ if (this._currentAbortController) {
292
+ try {
293
+ this._currentAbortController.abort();
294
+ } catch {
295
+ // Ignore abort errors as they are expected when debouncing
296
+ }
297
+ }
298
+ abortController = new AbortController();
299
+ this._currentAbortController = abortController;
300
+
301
+ const definition = this._parseOptionsToDefinition();
302
+ const response = await this._readClient.readSelected(definition, abortController?.signal);
303
+
304
+ if (!response.ok) {
305
+ throw response.error;
306
+ }
307
+
308
+ this._pageData = response.result;
309
+
310
+ if (this._currentOptions.pagination.useTotalItemCount && response.metadata.totalCount !== undefined) {
311
+ this._totalItemCount = response.metadata.totalCount;
312
+ } else {
313
+ const pageSize = this._currentOptions.pagination.pageSize!;
314
+ const currentPageNumber = this._currentOptions.pagination.pageNumber!;
315
+ const itemsOnCurrentPageCount = this._pageData.length;
316
+ this._totalItemCount = this._calculateTotal(pageSize, currentPageNumber, itemsOnCurrentPageCount, this._totalItemCount);
317
+ }
318
+
319
+ return this._pageData;
320
+ } catch (error: unknown) {
321
+ caughtError = error;
322
+
323
+ // Do not log abort-related errors as they are expected when debouncing
324
+ if (error instanceof DOMException && error.name === 'AbortError') {
325
+ return Promise.reject(error);
326
+ }
327
+
328
+ console.error('Error fetching data:', error);
329
+ return Promise.reject(error);
330
+ } finally {
331
+ this._isLoading = false;
332
+
333
+ let errorName: string | undefined;
334
+ if (caughtError && !(caughtError instanceof DOMException && caughtError.name === 'AbortError')) {
335
+ errorName = caughtError instanceof Error ? caughtError.name : undefined;
336
+ }
337
+ this._dataChangedCallback(this._isLoading, this._pageData, errorName);
338
+
339
+ // Clear the abort controller only if it belongs to this request (avoid racing with a newer one)
340
+
341
+ if (abortController && this._currentAbortController === abortController) {
342
+ this._currentAbortController = undefined;
343
+ }
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Takes current `CollectionViewAdapterOptions` and parses to `ReadSelectedDefinitionDto` which the cornerstone API expects.
349
+ *
350
+ * @returns - `ReadSelectedDefinitionDto` object
351
+ */
352
+ private _parseOptionsToDefinition(): ReadSelectedDefinitionDto<TDto> {
353
+ const definition: ReadSelectedDefinitionDto<TDto> = {
354
+ paginationDefinition: {
355
+ skip: (this._currentOptions.pagination.pageNumber! - 1) * this._currentOptions.pagination.pageSize!,
356
+ limit: this._currentOptions.pagination.pageSize!,
357
+ },
358
+ };
359
+
360
+ // Apply ordering
361
+ const orderByPaths = this._currentOptions.ordering.orderByPaths;
362
+
363
+ if (orderByPaths && orderByPaths.length > 0) {
364
+ definition.orderingDefinition = {
365
+ order: orderByPaths.map(orderPath => ({
366
+ propertyPath: orderPath.orderByPath.map(x => toPascalCase(x)) as PropertyPathDto,
367
+ direction: orderPath.orderDirection,
368
+ })),
369
+ };
370
+ }
371
+
372
+ // Apply search
373
+ definition.searchDefinition = this._currentOptions.search.searchDefinition;
374
+
375
+ return definition;
376
+ }
377
+
378
+ /**
379
+ * Calculates the total item count given pagination inputs.
380
+ *
381
+ * For example, with 10 items per page, on page 2 with 3 items, total = 13.
382
+ * If the total cannot be inferred (e.g., a full page is returned), the previous total is returned unchanged.
383
+ *
384
+ * @param pageSize - Number of items per page
385
+ * @param currentPageNumber - Current page number (1-based)
386
+ * @param itemsOnCurrentPageCount - Number of items in the fetched page
387
+ * @param previousTotal - Previous known total to preserve when indeterminate
388
+ * @returns The computed total item count
389
+ */
390
+ private _calculateTotal(pageSize: number, currentPageNumber: number, itemsOnCurrentPageCount: number, previousTotal: number): number {
391
+ if (itemsOnCurrentPageCount < pageSize && itemsOnCurrentPageCount !== 0) {
392
+ return pageSize * (currentPageNumber - 1) + itemsOnCurrentPageCount;
393
+ } else if (itemsOnCurrentPageCount === 0 && pageSize === 0) {
394
+ return 0;
395
+ }
396
+ return previousTotal;
397
+ }
398
+
399
+ /**
400
+ * Selects or deselects an item based on its ID.
401
+ * @param item - The item to select or deselect.
402
+ */
403
+ public toggleItemSelected(item: TDto) {
404
+ // if item with the same id is not already selected, add it
405
+ // if item with the same id is already selected, remove it
406
+ if (this._selectedItems.find(x => x.id === item.id)) {
407
+ this._selectedItems = this._selectedItems.filter(x => x.id !== item.id);
408
+ } else {
409
+ this._selectedItems.push(item);
410
+ }
411
+ }
412
+
413
+ public clearSelectedItems() {
414
+ this._selectedItems = [];
415
+ }
416
+
417
+ public selectItems(item: TDto) {
418
+ if (!this._selectedItems.find(x => x.id === item.id)) {
419
+ this._selectedItems.push(item);
420
+ }
421
+ }
422
+
423
+ public deselectItem(item: TDto) {
424
+ this._selectedItems = this._selectedItems.filter(x => x.id !== item.id);
425
+ }
426
+
427
+ /**
428
+ * Returns the currently selected items
429
+ */
430
+ public get selectedItems(): TDto[] {
431
+ return this._selectedItems;
432
+ }
433
+
434
+ /**
435
+ * Checks if an item is currently selected
436
+ * @param item - The item to check
437
+ */
438
+ public isItemSelected(item: TDto): boolean {
439
+ return this._selectedItems.some(x => x.id === item.id);
440
+ }
441
+
442
+ /**
443
+ * Selects all items on the current page
444
+ */
445
+ public selectAllItemsOnCurrentPage() {
446
+ for (const item of this._pageData) {
447
+ if (!this.isItemSelected(item)) {
448
+ this._selectedItems.push(item);
449
+ }
450
+ }
451
+ }
452
+
453
+ /**
454
+ * Deselects all items on the current page
455
+ */
456
+ public deselectAllItemsOnCurrentPage() {
457
+ const pageItemIds = this._pageData.map(item => item.id);
458
+ this._selectedItems = this._selectedItems.filter(item => !pageItemIds.includes(item.id));
459
+ }
460
+
461
+ /**
462
+ * Checks if all items on the current page are selected
463
+ */
464
+ public areAllItemsOnCurrentPageSelected(): boolean {
465
+ if (this._pageData.length === 0) return false;
466
+ return this._pageData.every(item => this.isItemSelected(item));
467
+ }
468
+
469
+ /**
470
+ * Checks if some (but not all) items on the current page are selected
471
+ */
472
+ public areSomeItemsOnCurrentPageSelected(): boolean {
473
+ if (this._pageData.length === 0) return false;
474
+ const selectedCount = this._pageData.filter(item => this.isItemSelected(item)).length;
475
+ return selectedCount > 0 && selectedCount < this._pageData.length;
476
+ }
477
+ }