Helper Functions Documentation

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.

Installation

To use the helper functions, simply install the package and import the required functions in your project files.

bash
npm install @anakin-gbit/helpers

getErrorString()

getErrorString(error: unknown): string — Extracts and returns the error message from an error object or returns a default message if no message is found.

Usage
tsx
import { getErrorString } from '@anakin-gbit/helpers';
const errorMessage = getErrorString(new Error("An error occurred"));
Parameters
  • error: The error object to retrieve the message from. Can be an instance of Error or any object with a message property.

delay()

delay(ms: number): Promise<void> — Returns a promise that resolves after a specified delay in milliseconds.

Usage
tsx
import { delay } from '@anakin-gbit/helpers';
await delay(1000); // Delay for 1 second
Parameters
  • ms: The number of milliseconds to wait before resolving the promise.

formatDate()

formatDate(unformattedDate: Date | string, inputFormat?: string, outputFormat?: string): string — Formats a date string or date object according to specified formats.

Usage
tsx
import { formatDate } from '@anakin-gbit/helpers';
const formattedDate = formatDate('2024-11-11', 'yyyy-mm-dd', 'dd/mm/yyyy');
Parameters
  • unformattedDate: The date to format, can be a Date object or a string.
  • inputFormat (optional): The format of the input date string. Defaults to 'yyyy-mm-dd'.
  • outputFormat (optional): The desired output format. Defaults to 'dd/mm/yyyy'.
Available Output Formats
  • dd/mm/yyyy: Day/Month/Year format
  • yyyy-dd/MM: Year-Day/Month format

sendEmail()

sendEmail(options: EmailOptions): Promise<void> — Sends an email using Nodemailer with server-side support.

Usage
tsx
import { sendEmail } from '@anakin-gbit/helpers';
await sendEmail({
  to: 'recipient@example.com',
  subject: 'Hello!',
  text: 'This is a test email.',
});
Parameters
  • options: An object with the following properties:
    • to: The recipient's email address (string).
    • subject: The subject line of the email (string).
    • text: The plain text content of the email (string).
Required Environment Variables
  • EMAIL_SERVER_HOST: SMTP server host
  • EMAIL_SERVER_PORT: SMTP server port
  • EMAIL_SERVER_USER: SMTP server username
  • EMAIL_SERVER_PASSWORD: SMTP server password
  • EMAIL_FROM: Email address used to send the email