alepha
Version:
Easy-to-use modern TypeScript framework for building many kind of applications.
174 lines (173 loc) • 4.57 kB
JavaScript
import { $inject, $module, AlephaError, KIND, Primitive, createPrimitive } from "alepha";
import { $logger } from "alepha/logger";
import { FileSystemProvider } from "alepha/system";
//#region ../../src/sms/providers/MemorySmsProvider.ts
var MemorySmsProvider = class {
log = $logger();
records = [];
async send(options) {
const { to, message } = options;
this.log.debug("Sending SMS to memory store", {
to,
message
});
for (const recipient of Array.isArray(to) ? to : [to]) this.records.push({
to: recipient,
message,
sentAt: /* @__PURE__ */ new Date()
});
}
/**
* Get the last SMS sent (for testing purposes).
*/
get last() {
return this.records[this.records.length - 1];
}
};
//#endregion
//#region ../../src/sms/providers/SmsProvider.ts
/**
* SMS provider interface.
*
* All methods are asynchronous and return promises.
*/
var SmsProvider = class {};
//#endregion
//#region ../../src/sms/primitives/$sms.ts
const $sms = (options = {}) => createPrimitive(SmsPrimitive, options);
/**
* SMS primitive for sending SMS messages through various providers.
*
* Usage:
* ```typescript
* class MyService {
* private readonly welcomeSms = $sms({ name: "welcome" });
*
* async sendWelcome(phoneNumber: string, userName: string) {
* await this.welcomeSms.send({
* to: phoneNumber,
* message: `Hello ${userName}! Welcome to our service.`
* });
* }
* }
* ```
*/
var SmsPrimitive = class extends Primitive {
provider = this.$provider();
get name() {
return this.options.name ?? `${this.config.propertyKey}`;
}
/**
* Send an SMS using the configured provider.
*/
async send(options) {
await this.alepha.events.emit("sms:sending", {
to: options.to,
template: this.name,
variables: {},
provider: this.provider,
abort: () => {
throw new AlephaError("SMS sending aborted by hook");
}
});
await this.provider.send(options);
await this.alepha.events.emit("sms:sent", {
to: options.to,
template: this.name,
provider: this.provider
});
}
$provider() {
if (!this.options.provider) return this.alepha.inject(SmsProvider);
if (this.options.provider === "memory") return this.alepha.inject(MemorySmsProvider);
return this.alepha.inject(this.options.provider);
}
};
$sms[KIND] = SmsPrimitive;
//#endregion
//#region ../../src/sms/errors/SmsError.ts
var SmsError = class extends AlephaError {
constructor(message, cause) {
super(message);
this.name = "SmsError";
this.cause = cause;
}
};
//#endregion
//#region ../../src/sms/providers/LocalSmsProvider.ts
var LocalSmsProvider = class {
log = $logger();
fs = $inject(FileSystemProvider);
directory;
constructor(options = {}) {
this.directory = options.directory ?? "node_modules/.alepha/sms";
}
async send(options) {
const { to, message } = options;
this.log.debug("Sending SMS to local file", {
to,
message,
directory: this.directory
});
try {
await this.fs.mkdir(this.directory, { recursive: true });
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
for (const recipient of Array.isArray(to) ? to : [to]) {
const filename = `${recipient.replace(/[^0-9+]/g, "_")},${timestamp}.sms.json`;
const filepath = this.fs.join(this.directory, filename);
const content = this.createSmsJson({
to: recipient,
message
});
await this.fs.writeFile(filepath, JSON.stringify(content, null, 2));
this.log.info("SMS saved to local file", {
filepath,
to
});
}
} catch (error) {
const message = `Failed to save SMS to local file: ${error instanceof Error ? error.message : String(error)}`;
this.log.error(message, {
to,
directory: this.directory
});
throw new SmsError(message, error instanceof Error ? error : void 0);
}
}
createSmsJson(options) {
return {
to: options.to,
message: options.message,
sentAt: (/* @__PURE__ */ new Date()).toISOString()
};
}
};
//#endregion
//#region ../../src/sms/index.ts
/**
* SMS delivery with multiple provider support.
*
* **Features:**
* - Send SMS with templates
* - Multiple recipients
* - Provider abstraction
*
* @module alepha.sms
*/
const AlephaSms = $module({
name: "alepha.sms",
primitives: [$sms],
services: [
SmsProvider,
MemorySmsProvider,
LocalSmsProvider
],
register: (alepha) => alepha.with({
optional: true,
provide: SmsProvider,
use: MemorySmsProvider
})
});
//#endregion
export { $sms, AlephaSms, LocalSmsProvider, MemorySmsProvider, SmsError, SmsPrimitive, SmsProvider };
//# sourceMappingURL=index.js.map