@emilgroup/insurance-sdk 1.3.0 → 1.4.0-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.
- package/base.ts +47 -30
- package/dist/base.d.ts +2 -1
- package/dist/base.js +33 -20
- package/dist/storage.d.ts +15 -0
- package/dist/storage.js +32 -0
- package/package.json +2 -3
- package/storage.ts +41 -0
package/base.ts
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* Do not edit the class manually.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
import { Configuration } from "./configuration";
|
|
16
|
+
import { defaultStorage } from './storage';
|
|
17
17
|
// Some imports not used depending on template conditions
|
|
18
18
|
// @ts-ignore
|
|
19
19
|
import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
@@ -34,7 +34,6 @@ export const COLLECTION_FORMATS = {
|
|
|
34
34
|
export interface LoginClass {
|
|
35
35
|
accessToken: string;
|
|
36
36
|
permissions: Array<string>;
|
|
37
|
-
newPasswordRequired: boolean;
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
export enum Environment {
|
|
@@ -42,6 +41,13 @@ export enum Environment {
|
|
|
42
41
|
Test = 'https://apiv2-test.emil.de',
|
|
43
42
|
}
|
|
44
43
|
|
|
44
|
+
let _retry_count = 0
|
|
45
|
+
let _retry = null
|
|
46
|
+
|
|
47
|
+
export function resetRetry() {
|
|
48
|
+
_retry_count = 0
|
|
49
|
+
}
|
|
50
|
+
|
|
45
51
|
/**
|
|
46
52
|
*
|
|
47
53
|
* @export
|
|
@@ -52,6 +58,11 @@ export interface RequestArgs {
|
|
|
52
58
|
options: AxiosRequestConfig;
|
|
53
59
|
}
|
|
54
60
|
|
|
61
|
+
interface TokenData {
|
|
62
|
+
accessToken: string;
|
|
63
|
+
username: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
const NETWORK_ERROR_MESSAGE = "Network Error";
|
|
56
67
|
|
|
57
68
|
/**
|
|
@@ -62,14 +73,20 @@ const NETWORK_ERROR_MESSAGE = "Network Error";
|
|
|
62
73
|
export class BaseAPI {
|
|
63
74
|
protected configuration: Configuration;
|
|
64
75
|
private lastRequestConfig?: AxiosRequestConfig;
|
|
76
|
+
private tokenData: TokenData;
|
|
65
77
|
|
|
66
78
|
constructor(configuration?: Configuration, protected basePath: string = BASE_PATH, protected axios: AxiosInstance = globalAxios) {
|
|
67
79
|
if (configuration) {
|
|
68
80
|
this.configuration = configuration;
|
|
69
81
|
this.basePath = configuration.basePath || this.basePath;
|
|
70
82
|
} else {
|
|
83
|
+
this.tokenData = defaultStorage.get<TokenData>('TOKEN');
|
|
84
|
+
const { accessToken, username } = this.tokenData;
|
|
85
|
+
|
|
71
86
|
this.configuration = new Configuration({
|
|
72
87
|
basePath: this.basePath,
|
|
88
|
+
accessToken: accessToken ? `Bearer ${accessToken}` : '',
|
|
89
|
+
username,
|
|
73
90
|
});
|
|
74
91
|
}
|
|
75
92
|
|
|
@@ -98,6 +115,12 @@ export class BaseAPI {
|
|
|
98
115
|
this.configuration.username = username;
|
|
99
116
|
this.configuration.accessToken = `Bearer ${accessToken}`;
|
|
100
117
|
|
|
118
|
+
this.tokenData = {
|
|
119
|
+
username,
|
|
120
|
+
accessToken,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
defaultStorage.set<TokenData>('TOKEN', this.tokenData);
|
|
101
124
|
}
|
|
102
125
|
|
|
103
126
|
async refreshToken(): Promise<string> {
|
|
@@ -119,6 +142,13 @@ export class BaseAPI {
|
|
|
119
142
|
|
|
120
143
|
const { data: { accessToken } } = await globalAxios.request<LoginClass>(options);
|
|
121
144
|
|
|
145
|
+
this.tokenData.accessToken = accessToken;
|
|
146
|
+
|
|
147
|
+
defaultStorage.set<TokenData>('TOKEN', {
|
|
148
|
+
...this.tokenData,
|
|
149
|
+
accessToken,
|
|
150
|
+
});
|
|
151
|
+
|
|
122
152
|
return accessToken;
|
|
123
153
|
}
|
|
124
154
|
|
|
@@ -140,25 +170,19 @@ export class BaseAPI {
|
|
|
140
170
|
},
|
|
141
171
|
async (err) => {
|
|
142
172
|
let originalConfig = err.config;
|
|
143
|
-
if (err.response) {
|
|
173
|
+
if (err.response && !(err.response instanceof XMLHttpRequest)) {
|
|
144
174
|
// Access Token was expired
|
|
145
|
-
if (err.response.status === 401 && !originalConfig._retry) {
|
|
175
|
+
if ((err.response.status === 401 || err.response.status === 403) && !originalConfig._retry) {
|
|
146
176
|
originalConfig._retry = true;
|
|
147
177
|
try {
|
|
148
178
|
const tokenString = await this.refreshToken();
|
|
149
179
|
const accessToken = `Bearer ${tokenString}`;
|
|
150
180
|
|
|
151
|
-
|
|
152
|
-
localVarHeaderParameter['Authorization'] = accessToken;
|
|
153
|
-
|
|
154
|
-
originalConfig.headers = {
|
|
155
|
-
...originalConfig.headers,
|
|
156
|
-
...localVarHeaderParameter,
|
|
157
|
-
};
|
|
181
|
+
originalConfig.headers['Authorization'] = `Bearer ${accessToken}`
|
|
158
182
|
|
|
159
183
|
this.configuration.accessToken = accessToken;
|
|
160
184
|
|
|
161
|
-
return axios(originalConfig);
|
|
185
|
+
return axios.request(originalConfig);
|
|
162
186
|
} catch (_error) {
|
|
163
187
|
if (_error.response && _error.response.data) {
|
|
164
188
|
return Promise.reject(_error.response.data);
|
|
@@ -166,31 +190,24 @@ export class BaseAPI {
|
|
|
166
190
|
return Promise.reject(_error);
|
|
167
191
|
}
|
|
168
192
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
193
|
+
} else if(err.message === NETWORK_ERROR_MESSAGE
|
|
194
|
+
&& err.isAxiosError
|
|
195
|
+
&& originalConfig.headers.hasOwnProperty('Authorization')
|
|
196
|
+
&& _retry_count < 4
|
|
197
|
+
){
|
|
198
|
+
_retry_count++;
|
|
173
199
|
try {
|
|
174
200
|
const tokenString = await this.refreshToken();
|
|
175
201
|
const accessToken = `Bearer ${tokenString}`;
|
|
176
202
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
this.lastRequestConfig.headers = {
|
|
181
|
-
...originalConfig.headers,
|
|
182
|
-
...localVarHeaderParameter,
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
originalConfig = {
|
|
186
|
-
...this.lastRequestConfig,
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
originalConfig._retry = true;
|
|
203
|
+
_retry = true;
|
|
204
|
+
originalConfig.headers['Authorization'] = accessToken;
|
|
190
205
|
|
|
191
206
|
this.configuration.accessToken = accessToken;
|
|
192
207
|
|
|
193
|
-
return axios(
|
|
208
|
+
return axios.request({
|
|
209
|
+
...originalConfig,
|
|
210
|
+
});
|
|
194
211
|
} catch (_error) {
|
|
195
212
|
if (_error.response && _error.response.data) {
|
|
196
213
|
return Promise.reject(_error.response.data);
|
package/dist/base.d.ts
CHANGED
|
@@ -25,12 +25,12 @@ export declare const COLLECTION_FORMATS: {
|
|
|
25
25
|
export interface LoginClass {
|
|
26
26
|
accessToken: string;
|
|
27
27
|
permissions: Array<string>;
|
|
28
|
-
newPasswordRequired: boolean;
|
|
29
28
|
}
|
|
30
29
|
export declare enum Environment {
|
|
31
30
|
Production = "https://apiv2.emil.de",
|
|
32
31
|
Test = "https://apiv2-test.emil.de"
|
|
33
32
|
}
|
|
33
|
+
export declare function resetRetry(): void;
|
|
34
34
|
/**
|
|
35
35
|
*
|
|
36
36
|
* @export
|
|
@@ -50,6 +50,7 @@ export declare class BaseAPI {
|
|
|
50
50
|
protected axios: AxiosInstance;
|
|
51
51
|
protected configuration: Configuration;
|
|
52
52
|
private lastRequestConfig?;
|
|
53
|
+
private tokenData;
|
|
53
54
|
constructor(configuration?: Configuration, basePath?: string, axios?: AxiosInstance);
|
|
54
55
|
selectEnvironment(env: Environment): void;
|
|
55
56
|
authorize(username: string, password: string): Promise<void>;
|
package/dist/base.js
CHANGED
|
@@ -78,8 +78,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
78
78
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
79
79
|
};
|
|
80
80
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
81
|
-
exports.RequiredError = exports.BaseAPI = exports.Environment = exports.COLLECTION_FORMATS = exports.BASE_PATH = void 0;
|
|
81
|
+
exports.RequiredError = exports.BaseAPI = exports.resetRetry = exports.Environment = exports.COLLECTION_FORMATS = exports.BASE_PATH = void 0;
|
|
82
82
|
var configuration_1 = require("./configuration");
|
|
83
|
+
var storage_1 = require("./storage");
|
|
83
84
|
// Some imports not used depending on template conditions
|
|
84
85
|
// @ts-ignore
|
|
85
86
|
var axios_1 = __importDefault(require("axios"));
|
|
@@ -99,6 +100,12 @@ var Environment;
|
|
|
99
100
|
Environment["Production"] = "https://apiv2.emil.de";
|
|
100
101
|
Environment["Test"] = "https://apiv2-test.emil.de";
|
|
101
102
|
})(Environment = exports.Environment || (exports.Environment = {}));
|
|
103
|
+
var _retry_count = 0;
|
|
104
|
+
var _retry = null;
|
|
105
|
+
function resetRetry() {
|
|
106
|
+
_retry_count = 0;
|
|
107
|
+
}
|
|
108
|
+
exports.resetRetry = resetRetry;
|
|
102
109
|
var NETWORK_ERROR_MESSAGE = "Network Error";
|
|
103
110
|
/**
|
|
104
111
|
*
|
|
@@ -116,8 +123,12 @@ var BaseAPI = /** @class */ (function () {
|
|
|
116
123
|
this.basePath = configuration.basePath || this.basePath;
|
|
117
124
|
}
|
|
118
125
|
else {
|
|
126
|
+
this.tokenData = storage_1.defaultStorage.get('TOKEN');
|
|
127
|
+
var _a = this.tokenData, accessToken = _a.accessToken, username = _a.username;
|
|
119
128
|
this.configuration = new configuration_1.Configuration({
|
|
120
129
|
basePath: this.basePath,
|
|
130
|
+
accessToken: accessToken ? "Bearer ".concat(accessToken) : '',
|
|
131
|
+
username: username,
|
|
121
132
|
});
|
|
122
133
|
}
|
|
123
134
|
this.attachInterceptor(axios);
|
|
@@ -147,6 +158,11 @@ var BaseAPI = /** @class */ (function () {
|
|
|
147
158
|
accessToken = response.data.accessToken;
|
|
148
159
|
this.configuration.username = username;
|
|
149
160
|
this.configuration.accessToken = "Bearer ".concat(accessToken);
|
|
161
|
+
this.tokenData = {
|
|
162
|
+
username: username,
|
|
163
|
+
accessToken: accessToken,
|
|
164
|
+
};
|
|
165
|
+
storage_1.defaultStorage.set('TOKEN', this.tokenData);
|
|
150
166
|
return [2 /*return*/];
|
|
151
167
|
}
|
|
152
168
|
});
|
|
@@ -174,6 +190,8 @@ var BaseAPI = /** @class */ (function () {
|
|
|
174
190
|
return [4 /*yield*/, axios_1.default.request(options)];
|
|
175
191
|
case 1:
|
|
176
192
|
accessToken = (_a.sent()).data.accessToken;
|
|
193
|
+
this.tokenData.accessToken = accessToken;
|
|
194
|
+
storage_1.defaultStorage.set('TOKEN', __assign(__assign({}, this.tokenData), { accessToken: accessToken }));
|
|
177
195
|
return [2 /*return*/, accessToken];
|
|
178
196
|
}
|
|
179
197
|
});
|
|
@@ -194,13 +212,13 @@ var BaseAPI = /** @class */ (function () {
|
|
|
194
212
|
axios.interceptors.response.use(function (res) {
|
|
195
213
|
return res;
|
|
196
214
|
}, function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
197
|
-
var originalConfig, tokenString, accessToken,
|
|
215
|
+
var originalConfig, tokenString, accessToken, _error_1, tokenString, accessToken, _error_2;
|
|
198
216
|
return __generator(this, function (_a) {
|
|
199
217
|
switch (_a.label) {
|
|
200
218
|
case 0:
|
|
201
219
|
originalConfig = err.config;
|
|
202
|
-
if (!err.response) return [3 /*break*/, 5];
|
|
203
|
-
if (!(err.response.status === 401 && !originalConfig._retry)) return [3 /*break*/, 4];
|
|
220
|
+
if (!(err.response && !(err.response instanceof XMLHttpRequest))) return [3 /*break*/, 5];
|
|
221
|
+
if (!((err.response.status === 401 || err.response.status === 403) && !originalConfig._retry)) return [3 /*break*/, 4];
|
|
204
222
|
originalConfig._retry = true;
|
|
205
223
|
_a.label = 1;
|
|
206
224
|
case 1:
|
|
@@ -209,24 +227,22 @@ var BaseAPI = /** @class */ (function () {
|
|
|
209
227
|
case 2:
|
|
210
228
|
tokenString = _a.sent();
|
|
211
229
|
accessToken = "Bearer ".concat(tokenString);
|
|
212
|
-
|
|
213
|
-
localVarHeaderParameter['Authorization'] = accessToken;
|
|
214
|
-
originalConfig.headers = __assign(__assign({}, originalConfig.headers), localVarHeaderParameter);
|
|
230
|
+
originalConfig.headers['Authorization'] = "Bearer ".concat(accessToken);
|
|
215
231
|
this.configuration.accessToken = accessToken;
|
|
216
|
-
return [2 /*return*/, axios(originalConfig)];
|
|
232
|
+
return [2 /*return*/, axios.request(originalConfig)];
|
|
217
233
|
case 3:
|
|
218
234
|
_error_1 = _a.sent();
|
|
219
235
|
if (_error_1.response && _error_1.response.data) {
|
|
220
236
|
return [2 /*return*/, Promise.reject(_error_1.response.data)];
|
|
221
237
|
}
|
|
222
238
|
return [2 /*return*/, Promise.reject(_error_1)];
|
|
223
|
-
case 4:
|
|
224
|
-
if (err.response.status === 403 && err.response.data) {
|
|
225
|
-
return [2 /*return*/, Promise.reject(err.response.data)];
|
|
226
|
-
}
|
|
227
|
-
return [3 /*break*/, 9];
|
|
239
|
+
case 4: return [3 /*break*/, 9];
|
|
228
240
|
case 5:
|
|
229
|
-
if (!(err.message === NETWORK_ERROR_MESSAGE
|
|
241
|
+
if (!(err.message === NETWORK_ERROR_MESSAGE
|
|
242
|
+
&& err.isAxiosError
|
|
243
|
+
&& originalConfig.headers.hasOwnProperty('Authorization')
|
|
244
|
+
&& _retry_count < 4)) return [3 /*break*/, 9];
|
|
245
|
+
_retry_count++;
|
|
230
246
|
_a.label = 6;
|
|
231
247
|
case 6:
|
|
232
248
|
_a.trys.push([6, 8, , 9]);
|
|
@@ -234,13 +250,10 @@ var BaseAPI = /** @class */ (function () {
|
|
|
234
250
|
case 7:
|
|
235
251
|
tokenString = _a.sent();
|
|
236
252
|
accessToken = "Bearer ".concat(tokenString);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
this.lastRequestConfig.headers = __assign(__assign({}, originalConfig.headers), localVarHeaderParameter);
|
|
240
|
-
originalConfig = __assign({}, this.lastRequestConfig);
|
|
241
|
-
originalConfig._retry = true;
|
|
253
|
+
_retry = true;
|
|
254
|
+
originalConfig.headers['Authorization'] = accessToken;
|
|
242
255
|
this.configuration.accessToken = accessToken;
|
|
243
|
-
return [2 /*return*/, axios(originalConfig)];
|
|
256
|
+
return [2 /*return*/, axios.request(__assign({}, originalConfig))];
|
|
244
257
|
case 8:
|
|
245
258
|
_error_2 = _a.sent();
|
|
246
259
|
if (_error_2.response && _error_2.response.data) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface IStorageConverter<D, SD> {
|
|
2
|
+
toStorageData(data: D): SD;
|
|
3
|
+
fromStorageData(storageData: SD): D;
|
|
4
|
+
}
|
|
5
|
+
export interface IStorage {
|
|
6
|
+
get<T>(key: string, converter?: IStorageConverter<T, any>): T | null;
|
|
7
|
+
set<T>(key: string, value: T, converter?: IStorageConverter<T, any>): void;
|
|
8
|
+
}
|
|
9
|
+
export declare class LocalStorage implements IStorage {
|
|
10
|
+
readonly storage: Storage;
|
|
11
|
+
constructor();
|
|
12
|
+
get<T>(key: string, converter?: IStorageConverter<T, any>): T | null;
|
|
13
|
+
set<T>(key: string, value: T, converter?: IStorageConverter<T, any>): void;
|
|
14
|
+
}
|
|
15
|
+
export declare const defaultStorage: IStorage;
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultStorage = exports.LocalStorage = void 0;
|
|
4
|
+
var LocalStorage = /** @class */ (function () {
|
|
5
|
+
function LocalStorage() {
|
|
6
|
+
this.storage = localStorage;
|
|
7
|
+
}
|
|
8
|
+
LocalStorage.prototype.get = function (key, converter) {
|
|
9
|
+
var jsonValue = this.storage.getItem(key);
|
|
10
|
+
if (jsonValue === null) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
var value = JSON.parse(jsonValue);
|
|
14
|
+
if (converter !== undefined) {
|
|
15
|
+
return converter.fromStorageData(value);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
LocalStorage.prototype.set = function (key, value, converter) {
|
|
22
|
+
var valueToStore = value;
|
|
23
|
+
if (converter !== undefined) {
|
|
24
|
+
valueToStore = converter.toStorageData(value);
|
|
25
|
+
}
|
|
26
|
+
var jsonValue = JSON.stringify(valueToStore);
|
|
27
|
+
this.storage.setItem(key, jsonValue);
|
|
28
|
+
};
|
|
29
|
+
return LocalStorage;
|
|
30
|
+
}());
|
|
31
|
+
exports.LocalStorage = LocalStorage;
|
|
32
|
+
exports.defaultStorage = new LocalStorage();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emilgroup/insurance-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-beta.0",
|
|
4
4
|
"description": "OpenAPI client for @emilgroup/insurance-sdk",
|
|
5
5
|
"author": "OpenAPI-Generator Contributors",
|
|
6
6
|
"keywords": [
|
|
@@ -18,8 +18,7 @@
|
|
|
18
18
|
"prepare": "npm run build"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"axios": "^0.
|
|
22
|
-
|
|
21
|
+
"axios": "^0.27.2"
|
|
23
22
|
},
|
|
24
23
|
"devDependencies": {
|
|
25
24
|
|
package/storage.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface IStorageConverter<D, SD> {
|
|
2
|
+
toStorageData( data: D ): SD;
|
|
3
|
+
fromStorageData( storageData: SD ): D;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface IStorage {
|
|
7
|
+
get<T>( key: string, converter?: IStorageConverter<T, any> ): T | null;
|
|
8
|
+
set<T>( key: string, value: T, converter?: IStorageConverter<T, any> ): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class LocalStorage implements IStorage {
|
|
12
|
+
readonly storage: Storage;
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
this.storage = localStorage;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get<T>( key: string, converter?: IStorageConverter<T, any> ): T | null {
|
|
19
|
+
const jsonValue = this.storage.getItem( key );
|
|
20
|
+
if ( jsonValue === null ) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const value = JSON.parse( jsonValue );
|
|
24
|
+
if ( converter !== undefined ) {
|
|
25
|
+
return converter.fromStorageData( value );
|
|
26
|
+
} else {
|
|
27
|
+
return value as T;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
set<T>( key: string, value: T, converter?: IStorageConverter<T, any> ): void {
|
|
32
|
+
let valueToStore: any = value;
|
|
33
|
+
if ( converter !== undefined ) {
|
|
34
|
+
valueToStore = converter.toStorageData( value );
|
|
35
|
+
}
|
|
36
|
+
const jsonValue = JSON.stringify( valueToStore );
|
|
37
|
+
this.storage.setItem( key, jsonValue );
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const defaultStorage: IStorage = new LocalStorage();
|