diff --git a/packages/restapi/src/lib/channels/getAllTags.ts b/packages/restapi/src/lib/channels/getAllTags.ts new file mode 100644 index 000000000..8ab2f46c8 --- /dev/null +++ b/packages/restapi/src/lib/channels/getAllTags.ts @@ -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); + }); +}; diff --git a/packages/restapi/src/lib/channels/getChannels.ts b/packages/restapi/src/lib/channels/getChannels.ts index 9ea71d88b..4fb8528e9 100644 --- a/packages/restapi/src/lib/channels/getChannels.ts +++ b/packages/restapi/src/lib/channels/getChannels.ts @@ -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} */ @@ -16,6 +14,7 @@ type getChannelsOptionsType = { limit?: number; sort?: string; order?: string; + filter?: number; } export const getChannels = async (options: getChannelsOptionsType) => { @@ -25,11 +24,12 @@ export const getChannels = async (options: getChannelsOptionsType) => { limit = 10, sort = CONSTANTS.FILTER.CHANNEL_LIST.SORT.SUBSCRIBER, order = CONSTANTS.FILTER.CHANNEL_LIST.ORDER.DESCENDING, + filter } = 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 : ''}`; return await axiosGet(requestUrl) .then((response) => { diff --git a/packages/restapi/src/lib/channels/index.ts b/packages/restapi/src/lib/channels/index.ts index 1dae8c02f..a2a20849c 100644 --- a/packages/restapi/src/lib/channels/index.ts +++ b/packages/restapi/src/lib/channels/index.ts @@ -11,3 +11,4 @@ export * from './subscribe'; export * from './subscribeV2'; export * from './unsubscribe'; export * from './unsubscribeV2'; +export * from './getAllTags' \ No newline at end of file diff --git a/packages/restapi/src/lib/channels/search.ts b/packages/restapi/src/lib/channels/search.ts index 0abe5a628..bef8f4037 100644 --- a/packages/restapi/src/lib/channels/search.ts +++ b/packages/restapi/src/lib/channels/search.ts @@ -1,11 +1,11 @@ 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 = { @@ -13,16 +13,19 @@ export type SearchChannelOptionsType = { env?: ENV; page?: number; limit?: number; -} + filter?: number; + // 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, + oldFormat = true, } = options || {}; if (!query) throw Error('"query" not provided!'); @@ -31,12 +34,27 @@ export const search = async ( const queryObj = { page, limit: getLimit(limit), - query: query + query, + ...(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.total || 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); }); -} +}; diff --git a/packages/restapi/src/lib/constantsV2.ts b/packages/restapi/src/lib/constantsV2.ts index c7662e75f..ef27e461c 100644 --- a/packages/restapi/src/lib/constantsV2.ts +++ b/packages/restapi/src/lib/constantsV2.ts @@ -65,6 +65,11 @@ const CONSTANTS = { ORDER: ChannelListOrderType, }, NOTIFICATION_TYPE: NotifictaionType, + TAGS: { + USER: 'USER', + PUSH: 'PUSH', + ALL: '*', + }, }, }; diff --git a/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts b/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts index 7f9b29391..e8a6f5861 100644 --- a/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts +++ b/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts @@ -53,6 +53,9 @@ export type FeedsOptions = { export type ChannelSearchOptions = { page?: number; limit?: number; + filter?: number; + // temp fix to support both new and old format + oldFormat?: boolean; }; // Types related to notification @@ -152,9 +155,15 @@ export type ChannelListOptions = { limit?: number; sort?: ChannelListSortType; order?: ChannelListOrderType; + filter?: number; }; - +export type TagListOptions = { + page?: number; + limit?: number; + order?: ChannelListOrderType; + filter?: "PUSH" | "USER" | "*"; +} diff --git a/packages/restapi/src/lib/pushNotification/channel.ts b/packages/restapi/src/lib/pushNotification/channel.ts index ae939e4fa..0039a522c 100644 --- a/packages/restapi/src/lib/pushNotification/channel.ts +++ b/packages/restapi/src/lib/pushNotification/channel.ts @@ -77,12 +77,16 @@ export class Channel extends PushNotificationBaseClass { const { page = Constants.PAGINATION.INITIAL_PAGE, limit = Constants.PAGINATION.LIMIT, + filter, + oldFormat = true } = options || {}; return await PUSH_CHANNEL.search({ query: query, page: page, limit: limit, + filter: filter, env: this.env, + oldFormat }); } catch (error) { throw new Error(`Push SDK Error: API : channel::search : ${error}`); @@ -472,6 +476,7 @@ export class Channel extends PushNotificationBaseClass { limit, sort = ChannelListSortType.SUBSCRIBER, order = ChannelListOrderType.DESCENDING, + filter, } = options || {}; return await PUSH_CHANNEL.getChannels({ @@ -480,6 +485,7 @@ export class Channel extends PushNotificationBaseClass { limit, sort, order, + filter }); } catch (error) { throw new Error(`Push SDK Error: Contract : channel::list : ${error}`); diff --git a/packages/restapi/src/lib/pushNotification/tags.ts b/packages/restapi/src/lib/pushNotification/tags.ts index dbe035102..b0fdcf3be 100644 --- a/packages/restapi/src/lib/pushNotification/tags.ts +++ b/packages/restapi/src/lib/pushNotification/tags.ts @@ -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; } @@ -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, @@ -39,24 +48,23 @@ export class Tags extends PushNotificationBaseClass { add = async (tags: Array) => { 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} tags - tags to be added * @returns the tags if the transaction is successfull @@ -64,16 +72,16 @@ export class Tags extends PushNotificationBaseClass { update = async (tags: Array) => { 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}`); } @@ -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}`); } @@ -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}`); + } + }; } diff --git a/packages/restapi/tests/lib/channel/getChannels.test.ts b/packages/restapi/tests/lib/channel/getChannels.test.ts new file mode 100644 index 000000000..1c97780d8 --- /dev/null +++ b/packages/restapi/tests/lib/channel/getChannels.test.ts @@ -0,0 +1,13 @@ +import { getChannels } from '../../../src/lib/channels/getChannels'; +import { ENV } from '../../../src/lib/constants'; +import { expect } from 'chai'; + +describe('PUSH_CHANNELS.getChannels', () => { + it('Should fetch channels based on the filter', async () => { + const res = await getChannels({ + filter: 80002, + env: ENV.DEV, + }); + console.log(res); + }); +}); diff --git a/packages/restapi/tests/lib/channel/search.test.ts b/packages/restapi/tests/lib/channel/search.test.ts new file mode 100644 index 000000000..d82c002ad --- /dev/null +++ b/packages/restapi/tests/lib/channel/search.test.ts @@ -0,0 +1,24 @@ +import { search } from '../../../src/lib/channels/search'; +import { ENV } from '../../../src/lib/constants'; +import { expect } from 'chai'; + +describe('PUSH_CHANNELS.search', () => { + it('Should fetch channels based on the filter', async () => { + const res = await search({ + filter: 80002, + env: ENV.DEV, + query: "Node" + }); + console.log(res); + }); + + it.only('Should fetch channels based on the filter in new format', async () => { + const res = await search({ + filter: 80002, + env: ENV.DEV, + query: "Node", + oldFormat: false + }); + console.log(res); + }); +}); diff --git a/packages/restapi/tests/lib/notification/channel.test.ts b/packages/restapi/tests/lib/notification/channel.test.ts index f2193db11..189ee28f0 100644 --- a/packages/restapi/tests/lib/notification/channel.test.ts +++ b/packages/restapi/tests/lib/notification/channel.test.ts @@ -106,6 +106,12 @@ describe('PushAPI.channel functionality', () => { expect(res).not.null; }); + it('With signer: Should return response in new fromat', async () => { + const res = await userBob.channel.search('Node', { oldFormat: false }); + // console.log(res); + expect(res).not.null; + }); + it('Should throw error for empty query', async () => { // const res = await userBob.channel.search('') await expect(() => userBob.channel.search('')).to.Throw; @@ -586,4 +592,11 @@ describe('PushAPI.channel functionality', () => { expect(res).not.null; }); }); + describe('tags', () => { + it('Should fetch all tags', async () => { + const res = await userAlice.channel.tags.list(); + console.log(res); + expect(res).not.null; + }); + }); });