Skip to content

Commit

Permalink
Merge branch 'main' of github.com:push-protocol/push-sdk into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitmalhotra1420 committed Oct 7, 2024
2 parents f972997 + 5bef248 commit 0ef40ee
Show file tree
Hide file tree
Showing 13 changed files with 30,528 additions and 21,653 deletions.
43 changes: 43 additions & 0 deletions packages/restapi/src/lib/channels/getAllTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getCAIPAddress, getAPIBaseUrls, getQueryParams } from '../helpers';
import Constants, { ENV } from '../constants';
import { axiosGet } from '../utils/axiosUtil';
import CONSTANTS from '../constantsV2';

/**
* GET v1/channels/tags/all
*/
export type GetAllTagsOptionsType = {
page?: number;
limit?: number;
order?: string;
filter?:string;
env?: ENV;
};

/**
* Returns the tags available based on the filter provided
*/
export const getAllTags = async (options: GetAllTagsOptionsType) => {
const {
page=1,
limit=10,
order=CONSTANTS.FILTER.CHANNEL_LIST.ORDER.DESCENDING,
filter=CONSTANTS.FILTER.TAGS.PUSH,
env = Constants.ENV.PROD,
} = options || {};

const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels`;
const queryObj = {
page,
limit,
order,
filter,
}
const requestUrl = `${apiEndpoint}/tags/all?${getQueryParams(queryObj)}`;
return await axiosGet(requestUrl)
.then((response) => response.data?.tags)
.catch((err) => {
console.error(`[EPNS-SDK] - API ${requestUrl}: `, err);
});
};
21 changes: 12 additions & 9 deletions packages/restapi/src/lib/channels/getChannels.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ENV } from '../constants';
import CONSTANTS from '../constantsV2';

import { getAPIBaseUrls, getCAIPAddress } from '../helpers';
import { axiosGet } from '../utils/axiosUtil';
import { parseSettings } from '../utils/parseSettings';

/**
* GET /v1/channels/{addressinCAIP}
*/
Expand All @@ -16,21 +14,26 @@ type getChannelsOptionsType = {
limit?: number;
sort?: string;
order?: string;
}
filter?: number;
tag?: string;
};

export const getChannels = async (options: getChannelsOptionsType) => {
const {
env = CONSTANTS.ENV.PROD,
page = 1,
limit = 10,
const {
env = CONSTANTS.ENV.PROD,
page = 1,
limit = 10,
sort = CONSTANTS.FILTER.CHANNEL_LIST.SORT.SUBSCRIBER,
order = CONSTANTS.FILTER.CHANNEL_LIST.ORDER.DESCENDING,
filter,
tag,
} = options || {};

const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels`;
const requestUrl = `${apiEndpoint}?page=${page}&limit=${limit}&sort=${sort}&order=${order}`;

const requestUrl = `${apiEndpoint}?page=${page}&limit=${limit}&sort=${sort}&order=${order}${
filter ? '&filter=' + filter : ''
}${tag ? '&tag=' + tag : ''}`;
return await axiosGet(requestUrl)
.then((response) => {
return response.data;
Expand Down
1 change: 1 addition & 0 deletions packages/restapi/src/lib/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './subscribe';
export * from './subscribeV2';
export * from './unsubscribe';
export * from './unsubscribeV2';
export * from './getAllTags'
41 changes: 31 additions & 10 deletions packages/restapi/src/lib/channels/search.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { getAPIBaseUrls, getQueryParams, getLimit } from '../helpers';
import Constants, {ENV} from '../constants';
import Constants, { ENV } from '../constants';
import { axiosGet } from '../utils/axiosUtil';

/**
* GET /v1/channels/search/
* GET /v1/channels/search/
* optional params: page=(1)&limit=(20{min:1}{max:30})&query=(searchquery)
*
*
*/

export type SearchChannelOptionsType = {
query: string;
env?: ENV;
page?: number;
limit?: number;
}
filter?: number;
tag?: string;
// temp fix to support both new and old format
oldFormat?: boolean;
};

export const search = async (
options: SearchChannelOptionsType
) => {
export const search = async (options: SearchChannelOptionsType) => {
const {
query,
env = Constants.ENV.PROD,
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
filter,
tag,
oldFormat = true,
} = options || {};

if (!query) throw Error('"query" not provided!');
Expand All @@ -31,12 +36,28 @@ export const search = async (
const queryObj = {
page,
limit: getLimit(limit),
query: query
query,
...(tag && { tag }),
...(filter && { filter }),
};
const requestUrl = `${apiEndpoint}?${getQueryParams(queryObj)}`;
return axiosGet(requestUrl)
.then((response) => response.data.channels)
.then((response) => {
const channels = response.data.channels;
const itemCount = response.data.itemCount || channels.length;

const formattedResponse = {
itemCount,
result: channels,
};

if (typeof oldFormat !== 'undefined' && oldFormat) {
return channels; // Old format: return array directly
}

return formattedResponse; // New format: return {itemCount, result}
})
.catch((err) => {
console.error(`[Push SDK] - API ${requestUrl}: `, err);
});
}
};
5 changes: 5 additions & 0 deletions packages/restapi/src/lib/constantsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ const CONSTANTS = {
ORDER: ChannelListOrderType,
},
NOTIFICATION_TYPE: NotifictaionType,
TAGS: {
USER: 'USER',
PUSH: 'PUSH',
ALL: '*',
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type FeedsOptions = {
export type ChannelSearchOptions = {
page?: number;
limit?: number;
filter?: number;
// temp fix to support both new and old format
oldFormat?: boolean;
tag?: string;
};

// Types related to notification
Expand Down Expand Up @@ -152,9 +156,16 @@ export type ChannelListOptions = {
limit?: number;
sort?: ChannelListSortType;
order?: ChannelListOrderType;
filter?: number;
tag?: string;
};


export type TagListOptions = {
page?: number;
limit?: number;
order?: ChannelListOrderType;
filter?: "PUSH" | "USER" | "*";
}



Expand Down
10 changes: 10 additions & 0 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ export class Channel extends PushNotificationBaseClass {
const {
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
filter,
tag,
oldFormat = true
} = options || {};
return await PUSH_CHANNEL.search({
query: query,
page: page,
limit: limit,
filter: filter,
tag: tag,
env: this.env,
oldFormat
});
} catch (error) {
throw new Error(`Push SDK Error: API : channel::search : ${error}`);
Expand Down Expand Up @@ -472,6 +478,8 @@ export class Channel extends PushNotificationBaseClass {
limit,
sort = ChannelListSortType.SUBSCRIBER,
order = ChannelListOrderType.DESCENDING,
filter,
tag,
} = options || {};

return await PUSH_CHANNEL.getChannels({
Expand All @@ -480,6 +488,8 @@ export class Channel extends PushNotificationBaseClass {
limit,
sort,
order,
filter,
tag
});
} catch (error) {
throw new Error(`Push SDK Error: Contract : channel::list : ${error}`);
Expand Down
62 changes: 45 additions & 17 deletions packages/restapi/src/lib/pushNotification/tags.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';
import { ChannelInfoOptions, ChannelSearchOptions } from './PushNotificationTypes';
import {
ChannelInfoOptions,
ChannelSearchOptions,
TagListOptions
} from './PushNotificationTypes';
import * as PUSH_CHANNEL from '../channels';
import { PushNotificationBaseClass } from './pushNotificationBase';
import { Channel } from './channel';

import ConstantsV2 from '../constantsV2';
export class Tags extends PushNotificationBaseClass {
private channel: Channel;

constructor(channel: Channel, signer?: SignerType, env?: ENV, account?: string) {
constructor(
channel: Channel,
signer?: SignerType,
env?: ENV,
account?: string
) {
super(signer, env, account);
this.channel = channel;
}
Expand All @@ -21,7 +30,7 @@ export class Tags extends PushNotificationBaseClass {
get = async (options?: ChannelInfoOptions) => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();
return await PUSH_CHANNEL.getTags({
channel: channel,
env: this.env,
Expand All @@ -39,41 +48,40 @@ export class Tags extends PushNotificationBaseClass {
add = async (tags: Array<string>) => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();

const resp = await this.channel.update({
name: channel.name,
description: channel.info,
url: channel.url,
icon: channel.icon,
tags: tags
tags: tags,
});

return { tags }

return { tags };
} catch (error) {
throw new Error(`Push SDK Error: Contract : tags::add : ${error}`);
}
};

/**
/**
* @description update tags for a channel
* @param {Array<string>} tags - tags to be added
* @returns the tags if the transaction is successfull
*/
update = async (tags: Array<string>) => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();
await this.channel.update({
name: channel.name,
description: channel.info,
url: channel.url,
icon: channel.icon,
tags: tags
tags: tags,
});

return { tags }
return { tags };
} catch (error) {
throw new Error(`Push SDK Error: Contract : tags::update : ${error}`);
}
Expand All @@ -86,17 +94,16 @@ export class Tags extends PushNotificationBaseClass {
remove = async () => {
try {
this.checkSignerObjectExists();
const channel = await this.channel.info()
const channel = await this.channel.info();
await this.channel.update({
name: channel.name,
description: channel.info,
url: channel.url,
icon: channel.icon,
tags: []
tags: [],
});

return { status: "success" }

return { status: 'success' };
} catch (error) {
throw new Error(`Push SDK Error: Contract : tags::remove : ${error}`);
}
Expand Down Expand Up @@ -125,5 +132,26 @@ export class Tags extends PushNotificationBaseClass {
} catch (error) {
throw new Error(`Push SDK Error: API : channel::tags::search : ${error}`);
}
}
};

list = async (options?: TagListOptions) => {
try {
const {
page = Constants.PAGINATION.INITIAL_PAGE,
limit = Constants.PAGINATION.LIMIT,
order = ConstantsV2.FILTER.CHANNEL_LIST.ORDER.DESCENDING,
filter = ConstantsV2.FILTER.TAGS.PUSH,
} = options || {};

return await PUSH_CHANNEL.getAllTags({
page: page,
limit: limit,
order: order,
filter: filter,
env: this.env,
});
} catch (error) {
throw new Error(`Push SDK Error: API : channel::tags::list : ${error}`);
}
};
}
Loading

0 comments on commit 0ef40ee

Please sign in to comment.