UNPKG

kufuli-whatsapp

Version:

Official Kufuli whatsapp API package

435 lines (428 loc) 11.7 kB
// src/Contact/index.ts var Contact = class { apiKey; apiSecret; axiosClient; requestConfig; constructor(apiKey, apiSecret, axiosClient) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.axiosClient = axiosClient; this.requestConfig = { "X-API-Key": this.apiKey, "X-API-Secret": this.apiSecret }; } getContacts = async (sessionId, cursor = 0, limit = 25, search) => { return await this.axiosClient.request({ url: `/contacts/${sessionId}`, method: "GET", params: { cursor, limit, search }, headers: this.requestConfig }); }; getBlockList = async (sessionId) => { return await this.axiosClient.request({ url: `/contacts/${sessionId}/blocklist`, method: "GET", headers: this.requestConfig }); }; updateBlockList = async (sessionId, data) => { return await this.axiosClient.request({ url: `/contacts/${sessionId}/blocklist/update`, data, method: "POST", headers: this.requestConfig }); }; check = async (sessionId, jid) => { return await this.axiosClient.request({ url: `/contacts/${sessionId}/${jid}`, method: "GET", headers: this.requestConfig }); }; photo = async (sessionId, jid) => { return await this.axiosClient.request({ url: `/contacts/${sessionId}/${jid}/photo`, method: "GET", headers: this.requestConfig }); }; }; var Contact_default = Contact; // src/interfaces/IMessage.interface.ts var FontType = /* @__PURE__ */ ((FontType2) => { FontType2[FontType2["CALISTOGA_REGULAR"] = 8] = "CALISTOGA_REGULAR"; FontType2[FontType2["COURIERPRIME_BOLD"] = 10] = "COURIERPRIME_BOLD"; FontType2[FontType2["EXO2_EXTRABOLD"] = 9] = "EXO2_EXTRABOLD"; FontType2[FontType2["FB_SCRIPT"] = 2] = "FB_SCRIPT"; FontType2[FontType2["MORNINGBREEZE_REGULAR"] = 7] = "MORNINGBREEZE_REGULAR"; FontType2[FontType2["SYSTEM"] = 0] = "SYSTEM"; FontType2[FontType2["SYSTEM_BOLD"] = 6] = "SYSTEM_BOLD"; FontType2[FontType2["SYSTEM_TEXT"] = 1] = "SYSTEM_TEXT"; return FontType2; })(FontType || {}); // src/Message/index.ts var Message = class { apiKey; apiSecret; axiosClient; requestConfig; constructor(apiKey, apiSecret, axiosClient) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.axiosClient = axiosClient; this.requestConfig = { "X-API-Key": this.apiKey, "X-API-Secret": this.apiSecret }; } async getMessages(sessionId, cursor, limit, search) { return await this.axiosClient.request({ url: `/messages/${sessionId}`, method: "GET", params: { cursor, limit, search }, headers: this.requestConfig }); } async sendTextMessage(sessionId, data) { return await this.axiosClient.request({ url: `/messages/${sessionId}/send-text`, method: "POST", data, headers: this.requestConfig }); } async sendImageMessage(sessionId, data) { return await this.axiosClient.request({ url: `/messages/${sessionId}/send-image`, method: "POST", data, headers: this.requestConfig }); } async sendVideoMessage(sessionId, data) { return await this.axiosClient.request({ url: `/messages/${sessionId}/send-video`, method: "POST", data, headers: this.requestConfig }); } async sendAudioMessage(sessionId, data) { return await this.axiosClient.request({ url: `/messages/${sessionId}/send-audio`, method: "POST", data, headers: this.requestConfig }); } async sendDocumentMessage(sessionId, data) { return await this.axiosClient.request({ url: `/messages/${sessionId}/send-document`, method: "POST", data, headers: this.requestConfig }); } }; var Message_default = Message; // src/Session/index.ts var Session = class { apiKey; apiSecret; axiosClient; requestConfig; constructor(apiKey, apiSecret, axiosClient) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.axiosClient = axiosClient; this.requestConfig = { "X-API-Key": this.apiKey, "X-API-Secret": this.apiSecret }; } connect = async (id, type, phoneNumber) => { var _a; try { const requestData = { sessionId: id, phoneNumber }; const res = await this.axiosClient.request({ url: `/sessions/add`, data: requestData, method: "POST", headers: this.requestConfig }); if (res && typeof res === "object") { if (type === "qr" && "qr" in res) { return res.qr; } else if (type === "phoneNumber" && "code" in res) { return res.code; } else { return { error: "Unexpected response format" }; } } else { return { error: "Unexpected response format" }; } } catch (error) { return { error: ((_a = error.response) == null ? void 0 : _a.message) || error.message || "An unexpected error occurred" }; } }; connectSSE = async (id, type) => { var _a; try { const requestData = { sessionId: id }; let res; switch (type) { case "phoneNumber": res = await this.axiosClient.request({ url: `/sessions/add/${type}`, data: requestData, method: "POST", headers: this.requestConfig }); break; case "qr": res = await this.axiosClient.request({ url: `/sessions/add/`, data: requestData, method: "POST", headers: this.requestConfig }); break; default: return { error: "Invalid connection type" }; } if (res && typeof res === "object") { if (type === "qr" && "qr" in res) { return res.qr; } else if (type === "phoneNumber" && "code" in res) { return res.code; } else { return { error: "Unexpected response format" }; } } else { return { error: "Unexpected response format" }; } } catch (error) { return { error: ((_a = error.response) == null ? void 0 : _a.message) || error.message || "An unexpected error occurred" }; } }; deleteSession = async (sessionId) => { return await this.axiosClient.request({ url: `/sessions/${sessionId}`, method: "DELETE", headers: this.requestConfig }); }; getSession = async (sessionId) => { return await this.axiosClient.request({ url: `/sessions/${sessionId}`, method: "GET", headers: this.requestConfig }); }; getSessionStatus = async (sessionId) => { return await this.axiosClient.request({ url: `/sessions/${sessionId}/status`, method: "GET", headers: this.requestConfig }); }; getSessions = async () => { return await this.axiosClient.request({ url: `/sessions/`, method: "GET", headers: this.requestConfig }); }; }; // src/Story/index.ts var Story = class { apiKey; apiSecret; axiosClient; requestConfig; constructor(apiKey, apiSecret, axiosClient) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.axiosClient = axiosClient; this.requestConfig = { "X-API-Key": this.apiKey, "X-API-Secret": this.apiSecret }; } // getStories = async ( // sessionId: string, // cursor?: number, // limit?: number, // search?: string // ): Promise<StoriesType> => { // return await this.axiosClient.request({ // url: `/stories/${sessionId}`, // method: "GET", // params: { // cursor: cursor, // limit: limit, // search: search, // }, // headers: this.requestConfig, // }); // }; async sendTextStory(sessionId, data) { return await this.axiosClient.request({ url: `/stories/${sessionId}/send-text`, method: "POST", data, headers: this.requestConfig }); } async sendImageStory(sessionId, data) { return await this.axiosClient.request({ url: `/stories/${sessionId}/send-image`, method: "POST", data, headers: this.requestConfig }); } async sendVideoStory(sessionId, data) { return await this.axiosClient.request({ url: `/stories/${sessionId}/send-video`, method: "POST", data, headers: this.requestConfig }); } async sendAudioStory(sessionId, data) { return await this.axiosClient.request({ url: `/stories/${sessionId}/send-audio`, method: "POST", data, headers: this.requestConfig }); } }; var Story_default = Story; // src/utils/axios.util.ts import axios from "axios"; var CustomAxiosConfig = class { environment; client; constructor(environment) { this.environment = environment; this.client = axios.create({ baseURL: this.environment === "test" ? "http://kufuli:8000/api/v1/whatsapp/api/v1" : "https://server.api.kufuli.com/api/v1/whatsapp/api/v1", headers: { "Content-Type": "application/json" }, timeout: 3e4 // 30 seconds timeout }); this.client.interceptors.request.use( (config) => { return config; }, (error) => { return Promise.reject(error); } ); this.client.interceptors.response.use( (response) => response, (error) => { if (error.code === "ECONNABORTED") { return Promise.reject({ status: 408, message: "Request timeout", data: "The request took too long to complete" }); } return Promise.reject(error); } ); } request = async (options) => { const onSuccess = (response) => { return response.data; }; const onError = (error) => { var _a, _b, _c; const status = ((_a = error.response) == null ? void 0 : _a.status) || 500; const message = ((_b = error.response) == null ? void 0 : _b.statusText) || "Unknown error"; const errorData = ((_c = error.response) == null ? void 0 : _c.data) || "An unexpected error occurred"; if (status === 401) { console.error("Unauthorized access:", errorData); } else if (status === 403) { console.error("Forbidden access:", errorData); } else if (status === 404) { console.error("Resource not found:", errorData); } throw { status, message, data: errorData }; }; try { const response = await this.client(options); return onSuccess(response); } catch (error) { return onError(error); } }; }; // src/index.ts var KufuliWhatsappApi = class { apiKey; apiSecret; environment; axiosClient; FontType; session; contact; message; story; constructor(apiKey, apiSecret, environment) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.environment = environment; this.axiosClient = new CustomAxiosConfig(this.environment); this.FontType = FontType; this.session = new Session( this.apiKey, this.apiSecret, this.axiosClient ); this.contact = new Contact_default( this.apiKey, this.apiSecret, this.axiosClient ); this.message = new Message_default( this.apiKey, this.apiSecret, this.axiosClient ); this.story = new Story_default(this.apiKey, this.apiSecret, this.axiosClient); } }; export { KufuliWhatsappApi };