@octoweb/config is an Angular friendly module than give you a better abstraction, security and maintainability of the environment config and its uses.
Abstraction Use ConfigService to get access to the environment properties.
Security and maintainability Using Joi you should be able to validate your environment file. If environment doesn't match with the schema, it will throw an error.
This library was generated with Angular CLI version 12.0.5.
It was build with:
- "@angular/common": "^12.0.5"
- "@angular/core": "^12.0.5"
- "joi": "^17.4.0"
yarn add @octoweb/config
or npm i @octoweb/config
app.module.ts
@NgModule({
...
imports: [
...,
ConfigModule.forRoot<IEnvironment>({
config: environment,
schema: configSchema,
})
...
],
...
})
export class AppModule { }
config.schema.ts
export const configSchema: joi.ObjectSchema = joi.object({
apiUrl: joi.when('production', { is: false, then: joi.string().required() }),
production: joi.boolean().default(false).required(),
});
app.component.ts
import { ConfigService } from '@octoweb/config';
@Component({...})
export class AppComponent implements OnInit {
...
constructor(private readonly configService: ConfigService) {}
public ngOnInit(): void {
this.configService.get<string>('apiUrl'); // Return apiUrl
this.configService.getAll<IEnvironment>(); // Return all environment
}
...
}