@solufy/evolution-sdk
Version:
Unofficial SDK for the Evolution Whatsapp API v2
116 lines (115 loc) • 3.37 kB
JavaScript
// src/api/errors.ts
import { z } from "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(
z.object({
message: z.array(
z.object({
exists: z.literal(false),
jid: z.string(),
number: z.string()
})
)
}),
"Provided number is not a valid WhatsApp number"
),
ErrorMessage(
z.object({
message: z.array(z.string().includes("Media upload failed on all hosts"))
}),
"Media upload failed on all hosts"
),
ErrorMessage(
z.object({
message: z.array(z.string().includes("AxiosError"))
}),
(response) => response.message[0]
),
ErrorMessage(
z.object({
message: z.array(z.string().includes("No session"))
}),
"No session found, try restarting your instance"
),
ErrorMessage(
z.object({
message: z.array(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 };
}
};
export {
ApiService
};
//# sourceMappingURL=service.mjs.map