@emilgroup/accounting-sdk 1.34.1-beta.0 → 1.34.1-beta.100

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 (182) hide show
  1. package/.openapi-generator/FILES +37 -0
  2. package/README.md +2 -2
  3. package/api/accounting-configs-api.ts +158 -28
  4. package/api/booking-entries-api.ts +252 -13
  5. package/api/commission-account-mappings-api.ts +1231 -0
  6. package/api/financial-accounts-api.ts +169 -24
  7. package/api/financial-transactions-api.ts +16 -6
  8. package/api/health-api.ts +1 -1
  9. package/api/number-ranges-api.ts +32 -12
  10. package/api/personal-accounts-api.ts +32 -12
  11. package/api/product-account-mappings-api.ts +1751 -0
  12. package/api.ts +4 -0
  13. package/base.ts +239 -43
  14. package/common.ts +3 -2
  15. package/configuration.ts +20 -0
  16. package/dist/api/accounting-configs-api.d.ts +54 -7
  17. package/dist/api/accounting-configs-api.js +206 -63
  18. package/dist/api/booking-entries-api.d.ts +121 -7
  19. package/dist/api/booking-entries-api.js +241 -26
  20. package/dist/api/commission-account-mappings-api.d.ts +685 -0
  21. package/dist/api/commission-account-mappings-api.js +1051 -0
  22. package/dist/api/financial-accounts-api.d.ts +82 -16
  23. package/dist/api/financial-accounts-api.js +168 -40
  24. package/dist/api/financial-transactions-api.d.ts +2 -2
  25. package/dist/api/financial-transactions-api.js +22 -14
  26. package/dist/api/health-api.d.ts +1 -1
  27. package/dist/api/health-api.js +1 -1
  28. package/dist/api/number-ranges-api.d.ts +4 -4
  29. package/dist/api/number-ranges-api.js +52 -30
  30. package/dist/api/personal-accounts-api.d.ts +4 -4
  31. package/dist/api/personal-accounts-api.js +52 -30
  32. package/dist/api/product-account-mappings-api.d.ts +972 -0
  33. package/dist/api/product-account-mappings-api.js +1493 -0
  34. package/dist/api.d.ts +2 -0
  35. package/dist/api.js +2 -0
  36. package/dist/base.d.ts +26 -0
  37. package/dist/base.js +243 -60
  38. package/dist/common.d.ts +1 -1
  39. package/dist/common.js +1 -0
  40. package/dist/configuration.d.ts +17 -0
  41. package/dist/configuration.js +2 -0
  42. package/dist/models/account-mapping-class.d.ts +2 -0
  43. package/dist/models/account-mapping-class.js +3 -1
  44. package/dist/models/accounting-config-class.d.ts +11 -0
  45. package/dist/models/accounting-config-class.js +5 -0
  46. package/dist/models/accounting-config-data-class.d.ts +7 -0
  47. package/dist/models/accounting-config-data-dto-default-account-mappings.d.ts +8 -0
  48. package/dist/models/accounting-config-data-dto-default-account-mappings.js +3 -1
  49. package/dist/models/accounting-config-data-dto.d.ts +7 -0
  50. package/dist/models/booking-entry-class.d.ts +35 -0
  51. package/dist/models/booking-entry-class.js +11 -1
  52. package/dist/models/commission-account-mapping-class.d.ts +49 -0
  53. package/dist/models/commission-account-mapping-class.js +15 -0
  54. package/dist/models/commission-agreement-account-mapping-detail-class.d.ts +44 -0
  55. package/dist/models/commission-agreement-account-mapping-detail-class.js +15 -0
  56. package/dist/models/commission-agreement-partner-class.d.ts +30 -0
  57. package/dist/models/commission-agreement-partner-class.js +15 -0
  58. package/dist/models/commission-agreement-with-account-mapping-class.d.ts +43 -0
  59. package/dist/models/commission-agreement-with-account-mapping-class.js +15 -0
  60. package/dist/models/commission-type-account-mapping-class.d.ts +31 -0
  61. package/dist/models/commission-type-account-mapping-class.js +15 -0
  62. package/dist/models/create-booking-entry-request-dto.d.ts +22 -0
  63. package/dist/models/create-booking-entry-request-dto.js +10 -0
  64. package/dist/models/create-financial-account-request-dto.d.ts +14 -0
  65. package/dist/models/create-financial-account-request-dto.js +3 -1
  66. package/dist/models/create-or-correct-opening-balance-dto.d.ts +25 -0
  67. package/dist/models/create-or-correct-opening-balance-dto.js +15 -0
  68. package/dist/models/create-or-correct-opening-balance-response-class.d.ts +25 -0
  69. package/dist/models/create-or-correct-opening-balance-response-class.js +15 -0
  70. package/dist/models/financial-account-class.d.ts +30 -1
  71. package/dist/models/financial-account-class.js +5 -1
  72. package/dist/models/financial-transaction-class.d.ts +6 -0
  73. package/dist/models/financial-transaction-data-dto.d.ts +7 -0
  74. package/dist/models/financial-transaction-data-dto.js +1 -0
  75. package/dist/models/get-activation-snapshot-response-class.d.ts +25 -0
  76. package/dist/models/get-activation-snapshot-response-class.js +15 -0
  77. package/dist/models/index.d.ts +35 -0
  78. package/dist/models/index.js +35 -0
  79. package/dist/models/list-commission-account-mappings-response-class.d.ts +43 -0
  80. package/dist/models/list-commission-account-mappings-response-class.js +15 -0
  81. package/dist/models/list-commission-agreements-with-account-mappings-response-class.d.ts +43 -0
  82. package/dist/models/list-commission-agreements-with-account-mappings-response-class.js +15 -0
  83. package/dist/models/list-commission-type-account-mappings-response-class.d.ts +43 -0
  84. package/dist/models/list-commission-type-account-mappings-response-class.js +15 -0
  85. package/dist/models/list-product-account-mappings-response-class.d.ts +43 -0
  86. package/dist/models/list-product-account-mappings-response-class.js +15 -0
  87. package/dist/models/list-products-with-account-mappings-response-class.d.ts +43 -0
  88. package/dist/models/list-products-with-account-mappings-response-class.js +15 -0
  89. package/dist/models/opening-balance-item-dto.d.ts +41 -0
  90. package/dist/models/opening-balance-item-dto.js +20 -0
  91. package/dist/models/personal-account-class.d.ts +6 -0
  92. package/dist/models/personal-account-range-class.d.ts +42 -0
  93. package/dist/models/personal-account-range-class.js +21 -0
  94. package/dist/models/personal-account-range-dto.d.ts +42 -0
  95. package/dist/models/personal-account-range-dto.js +21 -0
  96. package/dist/models/post-financial-event-request-dto.d.ts +46 -0
  97. package/dist/models/post-financial-event-request-dto.js +25 -0
  98. package/dist/models/post-financial-event-response-class.d.ts +25 -0
  99. package/dist/models/post-financial-event-response-class.js +15 -0
  100. package/dist/models/premium-item-account-mapping-class.d.ts +37 -0
  101. package/dist/models/premium-item-account-mapping-class.js +15 -0
  102. package/dist/models/product-account-mapping-class.d.ts +61 -0
  103. package/dist/models/product-account-mapping-class.js +15 -0
  104. package/dist/models/product-account-mapping-detail-class.d.ts +56 -0
  105. package/dist/models/product-account-mapping-detail-class.js +15 -0
  106. package/dist/models/product-risk-carrier-class.d.ts +42 -0
  107. package/dist/models/product-risk-carrier-class.js +15 -0
  108. package/dist/models/product-version-account-mapping-detail-class.d.ts +50 -0
  109. package/dist/models/product-version-account-mapping-detail-class.js +15 -0
  110. package/dist/models/product-version-with-account-mappings-class.d.ts +42 -0
  111. package/dist/models/product-version-with-account-mappings-class.js +15 -0
  112. package/dist/models/product-with-account-mappings-class.d.ts +49 -0
  113. package/dist/models/product-with-account-mappings-class.js +15 -0
  114. package/dist/models/put-premium-item-account-body-dto.d.ts +24 -0
  115. package/dist/models/put-premium-item-account-body-dto.js +15 -0
  116. package/dist/models/put-product-category-account-body-dto.d.ts +24 -0
  117. package/dist/models/put-product-category-account-body-dto.js +15 -0
  118. package/dist/models/put-product-risk-carrier-body-dto.d.ts +24 -0
  119. package/dist/models/put-product-risk-carrier-body-dto.js +15 -0
  120. package/dist/models/put-product-version-category-account-body-dto.d.ts +24 -0
  121. package/dist/models/put-product-version-category-account-body-dto.js +15 -0
  122. package/dist/models/resolved-account-mapping-class.d.ts +31 -0
  123. package/dist/models/resolved-account-mapping-class.js +15 -0
  124. package/dist/models/resolved-commission-account-mapping-class.d.ts +37 -0
  125. package/dist/models/resolved-commission-account-mapping-class.js +21 -0
  126. package/dist/models/update-commission-agreement-account-body-dto.d.ts +24 -0
  127. package/dist/models/update-commission-agreement-account-body-dto.js +15 -0
  128. package/dist/models/update-commission-type-account-body-dto.d.ts +24 -0
  129. package/dist/models/update-commission-type-account-body-dto.js +15 -0
  130. package/dist/models/update-financial-account-body-dto.d.ts +48 -0
  131. package/dist/models/update-financial-account-body-dto.js +15 -0
  132. package/dist/models/update-financial-account-response-class.d.ts +25 -0
  133. package/dist/models/update-financial-account-response-class.js +15 -0
  134. package/models/account-mapping-class.ts +3 -1
  135. package/models/accounting-config-class.ts +14 -0
  136. package/models/accounting-config-data-class.ts +7 -0
  137. package/models/accounting-config-data-dto-default-account-mappings.ts +9 -1
  138. package/models/accounting-config-data-dto.ts +7 -0
  139. package/models/booking-entry-class.ts +36 -0
  140. package/models/commission-account-mapping-class.ts +55 -0
  141. package/models/commission-agreement-account-mapping-detail-class.ts +50 -0
  142. package/models/commission-agreement-partner-class.ts +36 -0
  143. package/models/commission-agreement-with-account-mapping-class.ts +49 -0
  144. package/models/commission-type-account-mapping-class.ts +37 -0
  145. package/models/create-booking-entry-request-dto.ts +25 -0
  146. package/models/create-financial-account-request-dto.ts +15 -1
  147. package/models/create-or-correct-opening-balance-dto.ts +31 -0
  148. package/models/create-or-correct-opening-balance-response-class.ts +31 -0
  149. package/models/financial-account-class.ts +31 -1
  150. package/models/financial-transaction-class.ts +6 -0
  151. package/models/financial-transaction-data-dto.ts +7 -0
  152. package/models/get-activation-snapshot-response-class.ts +31 -0
  153. package/models/index.ts +35 -0
  154. package/models/list-commission-account-mappings-response-class.ts +49 -0
  155. package/models/list-commission-agreements-with-account-mappings-response-class.ts +49 -0
  156. package/models/list-commission-type-account-mappings-response-class.ts +49 -0
  157. package/models/list-product-account-mappings-response-class.ts +49 -0
  158. package/models/list-products-with-account-mappings-response-class.ts +49 -0
  159. package/models/opening-balance-item-dto.ts +50 -0
  160. package/models/personal-account-class.ts +6 -0
  161. package/models/personal-account-range-class.ts +51 -0
  162. package/models/personal-account-range-dto.ts +51 -0
  163. package/models/post-financial-event-request-dto.ts +55 -0
  164. package/models/post-financial-event-response-class.ts +31 -0
  165. package/models/premium-item-account-mapping-class.ts +43 -0
  166. package/models/product-account-mapping-class.ts +67 -0
  167. package/models/product-account-mapping-detail-class.ts +62 -0
  168. package/models/product-risk-carrier-class.ts +48 -0
  169. package/models/product-version-account-mapping-detail-class.ts +56 -0
  170. package/models/product-version-with-account-mappings-class.ts +48 -0
  171. package/models/product-with-account-mappings-class.ts +55 -0
  172. package/models/put-premium-item-account-body-dto.ts +30 -0
  173. package/models/put-product-category-account-body-dto.ts +30 -0
  174. package/models/put-product-risk-carrier-body-dto.ts +30 -0
  175. package/models/put-product-version-category-account-body-dto.ts +30 -0
  176. package/models/resolved-account-mapping-class.ts +37 -0
  177. package/models/resolved-commission-account-mapping-class.ts +46 -0
  178. package/models/update-commission-agreement-account-body-dto.ts +30 -0
  179. package/models/update-commission-type-account-body-dto.ts +30 -0
  180. package/models/update-financial-account-body-dto.ts +54 -0
  181. package/models/update-financial-account-response-class.ts +31 -0
  182. package/package.json +2 -2
package/api.ts CHANGED
@@ -22,18 +22,22 @@ import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObj
22
22
  import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base';
23
23
  import { AccountingConfigsApi } from './api';
24
24
  import { BookingEntriesApi } from './api';
25
+ import { CommissionAccountMappingsApi } from './api';
25
26
  import { FinancialAccountsApi } from './api';
26
27
  import { FinancialTransactionsApi } from './api';
27
28
  import { HealthApi } from './api';
28
29
  import { NumberRangesApi } from './api';
29
30
  import { PersonalAccountsApi } from './api';
31
+ import { ProductAccountMappingsApi } from './api';
30
32
 
31
33
 
32
34
  export * from './api/accounting-configs-api';
33
35
  export * from './api/booking-entries-api';
36
+ export * from './api/commission-account-mappings-api';
34
37
  export * from './api/financial-accounts-api';
35
38
  export * from './api/financial-transactions-api';
36
39
  export * from './api/health-api';
37
40
  export * from './api/number-ranges-api';
38
41
  export * from './api/personal-accounts-api';
42
+ export * from './api/product-account-mappings-api';
39
43
 
package/base.ts CHANGED
@@ -32,9 +32,20 @@ export const COLLECTION_FORMATS = {
32
32
  pipes: "|",
33
33
  };
34
34
 
35
+ export interface SessionClass {
36
+ sub: string;
37
+ username?: string;
38
+ email?: string;
39
+ tenantSlug?: string;
40
+ permissions?: string;
41
+ expiresAt?: number;
42
+ }
43
+
35
44
  export interface LoginClass {
36
45
  accessToken: string;
37
46
  permissions: string;
47
+ authMode?: string;
48
+ session?: SessionClass;
38
49
  }
39
50
 
40
51
  export interface SwitchWorkspaceRequest {
@@ -45,6 +56,8 @@ export interface SwitchWorkspaceRequest {
45
56
  export interface SwitchWorkspaceResponseClass {
46
57
  accessToken: string;
47
58
  permissions: string;
59
+ authMode?: string;
60
+ session?: SessionClass;
48
61
  }
49
62
 
50
63
  export enum Environment {
@@ -59,6 +72,10 @@ export enum Environment {
59
72
  let _retry_count = 0
60
73
  let _retry = null
61
74
 
75
+ // Marks each request with the account (username) of the SDK object that sent it: only SDK objects of that
76
+ // account refresh and resend it, never an object of another account open in the same browser.
77
+ const REQUEST_OWNER = '_emilSdkOwner';
78
+
62
79
  export function resetRetry() {
63
80
  _retry_count = 0
64
81
  }
@@ -77,10 +94,19 @@ interface TokenData {
77
94
  accessToken?: string;
78
95
  username?: string;
79
96
  permissions?: string;
97
+ // 'both' or 'session' when authservice keeps the tokens in a server session.
98
+ authMode?: string;
99
+ sub?: string;
80
100
  }
81
101
 
82
102
  const NETWORK_ERROR_MESSAGE = "Network Error";
83
103
  const TOKEN_DATA = 'APP_TOKEN';
104
+ // Sent only to authservice, to ask for a server session.
105
+ const AUTH_MODE_HEADER = 'X-Emil-Auth-Mode';
106
+ // Names the account on every call; one browser session can hold several.
107
+ const ACCOUNT_HEADER = 'X-Emil-Account';
108
+ const SESSION_MODES = ['both', 'session'];
109
+ const RESEND_SAFE_METHODS = ['get', 'head', 'options'];
84
110
 
85
111
  /**
86
112
  *
@@ -98,14 +124,31 @@ export class BaseAPI {
98
124
  this.loadTokenData();
99
125
 
100
126
  if (configuration) {
127
+ // Two accounts in one browser share APP_TOKEN: never take the token or session account saved for
128
+ // another account. The mode stays (an app naming its account keeps its session). Saved with the next refresh.
129
+ const ownUser = configuration.username;
130
+ const storedUser = this.tokenData.username;
131
+ if (ownUser && storedUser && ownUser.toLowerCase() !== storedUser.toLowerCase()) {
132
+ delete this.tokenData.accessToken;
133
+ delete this.tokenData.sub;
134
+ this.tokenData.username = ownUser;
135
+ }
101
136
  const { accessToken } = this.tokenData;
102
137
  this.configuration = configuration;
103
138
  this.basePath = configuration.basePath || this.basePath;
104
139
 
105
- // Use config token if provided, otherwise use tokenData token
106
- const configToken = this.configuration.accessToken;
107
- const storedToken = accessToken ? `Bearer ${accessToken}` : '';
108
- this.configuration.accessToken = configToken || storedToken;
140
+ if (configuration.cookieAuth) {
141
+ this.configuration.username = this.configuration.username || this.tokenData.username;
142
+ }
143
+
144
+ if (this.usesSession()) {
145
+ this.useSessionAuth();
146
+ } else {
147
+ // Use config token if provided, otherwise use tokenData token
148
+ const configToken = this.configuration.accessToken;
149
+ const storedToken = accessToken ? `Bearer ${accessToken}` : '';
150
+ this.configuration.accessToken = configToken || storedToken;
151
+ }
109
152
  } else {
110
153
  const { accessToken, username } = this.tokenData;
111
154
 
@@ -119,6 +162,110 @@ export class BaseAPI {
119
162
  this.attachInterceptor(axios);
120
163
  }
121
164
 
165
+ // The app's own account wins over APP_TOKEN, which holds whichever tab wrote last.
166
+ private sessionAccount(): string | undefined {
167
+ return this.configuration.account || this.tokenData.sub;
168
+ }
169
+
170
+ // Apps may pass the address as the second argument only; authservice is on the same host.
171
+ private authBasePath(): string {
172
+ return this.configuration.basePath || this.basePath;
173
+ }
174
+
175
+ private usesSession(): boolean {
176
+ return !!this.configuration.cookieAuth && SESSION_MODES.indexOf(this.tokenData.authMode) !== -1 && !!this.sessionAccount();
177
+ }
178
+
179
+ private useSessionAuth() {
180
+ const { headers, ...baseOptions } = this.configuration.baseOptions || {};
181
+ this.configuration.accessToken = '';
182
+ this.configuration.baseOptions = {
183
+ ...baseOptions,
184
+ withCredentials: true,
185
+ headers: { ...headers, [ACCOUNT_HEADER]: this.sessionAccount() },
186
+ };
187
+ }
188
+
189
+ private useHeaderAuth(accessToken: string, keepSessionData = false) {
190
+ this.configuration.accessToken = accessToken ? `Bearer ${accessToken}` : '';
191
+ this.tokenData.accessToken = accessToken;
192
+ // An app without cookieAuth does not own the session fields: its refresh leaves them for the session apps.
193
+ if (!keepSessionData || this.configuration.cookieAuth) {
194
+ delete this.tokenData.authMode;
195
+ delete this.tokenData.sub;
196
+ }
197
+ if (this.configuration.cookieAuth) {
198
+ const { withCredentials, headers, ...baseOptions } = this.configuration.baseOptions || {};
199
+ const { [ACCOUNT_HEADER]: account, ...otherHeaders } = headers || {};
200
+ this.configuration.baseOptions = { ...baseOptions, headers: otherHeaders };
201
+ }
202
+ }
203
+
204
+ private applyTokenReply({ accessToken, authMode, session }: LoginClass, fromRefresh = false) {
205
+ if (this.configuration.cookieAuth && SESSION_MODES.indexOf(authMode) !== -1 && session?.sub) {
206
+ this.tokenData.authMode = authMode;
207
+ this.tokenData.sub = session.sub;
208
+ if (this.configuration.account) {
209
+ // A workspace switch moves the app to another account.
210
+ this.configuration.account = session.sub;
211
+ }
212
+ // Both keeps the token for apps not moved yet; session mode keeps no token at all.
213
+ if (authMode === 'both' && accessToken) {
214
+ this.tokenData.accessToken = accessToken;
215
+ } else {
216
+ delete this.tokenData.accessToken;
217
+ }
218
+ this.useSessionAuth();
219
+ } else {
220
+ this.useHeaderAuth(accessToken, fromRefresh);
221
+ }
222
+ }
223
+
224
+ private sessionHeaders(): Record<string, string> {
225
+ if (!this.configuration.cookieAuth) {
226
+ return {};
227
+ }
228
+ return {
229
+ [AUTH_MODE_HEADER]: 'session',
230
+ ...(this.usesSession() && { [ACCOUNT_HEADER]: this.sessionAccount() }),
231
+ };
232
+ }
233
+
234
+ private withCurrentAuth(config: AxiosRequestConfig): AxiosRequestConfig {
235
+ delete config.headers['Authorization'];
236
+ delete config.headers[ACCOUNT_HEADER];
237
+ if (this.usesSession()) {
238
+ config.headers[ACCOUNT_HEADER] = this.sessionAccount();
239
+ config.withCredentials = true;
240
+ } else if (this.configuration.accessToken) {
241
+ config.headers['Authorization'] = this.configuration.accessToken;
242
+ }
243
+ return config;
244
+ }
245
+
246
+ // Both keeps the token, so a session call the browser could not complete can be sent again with it.
247
+ private canRetryWithToken(config: AxiosRequestConfig): boolean {
248
+ return !!config.withCredentials && !!config.headers?.[ACCOUNT_HEADER]
249
+ && !!this.tokenData.accessToken && this.tokenData.authMode !== 'session';
250
+ }
251
+
252
+ // A network error can come after the server ran the call, so only a call that is safe to repeat is sent again.
253
+ private canResend(config: AxiosRequestConfig): boolean {
254
+ return RESEND_SAFE_METHODS.indexOf((config.method || 'get').toLowerCase()) !== -1
255
+ || Object.keys(config.headers || {}).some((name) => name.toLowerCase() === 'idempotency-key');
256
+ }
257
+
258
+ private switchToToken() {
259
+ this.useHeaderAuth(this.tokenData.accessToken);
260
+ this.storeTokenData(this.tokenData);
261
+ }
262
+
263
+ private retryWithToken(axios: AxiosInstance, config: AxiosRequestConfig) {
264
+ this.switchToToken();
265
+ config.withCredentials = false;
266
+ return axios(this.withCurrentAuth(config));
267
+ }
268
+
122
269
  selectEnvironment(env: Environment) {
123
270
  this.selectBasePath(env);
124
271
  }
@@ -138,8 +285,8 @@ export class BaseAPI {
138
285
  async authorize(username: string, password: string, targetWorkspace?: string): Promise<void> {
139
286
  const options: AxiosRequestConfig = {
140
287
  method: 'POST',
141
- url: `${this.configuration.basePath}/authservice/v1/login`,
142
- headers: { 'Content-Type': 'application/json' },
288
+ url: `${this.authBasePath()}/authservice/v1/login`,
289
+ headers: { 'Content-Type': 'application/json', ...(this.configuration.cookieAuth && { [AUTH_MODE_HEADER]: 'session' }) },
143
290
  data: {
144
291
  username,
145
292
  password,
@@ -149,13 +296,10 @@ export class BaseAPI {
149
296
 
150
297
  const response = await globalAxios.request<LoginClass>(options);
151
298
 
152
- const { data: { accessToken, permissions } } = response;
153
-
154
299
  this.configuration.username = username;
155
- this.configuration.accessToken = `Bearer ${accessToken}`;
156
300
  this.tokenData.username = username;
157
- this.tokenData.accessToken = accessToken;
158
- this.tokenData.permissions = permissions;
301
+ this.tokenData.permissions = response.data.permissions;
302
+ this.applyTokenReply(response.data);
159
303
 
160
304
  // Switch workspace if provided
161
305
  if (targetWorkspace) {
@@ -175,10 +319,11 @@ export class BaseAPI {
175
319
  async switchWorkspace(targetWorkspace: string): Promise<void> {
176
320
  const options: AxiosRequestConfig = {
177
321
  method: 'POST',
178
- url: `${this.configuration.basePath}/authservice/v1/workspaces/switch`,
322
+ url: `${this.authBasePath()}/authservice/v1/workspaces/switch`,
179
323
  headers: {
180
324
  'Content-Type': 'application/json',
181
- 'Authorization': `Bearer ${this.configuration.accessToken}`,
325
+ ...this.sessionHeaders(),
326
+ ...(!this.usesSession() && !!this.configuration.accessToken && { 'Authorization': this.configuration.accessToken as string }),
182
327
  },
183
328
  data: {
184
329
  username: this.configuration.username,
@@ -189,11 +334,8 @@ export class BaseAPI {
189
334
 
190
335
  const response = await globalAxios.request<SwitchWorkspaceResponseClass>(options);
191
336
 
192
- const { data: { accessToken, permissions } } = response;
193
-
194
- this.configuration.accessToken = `Bearer ${accessToken}`;
195
- this.tokenData.accessToken = accessToken;
196
- this.tokenData.permissions = permissions;
337
+ this.tokenData.permissions = response.data.permissions;
338
+ this.applyTokenReply(response.data);
197
339
 
198
340
  this.storeTokenData({
199
341
  ...this.tokenData
@@ -209,9 +351,34 @@ export class BaseAPI {
209
351
 
210
352
  const refreshTokenAxios = globalAxios.create()
211
353
  refreshTokenAxios.interceptors.response.use(response => {
212
- const { permissions } = response.data;
213
-
354
+ const { permissions, accessToken, authMode } = response.data;
355
+ // Decided before the merge below: newer session data must not keep the new token from this object.
356
+ const onSession = this.usesSession();
357
+
358
+ // Newer data saved for this account (e.g. the session mode the shell saved later) wins over the copy
359
+ // read when the object was made; data of another account or another workspace (sub) is never taken.
360
+ const latest = ((typeof window !== 'undefined' && defaultStorage().get<TokenData>(TOKEN_DATA)) || {}) as TokenData;
361
+ const ownSub = this.sessionAccount();
362
+ if (latest.username?.toLowerCase() === username.toLowerCase()
363
+ && (!ownSub || !latest.sub || latest.sub === ownSub)) {
364
+ // A bare reset ({username}, an app's account check) keeps this copy's other fields; any fuller record
365
+ // is the truth, so fields it lacks were removed on purpose (e.g. a rollback) and stay removed.
366
+ const bareReset = Object.keys(latest).every((key) => key === 'username');
367
+ this.tokenData = bareReset ? { ...this.tokenData, ...latest } : { ...latest };
368
+ }
369
+ // The reply is for this account: label it, or an SDK object of another account would take the token.
370
+ if (this.tokenData.username?.toLowerCase() !== username.toLowerCase()) {
371
+ this.tokenData.username = username;
372
+ }
214
373
  this.tokenData.permissions = permissions;
374
+ // Keep the new token for the page's next SDK objects; session data is left as it is.
375
+ if (accessToken && !onSession) {
376
+ this.tokenData.accessToken = accessToken;
377
+ this.configuration.accessToken = `Bearer ${accessToken}`;
378
+ } else if (accessToken && authMode === 'both') {
379
+ // Both: this app stays on the session; the token is kept for apps not moved yet.
380
+ this.tokenData.accessToken = accessToken;
381
+ }
215
382
  this.storeTokenData(this.tokenData);
216
383
 
217
384
  return response;
@@ -219,9 +386,10 @@ export class BaseAPI {
219
386
 
220
387
  const options: AxiosRequestConfig = {
221
388
  method: 'POST',
222
- url: `${this.configuration.basePath}/authservice/v1/refresh-token`,
389
+ url: `${this.authBasePath()}/authservice/v1/refresh-token`,
223
390
  headers: {
224
391
  'Content-Type': 'application/json',
392
+ ...this.sessionHeaders(),
225
393
  },
226
394
  data: { username: username },
227
395
  withCredentials: true,
@@ -249,6 +417,18 @@ export class BaseAPI {
249
417
  this.storeTokenData(null);
250
418
  }
251
419
 
420
+ private ownAccount(): string {
421
+ return (this.configuration.username || '').toLowerCase();
422
+ }
423
+
424
+ protected withOwner(options?: AxiosRequestConfig): AxiosRequestConfig {
425
+ // An object without a username cannot refresh itself: its requests stay unmarked, as before.
426
+ if (!this.ownAccount()) {
427
+ return { ...options };
428
+ }
429
+ return { ...options, [REQUEST_OWNER]: this.ownAccount() } as AxiosRequestConfig;
430
+ }
431
+
252
432
  private attachInterceptor(axios: AxiosInstance) {
253
433
  axios.interceptors.response.use(
254
434
  (res) => {
@@ -256,26 +436,33 @@ export class BaseAPI {
256
436
  },
257
437
  async (err) => {
258
438
  let originalConfig = err.config;
439
+ // Sent for another account: only that account's SDK objects handle it. Unmarked requests as before.
440
+ const owner = originalConfig?.[REQUEST_OWNER];
441
+ if (owner !== undefined && owner !== this.ownAccount()) {
442
+ return Promise.reject(err);
443
+ }
259
444
  if (err.response && !(err.response instanceof XMLHttpRequest)) { // sometimes buggy and is of type request
260
445
  // Access Token was expired
261
446
  if ((err.response.status === 401 || err.response.status === 403)
262
447
  && !originalConfig._retry) {
263
448
  originalConfig._retry = true;
264
449
  try {
265
- const { accessToken: tokenString, permissions } = await this.refreshTokenInternal();
266
- const accessToken = `Bearer ${tokenString}`;
267
- this.tokenData.permissions = permissions;
268
-
269
- delete originalConfig.headers['Authorization']
270
-
271
- originalConfig.headers['Authorization'] = accessToken;
272
-
273
- this.configuration.accessToken = accessToken;
274
- this.tokenData.accessToken = tokenString;
275
-
450
+ const reply = await this.refreshTokenInternal();
451
+ this.tokenData.permissions = reply.permissions;
452
+ this.applyTokenReply(reply, true);
276
453
  this.storeTokenData(this.tokenData);
277
454
 
278
- return axios(originalConfig);
455
+ if (!this.usesSession()) {
456
+ return axios(this.withCurrentAuth(originalConfig));
457
+ }
458
+
459
+ // Both mode: if the browser does not send the session cookie (another site), use the token.
460
+ return axios(this.withCurrentAuth(originalConfig)).catch((retryError) => {
461
+ if (retryError?.response?.status !== 401 || !this.tokenData.accessToken) {
462
+ return Promise.reject(retryError);
463
+ }
464
+ return this.retryWithToken(axios, originalConfig);
465
+ });
279
466
  } catch (_error) {
280
467
  if (_error.response && _error.response.data) {
281
468
  return Promise.reject(_error.response.data);
@@ -283,26 +470,35 @@ export class BaseAPI {
283
470
  return Promise.reject(_error);
284
471
  }
285
472
  }
473
+ } else if (err.message === NETWORK_ERROR_MESSAGE && this.canRetryWithToken(originalConfig)) {
474
+ // Both mode: the browser blocked the session call or hid its reply; later calls use the token.
475
+ if (!this.canResend(originalConfig)) {
476
+ this.switchToToken();
477
+ return Promise.reject(err);
478
+ }
479
+ return this.retryWithToken(axios, originalConfig);
286
480
  } else if (err.message === NETWORK_ERROR_MESSAGE
287
- && originalConfig.headers.hasOwnProperty('Authorization')
481
+ && (originalConfig.headers.hasOwnProperty('Authorization')
482
+ || (this.configuration.cookieAuth && originalConfig.withCredentials && this.canResend(originalConfig)))
288
483
  && _retry_count < 4
289
484
  ) {
290
485
  _retry_count++;
291
486
  try {
292
- const { accessToken: tokenString, permissions } = await this.refreshTokenInternal();
293
- const accessToken = `Bearer ${tokenString}`;
294
- this.tokenData.permissions = permissions;
487
+ // In session mode an unreadable 401 (no CORS headers on the error reply) also lands here.
488
+ const reply = await this.refreshTokenInternal();
489
+ this.tokenData.permissions = reply.permissions;
295
490
 
296
491
  _retry = true;
297
- originalConfig.headers['Authorization'] = accessToken;
298
-
299
- this.configuration.accessToken = accessToken;
300
- this.tokenData.accessToken = tokenString;
301
-
492
+ this.applyTokenReply(reply, true);
302
493
  this.storeTokenData(this.tokenData);
303
494
 
495
+ // The limit is for refreshes that do not help; one that worked starts the count again.
496
+ // Not awaited, so a failed retry reaches the app as the same axios error as before.
304
497
  return axios.request({
305
- ...originalConfig,
498
+ ...this.withCurrentAuth(originalConfig),
499
+ }).then((retried) => {
500
+ _retry_count = 0;
501
+ return retried;
306
502
  });
307
503
  } catch (_error) {
308
504
  if (_error.response && _error.response.data) {
package/common.ts CHANGED
@@ -130,9 +130,10 @@ export const toPathString = function (url: URL) {
130
130
  * @export
131
131
  */
132
132
  export const createRequestFunction = function (axiosArgs: RequestArgs, globalAxios: AxiosInstance, BASE_PATH: string, configuration?: Configuration) {
133
- return <T = unknown, R = AxiosResponse<T>>(axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
133
+ // Typed by hand: since axios 1.20, request() returns an internal axios type that SDK declarations cannot name.
134
+ return <T = unknown, R = AxiosResponse<T>>(axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH): Promise<R> => {
134
135
  const axiosRequestArgs = {...axiosArgs.options, url: (configuration?.basePath || basePath) + axiosArgs.url};
135
- return axios.request<T, R>(axiosRequestArgs);
136
+ return axios.request<T, R>(axiosRequestArgs) as Promise<R>;
136
137
  };
137
138
  }
138
139
 
package/configuration.ts CHANGED
@@ -21,6 +21,8 @@ export interface ConfigurationParameters {
21
21
  basePath?: string;
22
22
  baseOptions?: any;
23
23
  formDataCtor?: new () => any;
24
+ cookieAuth?: boolean;
25
+ account?: string;
24
26
  }
25
27
 
26
28
  export class Configuration {
@@ -74,6 +76,22 @@ export class Configuration {
74
76
  */
75
77
  formDataCtor?: new () => any;
76
78
 
79
+ /**
80
+ * Use a server session when authservice makes one; otherwise the token, as without this option.
81
+ * Set `account` too, so each SDK object keeps its own account.
82
+ *
83
+ * @type {boolean}
84
+ * @memberof Configuration
85
+ */
86
+ cookieAuth?: boolean;
87
+ /**
88
+ * The session account (Cognito sub) this app shows, so the SDK never takes another tab's account.
89
+ *
90
+ * @type {string}
91
+ * @memberof Configuration
92
+ */
93
+ account?: string;
94
+
77
95
  constructor(param: ConfigurationParameters = {}) {
78
96
  this.apiKey = param.apiKey;
79
97
  this.username = param.username;
@@ -82,6 +100,8 @@ export class Configuration {
82
100
  this.basePath = param.basePath;
83
101
  this.baseOptions = param.baseOptions;
84
102
  this.formDataCtor = param.formDataCtor;
103
+ this.cookieAuth = param.cookieAuth;
104
+ this.account = param.account;
85
105
  }
86
106
 
87
107
  /**
@@ -16,6 +16,7 @@ import { CreateAndEnableSubledgerRequestDto } from '../models';
16
16
  import { CreateAndEnableSubledgerResponseClass } from '../models';
17
17
  import { ExportAccountingConfigResponseClass } from '../models';
18
18
  import { GetAccountingCategoriesResponseClass } from '../models';
19
+ import { GetActivationSnapshotResponseClass } from '../models';
19
20
  import { ValidateAccountingConfigRequestDto } from '../models';
20
21
  import { ValidateAccountingConfigResponseClass } from '../models';
21
22
  /**
@@ -57,6 +58,14 @@ export declare const AccountingConfigsApiAxiosParamCreator: (configuration?: Con
57
58
  * @throws {RequiredError}
58
59
  */
59
60
  getAccountingCategories: (authorization?: string, options?: AxiosRequestConfig) => Promise<RequestArgs>;
61
+ /**
62
+ * Returns the subledger configuration as it was when the subledger was enabled, along with who created it, when, and its enablement status. **Required Permissions** \"accounting-management.financial-accounts.view\"
63
+ * @summary Retrieve the get activation snapshot
64
+ * @param {string} [authorization] Bearer Token: provided by the login endpoint under the name accessToken.
65
+ * @param {*} [options] Override http request option.
66
+ * @throws {RequiredError}
67
+ */
68
+ getActivationSnapshot: (authorization?: string, options?: AxiosRequestConfig) => Promise<RequestArgs>;
60
69
  /**
61
70
  * This will reset the subledger, and remove all config and accounts. **Required Permissions** \"accounting-management.financial-accounts.delete\"
62
71
  * @summary Delete the reset subledger
@@ -123,6 +132,14 @@ export declare const AccountingConfigsApiFp: (configuration?: Configuration) =>
123
132
  * @throws {RequiredError}
124
133
  */
125
134
  getAccountingCategories(authorization?: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetAccountingCategoriesResponseClass>>;
135
+ /**
136
+ * Returns the subledger configuration as it was when the subledger was enabled, along with who created it, when, and its enablement status. **Required Permissions** \"accounting-management.financial-accounts.view\"
137
+ * @summary Retrieve the get activation snapshot
138
+ * @param {string} [authorization] Bearer Token: provided by the login endpoint under the name accessToken.
139
+ * @param {*} [options] Override http request option.
140
+ * @throws {RequiredError}
141
+ */
142
+ getActivationSnapshot(authorization?: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetActivationSnapshotResponseClass>>;
126
143
  /**
127
144
  * This will reset the subledger, and remove all config and accounts. **Required Permissions** \"accounting-management.financial-accounts.delete\"
128
145
  * @summary Delete the reset subledger
@@ -189,6 +206,14 @@ export declare const AccountingConfigsApiFactory: (configuration?: Configuration
189
206
  * @throws {RequiredError}
190
207
  */
191
208
  getAccountingCategories(authorization?: string, options?: any): AxiosPromise<GetAccountingCategoriesResponseClass>;
209
+ /**
210
+ * Returns the subledger configuration as it was when the subledger was enabled, along with who created it, when, and its enablement status. **Required Permissions** \"accounting-management.financial-accounts.view\"
211
+ * @summary Retrieve the get activation snapshot
212
+ * @param {string} [authorization] Bearer Token: provided by the login endpoint under the name accessToken.
213
+ * @param {*} [options] Override http request option.
214
+ * @throws {RequiredError}
215
+ */
216
+ getActivationSnapshot(authorization?: string, options?: any): AxiosPromise<GetActivationSnapshotResponseClass>;
192
217
  /**
193
218
  * This will reset the subledger, and remove all config and accounts. **Required Permissions** \"accounting-management.financial-accounts.delete\"
194
219
  * @summary Delete the reset subledger
@@ -280,6 +305,19 @@ export interface AccountingConfigsApiGetAccountingCategoriesRequest {
280
305
  */
281
306
  readonly authorization?: string;
282
307
  }
308
+ /**
309
+ * Request parameters for getActivationSnapshot operation in AccountingConfigsApi.
310
+ * @export
311
+ * @interface AccountingConfigsApiGetActivationSnapshotRequest
312
+ */
313
+ export interface AccountingConfigsApiGetActivationSnapshotRequest {
314
+ /**
315
+ * Bearer Token: provided by the login endpoint under the name accessToken.
316
+ * @type {string}
317
+ * @memberof AccountingConfigsApiGetActivationSnapshot
318
+ */
319
+ readonly authorization?: string;
320
+ }
283
321
  /**
284
322
  * Request parameters for resetSubledger operation in AccountingConfigsApi.
285
323
  * @export
@@ -346,7 +384,7 @@ export declare class AccountingConfigsApi extends BaseAPI {
346
384
  * @throws {RequiredError}
347
385
  * @memberof AccountingConfigsApi
348
386
  */
349
- createAndEnableSubledger(requestParameters: AccountingConfigsApiCreateAndEnableSubledgerRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<CreateAndEnableSubledgerResponseClass, any, {}>>;
387
+ createAndEnableSubledger(requestParameters: AccountingConfigsApiCreateAndEnableSubledgerRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<CreateAndEnableSubledgerResponseClass, any, {}, any>>;
350
388
  /**
351
389
  * This will create and enable the subledger, using an accounting config from a file. **Required Permissions** \"accounting-management.financial-accounts.create\"
352
390
  * @summary Create the create and enable subledger from file
@@ -355,7 +393,7 @@ export declare class AccountingConfigsApi extends BaseAPI {
355
393
  * @throws {RequiredError}
356
394
  * @memberof AccountingConfigsApi
357
395
  */
358
- createAndEnableSubledgerFromFile(requestParameters: AccountingConfigsApiCreateAndEnableSubledgerFromFileRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<CreateAndEnableSubledgerResponseClass, any, {}>>;
396
+ createAndEnableSubledgerFromFile(requestParameters: AccountingConfigsApiCreateAndEnableSubledgerFromFileRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<CreateAndEnableSubledgerResponseClass, any, {}, any>>;
359
397
  /**
360
398
  * This will export the accounting config. **Required Permissions** \"accounting-management.financial-accounts.view\"
361
399
  * @summary Retrieve the export accounting config
@@ -364,7 +402,7 @@ export declare class AccountingConfigsApi extends BaseAPI {
364
402
  * @throws {RequiredError}
365
403
  * @memberof AccountingConfigsApi
366
404
  */
367
- exportAccountingConfig(requestParameters?: AccountingConfigsApiExportAccountingConfigRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<ExportAccountingConfigResponseClass, any, {}>>;
405
+ exportAccountingConfig(requestParameters?: AccountingConfigsApiExportAccountingConfigRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<ExportAccountingConfigResponseClass, any, {}, any>>;
368
406
  /**
369
407
  * This will get the accounting categories. **Required Permissions** \"accounting-management.financial-accounts.view\"
370
408
  * @summary Retrieve the accounting categories
@@ -373,7 +411,16 @@ export declare class AccountingConfigsApi extends BaseAPI {
373
411
  * @throws {RequiredError}
374
412
  * @memberof AccountingConfigsApi
375
413
  */
376
- getAccountingCategories(requestParameters?: AccountingConfigsApiGetAccountingCategoriesRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<GetAccountingCategoriesResponseClass, any, {}>>;
414
+ getAccountingCategories(requestParameters?: AccountingConfigsApiGetAccountingCategoriesRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<GetAccountingCategoriesResponseClass, any, {}, any>>;
415
+ /**
416
+ * Returns the subledger configuration as it was when the subledger was enabled, along with who created it, when, and its enablement status. **Required Permissions** \"accounting-management.financial-accounts.view\"
417
+ * @summary Retrieve the get activation snapshot
418
+ * @param {AccountingConfigsApiGetActivationSnapshotRequest} requestParameters Request parameters.
419
+ * @param {*} [options] Override http request option.
420
+ * @throws {RequiredError}
421
+ * @memberof AccountingConfigsApi
422
+ */
423
+ getActivationSnapshot(requestParameters?: AccountingConfigsApiGetActivationSnapshotRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<GetActivationSnapshotResponseClass, any, {}, any>>;
377
424
  /**
378
425
  * This will reset the subledger, and remove all config and accounts. **Required Permissions** \"accounting-management.financial-accounts.delete\"
379
426
  * @summary Delete the reset subledger
@@ -382,7 +429,7 @@ export declare class AccountingConfigsApi extends BaseAPI {
382
429
  * @throws {RequiredError}
383
430
  * @memberof AccountingConfigsApi
384
431
  */
385
- resetSubledger(requestParameters?: AccountingConfigsApiResetSubledgerRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<void, any, {}>>;
432
+ resetSubledger(requestParameters?: AccountingConfigsApiResetSubledgerRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<void, any, {}, any>>;
386
433
  /**
387
434
  * This will validate accounting config. **Required Permissions** \"accounting-management.financial-accounts.create\"
388
435
  * @summary Create the validate accounting config
@@ -391,7 +438,7 @@ export declare class AccountingConfigsApi extends BaseAPI {
391
438
  * @throws {RequiredError}
392
439
  * @memberof AccountingConfigsApi
393
440
  */
394
- validateAccountingConfig(requestParameters: AccountingConfigsApiValidateAccountingConfigRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<ValidateAccountingConfigResponseClass, any, {}>>;
441
+ validateAccountingConfig(requestParameters: AccountingConfigsApiValidateAccountingConfigRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<ValidateAccountingConfigResponseClass, any, {}, any>>;
395
442
  /**
396
443
  * This will validate accounting config from a file. **Required Permissions** \"accounting-management.financial-accounts.create\"
397
444
  * @summary Create the validate accounting config from file
@@ -400,5 +447,5 @@ export declare class AccountingConfigsApi extends BaseAPI {
400
447
  * @throws {RequiredError}
401
448
  * @memberof AccountingConfigsApi
402
449
  */
403
- validateAccountingConfigFromFile(requestParameters: AccountingConfigsApiValidateAccountingConfigFromFileRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<ValidateAccountingConfigResponseClass, any, {}>>;
450
+ validateAccountingConfigFromFile(requestParameters: AccountingConfigsApiValidateAccountingConfigFromFileRequest, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<ValidateAccountingConfigResponseClass, any, {}, any>>;
404
451
  }