npm package: @macksterino/ts-credentials
Description: Module used for easy handling of credentials
Author: macksterino
Latest version: 1.0.20

# @macksterino/ts-credentials

## Purpose
Handling credentials individually or within an organization/company *CAN* be something that is managed very differently, which can create inconsistency.
It is also something that can take up a fair amount of time, needing to write functionality to fetch credential files, create interfaces or types if desired.
The goal with this module is to simply speed up this process and offer good ol' intellisense along with it.

## Usage
### Installation
`$ npm install --save-dev @macksterino/ts-credentials`

### Credentials file
Requirements:
- File format needs to be `JSON`.
- The first environment needs to be your development/test environment and the second environment needs to be your production environment, although you can name them whatever you want.
- All the module names and its information needs to be the same on both your development and production environment.

#### Example
```
{
	"development": {
		"SQL": {
			"server": "my development sql server",
			"database": "my development sql database",
			"username": "my development sql username",
			"password": "my development sql password"
		},
		"LDAP": {
			"url": "ldap://127.0.0.1:1389",
			"dn": "my development ldap username",
			"password": "my development ldap password"
		}
	},
	"production": {
		"SQL": {
			"server": "my production sql server",
			"database": "my production sql database",
			"username": "my production sql username",
			"password": "my production sql password"
		},
		"LDAP": {
			"url": "ldap://127.0.0.1:1389",
			"dn": "my production ldap username",
			"password": "my production ldap password"
		}
	}
}
```

### Module
#### Parameters and functions
##### Parameters
*The path to your credentials file.*
- `path: string`

*The name of your global environment variable. If it is not defined or if it does not exist it will default to your test environment.*
- `environmentVariableName?: string`

##### Functions
*Retrieves all the credentials. If an environment is not defined it will default to your test environment.*

**Note:** If an environment is defined here it will be prioritized over your environment variable, if that is defined.
- `getCredentials(environment?: string): Credentials`

#### Examples
##### Example 1
```
import { CredentialsHandler } from '@macksterino/ts-credentials';

const credentials = new CredentialsHandler({ path: './private/credentials.json' });

const config = credentials.getCredentials();
```

**Note:** At this point you will have to compile and run your code to generate your credential files and be able to get intellisense.

**You might also need to refresh your editor/IDE or navigate to node_modules/@macksterino/ts-credentials/dist/credentials/types.d.ts for intellisense to update. This is something I'm currently working on.**

Thereafter:
```
import { CredentialsHandler } from '@macksterino/ts-credentials';

const credentials = new CredentialsHandler({ path: './private/credentials.json' });

const config = credentials.getCredentials();
console.log(config.SQL);
console.log(config.LDAP);
```

##### Example 2
```
import { CredentialsHandler, CredentialsHandlerConfig } from '@macksterino/ts-credentials';

const credentialsConfig: CredentialsHandlerConfig = {
	path: './private/credentials.json'
};

const credentials = new CredentialsHandler(credentialsConfig);

const config = credentials.getCredentials();
console.log(config.SQL);
console.log(config.LDAP);
```

##### Examples with environments
```
import { CredentialsHandler, CredentialsHandlerConfig } from '@macksterino/ts-credentials';

const credentialsConfig: CredentialsHandlerConfig = {
	path: './private/credentials.json',
	environmentVariableName: 'ENVIRONMENT'
};

const credentials = new CredentialsHandler(credentialsConfig);

const config = credentials.getCredentials();
console.log(config.SQL);
console.log(config.LDAP);
```

If your environment variable exists, the module will read its value and see if it equals to the production environment value you specified inside your credentials file.
If it does not exist or if it the values don't match it will automatically default to your test environment.

If your environment variable exists and the values match but you specify an environment inside the `getCredentials()` function, the environment specified within the function will ultimately be the environment that is used.

##### Another example
```
import { CredentialsHandler } from '@macksterino/ts-credentials';

const credentials = new CredentialsHandler({ path: './private/credentials.json', environmentVariableName: 'ENVIRONMENT' });

const config = credentials.getCredentials('production');
console.log(config.SQL);
console.log(config.LDAP);
```

##### Recommended example
```
import { CredentialsHandler } from '@macksterino/ts-credentials';

const credentials = new CredentialsHandler({ path: './private/credentials.json', environmentVariableName: 'ENVIRONMENT' });

const config = credentials.getCredentials();
console.log(config.SQL);
console.log(config.LDAP);
```