@nguyennhuy/zalo-api
Version:
Unofficial Zalo API for JavaScript
39 lines (35 loc) • 1.43 kB
JavaScript
;
var ZaloApiError = require('../../Errors/ZaloApiError.cjs');
var utils = require('../../utils.cjs');
const searchByUsernameFactory = utils.apiFactory()((api, _, utils) => {
const serviceURL = utils.makeURL(`${api.zpwServiceMap.friend[0]}/api/friend/search/by-user-name`);
/**
* Search by username
*
* @param username The username to search for
*
* @throws ZaloApiError
*/
return async function searchByUsername(user_name) {
if (!user_name)
throw new ZaloApiError.ZaloApiError("Missing phoneNumber");
const params = {
user_name,
avatar_size: 240,
};
const encryptedParams = utils.encodeAES(JSON.stringify(params));
if (!encryptedParams)
throw new ZaloApiError.ZaloApiError("Failed to encrypt message");
const finalServiceUrl = new URL(serviceURL);
finalServiceUrl.searchParams.append("params", encryptedParams);
const response = await utils.request(utils.makeURL(finalServiceUrl.toString(), {
params: encryptedParams,
}));
return utils.resolve(response, (result) => {
if (result.error && result.error.code != 216)
throw new ZaloApiError.ZaloApiError(result.error.message, result.error.code);
return result.data;
});
};
});
exports.searchByUsernameFactory = searchByUsernameFactory;