NestJS

This document provides instructions for working with NestJS and the CommandK CLI. It explains how to run your NestJS application and manage secrets effectively using the CommandK CLI.

Prerequisites

Before you begin the integration process, make sure you have the following:

  1. API Access Token: To authenticate your Nest.js application with CommandK, you'll need an API access token. If you haven't obtained one yet, refer to this link for instructions on how to acquire it.

Integration Steps

Follow these steps to integrate CommandK with your Nest.js application:

Step 1: Create a Nest.js Application

If you haven't already created a Nest.js application, you can set up a new project using the Nest.js CLI or any other preferred method. Here, we'll use the Nest.js CLI for demonstration:

  1. Install the Nest.js CLI globally if you haven't already:

    npm install -g @nestjs/cli
    
  2. Create a new Nest.js project using the following command:

    nest new my-nest-app
    

    Replace my-nest-app with your desired project name.

  3. Follow the prompts to configure your Nest.js project. You can choose options such as the programming language (TypeScript/JavaScript), package manager, and more based on your project requirements.

Step 2: Access Secrets in Your Nest.js Application

Inside your Nest.js application, you can access secrets as environment variables. Open the main.ts (or main.js if you are using JavaScript) file, and add the following code snippet before starting the Nest.js application:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Access and set environment variables
  const commandkSecretKey = process.env.COMMANDK_SECRET_KEY || 'default-secret';
  // Use commandkSecretKey in your application as needed
  
  await app.listen(3000);
}
bootstrap();

In this code, COMMANDK_SECRET_KEY is the environment variable that will store your secret. If the variable is not found, it falls back to a default value ('default-secret').

Running Your Nest.js Application

To run your Nest.js application with the CommandK CLI, you can use the following command:

$ cmdk run <application-name> --environment development -- npm run start

Replace <application-name> with the actual name of your Nest.js application. This command will execute your Nest.js application in the development environment.

Running with dotenv

If your Nest.js application uses dotenv to manage environment variables, you can utilize the CommandK CLI to write these variables to a .env file and then run your service. Follow these steps:

  1. Execute the following command:
$ cmdk run <application-name> --environment development \
     --run-type file-store \
     --file-name .env.local \
     --file-format env \
     -- npm run start

Replace <application-name> with the actual name of your Nest.js application. This command will write your environment variables to the .env.local file in the specified env format and then run your Nest.js service.

Adjust the file name and format to match your project's configuration if necessary.

By following these instructions, you can seamlessly integrate the CommandK CLI with your Nest.js application, making it easy to manage secrets and run your application securely.