UNPKG

@owlrelay/api-sdk

Version:

OwlRelay API SDK for JavaScript and TypeScript

90 lines (84 loc) 2.51 kB
import { ofetch } from 'ofetch'; const version = "0.0.2"; function createApiClient({ apiKey, baseApiUrl }) { const apiClient = ofetch.create({ headers: { "Authorization": `Bearer ${apiKey}`, "X-OwlRelay-Source": `owlrelay-api-sdk-javascript/${version}` }, baseURL: baseApiUrl }); return { apiClient }; } function coerceDate(obj) { return { ...obj, createdAt: new Date(obj.createdAt), updatedAt: new Date(obj.updatedAt) }; } function getEmailIdentifier(args) { if ("emailId" in args) { return { emailIdentifier: args.emailId }; } if ("emailAddress" in args) { return { emailIdentifier: args.emailAddress }; } return { emailIdentifier: `${args.username}@${args.domain}` }; } const OWLRELAY_API_BASE_URL = "https://api.owlrelay.email"; function createClient({ apiKey, baseApiUrl = OWLRELAY_API_BASE_URL }) { const { apiClient } = createApiClient({ apiKey, baseApiUrl }); const updateEmail = async ({ emailId, ...body }) => { const { emailCallback } = await apiClient(`/api/email-callbacks/${emailId}`, { method: "PUT", body }); return coerceDate(emailCallback); }; return { updateEmail, enableEmail: async ({ emailId }) => { return updateEmail({ emailId, isEnabled: true }); }, disableEmail: async ({ emailId }) => { return updateEmail({ emailId, isEnabled: false }); }, getEmails: async () => { const { emailCallbacks } = await apiClient("/api/email-callbacks", { method: "GET" }); return emailCallbacks.map(coerceDate); }, createEmail: async (body) => { const { emailCallback } = await apiClient("/api/email-callbacks", { method: "POST", body }); return coerceDate(emailCallback); }, deleteEmail: async (args) => { const { emailIdentifier } = getEmailIdentifier(args); await apiClient(`/api/email-callbacks/${emailIdentifier}`, { method: "DELETE" }); }, getEmail: async ({ emailId }) => { const { emailCallback } = await apiClient(`/api/email-callbacks/${emailId}`, { method: "GET" }); return coerceDate(emailCallback); }, getEmailProcessings: async ({ emailId }) => { const { processings } = await apiClient(`/api/email-callbacks/${emailId}/processings`, { method: "GET" }); return processings.map(coerceDate); } }; } export { OWLRELAY_API_BASE_URL, createClient }; //# sourceMappingURL=index.mjs.map