naruyaizumi
Version:
A WebSockets library for interacting with WhatsApp Web
51 lines (50 loc) • 1.88 kB
JavaScript
import { Boom } from "@hapi/boom";
import { getBinaryNodeChild, S_WHATSAPP_NET } from "../WABinary/index.js";
const wMexQuery = (variables, queryId, query, generateMessageTag) => {
return query({
tag: "iq",
attrs: {
id: generateMessageTag(),
type: "get",
to: S_WHATSAPP_NET,
xmlns: "w:mex",
},
content: [
{
tag: "query",
attrs: { query_id: queryId },
content: Buffer.from(JSON.stringify({ variables }), "utf-8"),
},
],
});
};
export const executeWMexQuery = async (variables, queryId, dataPath, query, generateMessageTag) => {
const result = await wMexQuery(variables, queryId, query, generateMessageTag);
const child = getBinaryNodeChild(result, "result");
if (child?.content) {
const data = JSON.parse(child.content.toString());
if (data.errors && data.errors.length > 0) {
const errorMessages = data.errors
.map((err) => err.message || "Unknown error")
.join(", ");
const firstError = data.errors[0];
const errorCode = firstError.extensions?.error_code || 400;
throw new Boom(`GraphQL server error: ${errorMessages}`, {
statusCode: errorCode,
data: firstError,
});
}
const response = dataPath ? data?.data?.[dataPath] : data?.data;
if (typeof response !== "undefined") {
return response;
}
}
const action = (dataPath || "").startsWith("xwa2_")
? dataPath.substring(5).replace(/_/g, " ")
: dataPath?.replace(/_/g, " ");
throw new Boom(`Failed to ${action}, unexpected response structure.`, {
statusCode: 400,
data: result,
});
};
//# sourceMappingURL=mex.js.map