cometchat-api
simple cometchat api methods with typescript support
Install
# npm
npm install cometchat-api
# yarn
yarn add cometchat-api
# pnpm
pnpm add cometchat-api
Usage
import { useCometchatApi } from 'cometchat-api'
// set you env
const chat = useCometchatApi({
apiKey: process.env.COMETCHAT_API_KEY,
appId: process.env.COMETCHAT_APP_ID,
region: process.env.COMETCHAT_REGION
})
// use in code
const user = await chat.getUser('superhero1')
// {
// "data": {
// "uid": "superhero1",
// "name": "Iron Man"
// ...
// }
// }
if (user) {
console.log(user.data.name) // Iron Man
} else {
console.log(user) // null
}
Log Errors
import { isCometchatApiError } from 'cometchat-api'
import { AxiosError } from 'axios'
const errorHandler = (error: AxiosError) => {
if (error.response && isCometchatApiError(error.response.data)) {
console.error(error)
}
return Promise.reject(error)
}
// https://github.com/axios/axios#interceptors
chat.instance.interceptors.response.use(
(response) => response,
(error) => errorHandler(error)
)