Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Axios Wrapper and Pass SDK Version Header #872

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
echo "\nRunning GIT hooks..."
yarn cleanbuild
yarn nx affected --target=lint
yarn nx affected --target=test
#yarn nx affected --target=test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be commented

5 changes: 2 additions & 3 deletions packages/restapi/src/lib/alias/getAliasInfo.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 11 additions & 22 deletions packages/restapi/src/lib/channels/_getSubscribers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,14 +22,10 @@ const deprecationWarning = `

export const _getSubscribers = async (
options: GetSubscribersOptionsType
) : Promise<string[]> => {

): Promise<string[]> => {
console.warn(deprecationWarning);

const {
channel,
env = Constants.ENV.PROD,
} = options || {};
const { channel, env = Constants.ENV.PROD } = options || {};

const _channelAddress = await getCAIPAddress(env, channel, 'Channel');

Expand All @@ -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;
};
26 changes: 9 additions & 17 deletions packages/restapi/src/lib/channels/getChannel.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
};
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/channels/getDelegates.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
36 changes: 18 additions & 18 deletions packages/restapi/src/lib/channels/getSubscribers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,8 +17,11 @@ export type GetChannelSubscribersOptionsType = {
}

export const getSubscribers = async (
options: GetChannelSubscribersOptionsType
options: GetChannelSubscribersOptionsType
): Promise<Subscribers> => {

try {

const {
channel,
page = 1,
Expand Down Expand Up @@ -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}`);
}
};
} catch (err) {
console.error(`[Push SDK] - API - Error - API send() -: `, err);
throw Error(`[Push SDK] - API - Error - API send() -: ${err}`);
}
};
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/channels/search.ts
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/restapi/src/lib/channels/subscribe.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -88,7 +88,7 @@ export const subscribe = async (options: SubscribeOptionsType) => {
},
};

await axios.post(requestUrl, body);
await axiosPost(requestUrl, body);

if (typeof onSuccess === 'function') onSuccess();

Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/channels/subscribeV2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers';
import {
getDomainInformation,
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 6 additions & 4 deletions packages/restapi/src/lib/channels/unsubscribe.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -89,7 +91,7 @@ export const unsubscribe = async (options: UnSubscribeOptionsType) => {
},
};

await axios.post(requestUrl, body);
await axiosPost(requestUrl, body);

if (typeof onSuccess === 'function') onSuccess();

Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/channels/unsubscribeV2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import { getCAIPAddress, getConfig, getCAIPDetails, Signer } from '../helpers';
import {
getDomainInformation,
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 2 additions & 6 deletions packages/restapi/src/lib/chat/approveRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import { getAPIBaseUrls, isValidETHAddress } from '../helpers';
import Constants, { PACKAGE_BUILD } from '../constants';
import { EnvOptionsType, SignerType } from '../types';
Expand All @@ -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';
Expand Down Expand Up @@ -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;
})
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/chat/chat.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/chat/chats.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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({
Expand Down
Loading