@emilgroup/notification-sdk 0.1.1-beta.0

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 (160) hide show
  1. package/.openapi-generator/FILES +58 -0
  2. package/.openapi-generator/VERSION +1 -0
  3. package/.openapi-generator-ignore +23 -0
  4. package/README.md +51 -0
  5. package/api/default-api.ts +124 -0
  6. package/api/email-messages-api.ts +368 -0
  7. package/api/email-tracking-api.ts +537 -0
  8. package/api/email-verifications-api.ts +264 -0
  9. package/api/layouts-api.ts +673 -0
  10. package/api/notification-templates-api.ts +687 -0
  11. package/api/notifications-api.ts +161 -0
  12. package/api.ts +39 -0
  13. package/base.ts +331 -0
  14. package/common.ts +198 -0
  15. package/configuration.ts +110 -0
  16. package/dist/api/default-api.d.ts +70 -0
  17. package/dist/api/default-api.js +200 -0
  18. package/dist/api/email-messages-api.d.ts +205 -0
  19. package/dist/api/email-messages-api.js +401 -0
  20. package/dist/api/email-tracking-api.d.ts +301 -0
  21. package/dist/api/email-tracking-api.js +520 -0
  22. package/dist/api/email-verifications-api.d.ts +146 -0
  23. package/dist/api/email-verifications-api.js +309 -0
  24. package/dist/api/layouts-api.d.ts +373 -0
  25. package/dist/api/layouts-api.js +624 -0
  26. package/dist/api/notification-templates-api.d.ts +382 -0
  27. package/dist/api/notification-templates-api.js +630 -0
  28. package/dist/api/notifications-api.d.ts +93 -0
  29. package/dist/api/notifications-api.js +220 -0
  30. package/dist/api.d.ts +18 -0
  31. package/dist/api.js +36 -0
  32. package/dist/base.d.ts +86 -0
  33. package/dist/base.js +367 -0
  34. package/dist/common.d.ts +91 -0
  35. package/dist/common.js +276 -0
  36. package/dist/configuration.d.ts +89 -0
  37. package/dist/configuration.js +52 -0
  38. package/dist/index.d.ts +15 -0
  39. package/dist/index.js +36 -0
  40. package/dist/models/complete-email-verification-dto.d.ts +30 -0
  41. package/dist/models/complete-email-verification-dto.js +15 -0
  42. package/dist/models/complete-email-verification-response-class.d.ts +24 -0
  43. package/dist/models/complete-email-verification-response-class.js +15 -0
  44. package/dist/models/create-layout-request-dto.d.ts +48 -0
  45. package/dist/models/create-layout-request-dto.js +15 -0
  46. package/dist/models/create-layout-response-class.d.ts +25 -0
  47. package/dist/models/create-layout-response-class.js +15 -0
  48. package/dist/models/create-notification-template-request-dto.d.ts +66 -0
  49. package/dist/models/create-notification-template-request-dto.js +15 -0
  50. package/dist/models/create-notification-template-response-class.d.ts +25 -0
  51. package/dist/models/create-notification-template-response-class.js +15 -0
  52. package/dist/models/delete-layout-request-dto.d.ts +24 -0
  53. package/dist/models/delete-layout-request-dto.js +15 -0
  54. package/dist/models/delete-notification-template-request-dto.d.ts +24 -0
  55. package/dist/models/delete-notification-template-request-dto.js +15 -0
  56. package/dist/models/download-attachment-response-class.d.ts +36 -0
  57. package/dist/models/download-attachment-response-class.js +15 -0
  58. package/dist/models/email-attachment-class.d.ts +84 -0
  59. package/dist/models/email-attachment-class.js +15 -0
  60. package/dist/models/email-message-class.d.ts +140 -0
  61. package/dist/models/email-message-class.js +27 -0
  62. package/dist/models/email-thread-class.d.ts +83 -0
  63. package/dist/models/email-thread-class.js +20 -0
  64. package/dist/models/get-email-message-response-class.d.ts +25 -0
  65. package/dist/models/get-email-message-response-class.js +15 -0
  66. package/dist/models/get-email-thread-response-class.d.ts +25 -0
  67. package/dist/models/get-email-thread-response-class.js +15 -0
  68. package/dist/models/get-layout-request-dto.d.ts +24 -0
  69. package/dist/models/get-layout-request-dto.js +15 -0
  70. package/dist/models/get-layout-response-class.d.ts +25 -0
  71. package/dist/models/get-layout-response-class.js +15 -0
  72. package/dist/models/get-notification-template-request-dto.d.ts +30 -0
  73. package/dist/models/get-notification-template-request-dto.js +15 -0
  74. package/dist/models/get-notification-template-response-class.d.ts +25 -0
  75. package/dist/models/get-notification-template-response-class.js +15 -0
  76. package/dist/models/html-template-class.d.ts +54 -0
  77. package/dist/models/html-template-class.js +15 -0
  78. package/dist/models/index.d.ts +38 -0
  79. package/dist/models/index.js +54 -0
  80. package/dist/models/initiate-email-verification-dto.d.ts +30 -0
  81. package/dist/models/initiate-email-verification-dto.js +15 -0
  82. package/dist/models/initiate-email-verification-response-class.d.ts +24 -0
  83. package/dist/models/initiate-email-verification-response-class.js +15 -0
  84. package/dist/models/inline-response200.d.ts +54 -0
  85. package/dist/models/inline-response200.js +15 -0
  86. package/dist/models/inline-response503.d.ts +54 -0
  87. package/dist/models/inline-response503.js +15 -0
  88. package/dist/models/layout-class.d.ts +73 -0
  89. package/dist/models/layout-class.js +15 -0
  90. package/dist/models/list-email-attachments-response-class.d.ts +25 -0
  91. package/dist/models/list-email-attachments-response-class.js +15 -0
  92. package/dist/models/list-email-messages-response-class.d.ts +43 -0
  93. package/dist/models/list-email-messages-response-class.js +15 -0
  94. package/dist/models/list-email-threads-response-class.d.ts +43 -0
  95. package/dist/models/list-email-threads-response-class.js +15 -0
  96. package/dist/models/list-layouts-response-class.d.ts +30 -0
  97. package/dist/models/list-layouts-response-class.js +15 -0
  98. package/dist/models/list-notification-templates-response-class.d.ts +30 -0
  99. package/dist/models/list-notification-templates-response-class.js +15 -0
  100. package/dist/models/notification-template-class.d.ts +98 -0
  101. package/dist/models/notification-template-class.js +15 -0
  102. package/dist/models/s3-document-dto.d.ts +36 -0
  103. package/dist/models/s3-document-dto.js +15 -0
  104. package/dist/models/send-notification-request-dto.d.ts +115 -0
  105. package/dist/models/send-notification-request-dto.js +21 -0
  106. package/dist/models/send-notification-response-class.d.ts +24 -0
  107. package/dist/models/send-notification-response-class.js +15 -0
  108. package/dist/models/update-html-template-request-dto.d.ts +42 -0
  109. package/dist/models/update-html-template-request-dto.js +21 -0
  110. package/dist/models/update-layout-request-dto.d.ts +55 -0
  111. package/dist/models/update-layout-request-dto.js +15 -0
  112. package/dist/models/update-layout-response-class.d.ts +25 -0
  113. package/dist/models/update-layout-response-class.js +15 -0
  114. package/dist/models/update-notification-template-request-dto.d.ts +73 -0
  115. package/dist/models/update-notification-template-request-dto.js +15 -0
  116. package/dist/models/update-notification-template-response-class.d.ts +25 -0
  117. package/dist/models/update-notification-template-response-class.js +15 -0
  118. package/git_push.sh +57 -0
  119. package/index.ts +19 -0
  120. package/models/complete-email-verification-dto.ts +36 -0
  121. package/models/complete-email-verification-response-class.ts +30 -0
  122. package/models/create-layout-request-dto.ts +54 -0
  123. package/models/create-layout-response-class.ts +31 -0
  124. package/models/create-notification-template-request-dto.ts +72 -0
  125. package/models/create-notification-template-response-class.ts +31 -0
  126. package/models/delete-layout-request-dto.ts +30 -0
  127. package/models/delete-notification-template-request-dto.ts +30 -0
  128. package/models/download-attachment-response-class.ts +42 -0
  129. package/models/email-attachment-class.ts +90 -0
  130. package/models/email-message-class.ts +150 -0
  131. package/models/email-thread-class.ts +92 -0
  132. package/models/get-email-message-response-class.ts +31 -0
  133. package/models/get-email-thread-response-class.ts +31 -0
  134. package/models/get-layout-request-dto.ts +30 -0
  135. package/models/get-layout-response-class.ts +31 -0
  136. package/models/get-notification-template-request-dto.ts +36 -0
  137. package/models/get-notification-template-response-class.ts +31 -0
  138. package/models/html-template-class.ts +60 -0
  139. package/models/index.ts +38 -0
  140. package/models/initiate-email-verification-dto.ts +36 -0
  141. package/models/initiate-email-verification-response-class.ts +30 -0
  142. package/models/inline-response200.ts +48 -0
  143. package/models/inline-response503.ts +48 -0
  144. package/models/layout-class.ts +79 -0
  145. package/models/list-email-attachments-response-class.ts +31 -0
  146. package/models/list-email-messages-response-class.ts +49 -0
  147. package/models/list-email-threads-response-class.ts +49 -0
  148. package/models/list-layouts-response-class.ts +36 -0
  149. package/models/list-notification-templates-response-class.ts +36 -0
  150. package/models/notification-template-class.ts +104 -0
  151. package/models/s3-document-dto.ts +42 -0
  152. package/models/send-notification-request-dto.ts +124 -0
  153. package/models/send-notification-response-class.ts +30 -0
  154. package/models/update-html-template-request-dto.ts +51 -0
  155. package/models/update-layout-request-dto.ts +61 -0
  156. package/models/update-layout-response-class.ts +31 -0
  157. package/models/update-notification-template-request-dto.ts +79 -0
  158. package/models/update-notification-template-response-class.ts +31 -0
  159. package/package.json +27 -0
  160. package/tsconfig.json +23 -0
@@ -0,0 +1,161 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * EMIL NotificationService
5
+ * The EMIL NotificationService API description
6
+ *
7
+ * The version of the OpenAPI document: 1.0
8
+ * Contact: kontakt@emil.de
9
+ *
10
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11
+ * https://openapi-generator.tech
12
+ * Do not edit the class manually.
13
+ */
14
+
15
+
16
+ import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig } from 'axios';
17
+ import { Configuration } from '../configuration';
18
+ // Some imports not used depending on template conditions
19
+ // @ts-ignore
20
+ import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '../common';
21
+ // @ts-ignore
22
+ import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '../base';
23
+ // @ts-ignore
24
+ import { SendNotificationRequestDto } from '../models';
25
+ // @ts-ignore
26
+ import { SendNotificationResponseClass } from '../models';
27
+ /**
28
+ * NotificationsApi - axios parameter creator
29
+ * @export
30
+ */
31
+ export const NotificationsApiAxiosParamCreator = function (configuration?: Configuration) {
32
+ return {
33
+ /**
34
+ * undefined **Required Permissions** none
35
+ * @param {SendNotificationRequestDto} sendNotificationRequestDto
36
+ * @param {string} [authorization] Bearer Token
37
+ * @param {*} [options] Override http request option.
38
+ * @throws {RequiredError}
39
+ */
40
+ sendNotification: async (sendNotificationRequestDto: SendNotificationRequestDto, authorization?: string, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
41
+ // verify required parameter 'sendNotificationRequestDto' is not null or undefined
42
+ assertParamExists('sendNotification', 'sendNotificationRequestDto', sendNotificationRequestDto)
43
+ const localVarPath = `/notificationservice/v1/notifications`;
44
+ // use dummy base URL string because the URL constructor only accepts absolute URLs.
45
+ const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
46
+ let baseOptions;
47
+ let baseAccessToken;
48
+ if (configuration) {
49
+ baseOptions = configuration.baseOptions;
50
+ baseAccessToken = configuration.accessToken;
51
+ }
52
+
53
+ const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
54
+ const localVarHeaderParameter = {} as any;
55
+ const localVarQueryParameter = {} as any;
56
+
57
+ // authentication bearer required
58
+ // http bearer authentication required
59
+ await setBearerAuthToObject(localVarHeaderParameter, configuration)
60
+
61
+ if (authorization !== undefined && authorization !== null || baseAccessToken !== undefined && baseAccessToken !== null) {
62
+ localVarHeaderParameter['Authorization'] = String(authorization ? authorization : baseAccessToken);
63
+ }
64
+
65
+
66
+
67
+ localVarHeaderParameter['Content-Type'] = 'application/json';
68
+
69
+ setSearchParams(localVarUrlObj, localVarQueryParameter);
70
+ let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
71
+ localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
72
+ localVarRequestOptions.data = serializeDataIfNeeded(sendNotificationRequestDto, localVarRequestOptions, configuration)
73
+
74
+ return {
75
+ url: toPathString(localVarUrlObj),
76
+ options: localVarRequestOptions,
77
+ };
78
+ },
79
+ }
80
+ };
81
+
82
+ /**
83
+ * NotificationsApi - functional programming interface
84
+ * @export
85
+ */
86
+ export const NotificationsApiFp = function(configuration?: Configuration) {
87
+ const localVarAxiosParamCreator = NotificationsApiAxiosParamCreator(configuration)
88
+ return {
89
+ /**
90
+ * undefined **Required Permissions** none
91
+ * @param {SendNotificationRequestDto} sendNotificationRequestDto
92
+ * @param {string} [authorization] Bearer Token
93
+ * @param {*} [options] Override http request option.
94
+ * @throws {RequiredError}
95
+ */
96
+ async sendNotification(sendNotificationRequestDto: SendNotificationRequestDto, authorization?: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<SendNotificationResponseClass>> {
97
+ const localVarAxiosArgs = await localVarAxiosParamCreator.sendNotification(sendNotificationRequestDto, authorization, options);
98
+ return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
99
+ },
100
+ }
101
+ };
102
+
103
+ /**
104
+ * NotificationsApi - factory interface
105
+ * @export
106
+ */
107
+ export const NotificationsApiFactory = function (configuration?: Configuration, basePath?: string, axios?: AxiosInstance) {
108
+ const localVarFp = NotificationsApiFp(configuration)
109
+ return {
110
+ /**
111
+ * undefined **Required Permissions** none
112
+ * @param {SendNotificationRequestDto} sendNotificationRequestDto
113
+ * @param {string} [authorization] Bearer Token
114
+ * @param {*} [options] Override http request option.
115
+ * @throws {RequiredError}
116
+ */
117
+ sendNotification(sendNotificationRequestDto: SendNotificationRequestDto, authorization?: string, options?: any): AxiosPromise<SendNotificationResponseClass> {
118
+ return localVarFp.sendNotification(sendNotificationRequestDto, authorization, options).then((request) => request(axios, basePath));
119
+ },
120
+ };
121
+ };
122
+
123
+ /**
124
+ * Request parameters for sendNotification operation in NotificationsApi.
125
+ * @export
126
+ * @interface NotificationsApiSendNotificationRequest
127
+ */
128
+ export interface NotificationsApiSendNotificationRequest {
129
+ /**
130
+ *
131
+ * @type {SendNotificationRequestDto}
132
+ * @memberof NotificationsApiSendNotification
133
+ */
134
+ readonly sendNotificationRequestDto: SendNotificationRequestDto
135
+
136
+ /**
137
+ * Bearer Token
138
+ * @type {string}
139
+ * @memberof NotificationsApiSendNotification
140
+ */
141
+ readonly authorization?: string
142
+ }
143
+
144
+ /**
145
+ * NotificationsApi - object-oriented interface
146
+ * @export
147
+ * @class NotificationsApi
148
+ * @extends {BaseAPI}
149
+ */
150
+ export class NotificationsApi extends BaseAPI {
151
+ /**
152
+ * undefined **Required Permissions** none
153
+ * @param {NotificationsApiSendNotificationRequest} requestParameters Request parameters.
154
+ * @param {*} [options] Override http request option.
155
+ * @throws {RequiredError}
156
+ * @memberof NotificationsApi
157
+ */
158
+ public sendNotification(requestParameters: NotificationsApiSendNotificationRequest, options?: AxiosRequestConfig) {
159
+ return NotificationsApiFp(this.configuration).sendNotification(requestParameters.sendNotificationRequestDto, requestParameters.authorization, options).then((request) => request(this.axios, this.basePath));
160
+ }
161
+ }
package/api.ts ADDED
@@ -0,0 +1,39 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * EMIL NotificationService
5
+ * The EMIL NotificationService API description
6
+ *
7
+ * The version of the OpenAPI document: 1.0
8
+ * Contact: kontakt@emil.de
9
+ *
10
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11
+ * https://openapi-generator.tech
12
+ * Do not edit the class manually.
13
+ */
14
+
15
+
16
+ import { Configuration } from './configuration';
17
+ import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
18
+ // Some imports not used depending on template conditions
19
+ // @ts-ignore
20
+ import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common';
21
+ // @ts-ignore
22
+ import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base';
23
+ import { DefaultApi } from './api';
24
+ import { EmailMessagesApi } from './api';
25
+ import { EmailTrackingApi } from './api';
26
+ import { EmailVerificationsApi } from './api';
27
+ import { LayoutsApi } from './api';
28
+ import { NotificationTemplatesApi } from './api';
29
+ import { NotificationsApi } from './api';
30
+
31
+
32
+ export * from './api/default-api';
33
+ export * from './api/email-messages-api';
34
+ export * from './api/email-tracking-api';
35
+ export * from './api/email-verifications-api';
36
+ export * from './api/layouts-api';
37
+ export * from './api/notification-templates-api';
38
+ export * from './api/notifications-api';
39
+
package/base.ts ADDED
@@ -0,0 +1,331 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * EMIL NotificationService
5
+ * The EMIL NotificationService API description
6
+ *
7
+ * The version of the OpenAPI document: 1.0
8
+ * Contact: kontakt@emil.de
9
+ *
10
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11
+ * https://openapi-generator.tech
12
+ * Do not edit the class manually.
13
+ */
14
+
15
+
16
+ import { Configuration } from "./configuration";
17
+ import { defaultStorage } from "./common";
18
+ // Some imports not used depending on template conditions
19
+ // @ts-ignore
20
+ import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
21
+
22
+
23
+ export const BASE_PATH = "https://apiv2.emil.de".replace(/\/+$/, "");
24
+ /**
25
+ *
26
+ * @export
27
+ */
28
+ export const COLLECTION_FORMATS = {
29
+ csv: ",",
30
+ ssv: " ",
31
+ tsv: "\t",
32
+ pipes: "|",
33
+ };
34
+
35
+ export interface LoginClass {
36
+ accessToken: string;
37
+ permissions: string;
38
+ }
39
+
40
+ export interface SwitchWorkspaceRequest {
41
+ username: string;
42
+ targetWorkspace: string;
43
+ }
44
+
45
+ export interface SwitchWorkspaceResponseClass {
46
+ accessToken: string;
47
+ permissions: string;
48
+ }
49
+
50
+ export enum Environment {
51
+ Production = 'https://apiv2.emil.de',
52
+ Test = 'https://apiv2-test.emil.de',
53
+ Staging = 'https://apiv2-staging.emil.de',
54
+ Development = 'https://apiv2-dev.emil.de',
55
+ ProductionZurich = 'https://eu-central-2.apiv2.emil.de',
56
+ }
57
+
58
+ let _retry_count = 0
59
+ let _retry = null
60
+
61
+ export function resetRetry() {
62
+ _retry_count = 0
63
+ }
64
+
65
+ /**
66
+ *
67
+ * @export
68
+ * @interface RequestArgs
69
+ */
70
+ export interface RequestArgs {
71
+ url: string;
72
+ options: AxiosRequestConfig;
73
+ }
74
+
75
+ interface TokenData {
76
+ accessToken?: string;
77
+ username?: string;
78
+ permissions?: string;
79
+ }
80
+
81
+ const NETWORK_ERROR_MESSAGE = "Network Error";
82
+ const TOKEN_DATA = 'APP_TOKEN';
83
+
84
+ /**
85
+ *
86
+ * @export
87
+ * @class BaseAPI
88
+ */
89
+ export class BaseAPI {
90
+ protected configuration: Configuration | undefined;
91
+ private tokenData?: TokenData;
92
+
93
+ constructor(configuration?: Configuration,
94
+ protected basePath: string = BASE_PATH,
95
+ protected axios: AxiosInstance = globalAxios) {
96
+
97
+ this.loadTokenData();
98
+
99
+ if (configuration) {
100
+ const { accessToken } = this.tokenData;
101
+ this.configuration = configuration;
102
+ this.basePath = configuration.basePath || this.basePath;
103
+
104
+ // Use config token if provided, otherwise use tokenData token
105
+ const configToken = this.configuration.accessToken;
106
+ const storedToken = accessToken ? `Bearer ${accessToken}` : '';
107
+ this.configuration.accessToken = configToken || storedToken;
108
+ } else {
109
+ const { accessToken, username } = this.tokenData;
110
+
111
+ this.configuration = new Configuration({
112
+ basePath: this.basePath,
113
+ accessToken: accessToken ? `Bearer ${accessToken}` : '',
114
+ username,
115
+ });
116
+ }
117
+
118
+ this.attachInterceptor(axios);
119
+ }
120
+
121
+ selectEnvironment(env: Environment) {
122
+ this.selectBasePath(env);
123
+ }
124
+
125
+ selectBasePath(path: string) {
126
+ this.configuration.basePath = path;
127
+ }
128
+
129
+ getPermissions(): Array<string> {
130
+ if (!this.tokenData?.permissions) {
131
+ return [];
132
+ }
133
+
134
+ return this.tokenData.permissions.split(',');
135
+ }
136
+
137
+ async authorize(username: string, password: string, targetWorkspace?: string): Promise<void> {
138
+ const options: AxiosRequestConfig = {
139
+ method: 'POST',
140
+ url: `${this.configuration.basePath}/authservice/v1/login`,
141
+ headers: { 'Content-Type': 'application/json' },
142
+ data: {
143
+ username,
144
+ password,
145
+ },
146
+ withCredentials: true,
147
+ };
148
+
149
+ const response = await globalAxios.request<LoginClass>(options);
150
+
151
+ const { data: { accessToken, permissions } } = response;
152
+
153
+ this.configuration.username = username;
154
+ this.configuration.accessToken = `Bearer ${accessToken}`;
155
+ this.tokenData.username = username;
156
+ this.tokenData.accessToken = accessToken;
157
+ this.tokenData.permissions = permissions;
158
+
159
+ // Switch workspace if provided
160
+ if (targetWorkspace) {
161
+ await this.switchWorkspace(targetWorkspace);
162
+ } else {
163
+ // Only store if no workspace switch (since switchWorkspace will store after switching)
164
+ this.storeTokenData({
165
+ ...this.tokenData
166
+ });
167
+ }
168
+
169
+ this.storeTokenData({
170
+ ...this.tokenData
171
+ });
172
+ }
173
+
174
+ async switchWorkspace(targetWorkspace: string): Promise<void> {
175
+ const options: AxiosRequestConfig = {
176
+ method: 'POST',
177
+ url: `${this.configuration.basePath}/authservice/v1/workspaces/switch`,
178
+ headers: {
179
+ 'Content-Type': 'application/json',
180
+ 'Authorization': `Bearer ${this.configuration.accessToken}`,
181
+ },
182
+ data: {
183
+ username: this.configuration.username,
184
+ targetWorkspace,
185
+ } as SwitchWorkspaceRequest,
186
+ withCredentials: true,
187
+ };
188
+
189
+ const response = await globalAxios.request<SwitchWorkspaceResponseClass>(options);
190
+
191
+ const { data: { accessToken, permissions } } = response;
192
+
193
+ this.configuration.accessToken = `Bearer ${accessToken}`;
194
+ this.tokenData.accessToken = accessToken;
195
+ this.tokenData.permissions = permissions;
196
+
197
+ this.storeTokenData({
198
+ ...this.tokenData
199
+ });
200
+ }
201
+
202
+ async refreshTokenInternal(): Promise<LoginClass> {
203
+ const { username } = this.configuration;
204
+
205
+ if (!username) {
206
+ throw new Error('Failed to refresh token.');
207
+ }
208
+
209
+ const refreshTokenAxios = globalAxios.create()
210
+ refreshTokenAxios.interceptors.response.use(response => {
211
+ const { permissions } = response.data;
212
+
213
+ this.tokenData.permissions = permissions;
214
+ this.storeTokenData(this.tokenData);
215
+
216
+ return response;
217
+ })
218
+
219
+ const options: AxiosRequestConfig = {
220
+ method: 'POST',
221
+ url: `${this.configuration.basePath}/authservice/v1/refresh-token`,
222
+ headers: {
223
+ 'Content-Type': 'application/json',
224
+ },
225
+ data: { username: username },
226
+ withCredentials: true,
227
+ };
228
+
229
+ const response = await refreshTokenAxios.request<LoginClass>(options);
230
+ return response.data;
231
+ }
232
+
233
+ private storeTokenData(tokenData?: TokenData) {
234
+ if (typeof window !== 'undefined') {
235
+ defaultStorage().set<TokenData>(TOKEN_DATA, tokenData);
236
+ }
237
+ }
238
+
239
+ public loadTokenData() {
240
+ if (typeof window !== 'undefined') {
241
+ this.tokenData = defaultStorage().get<TokenData>(TOKEN_DATA) || {};
242
+ } else {
243
+ this.tokenData = {};
244
+ }
245
+ }
246
+
247
+ public cleanTokenData() {
248
+ this.storeTokenData(null);
249
+ }
250
+
251
+ private attachInterceptor(axios: AxiosInstance) {
252
+ axios.interceptors.response.use(
253
+ (res) => {
254
+ return res;
255
+ },
256
+ async (err) => {
257
+ let originalConfig = err.config;
258
+ if (err.response && !(err.response instanceof XMLHttpRequest)) { // sometimes buggy and is of type request
259
+ // Access Token was expired
260
+ if ((err.response.status === 401 || err.response.status === 403)
261
+ && !originalConfig._retry) {
262
+ originalConfig._retry = true;
263
+ try {
264
+ const { accessToken: tokenString, permissions } = await this.refreshTokenInternal();
265
+ const accessToken = `Bearer ${tokenString}`;
266
+ this.tokenData.permissions = permissions;
267
+
268
+ delete originalConfig.headers['Authorization']
269
+
270
+ originalConfig.headers['Authorization'] = accessToken;
271
+
272
+ this.configuration.accessToken = accessToken;
273
+ this.tokenData.accessToken = tokenString;
274
+
275
+ this.storeTokenData(this.tokenData);
276
+
277
+ return axios(originalConfig);
278
+ } catch (_error) {
279
+ if (_error.response && _error.response.data) {
280
+ return Promise.reject(_error.response.data);
281
+ }
282
+ return Promise.reject(_error);
283
+ }
284
+ }
285
+ } else if (err.message === NETWORK_ERROR_MESSAGE
286
+ && originalConfig.headers.hasOwnProperty('Authorization')
287
+ && _retry_count < 4
288
+ ) {
289
+ _retry_count++;
290
+ try {
291
+ const { accessToken: tokenString, permissions } = await this.refreshTokenInternal();
292
+ const accessToken = `Bearer ${tokenString}`;
293
+ this.tokenData.permissions = permissions;
294
+
295
+ _retry = true;
296
+ originalConfig.headers['Authorization'] = accessToken;
297
+
298
+ this.configuration.accessToken = accessToken;
299
+ this.tokenData.accessToken = tokenString;
300
+
301
+ this.storeTokenData(this.tokenData);
302
+
303
+ return axios.request({
304
+ ...originalConfig,
305
+ });
306
+ } catch (_error) {
307
+ if (_error.response && _error.response.data) {
308
+ return Promise.reject(_error.response.data);
309
+ }
310
+ return Promise.reject(_error);
311
+ }
312
+ }
313
+ return Promise.reject(err);
314
+ }
315
+ );
316
+ }
317
+ };
318
+
319
+ /**
320
+ *
321
+ * @export
322
+ * @class RequiredError
323
+ * @extends {Error}
324
+ */
325
+ export class RequiredError extends Error {
326
+ override name: "RequiredError" = "RequiredError";
327
+ constructor(public field: string, msg?: string) {
328
+ super(msg);
329
+ }
330
+ }
331
+