squarefi-bff-api-module 1.25.6 → 1.26.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/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,43 @@ 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 storage_1 = require("../utils/storage");
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, storage_1.getFromLocalStorage)('access_token');
48
- const modifiedHeaders = Object.assign(Object.assign({}, config.headers), { 'App-Enviroment': (0, sdk_react_1.isTMA)() ? constants_1.AppEnviroment.TELEGRAM : constants_1.AppEnviroment.WEB, 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'x-tenant-id': tenantId });
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;
59
59
  if (typeof window === 'undefined') {
60
60
  return Promise.reject(error);
61
61
  }
62
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
- const { response, config: failedRequest } = error;
64
- const refreshToken = (0, storage_1.getFromLocalStorage)('refresh_token');
65
- const isRetryRequest = failedRequest.headers['X-Retry-Request'];
66
- const isRefreshTokenRequest = (_b = response === null || response === void 0 ? void 0 : response.config) === null || _b === void 0 ? void 0 : _b.url.includes(auth_1.refreshTokenPath);
67
- const isTelegramSignInRequest = (_c = response === null || response === void 0 ? void 0 : response.config) === null || _c === void 0 ? void 0 : _c.url.includes(auth_1.telegramSignInPath);
68
- const isTelegramSignUpRequest = (_d = response === null || response === void 0 ? void 0 : response.config) === null || _d === void 0 ? void 0 : _d.url.includes(auth_1.telegramSignUpPath);
69
- const isRefreshNotRequired = !refreshToken && !(0, sdk_react_1.isTMA)();
63
+ const { response, config: failedRequestConfig } = error;
64
+ const { refresh_token } = (0, tokensFactory_1.getTokens)();
65
+ const isRetryRequest = (_b = failedRequestConfig.context) === null || _b === void 0 ? void 0 : _b.isRetryRequest;
66
+ const isRefreshTokenRequest = (_c = failedRequestConfig.url) === null || _c === void 0 ? void 0 : _c.includes(auth_1.refreshTokenPath);
67
+ const isTelegramSignInRequest = (_d = failedRequestConfig.url) === null || _d === void 0 ? void 0 : _d.includes(auth_1.telegramSignInPath);
68
+ const isTelegramSignUpRequest = (_e = failedRequestConfig.url) === null || _e === void 0 ? void 0 : _e.includes(auth_1.telegramSignUpPath);
69
+ const isRefreshNotRequired = !refresh_token && !(0, sdk_react_1.isTMA)();
70
70
  const isLogoutNeccesary = isRefreshNotRequired ||
71
71
  isTelegramSignInRequest ||
72
72
  isTelegramSignUpRequest ||
@@ -105,10 +105,10 @@ const createApiClient = ({ baseURL, isBearerToken, tenantId }) => {
105
105
  return new Promise((res, rej) => {
106
106
  requestQueue.push({
107
107
  resolve: () => {
108
- failedRequest.headers['X-Retry-Request'] = 'true';
109
- return res(instance(failedRequest));
108
+ failedRequestConfig.context = Object.assign(Object.assign({}, failedRequestConfig.context), { isRetryRequest: true });
109
+ return res(instance(failedRequestConfig));
110
110
  },
111
- reject: () => rej(instance(failedRequest)),
111
+ reject: () => rej(instance(failedRequestConfig)),
112
112
  });
113
113
  });
114
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squarefi-bff-api-module",
3
- "version": "1.25.6",
3
+ "version": "1.26.0",
4
4
  "description": "Squarefi BFF API client module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
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
- import { getFromLocalStorage } from '../utils/storage';
9
- import { deleteTokens, refreshTokens } from '../utils/tokensFactory';
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
- export const createApiClient = ({ baseURL, isBearerToken, tenantId }: CreateApiClientOptions) => {
26
- type RequestQueueItem = {
27
- resolve: Function;
28
- reject: Function;
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 = getFromLocalStorage('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,26 @@ 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
69
  if (error?.response?.status === ResponseStatus.UNAUTHORIZED) {
65
- const { response, config: failedRequest } = error;
66
- const refreshToken = getFromLocalStorage('refresh_token');
67
- const isRetryRequest = failedRequest.headers['X-Retry-Request'];
68
-
69
- const isRefreshTokenRequest = response?.config?.url.includes(refreshTokenPath);
70
- const isTelegramSignInRequest = response?.config?.url.includes(telegramSignInPath);
71
- const isTelegramSignUpRequest = response?.config?.url.includes(telegramSignUpPath);
72
- const isRefreshNotRequired = !refreshToken && !isTMA();
70
+ const { response, config: failedRequestConfig } = error;
71
+ const { refresh_token } = getTokens();
72
+ const isRetryRequest = failedRequestConfig.context?.isRetryRequest;
73
+
74
+ const isRefreshTokenRequest = failedRequestConfig.url?.includes(refreshTokenPath);
75
+ const isTelegramSignInRequest = failedRequestConfig.url?.includes(telegramSignInPath);
76
+ const isTelegramSignUpRequest = failedRequestConfig.url?.includes(telegramSignUpPath);
77
+ const isRefreshNotRequired = !refresh_token && !isTMA();
73
78
  const isLogoutNeccesary =
74
79
  isRefreshNotRequired ||
75
80
  isTelegramSignInRequest ||
@@ -111,10 +116,10 @@ export const createApiClient = ({ baseURL, isBearerToken, tenantId }: CreateApiC
111
116
  return new Promise((res, rej) => {
112
117
  requestQueue.push({
113
118
  resolve: () => {
114
- failedRequest.headers['X-Retry-Request'] = 'true';
115
- return res(instance(failedRequest));
119
+ failedRequestConfig.context = { ...failedRequestConfig.context, isRetryRequest: true };
120
+ return res(instance(failedRequestConfig));
116
121
  },
117
- reject: () => rej(instance(failedRequest)),
122
+ reject: () => rej(instance(failedRequestConfig)),
118
123
  });
119
124
  });
120
125
  }
@@ -1,4 +1,4 @@
1
- import { initData, isTMA, miniApp, init as initTelegramSDK } from '@telegram-apps/sdk-react';
1
+ import { initData, isTMA } from '@telegram-apps/sdk-react';
2
2
 
3
3
  import { deleteFromLocalStorage, getFromLocalStorage, setToLocalStorage } from './storage';
4
4
 
package/tsconfig.json CHANGED
@@ -10,6 +10,6 @@
10
10
  "forceConsistentCasingInFileNames": true,
11
11
  "moduleResolution": "node"
12
12
  },
13
- "include": ["src"],
13
+ "include": ["src", "types.d.ts"],
14
14
  "exclude": ["node_modules", "dist", "**/__tests__/*"]
15
15
  }
package/types.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import 'axios';
2
+
3
+ declare module 'axios' {
4
+ export interface AxiosRequestConfig {
5
+ context?: {
6
+ isRetryRequest?: boolean;
7
+ bypassUnauthorizedHandler?: boolean;
8
+ appEnvironment?: AppEnviroment;
9
+ };
10
+ }
11
+ }