@kaname-png/plugin-env v1.1.9
@kaname-png/plugin-env
Plugin for @sapphire/framework to manage environment variables with value parser.
Description
This plugin allows you to manage your environment variables via .env, .env.development, etc. files in a simple way and with the ease of getting the value converted into the type you need.
The plugin allows you to read the environment variables and parse it to the desired type and get for example, a boolean value from an environment variable.
Features
- Fully ready for TypeScript!
- Includes ESM ready entrypoint
- Easy to use
Installation
@kaname-png/plugin-env
depends on the following packages. Be sure to install these along with this package!
You can use the following command to install this package, or replace npm install
with your package manager of choice.
npm install @kaname-png/plugin-env @sapphire/framework discord.js
Usage
It is very important to assign an environment variable manually, this to know what kind of environment variables you need.
For example:
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
And with NodeJs versions higher than v15 (Logical Nullish Assignment):
process.env.NODE_ENV ??= 'development';
JavaScript
In your main or setup file, register the plugin:
require('@kaname-png/plugin-env/register');
Once the plugin is registered, we will have to decide whether to enable the plugin or not with the enabled
variable in the client options in the env
option.
require('@kaname-png/plugin-env/register');
async function main() {
const client = new SapphireClient({
/* your bot options */
env: {
enabled: true // You can decide when to enable the plugin or not.
/* ...More options available */
}
});
await client.login();
}
void main();
TypeScript
In your main or setup file, register the plugin:
import '@kaname-png/plugin-env/register';
Once the plugin is registered, we will have to decide whether to enable the plugin or not with the enabled
variable in the client options in the env
option.
import '@kaname-png/plugin-env/register';
async function main() {
const client = new SapphireClient({
/* your bot options */
env: {
enabled: true // You can decide when to enable the plugin or not.
/* ...More options available */
}
});
await client.login();
}
void main();
How to use
JavaScript
// Services
import { container } from '@sapphire/framework';
export class MyAwesomeService {
get() {
return container.env.string('MY_AWESOME_ENV');
}
}
// Commands, Listeners, etc.
import { Command } from '@sapphire/framework';
export class MyAwesomeCommand extends Command {
messageRun(message) {
return message.reply(this.container.env.string('MY_AWESOME_ENV'));
}
}
TypeScript
In TypeScript the variable name typing is safe, this means that you can't just put any environment variable key name, for that we will have to augment the interface that contains all the available environment variable names.
It is recommended to set it to type never
, although its type has no effect on the result.
import '@kaname-png/plugin-env/register';
async function main() {
const client = new SapphireClient({
/* your bot options */
env: {
enabled: true // You can decide when to enable the plugin or not.
/* ...More options available */
}
});
await client.login();
}
void main();
// It is recommended to do it for example in the main file that starts the client (main.js for example).
declare module '@kaname-png/plugin-env' {
interface EnvKeys {
MY_AWESOME_ENV: never;
}
}
// Services
import { container } from '@sapphire/framework';
export class MyAwesomeService {
public get() {
return container.env.string('MY_AWESOME_ENV');
}
}
// Commands, Listeners, etc.
import { Command } from '@sapphire/framework';
import { Message } from '@discord.js';
export class MyAwesomeCommand extends Command {
public messageRun(message: Message) {
return message.reply(this.container.env.string('MY_AWESOME_ENV'));
}
}
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
7 months ago
7 months ago
8 months ago
8 months ago
6 months ago
8 months ago
8 months ago
6 months ago
8 months ago
7 months ago
8 months ago
7 months ago
7 months ago
11 months ago
12 months ago
12 months ago
12 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago