notifycx
Version:
NotifyCX API Client
152 lines (150 loc) • 4.51 kB
JavaScript
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var Notify = class {
constructor(apiKey, apiUrl = "https://notify.cx/api") {
if (!apiKey) {
throw new Error("API key is required");
}
this.apiKey = apiKey;
this.apiUrl = apiUrl;
}
sendEmail(params) {
return __async(this, null, function* () {
try {
const response = yield fetch(`${this.apiUrl}/send-email`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
},
body: JSON.stringify(params)
});
const data = yield response.json();
if (!response.ok) {
throw new Error(data.message || response.statusText);
}
return data;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to send email: ${error.message}`);
}
throw new Error("Failed to send email: An unknown error occurred");
}
});
}
sendTestEmail(params) {
return __async(this, null, function* () {
try {
const response = yield fetch(`${this.apiUrl}/test/send-email`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
},
body: JSON.stringify(params)
});
const data = yield response.json();
if (!response.ok) {
throw new Error(data.message || response.statusText);
}
return data;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to send test email: ${error.message}`);
}
throw new Error("Failed to send test email: An unknown error occurred");
}
});
}
sendEmailFromTemplate(params) {
return __async(this, null, function* () {
try {
const response = yield fetch(`${this.apiUrl}/send-email-from-template`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
},
body: JSON.stringify({
templateId: params.templateId,
from: params == null ? void 0 : params.from,
to: params.to,
variables: params == null ? void 0 : params.variables
})
});
const data = yield response.json();
if (!response.ok) {
throw new Error(data.message || response.statusText);
}
return data;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to send email from template: ${error.message}`);
}
throw new Error(
"Failed to send email from template: An unknown error occurred"
);
}
});
}
sendTestEmailFromTemplate(params) {
return __async(this, null, function* () {
try {
const response = yield fetch(
`${this.apiUrl}/test/send-email-from-template`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
},
body: JSON.stringify({
templateId: params.templateId,
from: params == null ? void 0 : params.from,
to: params.to,
variables: params == null ? void 0 : params.variables
})
}
);
const data = yield response.json();
if (!response.ok) {
throw new Error(data.message || response.statusText);
}
return data;
} catch (error) {
if (error instanceof Error) {
throw new Error(
`Failed to send test email from template: ${error.message}`
);
}
throw new Error(
"Failed to send test email from template: An unknown error occurred"
);
}
});
}
};
var index_default = Notify;
export {
Notify,
index_default as default
};