@retter/sdk 0.7.1-development → 0.7.1-development-2

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.
@@ -0,0 +1,133 @@
1
+ /*! *****************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+
16
+ /**
17
+ * @license
18
+ * Copyright 2017 Google LLC
19
+ *
20
+ * Licensed under the Apache License, Version 2.0 (the "License");
21
+ * you may not use this file except in compliance with the License.
22
+ * You may obtain a copy of the License at
23
+ *
24
+ * http://www.apache.org/licenses/LICENSE-2.0
25
+ *
26
+ * Unless required by applicable law or agreed to in writing, software
27
+ * distributed under the License is distributed on an "AS IS" BASIS,
28
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ * See the License for the specific language governing permissions and
30
+ * limitations under the License.
31
+ */
32
+
33
+ /**
34
+ * @license
35
+ * Copyright 2018 Google LLC
36
+ *
37
+ * Licensed under the Apache License, Version 2.0 (the "License");
38
+ * you may not use this file except in compliance with the License.
39
+ * You may obtain a copy of the License at
40
+ *
41
+ * http://www.apache.org/licenses/LICENSE-2.0
42
+ *
43
+ * Unless required by applicable law or agreed to in writing, software
44
+ * distributed under the License is distributed on an "AS IS" BASIS,
45
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46
+ * See the License for the specific language governing permissions and
47
+ * limitations under the License.
48
+ */
49
+
50
+ /**
51
+ * @license
52
+ * Copyright 2019 Google LLC
53
+ *
54
+ * Licensed under the Apache License, Version 2.0 (the "License");
55
+ * you may not use this file except in compliance with the License.
56
+ * You may obtain a copy of the License at
57
+ *
58
+ * http://www.apache.org/licenses/LICENSE-2.0
59
+ *
60
+ * Unless required by applicable law or agreed to in writing, software
61
+ * distributed under the License is distributed on an "AS IS" BASIS,
62
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
63
+ * See the License for the specific language governing permissions and
64
+ * limitations under the License.
65
+ */
66
+
67
+ /**
68
+ * @license
69
+ * Copyright 2020 Google LLC
70
+ *
71
+ * Licensed under the Apache License, Version 2.0 (the "License");
72
+ * you may not use this file except in compliance with the License.
73
+ * You may obtain a copy of the License at
74
+ *
75
+ * http://www.apache.org/licenses/LICENSE-2.0
76
+ *
77
+ * Unless required by applicable law or agreed to in writing, software
78
+ * distributed under the License is distributed on an "AS IS" BASIS,
79
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
80
+ * See the License for the specific language governing permissions and
81
+ * limitations under the License.
82
+ */
83
+
84
+ /**
85
+ * @license
86
+ * Copyright 2020 Google LLC.
87
+ *
88
+ * Licensed under the Apache License, Version 2.0 (the "License");
89
+ * you may not use this file except in compliance with the License.
90
+ * You may obtain a copy of the License at
91
+ *
92
+ * http://www.apache.org/licenses/LICENSE-2.0
93
+ *
94
+ * Unless required by applicable law or agreed to in writing, software
95
+ * distributed under the License is distributed on an "AS IS" BASIS,
96
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97
+ * See the License for the specific language governing permissions and
98
+ * limitations under the License.
99
+ */
100
+
101
+ /**
102
+ * @license
103
+ * Copyright 2021 Google LLC
104
+ *
105
+ * Licensed under the Apache License, Version 2.0 (the "License");
106
+ * you may not use this file except in compliance with the License.
107
+ * You may obtain a copy of the License at
108
+ *
109
+ * http://www.apache.org/licenses/LICENSE-2.0
110
+ *
111
+ * Unless required by applicable law or agreed to in writing, software
112
+ * distributed under the License is distributed on an "AS IS" BASIS,
113
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ * See the License for the specific language governing permissions and
115
+ * limitations under the License.
116
+ */
117
+
118
+ /**
119
+ * @license
120
+ * Copyright 2022 Google LLC
121
+ *
122
+ * Licensed under the Apache License, Version 2.0 (the "License");
123
+ * you may not use this file except in compliance with the License.
124
+ * You may obtain a copy of the License at
125
+ *
126
+ * http://www.apache.org/licenses/LICENSE-2.0
127
+ *
128
+ * Unless required by applicable law or agreed to in writing, software
129
+ * distributed under the License is distributed on an "AS IS" BASIS,
130
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131
+ * See the License for the specific language governing permissions and
132
+ * limitations under the License.
133
+ */
package/dist/Auth.d.ts ADDED
@@ -0,0 +1,95 @@
1
+ import RetterRequest from './Request';
2
+ import { RetterAuthChangedEvent, RetterClientConfig, RetterTokenData, RetterTokenPayload } from './types';
3
+ export default class Auth {
4
+ /**
5
+ * Request class instance.
6
+ */
7
+ private http?;
8
+ private clientConfig?;
9
+ private tokenStorageKey?;
10
+ /**
11
+ * It is used when sdk not used with browser.
12
+ */
13
+ private currentTokenData?;
14
+ constructor(config: RetterClientConfig);
15
+ setHttp(http: RetterRequest): void;
16
+ /**
17
+ * Stores the token data in the platforms storage.
18
+ * For web, it uses localStorage.
19
+ * For node or other platforms, it uses the currentTokenData property.
20
+ *
21
+ * @param tokenData
22
+ * @returns void
23
+ */
24
+ storeTokenData(tokenData: RetterTokenData | string): Promise<void>;
25
+ /**
26
+ * Clears the token data from the platforms storage.
27
+ *
28
+ * @returns void
29
+ */
30
+ clearTokenData(): Promise<void>;
31
+ /**
32
+ * Retrieves the token data from the platforms storage.
33
+ *
34
+ * @returns RetterTokenData | undefined
35
+ */
36
+ getCurrentTokenData(): Promise<RetterTokenData | undefined>;
37
+ /**
38
+ * Current user means the decoded data from the access token.
39
+ * So, it returns the decoded access token.
40
+ *
41
+ * @returns RetterTokenPayload | undefined
42
+ */
43
+ getCurrentUser(): Promise<RetterTokenPayload | undefined>;
44
+ /**
45
+ * It checks if the token is expired or stored in the platforms storage.
46
+ * If it is expired, it tries to refresh the token.
47
+ * If it is not expired, it returns the token data.
48
+ *
49
+ * @returns RetterTokenData | undefined
50
+ */
51
+ getTokenData(): Promise<RetterTokenData | undefined>;
52
+ /**
53
+ * It decodes the jwt token.
54
+ *
55
+ * @param token
56
+ * @returns RetterTokenPayload
57
+ */
58
+ protected decodeToken(token: string): RetterTokenPayload;
59
+ /**
60
+ * It adds decoded access and refresh tokens to the token data.
61
+ * Also it adds the diff between the current time and the server time.
62
+ *
63
+ * @param tokenData RetterTokenData
64
+ * @returns RetterTokenData
65
+ */
66
+ protected formatTokenData(tokenData: RetterTokenData): RetterTokenData;
67
+ /**
68
+ * It tries to get a new access token from the server.
69
+ *
70
+ * @param refreshToken
71
+ * @returns RetterTokenData
72
+ */
73
+ protected getFreshToken(refreshToken: string): Promise<RetterTokenData>;
74
+ /**
75
+ * It tries to get a access token from the server.
76
+ * It uses the customToken to get the access token.
77
+ *
78
+ * @param token string // customToken
79
+ * @returns RetterTokenData
80
+ */
81
+ signIn(token: string): Promise<RetterTokenData>;
82
+ /**
83
+ * It tries to notify the server that the user is logged out.
84
+ *
85
+ * @returns void
86
+ */
87
+ signOut(): Promise<void>;
88
+ /**
89
+ * It determines the user's status from the token data.
90
+ *
91
+ * @param tokenData RetterTokenData
92
+ * @returns RetterAuthChangedEvent
93
+ */
94
+ getAuthStatus(tokenData?: RetterTokenData): RetterAuthChangedEvent;
95
+ }
package/dist/Auth.js ADDED
@@ -0,0 +1,311 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ var jwt_decode_1 = __importDefault(require("jwt-decode"));
43
+ var js_cookie_1 = __importDefault(require("js-cookie"));
44
+ var helpers_1 = require("./helpers");
45
+ var types_1 = require("./types");
46
+ var Auth = /** @class */ (function () {
47
+ function Auth(config) {
48
+ this.clientConfig = config;
49
+ this.tokenStorageKey = "RIO_TOKENS_KEY.".concat(config.projectId);
50
+ }
51
+ Auth.prototype.setHttp = function (http) {
52
+ this.http = http;
53
+ };
54
+ /**
55
+ * Stores the token data in the platforms storage.
56
+ * For web, it uses localStorage.
57
+ * For node or other platforms, it uses the currentTokenData property.
58
+ *
59
+ * @param tokenData
60
+ * @returns void
61
+ */
62
+ Auth.prototype.storeTokenData = function (tokenData) {
63
+ var _a;
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var runtime;
66
+ return __generator(this, function (_b) {
67
+ runtime = (0, helpers_1.getRuntime)();
68
+ if (typeof tokenData === 'undefined')
69
+ return [2 /*return*/];
70
+ if (runtime === types_1.Runtime.web) {
71
+ localStorage.setItem(this.tokenStorageKey, JSON.stringify(tokenData));
72
+ if ((_a = this.clientConfig) === null || _a === void 0 ? void 0 : _a.useCookies) {
73
+ js_cookie_1.default.set(this.tokenStorageKey, JSON.stringify(tokenData));
74
+ }
75
+ }
76
+ this.currentTokenData = tokenData;
77
+ return [2 /*return*/];
78
+ });
79
+ });
80
+ };
81
+ /**
82
+ * Clears the token data from the platforms storage.
83
+ *
84
+ * @returns void
85
+ */
86
+ Auth.prototype.clearTokenData = function () {
87
+ return __awaiter(this, void 0, void 0, function () {
88
+ var runtime;
89
+ return __generator(this, function (_a) {
90
+ runtime = (0, helpers_1.getRuntime)();
91
+ if (runtime === types_1.Runtime.web) {
92
+ localStorage.removeItem(this.tokenStorageKey);
93
+ js_cookie_1.default.remove(this.tokenStorageKey);
94
+ }
95
+ this.currentTokenData = undefined;
96
+ return [2 /*return*/];
97
+ });
98
+ });
99
+ };
100
+ /**
101
+ * Retrieves the token data from the platforms storage.
102
+ *
103
+ * @returns RetterTokenData | undefined
104
+ */
105
+ Auth.prototype.getCurrentTokenData = function () {
106
+ var _a;
107
+ return __awaiter(this, void 0, void 0, function () {
108
+ var data, runtime, item, item;
109
+ return __generator(this, function (_b) {
110
+ runtime = (0, helpers_1.getRuntime)();
111
+ if (runtime === types_1.Runtime.web) {
112
+ if ((_a = this.clientConfig) === null || _a === void 0 ? void 0 : _a.useCookies) {
113
+ item = js_cookie_1.default.get(this.tokenStorageKey);
114
+ if (item && item !== 'undefined')
115
+ data = JSON.parse(item);
116
+ }
117
+ if (!data) {
118
+ item = localStorage.getItem(this.tokenStorageKey);
119
+ if (item && item !== 'undefined')
120
+ data = JSON.parse(item);
121
+ }
122
+ }
123
+ else {
124
+ data = this.currentTokenData;
125
+ }
126
+ if (data && (!data.accessTokenDecoded || !data.refreshTokenDecoded)) {
127
+ data.accessTokenDecoded = this.decodeToken(data.accessToken);
128
+ data.refreshTokenDecoded = this.decodeToken(data.refreshToken);
129
+ }
130
+ return [2 /*return*/, data];
131
+ });
132
+ });
133
+ };
134
+ /**
135
+ * Current user means the decoded data from the access token.
136
+ * So, it returns the decoded access token.
137
+ *
138
+ * @returns RetterTokenPayload | undefined
139
+ */
140
+ Auth.prototype.getCurrentUser = function () {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ var tokenData;
143
+ return __generator(this, function (_a) {
144
+ switch (_a.label) {
145
+ case 0: return [4 /*yield*/, this.getCurrentTokenData()];
146
+ case 1:
147
+ tokenData = _a.sent();
148
+ return [2 /*return*/, tokenData === null || tokenData === void 0 ? void 0 : tokenData.accessTokenDecoded];
149
+ }
150
+ });
151
+ });
152
+ };
153
+ /**
154
+ * It checks if the token is expired or stored in the platforms storage.
155
+ * If it is expired, it tries to refresh the token.
156
+ * If it is not expired, it returns the token data.
157
+ *
158
+ * @returns RetterTokenData | undefined
159
+ */
160
+ Auth.prototype.getTokenData = function () {
161
+ var _a, _b;
162
+ return __awaiter(this, void 0, void 0, function () {
163
+ var tokenData, now, accessTokenExpiresAt, refreshTokenExpiresAt, freshTokenData;
164
+ return __generator(this, function (_c) {
165
+ switch (_c.label) {
166
+ case 0: return [4 /*yield*/, this.getCurrentTokenData()];
167
+ case 1:
168
+ tokenData = _c.sent();
169
+ if (!tokenData)
170
+ return [2 /*return*/, undefined];
171
+ now = tokenData.diff + Math.round(new Date().getTime() / 1000) + 30 // Plus 30 seconds, just in case.
172
+ ;
173
+ tokenData.accessTokenDecoded = this.decodeToken(tokenData.accessToken);
174
+ tokenData.refreshTokenDecoded = this.decodeToken(tokenData.refreshToken);
175
+ accessTokenExpiresAt = (_a = tokenData.accessTokenDecoded.exp) !== null && _a !== void 0 ? _a : 0;
176
+ refreshTokenExpiresAt = (_b = tokenData.refreshTokenDecoded.exp) !== null && _b !== void 0 ? _b : 0;
177
+ if (!(refreshTokenExpiresAt > now && accessTokenExpiresAt <= now)) return [3 /*break*/, 3];
178
+ return [4 /*yield*/, this.getFreshToken(tokenData.refreshToken)];
179
+ case 2:
180
+ freshTokenData = _c.sent();
181
+ freshTokenData.accessTokenDecoded = this.decodeToken(freshTokenData.accessToken);
182
+ freshTokenData.refreshTokenDecoded = this.decodeToken(freshTokenData.refreshToken);
183
+ return [2 /*return*/, freshTokenData];
184
+ case 3: return [2 /*return*/, tokenData];
185
+ }
186
+ });
187
+ });
188
+ };
189
+ /**
190
+ * It decodes the jwt token.
191
+ *
192
+ * @param token
193
+ * @returns RetterTokenPayload
194
+ */
195
+ Auth.prototype.decodeToken = function (token) {
196
+ return (0, jwt_decode_1.default)(token);
197
+ };
198
+ /**
199
+ * It adds decoded access and refresh tokens to the token data.
200
+ * Also it adds the diff between the current time and the server time.
201
+ *
202
+ * @param tokenData RetterTokenData
203
+ * @returns RetterTokenData
204
+ */
205
+ Auth.prototype.formatTokenData = function (tokenData) {
206
+ tokenData.accessTokenDecoded = this.decodeToken(tokenData.accessToken);
207
+ tokenData.refreshTokenDecoded = this.decodeToken(tokenData.refreshToken);
208
+ if (tokenData.accessTokenDecoded.iat) {
209
+ tokenData.diff = tokenData.accessTokenDecoded.iat - Math.floor(Date.now() / 1000);
210
+ }
211
+ return tokenData;
212
+ };
213
+ /**
214
+ * It tries to get a new access token from the server.
215
+ *
216
+ * @param refreshToken
217
+ * @returns RetterTokenData
218
+ */
219
+ Auth.prototype.getFreshToken = function (refreshToken) {
220
+ return __awaiter(this, void 0, void 0, function () {
221
+ var path, response;
222
+ return __generator(this, function (_a) {
223
+ switch (_a.label) {
224
+ case 0:
225
+ path = "/TOKEN/refresh";
226
+ return [4 /*yield*/, this.http.call(this.clientConfig.projectId, path, { method: 'post', data: { refreshToken: refreshToken } })];
227
+ case 1:
228
+ response = _a.sent();
229
+ return [2 /*return*/, this.formatTokenData(response.data)];
230
+ }
231
+ });
232
+ });
233
+ };
234
+ /**
235
+ * It tries to get a access token from the server.
236
+ * It uses the customToken to get the access token.
237
+ *
238
+ * @param token string // customToken
239
+ * @returns RetterTokenData
240
+ */
241
+ Auth.prototype.signIn = function (token) {
242
+ return __awaiter(this, void 0, void 0, function () {
243
+ var path, tokenData;
244
+ return __generator(this, function (_a) {
245
+ switch (_a.label) {
246
+ case 0:
247
+ path = "/TOKEN/auth";
248
+ return [4 /*yield*/, this.http.call(this.clientConfig.projectId, path, { method: 'post', data: { customToken: token } })];
249
+ case 1:
250
+ tokenData = (_a.sent()).data;
251
+ return [2 /*return*/, this.formatTokenData(tokenData)];
252
+ }
253
+ });
254
+ });
255
+ };
256
+ /**
257
+ * It tries to notify the server that the user is logged out.
258
+ *
259
+ * @returns void
260
+ */
261
+ Auth.prototype.signOut = function () {
262
+ return __awaiter(this, void 0, void 0, function () {
263
+ var tokenData, path, error_1;
264
+ return __generator(this, function (_a) {
265
+ switch (_a.label) {
266
+ case 0:
267
+ _a.trys.push([0, 4, , 5]);
268
+ return [4 /*yield*/, this.getCurrentTokenData()];
269
+ case 1:
270
+ tokenData = _a.sent();
271
+ if (!tokenData) return [3 /*break*/, 3];
272
+ path = "/TOKEN/signOut";
273
+ return [4 /*yield*/, this.http.call(this.clientConfig.projectId, path, {
274
+ method: 'post',
275
+ headers: {
276
+ Authorization: "Bearer ".concat(tokenData.accessToken),
277
+ },
278
+ })];
279
+ case 2:
280
+ _a.sent();
281
+ _a.label = 3;
282
+ case 3: return [3 /*break*/, 5];
283
+ case 4:
284
+ error_1 = _a.sent();
285
+ return [3 /*break*/, 5];
286
+ case 5: return [2 /*return*/];
287
+ }
288
+ });
289
+ });
290
+ };
291
+ /**
292
+ * It determines the user's status from the token data.
293
+ *
294
+ * @param tokenData RetterTokenData
295
+ * @returns RetterAuthChangedEvent
296
+ */
297
+ Auth.prototype.getAuthStatus = function (tokenData) {
298
+ if (tokenData && tokenData.accessTokenDecoded) {
299
+ var _a = tokenData.accessTokenDecoded, uid = _a.userId, identity = _a.identity;
300
+ return {
301
+ uid: uid,
302
+ identity: identity,
303
+ authStatus: types_1.RetterAuthStatus.SIGNED_IN,
304
+ };
305
+ }
306
+ return { authStatus: types_1.RetterAuthStatus.SIGNED_OUT };
307
+ };
308
+ return Auth;
309
+ }());
310
+ exports.default = Auth;
311
+ //# sourceMappingURL=Auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Auth.js","sourceRoot":"","sources":["../src/Auth.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0DAA4B;AAC5B,wDAA+B;AAG/B,qCAAsC;AACtC,iCAAoI;AAEpI;IAeI,cAAY,MAA0B;QAClC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAA;QAC1B,IAAI,CAAC,eAAe,GAAG,yBA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;oCACL,aAAa,EAAE,iBAAU,SAAS,CAAC,WAAW,CAAE;iCACnD;6BACJ,CAAC,EAAA;;wBALF,SAKE,CAAA;;;;;;;;;;KAGb;IAED;;;;;OAKG;IACI,4BAAa,GAApB,UAAqB,SAA2B;QAC5C,IAAI,SAAS,IAAI,SAAS,CAAC,kBAAkB,EAAE;YACrC,IAAA,KAA4B,SAAS,CAAC,kBAAkB,EAA9C,GAAG,YAAA,EAAE,QAAQ,cAAiC,CAAA;YAE9D,OAAO;gBACH,GAAG,KAAA;gBACH,QAAQ,UAAA;gBACR,UAAU,EAAE,wBAAgB,CAAC,SAAS;aACzC,CAAA;SACJ;QAED,OAAO,EAAE,UAAU,EAAE,wBAAgB,CAAC,UAAU,EAAE,CAAA;IACtD,CAAC;IACL,WAAC;AAAD,CAAC,AApOD,IAoOC"}
@@ -0,0 +1,13 @@
1
+ import { AxiosResponse } from 'axios';
2
+ import { RetterClientConfig } from './types';
3
+ export default class Request {
4
+ private url?;
5
+ private region?;
6
+ private culture?;
7
+ private platform?;
8
+ private axiosInstance?;
9
+ constructor(config: RetterClientConfig);
10
+ protected createAxiosInstance(): void;
11
+ protected buildUrl(projectId: string, path: string): string;
12
+ call<T>(projectId: string, path: string, params?: any): Promise<AxiosResponse<T>>;
13
+ }
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var axios_1 = __importDefault(require("axios"));
54
+ var helpers_1 = require("./helpers");
55
+ var types_1 = require("./types");
56
+ var RetterRegions = [
57
+ {
58
+ id: types_1.RetterRegion.euWest1,
59
+ url: 'api.retter.io',
60
+ },
61
+ {
62
+ id: types_1.RetterRegion.euWest1Beta,
63
+ url: 'test-api.retter.io',
64
+ },
65
+ ];
66
+ var Request = /** @class */ (function () {
67
+ function Request(config) {
68
+ this.createAxiosInstance();
69
+ this.url = config.url;
70
+ if (!config.region)
71
+ config.region = types_1.RetterRegion.euWest1;
72
+ this.region = RetterRegions.find(function (region) { return region.id === config.region; });
73
+ this.culture = config.culture;
74
+ this.platform = config.culture;
75
+ }
76
+ Request.prototype.createAxiosInstance = function () {
77
+ this.axiosInstance = axios_1.default.create({
78
+ responseType: 'json',
79
+ headers: {
80
+ 'Content-Type': 'application/json',
81
+ },
82
+ timeout: 30000,
83
+ });
84
+ };
85
+ Request.prototype.buildUrl = function (projectId, path) {
86
+ var prefix = this.url ? "".concat(this.url) : "".concat(projectId, ".").concat(this.region.url);
87
+ return "https://".concat(prefix, "/").concat(projectId, "/").concat(path.startsWith('/') ? path.substr(1) : path);
88
+ };
89
+ Request.prototype.call = function (projectId, path, params) {
90
+ return __awaiter(this, void 0, void 0, function () {
91
+ var queryStringParams, data, error_1;
92
+ return __generator(this, function (_a) {
93
+ switch (_a.label) {
94
+ case 0:
95
+ _a.trys.push([0, 2, , 3]);
96
+ queryStringParams = __assign({}, params.params);
97
+ if (!queryStringParams.__culture && this.culture)
98
+ queryStringParams.__culture = this.culture;
99
+ if (!queryStringParams.__platform && this.platform)
100
+ queryStringParams.__platform = this.platform;
101
+ if (params.method === 'get' && params.base64Encode !== false && params.data) {
102
+ data = (0, helpers_1.base64Encode)(JSON.stringify((0, helpers_1.sort)(params.data)));
103
+ delete params.data;
104
+ queryStringParams.data = data;
105
+ queryStringParams.__isbase64 = true;
106
+ }
107
+ return [4 /*yield*/, this.axiosInstance(__assign(__assign({ url: this.buildUrl(projectId, path) }, params), { params: queryStringParams }))];
108
+ case 1: return [2 /*return*/, _a.sent()];
109
+ case 2:
110
+ error_1 = _a.sent();
111
+ throw error_1;
112
+ case 3: return [2 /*return*/];
113
+ }
114
+ });
115
+ });
116
+ };
117
+ return Request;
118
+ }());
119
+ exports.default = Request;
120
+ //# sourceMappingURL=Request.js.map