waitlist-mailer
Version:
Modern, modular TypeScript library for managing waitlists with pluggable storage and mail providers. Supports MongoDB, SQL databases, and custom adapters with zero required dependencies for basic usage.
127 lines (125 loc) • 3.82 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);
// lib/adapters/storage/MemoryStorage.ts
var MemoryStorage_exports = {};
__export(MemoryStorage_exports, {
MemoryStorage: () => MemoryStorage
});
module.exports = __toCommonJS(MemoryStorage_exports);
var MemoryStorage = class {
constructor() {
this.storage = /* @__PURE__ */ new Map();
}
/**
* Add an email to memory storage.
* @param email - The email to add
* @param data - Optional metadata
* @throws {Error} If the email already exists
*/
async add(email, data) {
const normalized = email.toLowerCase();
if (this.storage.has(normalized)) {
throw new Error(`Email already exists: ${email}`);
}
this.storage.set(normalized, {
email,
metadata: data,
createdAt: /* @__PURE__ */ new Date()
});
}
/**
* Remove an email from memory storage.
* @param email - The email to remove
* @returns true if the email was found and removed
*/
async remove(email) {
return this.storage.delete(email.toLowerCase());
}
/**
* Check if an email exists in storage.
* @param email - The email to check
* @returns true if the email exists
*/
async exists(email) {
return this.storage.has(email.toLowerCase());
}
/**
* Retrieve all entries from storage.
* @returns Array of all WaitlistEntry objects
*/
async getAll() {
return Array.from(this.storage.values());
}
/**
* Count total entries in storage.
* @returns Number of entries
*/
async count() {
return this.storage.size;
}
/**
* Clear all entries from storage.
*/
async clear() {
this.storage.clear();
}
/**
* Search entries by email pattern.
* For MemoryStorage, this filters the in-memory Map.
* @param pattern - The pattern to search for
* @param options - Optional search options
* @returns Matching entries
*/
async search(pattern, options = {}) {
const { limit, offset = 0, caseInsensitive = true } = options;
const searchPattern = caseInsensitive ? pattern.toLowerCase() : pattern;
let results = [];
for (const entry of this.storage.values()) {
const emailToMatch = caseInsensitive ? entry.email.toLowerCase() : entry.email;
if (emailToMatch.includes(searchPattern)) {
results.push(entry);
}
}
if (offset > 0) {
results = results.slice(offset);
}
if (limit !== void 0 && limit > 0) {
results = results.slice(0, limit);
}
return results;
}
/**
* Iterate over entries in batches.
* For MemoryStorage, yields entries from the in-memory Map.
* @param batchSize - Number of entries per batch (default: 100)
*/
async *iterate(batchSize = 100) {
const entries = Array.from(this.storage.values());
for (let i = 0; i < entries.length; i += batchSize) {
const batch = entries.slice(i, i + batchSize);
for (const entry of batch) {
yield entry;
}
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MemoryStorage
});