Skip to content

Commit

Permalink
feat: add group pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
strykerin committed Aug 31, 2023
1 parent 78a1177 commit 0ad4154
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 65 deletions.
65 changes: 33 additions & 32 deletions packages/restapi/src/lib/chat/getGroup.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
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';

/**
* GET /v1/chat/groups/:chatId
*/

export interface GetGroupType {
chatId: string,
env?: ENV
chatId: string;
env?: ENV;
page?: number;
limit?: number;
}

export const getGroup = async (
options: GetGroupType
): Promise<GroupDTO> => {
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<GroupDTO> => {
const {
chatId,
env = Constants.ENV.PROD,
page = 1,
limit = 30,
} = 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}?page=${page}&limit=${limit}`;
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}`);
}
};
76 changes: 43 additions & 33 deletions packages/restapi/src/lib/chat/getGroupByName.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
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';

/**
* GET /v1/chat/groups/:chatId
*/

export interface GetGroupByNameType {
groupName: string,
env?: ENV,
groupName: string;
env?: ENV;
page?: number;
limit?: number;
}

export const getGroupByName = async (
options: GetGroupByNameType
options: GetGroupByNameType
): Promise<GroupDTO> => {
const { groupName, env = Constants.ENV.PROD } = options || {};
try {
console.log("=============================================");
console.log("NOTICE: The method 'getGroupByName' will be deprecated on January 1st, 2024. Please update your code to remove this.");
console.log("=============================================");
if (groupName == null || groupName.length == 0) {
throw new Error(`Group Name cannot be null or empty`);
}

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);
});
} catch (err) {
console.error(`[Push SDK] - API - Error - API ${getGroupByName.name} -: `, err);
throw Error(`[Push SDK] - API - Error - API ${getGroupByName.name} -: ${err}`);
const {
groupName,
env = Constants.ENV.PROD,
page = 1,
limit = 30,
} = options || {};
try {
console.log('=============================================');
console.log(
"NOTICE: The method 'getGroupByName' will be deprecated on January 1st, 2024. Please update your code to remove this."
);
console.log('=============================================');
if (groupName == null || groupName.length == 0) {
throw new Error(`Group Name cannot be null or empty`);
}
};

const API_BASE_URL = getAPIBaseUrls(env);
const requestUrl = `${API_BASE_URL}/v1/chat/groups?groupName=${groupName}&page=${page}&limit=${limit}`;
return axios
.get(requestUrl)
.then((response) => {
return response.data;
})
.catch((err) => {
if (err?.response?.data) console.log(err);
throw new Error(err?.response?.data);
});
} catch (err) {
console.error(
`[Push SDK] - API - Error - API ${getGroupByName.name} -: `,
err
);
throw Error(
`[Push SDK] - API - Error - API ${getGroupByName.name} -: ${err}`
);
}
};

0 comments on commit 0ad4154

Please sign in to comment.