squarefi-bff-api-module 1.25.6 → 1.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/auth.js +8 -3
- package/dist/utils/apiClientFactory.js +19 -18
- package/package.json +1 -1
- package/src/api/auth.ts +8 -3
- package/src/utils/apiClientFactory.ts +35 -27
- package/src/utils/tokensFactory.ts +1 -1
- package/tsconfig.json +1 -1
- package/types.d.ts +11 -0
package/dist/api/auth.js
CHANGED
|
@@ -23,33 +23,38 @@ exports.auth = {
|
|
|
23
23
|
verify: {
|
|
24
24
|
email: (email, token) => apiClientFactory_1.apiClientV2.postRequest('/auth/verify/email/otp', {
|
|
25
25
|
data: { email, token, type: 'email' },
|
|
26
|
+
context: { bypassUnauthorizedHandler: true },
|
|
26
27
|
}),
|
|
27
28
|
phone: (phone, token) => apiClientFactory_1.apiClientV2.postRequest('/auth/verify/phone/otp', {
|
|
28
29
|
data: { phone: (0, converters_1.convertPhoneToSupabaseFormat)(phone), token, type: 'sms' },
|
|
30
|
+
context: { bypassUnauthorizedHandler: true },
|
|
29
31
|
}),
|
|
30
32
|
},
|
|
31
33
|
},
|
|
32
34
|
signin: {
|
|
33
35
|
omni: {
|
|
34
|
-
email: (data) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-in/omni/email/otp', { data }),
|
|
36
|
+
email: (data) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-in/omni/email/otp', { data, context: { bypassUnauthorizedHandler: true } }),
|
|
35
37
|
phone: (_a) => {
|
|
36
38
|
var { phone } = _a, data = __rest(_a, ["phone"]);
|
|
37
39
|
return apiClientFactory_1.apiClientV2.postRequest('/auth/sign-in/omni/phone/otp', {
|
|
38
40
|
data: Object.assign({ phone: (0, converters_1.convertPhoneToSupabaseFormat)(phone) }, data),
|
|
41
|
+
context: { bypassUnauthorizedHandler: true },
|
|
39
42
|
});
|
|
40
43
|
},
|
|
41
44
|
},
|
|
42
|
-
byType: (data) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-in', { data }),
|
|
45
|
+
byType: (data) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-in', { data, context: { bypassUnauthorizedHandler: true } }),
|
|
43
46
|
telegram: (data) => apiClientFactory_1.apiClientV2.postRequest(exports.telegramSignInPath, { data }),
|
|
44
47
|
password: (email, password // check on backend V2
|
|
45
48
|
) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-in/password/email', {
|
|
46
49
|
data: { email, password },
|
|
50
|
+
context: { bypassUnauthorizedHandler: true },
|
|
47
51
|
}),
|
|
48
52
|
},
|
|
49
53
|
signup: {
|
|
50
|
-
byType: (data) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-up', { data }),
|
|
54
|
+
byType: (data) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-up', { data, context: { bypassUnauthorizedHandler: true } }),
|
|
51
55
|
password: (email, password) => apiClientFactory_1.apiClientV2.postRequest('/auth/sign-up/password/email', {
|
|
52
56
|
data: { email, password },
|
|
57
|
+
context: { bypassUnauthorizedHandler: true },
|
|
53
58
|
}),
|
|
54
59
|
telegram: (data) => apiClientFactory_1.apiClientV2.postRequest(exports.telegramSignUpPath, { data }),
|
|
55
60
|
},
|
|
@@ -30,43 +30,44 @@ const sdk_react_1 = require("@telegram-apps/sdk-react");
|
|
|
30
30
|
const axios_1 = __importDefault(require("axios"));
|
|
31
31
|
const auth_1 = require("../api/auth");
|
|
32
32
|
const constants_1 = require("../constants");
|
|
33
|
-
const
|
|
34
|
-
const tokensFactory_1 = require("../utils/tokensFactory");
|
|
33
|
+
const tokensFactory_1 = require("./tokensFactory");
|
|
35
34
|
// eslint-disable-next-line no-constant-condition
|
|
36
35
|
const apiV1BaseURL = (_a = process.env.API_URL) !== null && _a !== void 0 ? _a : 'ENV variable API_URL is not defined';
|
|
37
36
|
const apiV2BaseURL = (_b = process.env.API_V2_URL) !== null && _b !== void 0 ? _b : 'ENV variable API_V2_URL is not defined';
|
|
38
37
|
const apiTOTPBaseURL = (_c = process.env.API_TOTP_URL) !== null && _c !== void 0 ? _c : 'ENV variable API_TOTP_URL is not defined';
|
|
39
38
|
const envTenantId = (_d = process.env.TENANT_ID) !== null && _d !== void 0 ? _d : 'ENV variable TENANT_ID is not defined';
|
|
40
39
|
const envLogoutURL = (_e = process.env.LOGOUT_URL) !== null && _e !== void 0 ? _e : '/auth/logout';
|
|
40
|
+
let isTokenRefreshing = false;
|
|
41
|
+
let requestQueue = [];
|
|
41
42
|
const createApiClient = ({ baseURL, isBearerToken, tenantId }) => {
|
|
42
43
|
const instance = axios_1.default.create({
|
|
43
44
|
baseURL,
|
|
44
45
|
timeout: 60000,
|
|
45
46
|
});
|
|
46
47
|
instance.interceptors.request.use((config) => {
|
|
47
|
-
const access_token = (0,
|
|
48
|
-
const modifiedHeaders = Object.assign(Object.assign({}, config.headers), { '
|
|
48
|
+
const { access_token } = (0, tokensFactory_1.getTokens)();
|
|
49
|
+
const modifiedHeaders = Object.assign(Object.assign({}, config.headers), { 'Content-Type': 'application/json', 'x-tenant-id': tenantId });
|
|
49
50
|
if (access_token) {
|
|
50
51
|
const authHeader = isBearerToken ? `Bearer ${access_token}` : access_token;
|
|
51
52
|
modifiedHeaders.Authorization = authHeader;
|
|
52
53
|
}
|
|
54
|
+
config.context = Object.assign(Object.assign({}, config.context), { appEnvironment: (0, sdk_react_1.isTMA)() ? constants_1.AppEnviroment.TELEGRAM : constants_1.AppEnviroment.WEB });
|
|
53
55
|
return Object.assign(Object.assign({}, config), { headers: modifiedHeaders });
|
|
54
56
|
});
|
|
55
|
-
let isTokenRefreshing = false;
|
|
56
|
-
let requestQueue = [];
|
|
57
57
|
instance.interceptors.response.use((response) => response, (error) => {
|
|
58
|
-
var _a, _b, _c, _d;
|
|
58
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
59
59
|
if (typeof window === 'undefined') {
|
|
60
60
|
return Promise.reject(error);
|
|
61
61
|
}
|
|
62
|
-
if (((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === constants_1.ResponseStatus.UNAUTHORIZED
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
const
|
|
62
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === constants_1.ResponseStatus.UNAUTHORIZED &&
|
|
63
|
+
!((_c = (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.config.context) === null || _c === void 0 ? void 0 : _c.bypassUnauthorizedHandler)) {
|
|
64
|
+
const { response, config: failedRequestConfig } = error;
|
|
65
|
+
const { refresh_token } = (0, tokensFactory_1.getTokens)();
|
|
66
|
+
const isRetryRequest = (_d = failedRequestConfig.context) === null || _d === void 0 ? void 0 : _d.isRetryRequest;
|
|
67
|
+
const isRefreshTokenRequest = (_e = failedRequestConfig.url) === null || _e === void 0 ? void 0 : _e.includes(auth_1.refreshTokenPath);
|
|
68
|
+
const isTelegramSignInRequest = (_f = failedRequestConfig.url) === null || _f === void 0 ? void 0 : _f.includes(auth_1.telegramSignInPath);
|
|
69
|
+
const isTelegramSignUpRequest = (_g = failedRequestConfig.url) === null || _g === void 0 ? void 0 : _g.includes(auth_1.telegramSignUpPath);
|
|
70
|
+
const isRefreshNotRequired = !refresh_token && !(0, sdk_react_1.isTMA)();
|
|
70
71
|
const isLogoutNeccesary = isRefreshNotRequired ||
|
|
71
72
|
isTelegramSignInRequest ||
|
|
72
73
|
isTelegramSignUpRequest ||
|
|
@@ -105,10 +106,10 @@ const createApiClient = ({ baseURL, isBearerToken, tenantId }) => {
|
|
|
105
106
|
return new Promise((res, rej) => {
|
|
106
107
|
requestQueue.push({
|
|
107
108
|
resolve: () => {
|
|
108
|
-
|
|
109
|
-
return res(instance(
|
|
109
|
+
failedRequestConfig.context = Object.assign(Object.assign({}, failedRequestConfig.context), { isRetryRequest: true });
|
|
110
|
+
return res(instance(failedRequestConfig));
|
|
110
111
|
},
|
|
111
|
-
reject: () => rej(instance(
|
|
112
|
+
reject: () => rej(instance(failedRequestConfig)),
|
|
112
113
|
});
|
|
113
114
|
});
|
|
114
115
|
}
|
package/package.json
CHANGED
package/src/api/auth.ts
CHANGED
|
@@ -15,24 +15,27 @@ export const auth = {
|
|
|
15
15
|
email: (email: string, token: string): Promise<API.Auth.VerifyOtp.Response> =>
|
|
16
16
|
apiClientV2.postRequest<API.Auth.VerifyOtp.Response>('/auth/verify/email/otp', {
|
|
17
17
|
data: { email, token, type: 'email' },
|
|
18
|
+
context: { bypassUnauthorizedHandler: true },
|
|
18
19
|
}),
|
|
19
20
|
phone: (phone: string, token: string): Promise<API.Auth.VerifyOtp.Response> =>
|
|
20
21
|
apiClientV2.postRequest<API.Auth.VerifyOtp.Response>('/auth/verify/phone/otp', {
|
|
21
22
|
data: { phone: convertPhoneToSupabaseFormat(phone), token, type: 'sms' },
|
|
23
|
+
context: { bypassUnauthorizedHandler: true },
|
|
22
24
|
}),
|
|
23
25
|
},
|
|
24
26
|
},
|
|
25
27
|
signin: {
|
|
26
28
|
omni: {
|
|
27
29
|
email: (data: API.Auth.SignIn.Omni.Email.OTP.Request): Promise<API.Auth.Tokens> =>
|
|
28
|
-
apiClientV2.postRequest('/auth/sign-in/omni/email/otp', { data }),
|
|
30
|
+
apiClientV2.postRequest('/auth/sign-in/omni/email/otp', { data, context: { bypassUnauthorizedHandler: true } }),
|
|
29
31
|
phone: ({ phone, ...data }: API.Auth.SignIn.Omni.Phone.OTP.Request): Promise<API.Auth.Tokens> =>
|
|
30
32
|
apiClientV2.postRequest('/auth/sign-in/omni/phone/otp', {
|
|
31
33
|
data: { phone: convertPhoneToSupabaseFormat(phone), ...data },
|
|
34
|
+
context: { bypassUnauthorizedHandler: true },
|
|
32
35
|
}),
|
|
33
36
|
},
|
|
34
37
|
byType: (data: API.Auth.SignIn.ByType.Request): Promise<API.Auth.Tokens> =>
|
|
35
|
-
apiClientV2.postRequest('/auth/sign-in', { data }),
|
|
38
|
+
apiClientV2.postRequest('/auth/sign-in', { data, context: { bypassUnauthorizedHandler: true } }),
|
|
36
39
|
telegram: (data: API.Auth.Telegram.Signin.Request): Promise<API.Auth.Telegram.Signin.Response> =>
|
|
37
40
|
apiClientV2.postRequest<API.Auth.Telegram.Signin.Response>(telegramSignInPath, { data }),
|
|
38
41
|
password: (
|
|
@@ -41,14 +44,16 @@ export const auth = {
|
|
|
41
44
|
): Promise<API.Auth.Tokens> =>
|
|
42
45
|
apiClientV2.postRequest<API.Auth.Tokens>('/auth/sign-in/password/email', {
|
|
43
46
|
data: { email, password },
|
|
47
|
+
context: { bypassUnauthorizedHandler: true },
|
|
44
48
|
}),
|
|
45
49
|
},
|
|
46
50
|
signup: {
|
|
47
51
|
byType: (data: API.Auth.SignUp.ByType.Request): Promise<API.Auth.Tokens> =>
|
|
48
|
-
apiClientV2.postRequest('/auth/sign-up', { data }),
|
|
52
|
+
apiClientV2.postRequest('/auth/sign-up', { data, context: { bypassUnauthorizedHandler: true } }),
|
|
49
53
|
password: (email: string, password: string): Promise<API.Auth.Tokens> =>
|
|
50
54
|
apiClientV2.postRequest<API.Auth.Tokens>('/auth/sign-up/password/email', {
|
|
51
55
|
data: { email, password },
|
|
56
|
+
context: { bypassUnauthorizedHandler: true },
|
|
52
57
|
}),
|
|
53
58
|
telegram: (data: API.Auth.Telegram.Signup.Request): Promise<API.Auth.Telegram.Signup.Response> =>
|
|
54
59
|
apiClientV2.postRequest<API.Auth.Telegram.Signup.Response>(telegramSignUpPath, { data }),
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import { isTMA } from '@telegram-apps/sdk-react';
|
|
3
|
-
import axios, { AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';
|
|
3
|
+
import axios, { AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
4
4
|
|
|
5
5
|
import { telegramSignUpPath, telegramSignInPath, refreshTokenPath } from '../api/auth';
|
|
6
6
|
|
|
7
7
|
import { AppEnviroment, ResponseStatus } from '../constants';
|
|
8
|
-
|
|
9
|
-
import { deleteTokens, refreshTokens } from '
|
|
8
|
+
|
|
9
|
+
import { deleteTokens, getTokens, refreshTokens } from './tokensFactory';
|
|
10
10
|
|
|
11
11
|
// eslint-disable-next-line no-constant-condition
|
|
12
12
|
|
|
@@ -16,31 +16,37 @@ const apiTOTPBaseURL = process.env.API_TOTP_URL ?? 'ENV variable API_TOTP_URL is
|
|
|
16
16
|
const envTenantId = process.env.TENANT_ID ?? 'ENV variable TENANT_ID is not defined';
|
|
17
17
|
const envLogoutURL = process.env.LOGOUT_URL ?? '/auth/logout';
|
|
18
18
|
|
|
19
|
+
type AxiosError = {
|
|
20
|
+
response: AxiosResponse;
|
|
21
|
+
config: InternalAxiosRequestConfig;
|
|
22
|
+
};
|
|
23
|
+
|
|
19
24
|
type CreateApiClientOptions = {
|
|
20
25
|
baseURL: string;
|
|
21
26
|
tenantId: string;
|
|
22
27
|
isBearerToken?: boolean;
|
|
23
28
|
};
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
type RequestQueueItem = {
|
|
31
|
+
resolve: Function;
|
|
32
|
+
reject: Function;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
let isTokenRefreshing = false;
|
|
36
|
+
let requestQueue: RequestQueueItem[] = [];
|
|
30
37
|
|
|
38
|
+
export const createApiClient = ({ baseURL, isBearerToken, tenantId }: CreateApiClientOptions) => {
|
|
31
39
|
const instance = axios.create({
|
|
32
40
|
baseURL,
|
|
33
41
|
timeout: 60000,
|
|
34
42
|
});
|
|
35
43
|
|
|
36
44
|
instance.interceptors.request.use((config) => {
|
|
37
|
-
const access_token =
|
|
45
|
+
const { access_token } = getTokens();
|
|
38
46
|
|
|
39
47
|
const modifiedHeaders = {
|
|
40
48
|
...config.headers,
|
|
41
|
-
'App-Enviroment': isTMA() ? AppEnviroment.TELEGRAM : AppEnviroment.WEB,
|
|
42
49
|
'Content-Type': 'application/json',
|
|
43
|
-
'Access-Control-Allow-Origin': '*',
|
|
44
50
|
'x-tenant-id': tenantId,
|
|
45
51
|
};
|
|
46
52
|
|
|
@@ -49,27 +55,29 @@ export const createApiClient = ({ baseURL, isBearerToken, tenantId }: CreateApiC
|
|
|
49
55
|
modifiedHeaders.Authorization = authHeader;
|
|
50
56
|
}
|
|
51
57
|
|
|
58
|
+
config.context = { ...config.context, appEnvironment: isTMA() ? AppEnviroment.TELEGRAM : AppEnviroment.WEB };
|
|
59
|
+
|
|
52
60
|
return { ...config, headers: modifiedHeaders } as unknown as InternalAxiosRequestConfig;
|
|
53
61
|
});
|
|
54
62
|
|
|
55
|
-
let isTokenRefreshing = false;
|
|
56
|
-
let requestQueue: RequestQueueItem[] = [];
|
|
57
|
-
|
|
58
63
|
instance.interceptors.response.use(
|
|
59
64
|
(response) => response,
|
|
60
|
-
(error) => {
|
|
65
|
+
(error: AxiosError) => {
|
|
61
66
|
if (typeof window === 'undefined') {
|
|
62
67
|
return Promise.reject(error);
|
|
63
68
|
}
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
const
|
|
69
|
+
if (
|
|
70
|
+
error?.response?.status === ResponseStatus.UNAUTHORIZED &&
|
|
71
|
+
!error?.response?.config.context?.bypassUnauthorizedHandler
|
|
72
|
+
) {
|
|
73
|
+
const { response, config: failedRequestConfig } = error;
|
|
74
|
+
const { refresh_token } = getTokens();
|
|
75
|
+
const isRetryRequest = failedRequestConfig.context?.isRetryRequest;
|
|
76
|
+
|
|
77
|
+
const isRefreshTokenRequest = failedRequestConfig.url?.includes(refreshTokenPath);
|
|
78
|
+
const isTelegramSignInRequest = failedRequestConfig.url?.includes(telegramSignInPath);
|
|
79
|
+
const isTelegramSignUpRequest = failedRequestConfig.url?.includes(telegramSignUpPath);
|
|
80
|
+
const isRefreshNotRequired = !refresh_token && !isTMA();
|
|
73
81
|
const isLogoutNeccesary =
|
|
74
82
|
isRefreshNotRequired ||
|
|
75
83
|
isTelegramSignInRequest ||
|
|
@@ -111,10 +119,10 @@ export const createApiClient = ({ baseURL, isBearerToken, tenantId }: CreateApiC
|
|
|
111
119
|
return new Promise((res, rej) => {
|
|
112
120
|
requestQueue.push({
|
|
113
121
|
resolve: () => {
|
|
114
|
-
|
|
115
|
-
return res(instance(
|
|
122
|
+
failedRequestConfig.context = { ...failedRequestConfig.context, isRetryRequest: true };
|
|
123
|
+
return res(instance(failedRequestConfig));
|
|
116
124
|
},
|
|
117
|
-
reject: () => rej(instance(
|
|
125
|
+
reject: () => rej(instance(failedRequestConfig)),
|
|
118
126
|
});
|
|
119
127
|
});
|
|
120
128
|
}
|
package/tsconfig.json
CHANGED