@solufy/evolution-sdk
Version:
Unofficial SDK for the Evolution Whatsapp API v2
142 lines (139 loc) • 4.6 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/api/service.ts
var service_exports = {};
__export(service_exports, {
ApiService: () => ApiService
});
module.exports = __toCommonJS(service_exports);
// src/api/errors.ts
var import_zod = require("zod");
var EvolutionApiError = class _EvolutionApiError extends Error {
constructor(message, cause) {
const error = getErrorMessage(cause);
super(message, error ? void 0 : { cause });
this.name = _EvolutionApiError.name;
this.message = error ?? message;
}
};
var ErrorMessages = [
ErrorMessage(
import_zod.z.object({
message: import_zod.z.array(
import_zod.z.object({
exists: import_zod.z.literal(false),
jid: import_zod.z.string(),
number: import_zod.z.string()
})
)
}),
"Provided number is not a valid WhatsApp number"
),
ErrorMessage(
import_zod.z.object({
message: import_zod.z.array(import_zod.z.string().includes("Media upload failed on all hosts"))
}),
"Media upload failed on all hosts"
),
ErrorMessage(
import_zod.z.object({
message: import_zod.z.array(import_zod.z.string().includes("AxiosError"))
}),
(response) => response.message[0]
),
ErrorMessage(
import_zod.z.object({
message: import_zod.z.array(import_zod.z.string().includes("No session"))
}),
"No session found, try restarting your instance"
),
ErrorMessage(
import_zod.z.object({
message: import_zod.z.array(import_zod.z.string().includes("AggregateError"))
}),
"AggregateError"
)
];
function getErrorMessage(response) {
const error = ErrorMessages.find(
(message) => message.schema.safeParse(response).success
);
return error ? typeof error.message === "string" ? error.message : (
// biome-ignore lint/suspicious/noExplicitAny: Generic
error.message(response)
) : void 0;
}
function ErrorMessage(schema, message) {
return { schema, message };
}
// src/api/service.ts
var ApiService = class {
constructor(options) {
this.options = options;
}
async get(path, options = {}) {
return this.request(path, { ...options, method: "GET" });
}
async post(path, options = {}) {
return this.request(path, { ...options, method: "POST" });
}
async put(path, options = {}) {
return this.request(path, { ...options, method: "PUT" });
}
async patch(path, options = {}) {
return this.request(path, { ...options, method: "PATCH" });
}
async delete(path, options = {}) {
return this.request(path, { ...options, method: "DELETE" });
}
async request(path, options = {}) {
const { init, params } = this.makeInit(options);
const url = new URL(
`/${path}/${this.options.instance}/?${params}`,
this.options.serverUrl
);
const response = await fetch(url, init);
const data = await response.json();
if (!response.ok || "error" in data) {
throw new EvolutionApiError(
`${this.options.instance} ${data.error || "Unknown Error"}`,
data.response
);
}
return data;
}
makeInit(options) {
const { params: _, headers, body, ...rest } = options;
const paramsInit = options.params && Object.entries(options.params).filter(([, value]) => Boolean(value)).map(([key, value]) => [key, String(value)]);
const params = new URLSearchParams(paramsInit);
const init = {
...rest,
headers: { ...headers || {}, apikey: this.options.token }
};
if (body) {
init.headers["Content-Type"] = body instanceof FormData ? "multipart/form-data" : "application/json";
init.body = body instanceof FormData ? body : JSON.stringify(body);
}
return { init, params };
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ApiService
});
//# sourceMappingURL=service.js.map