diff --git a/packages/restapi/src/lib/alias/getAliasInfo.ts b/packages/restapi/src/lib/alias/getAliasInfo.ts index 24675407f..a1636293e 100644 --- a/packages/restapi/src/lib/alias/getAliasInfo.ts +++ b/packages/restapi/src/lib/alias/getAliasInfo.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { getAPIBaseUrls, getCAIPWithChainId } from '../helpers'; import Constants, { ENV } from '../constants'; import { ALIAS_CHAIN, ALIAS_CHAIN_ID } from '../config'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /v1/alias/{aliasAddressinCAIP}/channel @@ -29,8 +29,7 @@ export const getAliasInfo = async (options: GetAliasInfoOptionsType) => { const apiEndpoint = `${API_BASE_URL}/v1/alias`; const requestUrl = `${apiEndpoint}/${_alias}/channel`; - return await axios - .get(requestUrl) + return await axiosGet(requestUrl) .then((response) => response.data) .catch((err) => { console.error(`[EPNS-SDK] - API ${requestUrl}: `, err); diff --git a/packages/restapi/src/lib/channels/_getSubscribers.ts b/packages/restapi/src/lib/channels/_getSubscribers.ts index 5a8d7d07e..60a05a764 100644 --- a/packages/restapi/src/lib/channels/_getSubscribers.ts +++ b/packages/restapi/src/lib/channels/_getSubscribers.ts @@ -1,15 +1,11 @@ -import axios from "axios"; -import { - getCAIPAddress, - getAPIBaseUrls, - getCAIPDetails -} from '../helpers'; -import Constants, {ENV} from '../constants'; +import { getCAIPAddress, getAPIBaseUrls, getCAIPDetails } from '../helpers'; +import Constants, { ENV } from '../constants'; +import { axiosPost } from '../utils/axiosUtil'; export type GetSubscribersOptionsType = { channel: string; // plain ETH Format only - env?: ENV -} + env?: ENV; +}; /** * LEGACY SDK method, kept to support old functionality @@ -26,14 +22,10 @@ const deprecationWarning = ` export const _getSubscribers = async ( options: GetSubscribersOptionsType -) : Promise => { - +): Promise => { console.warn(deprecationWarning); - const { - channel, - env = Constants.ENV.PROD, - } = options || {}; + const { channel, env = Constants.ENV.PROD } = options || {}; const _channelAddress = await getCAIPAddress(env, channel, 'Channel'); @@ -49,12 +41,9 @@ export const _getSubscribers = async ( const body = { channel: channelCAIPDetails.address, // deprecated API expects ETH address format blockchain: chainId, - op: "read" + op: 'read', }; - const apiResponse = await axios.post(requestUrl, body); - - const { data: { subscribers = [] } } = apiResponse; - - return subscribers; -} + const response = await axiosPost<{ subscribers: string[] }>(requestUrl, body); + return response.data.subscribers; +}; diff --git a/packages/restapi/src/lib/channels/getChannel.ts b/packages/restapi/src/lib/channels/getChannel.ts index eb9a661da..e264c20b8 100644 --- a/packages/restapi/src/lib/channels/getChannel.ts +++ b/packages/restapi/src/lib/channels/getChannel.ts @@ -1,35 +1,27 @@ -import axios from 'axios'; -import { - getCAIPAddress, - getAPIBaseUrls -} from '../helpers'; -import Constants, {ENV} from '../constants'; +import { getCAIPAddress, getAPIBaseUrls } from '../helpers'; +import Constants, { ENV } from '../constants'; +import { axiosGet } from '../utils/axiosUtil'; /** - * GET /v1/channels/{addressinCAIP} + * GET /v1/channels/{addressinCAIP} */ export type GetChannelOptionsType = { channel: string; env?: ENV; -} +}; -export const getChannel = async ( - options: GetChannelOptionsType -) => { - const { - channel, - env = Constants.ENV.PROD, - } = options || {}; +export const getChannel = async (options: GetChannelOptionsType) => { + const { channel, env = Constants.ENV.PROD } = options || {}; const _channel = await getCAIPAddress(env, channel, 'Channel'); const API_BASE_URL = getAPIBaseUrls(env); const apiEndpoint = `${API_BASE_URL}/v1/channels`; const requestUrl = `${apiEndpoint}/${_channel}`; - return await axios.get(requestUrl) + return await axiosGet(requestUrl) .then((response) => response.data) .catch((err) => { console.error(`[Push SDK] - API ${requestUrl}: `, err); }); -} +}; diff --git a/packages/restapi/src/lib/channels/getDelegates.ts b/packages/restapi/src/lib/channels/getDelegates.ts index ef825c96c..c2a81cbfa 100644 --- a/packages/restapi/src/lib/channels/getDelegates.ts +++ b/packages/restapi/src/lib/channels/getDelegates.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import { getCAIPAddress, getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET v1/channels/${channelAddressInCAIP}/delegates @@ -32,7 +32,7 @@ export const getDelegates = async ( const apiEndpoint = `${API_BASE_URL}/v1/channels`; const requestUrl = `${apiEndpoint}/${_channel}/delegates`; - return await axios.get(requestUrl) + return await axiosGet(requestUrl) .then((response) => response.data?.delegates) .catch((err) => { console.error(`[EPNS-SDK] - API ${requestUrl}: `, err); diff --git a/packages/restapi/src/lib/channels/getSubscribers.ts b/packages/restapi/src/lib/channels/getSubscribers.ts index 7ce083061..b454fd958 100644 --- a/packages/restapi/src/lib/channels/getSubscribers.ts +++ b/packages/restapi/src/lib/channels/getSubscribers.ts @@ -1,14 +1,7 @@ -import axios from 'axios'; -import { - getCAIPAddress, - getAPIBaseUrls, -} from '../helpers'; -import Constants, {ENV} from '../constants'; -import { - Subscribers -} from '../types'; -import { ResourceLimits } from 'worker_threads'; - +import { getCAIPAddress, getAPIBaseUrls } from '../helpers'; +import Constants, { ENV } from '../constants'; +import { Subscribers } from '../types'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /v1/channels/:channelId/:subscribers @@ -24,8 +17,11 @@ export type GetChannelSubscribersOptionsType = { } export const getSubscribers = async ( - options: GetChannelSubscribersOptionsType + options: GetChannelSubscribersOptionsType ): Promise => { + + try { + const { channel, page = 1, @@ -57,13 +53,17 @@ export const getSubscribers = async ( if(category){ apiEndpoint = apiEndpoint+`&category=${category}` } - return await axios.get(apiEndpoint) - .then((response) => response.data) - .catch((err) => { - console.error(`[Push SDK] - API ${apiEndpoint}: `, err); - }); + return await axiosGet(apiEndpoint) + .then((response) => response.data) + .catch((err) => { + console.error(`[Push SDK] - API ${apiEndpoint}: `, err); + }); } catch (err) { console.error(`[Push SDK] - API - Error - API send() -: `, err); throw Error(`[Push SDK] - API - Error - API send() -: ${err}`); } -}; \ No newline at end of file + } catch (err) { + console.error(`[Push SDK] - API - Error - API send() -: `, err); + throw Error(`[Push SDK] - API - Error - API send() -: ${err}`); + } +}; diff --git a/packages/restapi/src/lib/channels/search.ts b/packages/restapi/src/lib/channels/search.ts index b6dcfae6c..0abe5a628 100644 --- a/packages/restapi/src/lib/channels/search.ts +++ b/packages/restapi/src/lib/channels/search.ts @@ -1,6 +1,6 @@ -import axios from 'axios'; import { getAPIBaseUrls, getQueryParams, getLimit } from '../helpers'; import Constants, {ENV} from '../constants'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /v1/channels/search/ @@ -34,7 +34,7 @@ export const search = async ( query: query }; const requestUrl = `${apiEndpoint}?${getQueryParams(queryObj)}`; - return axios.get(requestUrl) + return axiosGet(requestUrl) .then((response) => response.data.channels) .catch((err) => { console.error(`[Push SDK] - API ${requestUrl}: `, err); diff --git a/packages/restapi/src/lib/channels/subscribe.ts b/packages/restapi/src/lib/channels/subscribe.ts index 3b9c44145..a99723472 100644 --- a/packages/restapi/src/lib/channels/subscribe.ts +++ b/packages/restapi/src/lib/channels/subscribe.ts @@ -1,12 +1,12 @@ -import axios from 'axios'; import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers'; import { getTypeInformation, getDomainInformation, getSubscriptionMessage, } from './signature.helpers'; -import Constants, { ENV } from '../constants'; -import { SignerType } from '../types'; +import Constants, {ENV} from '../constants'; +import { SignerType } from "../types"; +import { axiosPost } from "../utils/axiosUtil"; export type SubscribeOptionsType = { signer: SignerType; channelAddress: string; @@ -88,7 +88,7 @@ export const subscribe = async (options: SubscribeOptionsType) => { }, }; - await axios.post(requestUrl, body); + await axiosPost(requestUrl, body); if (typeof onSuccess === 'function') onSuccess(); diff --git a/packages/restapi/src/lib/channels/subscribeV2.ts b/packages/restapi/src/lib/channels/subscribeV2.ts index a710e56cc..e37188c10 100644 --- a/packages/restapi/src/lib/channels/subscribeV2.ts +++ b/packages/restapi/src/lib/channels/subscribeV2.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers'; import { getDomainInformation, @@ -7,6 +6,7 @@ import { } from './signature.helpers'; import Constants, { ENV } from '../constants'; import { SignerType } from '../types'; +import { axiosPost } from '../utils/axiosUtil'; export type SubscribeOptionsV2Type = { signer: SignerType; @@ -87,7 +87,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => { message: messageInformation.data, }; - const res = await axios.post(requestUrl, body); + const res = await axiosPost(requestUrl, body); if (typeof onSuccess === 'function') onSuccess(); diff --git a/packages/restapi/src/lib/channels/unsubscribe.ts b/packages/restapi/src/lib/channels/unsubscribe.ts index 342f9d037..03c3265db 100644 --- a/packages/restapi/src/lib/channels/unsubscribe.ts +++ b/packages/restapi/src/lib/channels/unsubscribe.ts @@ -1,12 +1,14 @@ -import axios from 'axios'; import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers'; import { getTypeInformation, getDomainInformation, getSubscriptionMessage, } from './signature.helpers'; -import Constants, { ENV } from '../constants'; -import { SignerType } from '../types'; +import Constants, {ENV} from '../constants'; +import { SignerType } from "../types"; +import { axiosPost } from "../utils/axiosUtil"; + + export type UnSubscribeOptionsType = { signer: SignerType; @@ -89,7 +91,7 @@ export const unsubscribe = async (options: UnSubscribeOptionsType) => { }, }; - await axios.post(requestUrl, body); + await axiosPost(requestUrl, body); if (typeof onSuccess === 'function') onSuccess(); diff --git a/packages/restapi/src/lib/channels/unsubscribeV2.ts b/packages/restapi/src/lib/channels/unsubscribeV2.ts index 83cd84258..545273ca2 100644 --- a/packages/restapi/src/lib/channels/unsubscribeV2.ts +++ b/packages/restapi/src/lib/channels/unsubscribeV2.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers'; import { getDomainInformation, @@ -7,6 +6,7 @@ import { } from './signature.helpers'; import Constants, { ENV } from '../constants'; import { SignerType } from '../types'; +import { axiosPost } from '../utils/axiosUtil'; export type UnSubscribeOptionsV2Type = { signer: SignerType; @@ -87,7 +87,7 @@ export const unsubscribeV2 = async (options: UnSubscribeOptionsV2Type) => { message: messageInformation.data, }; - const res = await axios.post(requestUrl, body); + const res = await axiosPost(requestUrl, body); if (typeof onSuccess === 'function') onSuccess(); diff --git a/packages/restapi/src/lib/chat/approveRequest.ts b/packages/restapi/src/lib/chat/approveRequest.ts index dd84b4d77..5b175c59d 100644 --- a/packages/restapi/src/lib/chat/approveRequest.ts +++ b/packages/restapi/src/lib/chat/approveRequest.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType } from '../types'; @@ -11,6 +10,7 @@ import { IPGPHelper, } from './helpers'; import * as CryptoJS from 'crypto-js'; +import { axiosPut } from '../utils/axiosUtil'; import * as AES from '../chat/helpers/aes'; import { getGroupInfo } from './getGroupInfo'; import { getAllGroupMembersPublicKeys } from './getAllGroupMembersPublicKeys'; @@ -165,13 +165,9 @@ export const approveCore = async ( encryptedSecret, }; - /** - * API CALL TO PUSH NODES - */ const API_BASE_URL = getAPIBaseUrls(env); const apiEndpoint = `${API_BASE_URL}/v1/chat/request/accept`; - return axios - .put(apiEndpoint, body) + return axiosPut(apiEndpoint, body) .then((response) => { return response.data; }) diff --git a/packages/restapi/src/lib/chat/chat.ts b/packages/restapi/src/lib/chat/chat.ts index e80533070..dd00dacdd 100644 --- a/packages/restapi/src/lib/chat/chat.ts +++ b/packages/restapi/src/lib/chat/chat.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress, walletToPCAIP10 } from '../helpers'; import Constants, { ENV } from '../constants'; import { IFeeds } from '../types'; +import { axiosGet } from '../utils/axiosUtil'; import { PGPHelper, addDeprecatedInfo, getInboxLists, getUserDID } from './helpers'; export const chat = async (options: { @@ -24,7 +24,7 @@ export const chat = async (options: { const API_BASE_URL = getAPIBaseUrls(env); const apiEndpoint = `${API_BASE_URL}/v1/chat/users/${user}/chat/${recipientWallet}`; try { - const response = await axios.get(apiEndpoint); + const response = await axiosGet(apiEndpoint); // If no chat between users, then returns {} const chat: IFeeds = response.data; if (Object.keys(chat).length !== 0) { diff --git a/packages/restapi/src/lib/chat/chats.ts b/packages/restapi/src/lib/chat/chats.ts index 34ab2ee39..9aa7bbd50 100644 --- a/packages/restapi/src/lib/chat/chats.ts +++ b/packages/restapi/src/lib/chat/chats.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { ENV } from '../constants'; import { IFeeds } from '../types'; import { getInboxLists, getUserDID, addDeprecatedInfo, IPGPHelper, PGPHelper } from './helpers'; +import { axiosGet } from '../utils/axiosUtil'; export type ChatsOptionsType = { account: string; @@ -50,7 +50,7 @@ export const chatsCore = async (options: ChatsOptionsType, pgpHelper: IPGPHelper const apiEndpoint = `${API_BASE_URL}/v1/chat/users/${user}/chats?page=${page}&limit=${limit}`; const requestUrl = `${apiEndpoint}`; try { - const response = await axios.get(requestUrl); + const response = await axiosGet(requestUrl); const chats: IFeeds[] = response.data.chats; const updatedChats = addDeprecatedInfo(chats); const feeds: IFeeds[] = await getInboxLists({ diff --git a/packages/restapi/src/lib/chat/createGroup.ts b/packages/restapi/src/lib/chat/createGroup.ts index b4ae312e4..990157303 100644 --- a/packages/restapi/src/lib/chat/createGroup.ts +++ b/packages/restapi/src/lib/chat/createGroup.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants from '../constants'; import { EnvOptionsType, GroupDTO, SignerType, Rules } from '../types'; @@ -14,6 +13,7 @@ import { getConnectedUserV2Core, } from './helpers'; import * as CryptoJS from 'crypto-js'; +import { axiosPost } from '../utils/axiosUtil'; export interface ChatCreateGroupType extends EnvOptionsType { account?: string | null; @@ -149,8 +149,7 @@ export const createGroupCore = async ( rules ); - return axios - .post(apiEndpoint, body) + return axiosPost(apiEndpoint, body) .then((response) => { return response.data; }) diff --git a/packages/restapi/src/lib/chat/getGroup.ts b/packages/restapi/src/lib/chat/getGroup.ts index 9846ac6b3..2c800da2c 100644 --- a/packages/restapi/src/lib/chat/getGroup.ts +++ b/packages/restapi/src/lib/chat/getGroup.ts @@ -1,43 +1,30 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; -import Constants, {ENV} from '../constants'; -import { - GroupDTO -} from '../types'; - +import Constants, { ENV } from '../constants'; +import { GroupDTO } from '../types'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /v1/chat/groups/:chatId */ export interface GetGroupType { - chatId: string, - env?: ENV + chatId: string; + env?: ENV; } -export const getGroup = async ( - options: GetGroupType -): Promise => { - const { chatId, env = Constants.ENV.PROD } = options || {}; - try { - if (chatId == null || chatId.length == 0) { - throw new Error(`chatId cannot be null or empty`); - } - - const API_BASE_URL = getAPIBaseUrls(env); - const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}`; - return axios - .get(requestUrl) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) - throw new Error(err?.response?.data); - throw new Error(err); - }); - } catch (err) { - console.error(`[Push SDK] - API - Error - API ${getGroup.name} -: `, err); - throw Error(`[Push SDK] - API - Error - API ${getGroup.name} -: ${err}`); +export const getGroup = async (options: GetGroupType): Promise => { + const { chatId, env = Constants.ENV.PROD } = options || {}; + try { + if (chatId == null || chatId.length == 0) { + throw new Error(`chatId cannot be null or empty`); } -}; \ No newline at end of file + + const API_BASE_URL = getAPIBaseUrls(env); + const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}`; + const response = await axiosGet(requestUrl); + return response.data; + } catch (err) { + console.error(`[Push SDK] - API - Error - API ${getGroup.name} -: `, err); + throw Error(`[Push SDK] - API - Error - API ${getGroup.name} -: ${err}`); + } +}; diff --git a/packages/restapi/src/lib/chat/getGroupAccess.ts b/packages/restapi/src/lib/chat/getGroupAccess.ts index 51653f097..981a45a40 100644 --- a/packages/restapi/src/lib/chat/getGroupAccess.ts +++ b/packages/restapi/src/lib/chat/getGroupAccess.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; import { GroupAccess } from '../types'; import { getUserDID } from './helpers'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /v1/chat/groups/:chatId/access/:did @@ -32,15 +32,8 @@ export const getGroupAccess = async ( const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/access/${user}`; - return axios - .get(requestUrl) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) throw new Error(err?.response?.data); - throw new Error(err); - }); + const response = await axiosGet(requestUrl); + return response.data; } catch (err) { console.error( `[Push SDK] - API - Error - API ${getGroupAccess.name} -: `, diff --git a/packages/restapi/src/lib/chat/getGroupByName.ts b/packages/restapi/src/lib/chat/getGroupByName.ts index 36f66b69e..af2af883d 100644 --- a/packages/restapi/src/lib/chat/getGroupByName.ts +++ b/packages/restapi/src/lib/chat/getGroupByName.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, {ENV} from '../constants'; import { GroupDTO } from '../types'; +import { axiosGet } from '../utils/axiosUtil'; /** @@ -29,16 +29,8 @@ export const getGroupByName = async ( const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v1/chat/groups?groupName=${groupName}`; - return axios - .get(requestUrl) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) - throw new Error(err?.response?.data); - throw new Error(err); - }); + const response = await axiosGet(requestUrl); + return response.data; } catch (err) { console.error(`[Push SDK] - API - Error - API ${getGroupByName.name} -: `, err); throw Error(`[Push SDK] - API - Error - API ${getGroupByName.name} -: ${err}`); diff --git a/packages/restapi/src/lib/chat/getGroupMemberStatus.ts b/packages/restapi/src/lib/chat/getGroupMemberStatus.ts index 8c9f0282c..b2c967981 100644 --- a/packages/restapi/src/lib/chat/getGroupMemberStatus.ts +++ b/packages/restapi/src/lib/chat/getGroupMemberStatus.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; -import { GroupAccess, GroupMemberStatus } from '../types'; -import { getUserDID } from './helpers'; +import { GroupMemberStatus } from '../types'; +import { getUserDID } from './helpers'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /v1/chat/groups/:chatId/access/:did @@ -33,15 +33,8 @@ export const getGroupMemberStatus = async ( const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members/${user}/status`; - return axios - .get(requestUrl) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) throw new Error(err?.response?.data); - throw new Error(err); - }); + const response = await axiosGet(requestUrl); + return response.data; } catch (err) { console.error( `[Push SDK] - API - Error - API ${getGroupMemberStatus.name} -: `, diff --git a/packages/restapi/src/lib/chat/helpers/group.ts b/packages/restapi/src/lib/chat/helpers/group.ts index fbb121db2..148cf5a54 100644 --- a/packages/restapi/src/lib/chat/helpers/group.ts +++ b/packages/restapi/src/lib/chat/helpers/group.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { AccountEnvOptionsType, GroupDTO } from '../../types'; import { getAPIBaseUrls } from '../../helpers'; import Constants from '../../constants'; +import { axiosGet } from '../../utils/axiosUtil'; /** * GET /v1/chat/groups/ @@ -11,8 +11,7 @@ export const getGroup = async (options: AccountEnvOptionsType, chatId: string): const { env = Constants.ENV.PROD } = options || {}; const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}`; - return axios - .get(requestUrl) + return axiosGet(requestUrl) .then((response) => { return response.data; }) diff --git a/packages/restapi/src/lib/chat/helpers/service.ts b/packages/restapi/src/lib/chat/helpers/service.ts index f573840aa..7d9a4da22 100644 --- a/packages/restapi/src/lib/chat/helpers/service.ts +++ b/packages/restapi/src/lib/chat/helpers/service.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import Constants, { ENV } from '../../constants'; import { generateHash, @@ -15,6 +14,7 @@ import { } from '../../types'; import { getEip191Signature } from './crypto'; import { populateDeprecatedUser } from '../../utils/populateIUser'; +import { axiosGet, axiosPost, axiosPut } from '../../utils/axiosUtil'; type CreateUserOptionsType = { user: string; @@ -62,8 +62,7 @@ export const createUserService = async (options: CreateUserOptionsType) => { ...signatureObj, }; - return axios - .post(requestUrl, body) + return axiosPost(requestUrl, body) .then(async (response) => { if (response.data) response.data.publicKey = await verifyProfileKeys( @@ -107,8 +106,7 @@ export const authUpdateUserService = async (options: CreateUserOptionsType) => { // Exclude the "did" property from the "body" object const { did, ...body } = { ...data, ...signatureObj }; - return axios - .put(requestUrl, body) + return axiosPut(requestUrl, body) .then(async (response) => { if (response.data) response.data.publicKey = await verifyProfileKeys( @@ -136,8 +134,7 @@ export const getConversationHashService = async ( account )}/conversations/${conversationId}/hash`; - return axios - .get(requestUrl) + return axiosGet(requestUrl) .then((response) => { return response.data; }) @@ -163,8 +160,7 @@ export const getMessagesService = async (options: GetMessagesOptionsType) => { const requestUrl = `${apiEndpoint}?${getQueryParams(queryObj)}`; - return axios - .get(requestUrl) + return axiosGet(requestUrl) .then((response) => { return response.data; }) diff --git a/packages/restapi/src/lib/chat/ipfs.ts b/packages/restapi/src/lib/chat/ipfs.ts index d9d57b03b..4281cf6d5 100644 --- a/packages/restapi/src/lib/chat/ipfs.ts +++ b/packages/restapi/src/lib/chat/ipfs.ts @@ -1,36 +1,39 @@ -import axios from "axios"; -import Constants, {ENV} from "../constants"; -import { getAPIBaseUrls } from "../helpers"; +import Constants, { ENV } from '../constants'; +import { getAPIBaseUrls } from '../helpers'; +import { axiosGet } from '../utils/axiosUtil'; export interface Message { - fromCAIP10: string; - toCAIP10: string; - fromDID: string; - toDID: string; - messageType: string; - messageContent: string; - signature: string; - sigType: string; - timestamp?: number; - encType: string; - encryptedSecret: string; - link: string | null; + fromCAIP10: string; + toCAIP10: string; + fromDID: string; + toDID: string; + messageType: string; + messageContent: string; + signature: string; + sigType: string; + timestamp?: number; + encType: string; + encryptedSecret: string; + link: string | null; } export interface IPFSOptionsType { - env?: ENV, + env?: ENV; } -export async function getCID(cid: string, options: IPFSOptionsType): Promise { - const { env = Constants.ENV.PROD } = options || {}; - const API_BASE_URL = getAPIBaseUrls(env); - const apiEndpoint = `${API_BASE_URL}/v1/ipfs/${cid}`; - try { - const response = await axios.get(apiEndpoint) - const message: Message = response.data; - return message; - } catch (err) { - console.error(`[Push SDK] - API ${getCID.name}: `, err); - throw Error(`[Push SDK] - API ${getCID.name}: ${err}`); - } -} \ No newline at end of file +export async function getCID( + cid: string, + options: IPFSOptionsType +): Promise { + const { env = Constants.ENV.PROD } = options || {}; + const API_BASE_URL = getAPIBaseUrls(env); + const apiEndpoint = `${API_BASE_URL}/v1/ipfs/${cid}`; + try { + const response = await axiosGet(apiEndpoint); + const message: Message = response.data; + return message; + } catch (err) { + console.error(`[Push SDK] - API ${getCID.name}: `, err); + throw Error(`[Push SDK] - API ${getCID.name}: ${err}`); + } +} diff --git a/packages/restapi/src/lib/chat/rejectRequest.ts b/packages/restapi/src/lib/chat/rejectRequest.ts index 57c884e3d..ef6a00807 100644 --- a/packages/restapi/src/lib/chat/rejectRequest.ts +++ b/packages/restapi/src/lib/chat/rejectRequest.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants from '../constants'; import { EnvOptionsType, SignerType } from '../types'; @@ -12,6 +11,7 @@ import { rejectRequestPayload, } from './helpers'; import * as CryptoJS from 'crypto-js'; +import { axiosPut } from '../utils/axiosUtil'; interface RejectRequestOptionsType extends EnvOptionsType { /** @@ -80,8 +80,7 @@ export const reject = async ( signature ); - return axios - .put(apiEndpoint, body) + return axiosPut(apiEndpoint, body) .then((response) => { return response.data; }) diff --git a/packages/restapi/src/lib/chat/requests.ts b/packages/restapi/src/lib/chat/requests.ts index 735d3ef1b..551540902 100644 --- a/packages/restapi/src/lib/chat/requests.ts +++ b/packages/restapi/src/lib/chat/requests.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { ENV } from '../constants'; import { IFeeds } from '../types'; +import { axiosGet } from '../utils/axiosUtil'; import { IPGPHelper, PGPHelper, addDeprecatedInfo, getInboxLists, getUserDID } from './helpers'; export type RequestOptionsType = { @@ -54,7 +54,7 @@ export const requestsCore = async ( if (!isValidETHAddress(user)) { throw new Error(`Invalid address!`); } - const response = await axios.get(apiEndpoint); + const response = await axiosGet(apiEndpoint); const requests: IFeeds[] = response.data.requests; const updatedRequests = addDeprecatedInfo(requests); const Feeds: IFeeds[] = await getInboxLists({ diff --git a/packages/restapi/src/lib/chat/searchGroups.ts b/packages/restapi/src/lib/chat/searchGroups.ts index 469b27787..c5f1a1a96 100644 --- a/packages/restapi/src/lib/chat/searchGroups.ts +++ b/packages/restapi/src/lib/chat/searchGroups.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; import { GroupDTO } from '../types'; +import { axiosPost } from '../utils/axiosUtil'; /** * POST /v1/chat/groups/search @@ -28,21 +28,13 @@ export const search = async ( const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v1/chat/groups/search`; - return axios - .post(requestUrl, { - searchTerm, - pageNumber, - pageSize, - }) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) { - throw new Error(err?.response?.data); - } - throw new Error(err); - }); + const response = await axiosPost(requestUrl, { + searchTerm, + pageNumber, + pageSize, + }); + + return response.data; } catch (err) { console.error(`[Push SDK] - API - Error - API ${search.name} -: `, err); throw Error(`[Push SDK] - API - Error - API ${search.name} -: ${err}`); diff --git a/packages/restapi/src/lib/chat/send.ts b/packages/restapi/src/lib/chat/send.ts index f734681ee..f915ed37b 100644 --- a/packages/restapi/src/lib/chat/send.ts +++ b/packages/restapi/src/lib/chat/send.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { MessageType, ENV } from '../constants'; import { ChatSendOptionsType, MessageWithCID, SignerType } from '../types'; @@ -14,6 +13,7 @@ import { conversationHash } from './conversationHash'; import { ISendMessagePayload, sendMessagePayloadCore } from './helpers'; import { MessageObj } from '../types/messageTypes'; import { validateMessageObj } from '../validations/messageObject'; +import { axiosPost } from '../utils/axiosUtil'; import { getGroupInfo } from './getGroupInfo'; /** @@ -96,7 +96,9 @@ export const sendCore = async ( env, pgpHelper ); - return (await axios.post(apiEndpoint, body)).data; + + const response = await axiosPost(apiEndpoint, body); + return response.data; } catch (err) { console.error(`[Push SDK] - API - Error - API ${send.name} -: `, err); throw Error(`[Push SDK] - API - Error - API ${send.name} -: ${err}`); diff --git a/packages/restapi/src/lib/chat/updateGroup.ts b/packages/restapi/src/lib/chat/updateGroup.ts index b1cf9fddb..2eda4d298 100644 --- a/packages/restapi/src/lib/chat/updateGroup.ts +++ b/packages/restapi/src/lib/chat/updateGroup.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants from '../constants'; import { @@ -20,6 +19,7 @@ import { updateGroupRequestValidator, } from './helpers'; import * as CryptoJS from 'crypto-js'; +import { axiosPut } from '../utils/axiosUtil'; import { getGroup } from './getGroup'; import * as AES from '../chat/helpers/aes'; import { getGroupMemberStatus } from './getGroupMemberStatus'; @@ -182,16 +182,8 @@ export const updateGroupCore = async ( rules ); - return axios - .put(apiEndpoint, body) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) - throw new Error(JSON.stringify(err.response.data)); - throw new Error(err); - }); + const response = await axiosPut(apiEndpoint, body); + return response.data; } catch (err) { console.error( `[Push SDK] - API - Error - API ${updateGroup.name} -: `, diff --git a/packages/restapi/src/lib/chat/updateGroupProfile.ts b/packages/restapi/src/lib/chat/updateGroupProfile.ts index d33757c24..a873b7bfa 100644 --- a/packages/restapi/src/lib/chat/updateGroupProfile.ts +++ b/packages/restapi/src/lib/chat/updateGroupProfile.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants from '../constants'; import { @@ -16,6 +15,7 @@ import { updateGroupRequestValidator, } from './helpers'; import * as CryptoJS from 'crypto-js'; +import { axiosPut } from '../utils/axiosUtil'; import { getGroupInfo } from './getGroupInfo'; export interface ChatUpdateGroupProfileType extends EnvOptionsType { @@ -106,15 +106,8 @@ export const updateGroupProfile = async ( } = bodyToBeHashed; (body as any).profileVerificationProof = profileVerificationProof; - return axios - .put(apiEndpoint, body) - .then((response) => { - return response.data; - }) - .catch((err) => { - if (err?.response?.data) throw new Error(err?.response?.data); - throw new Error(err); - }); + const response = await axiosPut(apiEndpoint, body); + return response.data; } catch (err) { console.error( `[Push SDK] - API - Error - API ${updateGroupProfile.name} -: `, diff --git a/packages/restapi/src/lib/payloads/sendNotifications.ts b/packages/restapi/src/lib/payloads/sendNotifications.ts index 6101d6a54..482faa3d3 100644 --- a/packages/restapi/src/lib/payloads/sendNotifications.ts +++ b/packages/restapi/src/lib/payloads/sendNotifications.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { ISendNotificationInputOptions } from '../types'; import { getPayloadForAPIInput, @@ -27,6 +26,7 @@ import { } from './constants'; import { ENV } from '../constants'; import { getChannel } from '../channels/getChannel'; +import { axiosPost } from '../utils/axiosUtil'; /** * Validate options for some scenarios */ @@ -240,7 +240,7 @@ export async function sendNotification(options: ISendNotificationInputOptions) { }; const requestURL = `${API_BASE_URL}/v1/payloads/`; - return await axios.post(requestURL, apiPayload, { + return await axiosPost(requestURL, apiPayload, { headers: { 'Content-Type': 'application/json', }, diff --git a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts index 26b989cbd..9e48eb8fa 100644 --- a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts +++ b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts @@ -10,7 +10,6 @@ import * as config from '../config'; import { getAccountAddress } from '../chat/helpers'; import { IDENTITY_TYPE, NOTIFICATION_TYPE } from '../payloads/constants'; import { ethers, Signer as EthersSigner } from 'ethers'; -import axios from 'axios'; import { createPublicClient, http, @@ -26,7 +25,7 @@ import { getFallbackETHCAIPAddress, validateCAIP, } from '../helpers'; -import * as PUSH_ALIAS from '../alias'; +import { axiosGet, axiosPost } from '../utils/axiosUtil'; import { PushAPI } from '../pushapi/PushAPI'; // ERROR CONSTANTS @@ -655,7 +654,7 @@ export class PushNotificationBaseClass { protected async uploadToIPFSViaPushNode(data: string): Promise { try { - const response = await axios.post( + const response = await axiosPost( `${config.CORE_CONFIG[this.env!].API_BASE_URL}/v1/ipfs/upload`, { data } ); @@ -800,8 +799,7 @@ export class PushNotificationBaseClass { const API_BASE_URL = getAPIBaseUrls(this.env!); const apiEndpoint = `${API_BASE_URL}/v1/alias`; const requestUrl = `${apiEndpoint}/${address}/channel`; - const aliasInfo = await axios - .get(requestUrl) + const aliasInfo = await axiosGet(requestUrl) .then((response) => response.data) .catch((err) => { console.error(`[EPNS-SDK] - API ${requestUrl}: `, err); diff --git a/packages/restapi/src/lib/space/requests.ts b/packages/restapi/src/lib/space/requests.ts index 6f85dd131..b6821f590 100644 --- a/packages/restapi/src/lib/space/requests.ts +++ b/packages/restapi/src/lib/space/requests.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { ENV } from '../constants'; import { SpaceIFeeds } from '../types'; import { getSpaceInboxLists, getUserDID } from '../chat/helpers'; +import { axiosGet } from '../utils/axiosUtil'; export type RequestOptionsType = { account: string; @@ -47,7 +47,7 @@ export const requests = async ( if (!isValidETHAddress(user)) { throw new Error(`Invalid address!`); } - const response = await axios.get(apiEndpoint); + const response = await axiosGet(apiEndpoint); const requests: SpaceIFeeds[] = response.data.requests; const Feeds: SpaceIFeeds[] = await getSpaceInboxLists({ lists: requests, diff --git a/packages/restapi/src/lib/space/search.ts b/packages/restapi/src/lib/space/search.ts index b46288a94..84c1f02fa 100644 --- a/packages/restapi/src/lib/space/search.ts +++ b/packages/restapi/src/lib/space/search.ts @@ -1,7 +1,7 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; import { SpaceDTO } from '../types'; +import { axiosPost } from '../utils/axiosUtil'; /** * POST /v1/spaces/search @@ -28,8 +28,7 @@ export const search = async ( const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v1/spaces/search`; - return axios - .post(requestUrl, { + return axiosPost(requestUrl, { searchTerm, pageNumber, pageSize, diff --git a/packages/restapi/src/lib/space/spaceFeed.ts b/packages/restapi/src/lib/space/spaceFeed.ts index 2d4c99419..5084feebd 100644 --- a/packages/restapi/src/lib/space/spaceFeed.ts +++ b/packages/restapi/src/lib/space/spaceFeed.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { ENV } from '../constants'; import { SpaceIFeeds } from '../types'; import { getSpaceInboxLists, getUserDID } from './../chat/helpers'; +import { axiosGet } from '../utils/axiosUtil'; export const spaceFeed = async (options: { account: string; @@ -24,7 +24,7 @@ export const spaceFeed = async (options: { const API_BASE_URL = getAPIBaseUrls(env); const apiEndpoint = `${API_BASE_URL}/v1/spaces/users/${user}/space/${recipientWallet}`; try { - const response = await axios.get(apiEndpoint); + const response = await axiosGet(apiEndpoint); // If no chat between users, then returns {} const space: SpaceIFeeds = response.data; if (Object.keys(space).length !== 0) { diff --git a/packages/restapi/src/lib/space/spaces.ts b/packages/restapi/src/lib/space/spaces.ts index e23253071..1f802781c 100644 --- a/packages/restapi/src/lib/space/spaces.ts +++ b/packages/restapi/src/lib/space/spaces.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; import Constants, { ENV } from '../constants'; import { SpaceIFeeds } from '../types'; import { getSpaceInboxLists, getUserDID } from '../chat/helpers'; +import { axiosGet } from '../utils/axiosUtil'; export type ChatsOptionsType = { account: string; @@ -45,7 +45,7 @@ export const spaces = async (options: ChatsOptionsType): Promise const requestUrl = `${apiEndpoint}`; try { const toDecrypt = false; - const response = await axios.get(requestUrl); + const response = await axiosGet(requestUrl); const spaces: SpaceIFeeds[] = response.data.spaces; const feeds: SpaceIFeeds[] = await getSpaceInboxLists({ lists: spaces, diff --git a/packages/restapi/src/lib/space/start.ts b/packages/restapi/src/lib/space/start.ts index 770d99c5e..9bcb4dcff 100644 --- a/packages/restapi/src/lib/space/start.ts +++ b/packages/restapi/src/lib/space/start.ts @@ -12,7 +12,6 @@ import { import { get } from './get'; import { updateGroup } from '../chat/updateGroup'; import getMergeStreamObject from './helpers/getMergeStreamObject'; -import axios from 'axios'; export interface StartSpaceType extends EnvOptionsType { spaceId: string; diff --git a/packages/restapi/src/lib/space/trending.ts b/packages/restapi/src/lib/space/trending.ts index 1a81830fe..b385cbbeb 100644 --- a/packages/restapi/src/lib/space/trending.ts +++ b/packages/restapi/src/lib/space/trending.ts @@ -1,8 +1,8 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; import { SpaceIFeeds } from '../types'; import { getTrendingSpaceInboxLists } from '../chat/helpers'; +import { axiosGet } from '../utils/axiosUtil'; export type TrendingOptionsType = { page?: number; @@ -32,7 +32,7 @@ export const trending = async ( const apiEndpoint = `${API_BASE_URL}/v1/spaces/trending?page=${page}&limit=${limit}`; try { - const response = await axios.get(apiEndpoint); + const response = await axiosGet(apiEndpoint); const spaces: SpaceIFeeds[] = response.data.spaces; const Feeds: SpaceIFeeds[] = await getTrendingSpaceInboxLists({ lists: spaces, diff --git a/packages/restapi/src/lib/user/getDelegations.ts b/packages/restapi/src/lib/user/getDelegations.ts index ba211d09b..fe7fc175a 100644 --- a/packages/restapi/src/lib/user/getDelegations.ts +++ b/packages/restapi/src/lib/user/getDelegations.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import { getCAIPAddress, getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; +import { axiosGet } from '../utils/axiosUtil'; /** * GET /users/:userAddressInCAIP/delegations @@ -32,7 +32,7 @@ export const getDelegations = async ( const apiEndpoint = `${API_BASE_URL}/v1/users/${_user}/delegations`; const requestUrl = `${apiEndpoint}`; - return axios.get(requestUrl) + return axiosGet(requestUrl) .then((response) => response.data?.delegations || []) .catch((err) => { console.error(`[EPNS-SDK] - API ${requestUrl}: `, err); diff --git a/packages/restapi/src/lib/user/getFeeds.ts b/packages/restapi/src/lib/user/getFeeds.ts index 87c7fd42a..4317838f8 100644 --- a/packages/restapi/src/lib/user/getFeeds.ts +++ b/packages/restapi/src/lib/user/getFeeds.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getCAIPAddress, getAPIBaseUrls, @@ -7,6 +6,7 @@ import { } from '../helpers'; import Constants, {ENV} from '../constants'; import { parseApiResponse } from '../utils'; +import { axiosGet } from '../utils/axiosUtil'; export type FeedsOptionsType = { user: string; @@ -40,7 +40,7 @@ export const getFeeds = async ( }; const requestUrl = `${apiEndpoint}?${getQueryParams(queryObj)}`; - return axios.get(requestUrl) + return axiosGet(requestUrl) .then((response) => { if (raw) { return response?.data?.feeds || []; diff --git a/packages/restapi/src/lib/user/getFeedsPerChannel.ts b/packages/restapi/src/lib/user/getFeedsPerChannel.ts index 333f8556f..a82dcc3e2 100644 --- a/packages/restapi/src/lib/user/getFeedsPerChannel.ts +++ b/packages/restapi/src/lib/user/getFeedsPerChannel.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import { getCAIPAddress, getAPIBaseUrls, @@ -7,6 +6,7 @@ import { } from '../helpers'; import Constants, { ENV } from '../constants'; import { parseApiResponse } from '../utils'; +import { axiosGet } from '../utils/axiosUtil'; export type FeedsPerChannelOptionsType = { user: string; @@ -45,8 +45,7 @@ export const getFeedsPerChannel = async ( }; const requestUrl = `${apiEndpoint}?${getQueryParams(queryObj)}`; - return axios - .get(requestUrl) + return axiosGet(requestUrl) .then((response) => { if (raw) { return response?.data?.feeds || []; diff --git a/packages/restapi/src/lib/user/getSubscriptions.ts b/packages/restapi/src/lib/user/getSubscriptions.ts index 7e45d061e..b98452081 100644 --- a/packages/restapi/src/lib/user/getSubscriptions.ts +++ b/packages/restapi/src/lib/user/getSubscriptions.ts @@ -1,6 +1,6 @@ -import axios from 'axios'; import { getCAIPAddress, getAPIBaseUrls } from '../helpers'; import Constants, {ENV} from '../constants'; +import { axiosGet } from '../utils/axiosUtil'; export type UserSubscriptionsOptionsType = { user: string; @@ -20,7 +20,7 @@ export const getSubscriptions = async ( const apiEndpoint = `${API_BASE_URL}/v1/users/${_user}/subscriptions`; const requestUrl = `${apiEndpoint}`; - return axios.get(requestUrl) + return axiosGet(requestUrl) .then((response) => response.data?.subscriptions || []) .catch((err) => { console.error(`[Push SDK] - API ${requestUrl}: `, err); diff --git a/packages/restapi/src/lib/user/getUser.ts b/packages/restapi/src/lib/user/getUser.ts index 6bf22e8f6..a3012c1a8 100644 --- a/packages/restapi/src/lib/user/getUser.ts +++ b/packages/restapi/src/lib/user/getUser.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import { AccountEnvOptionsType, IUser } from '../types'; import { isValidETHAddress, walletToPCAIP10 } from '../helpers/address'; import { getAPIBaseUrls, verifyProfileKeys } from '../helpers'; import Constants from '../constants'; import { populateDeprecatedUser } from '../utils/populateIUser'; +import { axiosGet } from '../utils/axiosUtil'; export const get = async (options: AccountEnvOptionsType): Promise => { const { account, env = Constants.ENV.PROD } = options || {}; @@ -13,8 +13,7 @@ export const get = async (options: AccountEnvOptionsType): Promise => { const caip10 = walletToPCAIP10(account); const API_BASE_URL = getAPIBaseUrls(env); const requestUrl = `${API_BASE_URL}/v2/users/?caip10=${caip10}`; - return axios - .get(requestUrl) + return axiosGet(requestUrl) .then(async (response) => { if (response.data) { response.data.publicKey = await verifyProfileKeys( diff --git a/packages/restapi/src/lib/user/getUsersBatch.ts b/packages/restapi/src/lib/user/getUsersBatch.ts index 3a15699db..2ec31fa4c 100644 --- a/packages/restapi/src/lib/user/getUsersBatch.ts +++ b/packages/restapi/src/lib/user/getUsersBatch.ts @@ -1,9 +1,9 @@ -import axios from 'axios'; import { IUser } from '../types'; import { isValidETHAddress, walletToPCAIP10 } from '../helpers/address'; import { getAPIBaseUrls, verifyProfileKeys } from '../helpers'; import Constants, { ENV } from '../constants'; import { populateDeprecatedUser } from '../utils/populateIUser'; +import { axiosPost } from '../utils/axiosUtil'; export interface GetBatchType { userIds: string[]; @@ -32,8 +32,7 @@ export const getBatch = async (options: GetBatchType): Promise => { const pcaipUserIds = userIds.map(walletToPCAIP10); const requestBody = { userIds: pcaipUserIds }; - return axios - .post(requestUrl, requestBody) + return axiosPost(requestUrl, requestBody) .then((response) => { response.data.users.forEach(async (user: any, index: number) => { response.data.users[index].publicKey = await verifyProfileKeys( diff --git a/packages/restapi/src/lib/user/profile.updateUser.ts b/packages/restapi/src/lib/user/profile.updateUser.ts index d62980d0e..3d83b14c1 100644 --- a/packages/restapi/src/lib/user/profile.updateUser.ts +++ b/packages/restapi/src/lib/user/profile.updateUser.ts @@ -1,4 +1,3 @@ -import axios from 'axios'; import * as CryptoJS from 'crypto-js'; import { IPGPHelper, PGPHelper, getUserDID } from '../chat/helpers'; import Constants, { ENV } from '../constants'; @@ -11,6 +10,7 @@ import { IUser, ProgressHookType, ProgressHookTypeFunction } from '../types'; import { get } from './getUser'; import { populateDeprecatedUser } from '../utils/populateIUser'; import PROGRESSHOOK from '../progressHook'; +import { axiosPut } from '../utils/axiosUtil'; export type ProfileUpdateProps = { /** @@ -106,7 +106,7 @@ export const profileUpdateCore = async ( // Report Progress progressHook?.(PROGRESSHOOK['PUSH-PROFILE-UPDATE-01'] as ProgressHookType); - const response = await axios.put(apiEndpoint, body); + const response = await axiosPut(apiEndpoint, body); if (response.data) response.data.publicKey = await verifyProfileKeys( response.data.encryptedPrivateKey, diff --git a/packages/restapi/src/lib/utils/axiosUtil.ts b/packages/restapi/src/lib/utils/axiosUtil.ts new file mode 100644 index 000000000..9f1db0ba9 --- /dev/null +++ b/packages/restapi/src/lib/utils/axiosUtil.ts @@ -0,0 +1,67 @@ +import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; +// eslint-disable-next-line @typescript-eslint/no-var-requires +const packageJson = require('../../../../restapi/package.json'); +const version = packageJson.version; + +const addSdkVersionHeader = ( + config?: AxiosRequestConfig +): AxiosRequestConfig => { + const headers = { ...config?.headers, 'X-JS-SDK-VERSION': version }; + return { ...config, headers }; +}; + +const checkForDeprecationHeader = ( + response: AxiosResponse +): AxiosResponse => { + const deprecationNotice = response.headers['x-deprecation-notice']; + if (deprecationNotice) { + const method = response.config.method?.toUpperCase(); + const path = response.config.url; + console.warn( + `%cDeprecation Notice%c Method: ${method}, Path: ${path}, Notice: ${deprecationNotice}`, + 'color: white; background-color: red; font-weight: bold; padding: 2px 4px;', + 'color: red; font-weight: bold;' + ); + } + return response; +}; + +const axiosGet = async ( + url: string, + config?: AxiosRequestConfig +): Promise> => { + return axios + .get(url, addSdkVersionHeader(config)) + .then((response) => checkForDeprecationHeader(response)); +}; + +const axiosPost = async ( + url: string, + data: any, + config?: AxiosRequestConfig +): Promise> => { + return axios + .post(url, data, addSdkVersionHeader(config)) + .then((response) => checkForDeprecationHeader(response)); +}; + +const axiosPut = async ( + url: string, + data: any, + config?: AxiosRequestConfig +): Promise> => { + return axios + .put(url, data, addSdkVersionHeader(config)) + .then((response) => checkForDeprecationHeader(response)); +}; + +const axiosDelete = async ( + url: string, + config?: AxiosRequestConfig +): Promise> => { + return axios + .delete(url, addSdkVersionHeader(config)) + .then((response) => checkForDeprecationHeader(response)); +}; + +export { axiosGet, axiosPost, axiosPut, axiosDelete }; diff --git a/packages/restapi/src/lib/video/helpers/getIceServerConfig.ts b/packages/restapi/src/lib/video/helpers/getIceServerConfig.ts index cb0d5ebaa..afc6e2e72 100644 --- a/packages/restapi/src/lib/video/helpers/getIceServerConfig.ts +++ b/packages/restapi/src/lib/video/helpers/getIceServerConfig.ts @@ -1,14 +1,14 @@ -import axios from 'axios'; import { getAPIBaseUrls } from '../../helpers'; import Constants from '../../constants'; import * as CryptoJS from 'crypto-js'; +import { axiosGet } from '../../utils/axiosUtil'; const ENCRYPTION_KEY = 'turnserversecret'; export const getIceServerConfig = async (env = Constants.ENV.PROD) => { const API_BASE_URL = getAPIBaseUrls(env); const apiEndpoint = `${API_BASE_URL}/v1/turnserver/iceconfig`; - const { data: encryptedData } = await axios.get(apiEndpoint); + const { data: encryptedData } = await axiosGet(apiEndpoint); const { config: decryptedData } = JSON.parse( CryptoJS.AES.decrypt(encryptedData, ENCRYPTION_KEY).toString(