Utilities

The SSK-Core utilities module provides key helper functions designed to streamline environment management, client/server detection, and secure access to environment variables. These utilities are a crucial part of SSK-Core's functionality and are intended to simplify tasks that are often needed across different parts of your project.


Core Features

  1. Environment Detection: Quickly determine whether you're in a development environment or if the code is running on the client (browser) or server (Node.js).

  2. Dynamic Host Retrieval: Automate the retrieval of host URLs based on whether you're running locally or in a production environment. This simplifies managing local vs. production setups.

  3. Environment Variable Access: Securely fetch and validate environment variables with optional error handling for missing configurations, reducing the chance of runtime failures in production environments.


How to Use

Environment Variable Access

You can easily retrieve environment variables using getEnvVar. For example, if you need to access an API key, you can do so like this:

const apiKey = getEnvVar('API_KEY')

Development Environment Check

Check if the project is running in a development environment using checkIsDev:

if (checkIsDev()) {
  console.log('Development mode is active')
}

Host URL Retrieval

To automatically get the appropriate host URL based on the environment:

const currentHost = getHost()
console.log(`Current host: ${currentHost}`)

This feature ensures that local development and production environments are handled seamlessly, preventing hardcoded URLs from causing issues.


Example Usage in Server Actions

The utilities integrate seamlessly with server actions. For example, to interact with a database in a server-side action, use the following approach:

await db.user.create({
  data: {
    ...userData,
    emailVerifToken: verificationToken,
  },
})

This keeps your data management smooth and aligned with SSK-Core's architecture.


By leveraging these utilities, you can simplify environment management, improve development efficiency, and ensure smooth transitions between local and production environments.

Last updated