@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,200 @@
1
+ import { apiInitializationService } from '../../..';
2
+ import { ErrorCode } from '../../../../data';
3
+ import { fail, ok } from '../../../../utils';
4
+ /**
5
+ * AuthService class is responsible for managing user authentication operations.
6
+ * It supports login, logout, and checking the current user's details.
7
+ *
8
+ * @export
9
+ * @class AuthService
10
+ */
11
+ export class AuthService {
12
+ baseControllerPath;
13
+ /**
14
+ * Constructor
15
+ * @param baseControllerPath Base path to Auth API controller
16
+ * @param httpService HTTP service implementation to use for requests
17
+ */
18
+ constructor(baseControllerPath = 'Auth', httpService) {
19
+ this.baseControllerPath = baseControllerPath;
20
+ this._httpService = httpService;
21
+ }
22
+ // #region HTTP service
23
+ _httpService = undefined;
24
+ /**
25
+ * Gets globally selected HTTP service
26
+ */
27
+ get httpService() {
28
+ return this._httpService || apiInitializationService._getHttpService();
29
+ }
30
+ // #endregion
31
+ /**
32
+ * Holds currently authenticated user's details
33
+ */
34
+ user = undefined;
35
+ /**
36
+ * Initializes the Authentication status by checking with the API if the current user is authenticated or not
37
+ *
38
+ * @async
39
+ * @return {Promise<void>} Resolves when initialization is complete.
40
+ */
41
+ async initialize() {
42
+ // Check user's authentication status
43
+ await this.whoAmI();
44
+ }
45
+ /**
46
+ * Checks if user is authenticated and retrieves the current user's details if they are
47
+ *
48
+ * @return {Promise<UserInfoDto<TKey, TUser>>} Currently logged in user's details
49
+ * @throws {Error} Error if fetch fails
50
+ */
51
+ async whoAmI() {
52
+ try {
53
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, '/whoami');
54
+ const res = await this.httpService.request(url, { credentials: 'include' });
55
+ if (!res.ok) {
56
+ this.user = undefined;
57
+ return fail(await res.json());
58
+ }
59
+ const response = ok(await res.json());
60
+ if (!response.ok) {
61
+ this.user = undefined;
62
+ return response;
63
+ }
64
+ this.user = response.result.user;
65
+ return response;
66
+ }
67
+ catch (err) {
68
+ console.error(err);
69
+ return fail({
70
+ error: {
71
+ code: ErrorCode.UnknownError,
72
+ message: 'Failed checking authentication status',
73
+ metadata: {},
74
+ },
75
+ });
76
+ }
77
+ }
78
+ /**
79
+ * Authenticates a user using their username and password, and retrieves user's details
80
+ *
81
+ * @param {string} username Login credentials: username
82
+ * @param {string} password Login credentials: password
83
+ * @return {Promise<TUser>} Currently logged in user's details
84
+ * @throws {Error} Error if fetch fails
85
+ */
86
+ async signIn(username, password) {
87
+ try {
88
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, '/signin');
89
+ const res = await this.httpService.request(url, {
90
+ method: 'POST',
91
+ headers: { 'Content-Type': 'application/json' },
92
+ body: JSON.stringify({ username, password }),
93
+ credentials: 'include',
94
+ });
95
+ if (!res.ok) {
96
+ return fail(await res.json());
97
+ }
98
+ // TODO: Handle tokens if not using cookies
99
+ const whoAmIRes = await this.whoAmI();
100
+ if (!whoAmIRes.ok) {
101
+ return whoAmIRes;
102
+ }
103
+ return ok({
104
+ result: whoAmIRes.result.user,
105
+ metadata: whoAmIRes.metadata,
106
+ });
107
+ }
108
+ catch (err) {
109
+ console.error(err);
110
+ return fail({
111
+ error: {
112
+ code: ErrorCode.UnknownError,
113
+ message: 'Failed signing in',
114
+ metadata: {},
115
+ },
116
+ });
117
+ }
118
+ }
119
+ /**
120
+ * Deauthenticates current user
121
+ *
122
+ * @return {Promise<void>}
123
+ * @throws {Error} Error if fetch fails
124
+ */
125
+ async signOut() {
126
+ try {
127
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, '/signout');
128
+ const res = await this.httpService.request(url, { credentials: 'include' });
129
+ if (!res.ok) {
130
+ return fail(await res.json());
131
+ }
132
+ this.user = undefined;
133
+ return ok(await res.json());
134
+ }
135
+ catch (err) {
136
+ console.error(err);
137
+ return fail({
138
+ error: {
139
+ code: ErrorCode.UnknownError,
140
+ message: 'Failed signing out',
141
+ metadata: {},
142
+ },
143
+ });
144
+ }
145
+ }
146
+ /**
147
+ * Registers a new user by sending user details to the API.
148
+ *
149
+ * @param {TRegisterUser} user User object containing registration data (e.g. username, password, and optionally firstName, lastName, etc.)
150
+ * @return {Promise<ApiResponseDto<TUser, EmptyMetadataDto>>} API response with registered user details or error info
151
+ * @throws {Error} Error if the request fails
152
+ */
153
+ async register(user) {
154
+ try {
155
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, '/register');
156
+ const res = await this.httpService.request(url, {
157
+ method: 'POST',
158
+ headers: { 'Content-Type': 'application/json' },
159
+ body: JSON.stringify(user),
160
+ credentials: 'include',
161
+ });
162
+ if (!res.ok) {
163
+ return fail(await res.json());
164
+ }
165
+ // TODO: Handle tokens if not using cookies
166
+ const whoAmIRes = await this.whoAmI();
167
+ if (!whoAmIRes.ok) {
168
+ return whoAmIRes;
169
+ }
170
+ return ok({
171
+ result: whoAmIRes.result.user,
172
+ metadata: whoAmIRes.metadata,
173
+ });
174
+ }
175
+ catch (err) {
176
+ console.error(err);
177
+ return fail({
178
+ error: {
179
+ code: ErrorCode.UnknownError,
180
+ message: 'Failed registering user',
181
+ metadata: {},
182
+ },
183
+ });
184
+ }
185
+ }
186
+ /**
187
+ * If any API response returns an "Unauthenticated" response, call this method
188
+ * to update local authentication state to unauthenticated
189
+ */
190
+ handleNoAuthApiResponse() {
191
+ this.user = undefined;
192
+ }
193
+ // #region Client-side authorization helpers
194
+ /**
195
+ * True if a user is currently loaded (post whoAmI/signIn)
196
+ */
197
+ isAuthenticated() {
198
+ return !!this.user;
199
+ }
200
+ }
@@ -0,0 +1,48 @@
1
+ import { IHttpService } from '../../..';
2
+ import { ApiResponseDto, EmptyMetadataDto, ReadMetadataDto, RoleDto } from '../../../../data';
3
+ /**
4
+ * Client for authorization management operations
5
+ *
6
+ * @export
7
+ * @class AuthorizationManagementControllerClient
8
+ */
9
+ export declare class AuthorizationManagementControllerClient {
10
+ protected readonly baseControllerPath: string;
11
+ /**
12
+ * Constructor
13
+ * @param baseControllerPath Base path to API controller
14
+ * @param httpService HTTP service implementation to use for requests
15
+ */
16
+ constructor(baseControllerPath?: string, httpService?: IHttpService);
17
+ private readonly _httpService?;
18
+ /**
19
+ * Gets globally selected HTTP service
20
+ */
21
+ protected get httpService(): IHttpService;
22
+ /**
23
+ * Gets all roles in the system
24
+ * @param includeClaims - Whether to include claims in the response
25
+ * @param signal - Optional cancellation signal
26
+ * @returns List of all roles
27
+ */
28
+ getAllRoles(includeClaims: boolean, signal?: AbortSignal): Promise<ApiResponseDto<RoleDto[], ReadMetadataDto>>;
29
+ /**
30
+ * Gets a role with its claims
31
+ * @param roleName - The name of the role
32
+ * @param signal
33
+ * @returns The role with its claims
34
+ */
35
+ getRoleWithClaims(roleName: string, signal?: AbortSignal): Promise<ApiResponseDto<RoleDto, ReadMetadataDto>>;
36
+ /**
37
+ * Creates or updates a role
38
+ * @param roleDto - The role to create or update
39
+ * @returns The created or updated role
40
+ */
41
+ upsertRole(roleDto: RoleDto): Promise<ApiResponseDto<RoleDto, EmptyMetadataDto>>;
42
+ /**
43
+ * Deletes a role
44
+ * @param roleName - The name of the role to delete
45
+ * @returns Promise that resolves when the role is deleted successfully
46
+ */
47
+ deleteRole(roleName: string): Promise<ApiResponseDto<undefined, EmptyMetadataDto>>;
48
+ }
@@ -0,0 +1,89 @@
1
+ import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
2
+ import { AuthorizationManagementControllerClient } from '.';
3
+ import fetchOrig from 'node-fetch';
4
+ import fetchCookie from 'fetch-cookie';
5
+ import { CookieJar } from 'tough-cookie';
6
+ // Create a fetch instance that handles auth cookies
7
+ const jar = new CookieJar();
8
+ const cookieFetch = fetchCookie(fetchOrig, jar);
9
+ // Custom IHttpService implementation for tests
10
+ class CookieFetchHttpService {
11
+ async request(url, config) {
12
+ const response = await cookieFetch(url, {
13
+ method: config?.method || 'GET',
14
+ headers: config?.headers,
15
+ body: config?.body,
16
+ signal: config?.signal,
17
+ });
18
+ const headers = {};
19
+ response.headers.forEach((value, key) => {
20
+ headers[key] = value;
21
+ });
22
+ return {
23
+ ok: response.ok,
24
+ status: response.status,
25
+ statusText: response.statusText,
26
+ headers,
27
+ json: () => response.json(),
28
+ text: () => response.text(),
29
+ };
30
+ }
31
+ }
32
+ const credentials = {
33
+ username: 'admin@test.com',
34
+ password: 'Password1234!',
35
+ };
36
+ const baseUrl = 'http://localhost:5000/api';
37
+ describe('AuthorizationManagementControllerClient', () => {
38
+ let client;
39
+ let fetchService = new CookieFetchHttpService();
40
+ beforeAll(async () => {
41
+ await fetchService.request(`${baseUrl}/auth/signin`, {
42
+ method: 'POST',
43
+ headers: { 'Content-Type': 'application/json' },
44
+ body: JSON.stringify(credentials),
45
+ });
46
+ });
47
+ beforeEach(() => {
48
+ client = new AuthorizationManagementControllerClient('/AuthorizationManagement', fetchService);
49
+ });
50
+ it('should fetch all roles', async () => {
51
+ const result = (await client.getAllRoles(false));
52
+ expect(result.ok).toBe(true);
53
+ expect(result.result).toBeDefined();
54
+ });
55
+ it('should fetch all roles with claims', async () => {
56
+ const result = (await client.getAllRoles(true));
57
+ expect(result.ok).toBe(true);
58
+ expect(result.result).toBeDefined();
59
+ expect(result.result[0].claims).toBeDefined();
60
+ });
61
+ it('should return role with claims by name', async () => {
62
+ const newRole = { name: 'New Role Test Claims', claims: [{ type: 'Test Claim Type', value: 'Test Claim Value' }] };
63
+ const create = (await client.upsertRole(newRole));
64
+ expect(create.ok).toBe(true);
65
+ const result = (await client.getRoleWithClaims(newRole.name));
66
+ expect(result.ok).toBe(true);
67
+ expect(result.result).toBeDefined();
68
+ expect(result.result.claims).toBeDefined();
69
+ expect(result.result.name).toBe(newRole.name);
70
+ expect(result.result.claims.length).toBe(newRole.claims.length);
71
+ expect(result.result.claims).toEqual(newRole.claims);
72
+ });
73
+ it('should create a new role with claims', async () => {
74
+ const newRole = { name: 'New Role', claims: [{ type: 'Test Claim Type', value: 'Test Claim Value' }] };
75
+ const result = (await client.upsertRole(newRole));
76
+ expect(result.ok).toBe(true);
77
+ expect(result.result).toBeDefined();
78
+ expect(result.result.name).toBe(newRole.name);
79
+ expect(result.result.claims.length).toBe(newRole.claims.length);
80
+ expect(result.result.claims).toEqual(newRole.claims);
81
+ });
82
+ it('should delete role', async () => {
83
+ const role = { name: 'Temp Role', claims: [] };
84
+ const create = (await client.upsertRole(role));
85
+ expect(create.ok).toBe(true);
86
+ const deleteResult = await client.deleteRole(role.name);
87
+ expect(deleteResult.ok).toBe(true);
88
+ });
89
+ });
@@ -0,0 +1,148 @@
1
+ import { apiInitializationService } from '../../..';
2
+ import { ErrorCode } from '../../../../data';
3
+ import { fail, ok } from '../../../../utils/result';
4
+ /**
5
+ * Client for authorization management operations
6
+ *
7
+ * @export
8
+ * @class AuthorizationManagementControllerClient
9
+ */
10
+ export class AuthorizationManagementControllerClient {
11
+ baseControllerPath;
12
+ /**
13
+ * Constructor
14
+ * @param baseControllerPath Base path to API controller
15
+ * @param httpService HTTP service implementation to use for requests
16
+ */
17
+ constructor(baseControllerPath = 'AuthorizationManagement', httpService) {
18
+ this.baseControllerPath = baseControllerPath;
19
+ this._httpService = httpService;
20
+ }
21
+ // #region HTTP service
22
+ _httpService = undefined;
23
+ /**
24
+ * Gets globally selected HTTP service
25
+ */
26
+ get httpService() {
27
+ return this._httpService || apiInitializationService._getHttpService();
28
+ }
29
+ // #endregion
30
+ /**
31
+ * Gets all roles in the system
32
+ * @param includeClaims - Whether to include claims in the response
33
+ * @param signal - Optional cancellation signal
34
+ * @returns List of all roles
35
+ */
36
+ async getAllRoles(includeClaims, signal) {
37
+ try {
38
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, `/GetAllRoles?includeClaims=${includeClaims}`);
39
+ const res = await this.httpService.request(url, {
40
+ credentials: 'include',
41
+ signal,
42
+ });
43
+ if (!res.ok) {
44
+ return fail(await res.json());
45
+ }
46
+ return ok(await res.json());
47
+ }
48
+ catch (err) {
49
+ console.error(err);
50
+ return fail({
51
+ error: {
52
+ code: ErrorCode.UnknownError,
53
+ message: 'Unknown error fetching all roles',
54
+ metadata: {},
55
+ },
56
+ });
57
+ }
58
+ }
59
+ /**
60
+ * Gets a role with its claims
61
+ * @param roleName - The name of the role
62
+ * @param signal
63
+ * @returns The role with its claims
64
+ */
65
+ async getRoleWithClaims(roleName, signal) {
66
+ try {
67
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, `/GetRoleWithClaims/${encodeURIComponent(roleName)}`);
68
+ const res = await this.httpService.request(url, {
69
+ credentials: 'include',
70
+ signal,
71
+ });
72
+ if (!res.ok) {
73
+ return fail(await res.json());
74
+ }
75
+ return ok(await res.json());
76
+ }
77
+ catch (err) {
78
+ console.error(err);
79
+ return fail({
80
+ error: {
81
+ code: ErrorCode.UnknownError,
82
+ message: 'Unknown error fetching role with claims',
83
+ metadata: {},
84
+ },
85
+ });
86
+ }
87
+ }
88
+ /**
89
+ * Creates or updates a role
90
+ * @param roleDto - The role to create or update
91
+ * @returns The created or updated role
92
+ */
93
+ async upsertRole(roleDto) {
94
+ try {
95
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, `/UpsertRole`);
96
+ const res = await this.httpService.request(url, {
97
+ method: 'PUT',
98
+ headers: {
99
+ 'Content-Type': 'application/json',
100
+ },
101
+ body: JSON.stringify(roleDto),
102
+ credentials: 'include',
103
+ });
104
+ if (!res.ok) {
105
+ return fail(await res.json());
106
+ }
107
+ return ok(await res.json());
108
+ }
109
+ catch (err) {
110
+ console.error(err);
111
+ return fail({
112
+ error: {
113
+ code: ErrorCode.UnknownError,
114
+ message: 'Unknown error upserting role',
115
+ metadata: {},
116
+ },
117
+ });
118
+ }
119
+ }
120
+ /**
121
+ * Deletes a role
122
+ * @param roleName - The name of the role to delete
123
+ * @returns Promise that resolves when the role is deleted successfully
124
+ */
125
+ async deleteRole(roleName) {
126
+ try {
127
+ const url = await apiInitializationService.getApiUrl(this.baseControllerPath, `/DeleteRole/${encodeURIComponent(roleName)}`);
128
+ const res = await this.httpService.request(url, {
129
+ method: 'DELETE',
130
+ credentials: 'include',
131
+ });
132
+ if (!res.ok) {
133
+ return fail(await res.json());
134
+ }
135
+ return ok(undefined);
136
+ }
137
+ catch (err) {
138
+ console.error(err);
139
+ return fail({
140
+ error: {
141
+ code: ErrorCode.UnknownError,
142
+ message: `Unknown error while deleting the role ${roleName}`,
143
+ metadata: {},
144
+ },
145
+ });
146
+ }
147
+ }
148
+ }
@@ -0,0 +1,2 @@
1
+ export * from './AuthorizationManagementControllerClient';
2
+ export * from './AuthService';
@@ -0,0 +1,2 @@
1
+ export * from './AuthorizationManagementControllerClient';
2
+ export * from './AuthService';
@@ -0,0 +1 @@
1
+ export * from './client';
@@ -0,0 +1 @@
1
+ export * from './client';
@@ -0,0 +1,2 @@
1
+ export * from './api';
2
+ export * from './auth';
@@ -0,0 +1,2 @@
1
+ export * from './api';
2
+ export * from './auth';
@@ -0,0 +1,17 @@
1
+ import { IPolicy } from '../../data';
2
+ /**
3
+ * Checks if a policy is valid for the given parameters.
4
+ *
5
+ * @param policy Policy to check, either an instance or a class.
6
+ * @param params Parameters to pass to the policy check function.
7
+ * @returns A promise that resolves if the policy is valid, rejects otherwise.
8
+ */
9
+ export declare function check<TParams extends unknown[]>(policy: IPolicy<TParams> | (new () => IPolicy<TParams>), ...params: TParams): Promise<boolean>;
10
+ /**
11
+ * Composes multiple policies into a single policy.
12
+ *
13
+ * @param composition Composition method, either 'AND' or 'OR'.
14
+ * @param policies Policies to compose.
15
+ * @returns A new policy that represents the composition of the policies.
16
+ */
17
+ export declare function compose<TParams extends unknown[]>(composition: 'AND' | 'OR', ...policies: IPolicy<TParams>[]): IPolicy<TParams>;
@@ -0,0 +1,45 @@
1
+ import { PolicyBase } from '../../data';
2
+ /**
3
+ * Checks if a policy is valid for the given parameters.
4
+ *
5
+ * @param policy Policy to check, either an instance or a class.
6
+ * @param params Parameters to pass to the policy check function.
7
+ * @returns A promise that resolves if the policy is valid, rejects otherwise.
8
+ */
9
+ export async function check(policy, ...params) {
10
+ // If passed an instance of a policy
11
+ if (policy instanceof PolicyBase) {
12
+ return policy.check(...params);
13
+ }
14
+ // If passed a policy class
15
+ else {
16
+ return new policy().check(...params);
17
+ }
18
+ }
19
+ /**
20
+ * Composes multiple policies into a single policy.
21
+ *
22
+ * @param composition Composition method, either 'AND' or 'OR'.
23
+ * @param policies Policies to compose.
24
+ * @returns A new policy that represents the composition of the policies.
25
+ */
26
+ export function compose(composition, ...policies) {
27
+ return {
28
+ check: async (...params) => {
29
+ // Check all policies
30
+ if (composition === 'AND') {
31
+ const results = await Promise.allSettled(policies.map(policy => policy.check(...params)));
32
+ return results.every(result => result);
33
+ }
34
+ // Check any policies, exit as soon as any are true
35
+ else if (composition === 'OR') {
36
+ const results = await Promise.allSettled(policies.map(policy => policy.check(...params)));
37
+ return results.some(result => result);
38
+ }
39
+ // Invalid composition
40
+ else {
41
+ throw new Error('Invalid composition');
42
+ }
43
+ },
44
+ };
45
+ }
@@ -0,0 +1,2 @@
1
+ export * from './result';
2
+ export * from './search';
@@ -0,0 +1,2 @@
1
+ export * from './result';
2
+ export * from './search';
@@ -0,0 +1,21 @@
1
+ type TResultBase = {
2
+ readonly ok: boolean;
3
+ };
4
+ /**
5
+ * Represents a result of an operation.
6
+ * It can be either a success or an error.
7
+ */
8
+ export type Result<TSuccess extends TResultBase, TError extends TResultBase> = TSuccess | TError;
9
+ /**
10
+ * Creates a success result from the given argument.
11
+ * @param arg - The argument to create a success result from.
12
+ * @returns A success result.
13
+ */
14
+ export declare function ok<TSuccess extends TResultBase, TError extends TResultBase>(arg?: Omit<TSuccess, 'ok'>): Result<TSuccess, TError>;
15
+ /**
16
+ * Creates an error result from the given argument.
17
+ * @param arg - The argument to create an error result from.
18
+ * @returns An error result.
19
+ */
20
+ export declare function fail<TSuccess extends TResultBase, TError extends TResultBase>(arg: Omit<TError, 'ok'>): Result<TSuccess, TError>;
21
+ export {};
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Creates a success result from the given argument.
3
+ * @param arg - The argument to create a success result from.
4
+ * @returns A success result.
5
+ */
6
+ export function ok(arg) {
7
+ return { ok: true, ...arg };
8
+ }
9
+ /**
10
+ * Creates an error result from the given argument.
11
+ * @param arg - The argument to create an error result from.
12
+ * @returns An error result.
13
+ */
14
+ export function fail(arg) {
15
+ return { ok: false, ...arg };
16
+ }
@@ -0,0 +1,34 @@
1
+ import { ReadSelectedComparisonOperator, ReadSelectedLogicalOperator, ReadSelectedPropertyType, ReadSelectedSearchDefinitionDto } from '../../data';
2
+ /**
3
+ * Creates a ReadSelectedSearchDefinitionDto ready to be used with the CollectionViewAdapter
4
+ *
5
+ * @param logicalOperator - specify `ReadSelectedLogicalOperator` that will apply to all ReadSelectedSearchPropertyDefinitionDto property definitions included in this search definition
6
+ * @param conditions - Variable number of condition objects containing:
7
+ * - propertyName: The property name from the DTO to search on
8
+ * - operator: The `ReadSelectedComparisonOperator` enum
9
+ * - value: The value to compare against
10
+ * - valueType: The `ReadSelectedPropertyType` being compared
11
+ */
12
+ export declare function condition<T>(logicalOperator: ReadSelectedLogicalOperator, ...conditions: Array<{
13
+ propertyName: keyof T;
14
+ operator: ReadSelectedComparisonOperator;
15
+ value: unknown;
16
+ valueType: ReadSelectedPropertyType;
17
+ }>): ReadSelectedSearchDefinitionDto<T>;
18
+ /**
19
+ * Combines multiple ReadSelectedSearchDefinitionDto with AND logic
20
+ */
21
+ export declare function and<T>(...definitions: ReadSelectedSearchDefinitionDto<T>[]): ReadSelectedSearchDefinitionDto<T>;
22
+ /**
23
+ * Combines multiple ReadSelectedSearchDefinitionDto with OR logic
24
+ */
25
+ export declare function or<T>(...definitions: ReadSelectedSearchDefinitionDto<T>[]): ReadSelectedSearchDefinitionDto<T>;
26
+ /**
27
+ * Preset implementation for filtering by searchTerm. String types will search by provided `textSearchableProperties` with `ReadSelectedComparisonOperator.Contains`. Numeric types will search by provided `numericSearchableProperties`, `parseInt` the search term and use `ReadSelectedComparisonOperator.Equal`.
28
+ */
29
+ export declare function searchTerm<T>(searchTerm: string, textSearchableProperties: (keyof T)[], numericSearchableProperties: (keyof T)[]): void | ReadSelectedSearchDefinitionDto<T>;
30
+ /**
31
+ * Preset implementation for filtering a date range. User may select desired `ReadSelectedComparisonOperator` and `ReadSelectedPropertyType` for the from and to dates. Valid object property must be provided for `searchableProperty`.
32
+ */
33
+ export declare function dateInterval<T>(fromDate: Date, toDate: Date, fromDateComparisonOperator: ReadSelectedComparisonOperator, toDateComparisonOperator: ReadSelectedComparisonOperator, valueType: ReadSelectedPropertyType, searchableProperty: keyof T): void | ReadSelectedSearchDefinitionDto<T>;
34
+ export declare function toPascalCase<T>(str: keyof T): keyof T;