This package provides a set of utility functions for common tasks like error handling, date formatting, email sending, and more. These functions are designed to make your codebase more efficient and reusable.
To use the helper functions, simply install the package and import the required functions in your project files.
npm install @anakin-gbit/helpers
getErrorString(error: unknown): string
— Extracts and returns the error message from an error object or returns a default message if no message is found.
import { getErrorString } from '@anakin-gbit/helpers';
const errorMessage = getErrorString(new Error("An error occurred"));
Error
or any object with a message
property.delay(ms: number): Promise<void>
— Returns a promise that resolves after a specified delay in milliseconds.
import { delay } from '@anakin-gbit/helpers';
await delay(1000); // Delay for 1 second
formatDate(unformattedDate: Date | string, inputFormat?: string, outputFormat?: string): string
— Formats a date string or date object according to specified formats.
import { formatDate } from '@anakin-gbit/helpers';
const formattedDate = formatDate('2024-11-11', 'yyyy-mm-dd', 'dd/mm/yyyy');
Date
object or a string.'yyyy-mm-dd'
.'dd/mm/yyyy'
.dd/mm/yyyy
: Day/Month/Year formatyyyy-dd/MM
: Year-Day/Month formatsendEmail(options: EmailOptions): Promise<void>
— Sends an email using Nodemailer with server-side support.
import { sendEmail } from '@anakin-gbit/helpers';
await sendEmail({
to: 'recipient@example.com',
subject: 'Hello!',
text: 'This is a test email.',
});
EMAIL_SERVER_HOST
: SMTP server hostEMAIL_SERVER_PORT
: SMTP server portEMAIL_SERVER_USER
: SMTP server usernameEMAIL_SERVER_PASSWORD
: SMTP server passwordEMAIL_FROM
: Email address used to send the email