npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pristine-ts/configuration

v0.0.363

Published

The Configuration Module is used to provide a way for your own module or other modules in Pristine to be dynamically configured. This allows any service injected in the Container to retrieve a parameter. The parameter will be injected as `%module_keyname.

Downloads

1,147

Readme

Configuration Module

The Configuration Module is used to provide a way for your own module or other modules in Pristine to be dynamically configured. This allows any service injected in the Container to retrieve a parameter. The parameter will be injected as %module_keyname.parameter_name% (it follows the syntax used by Symfony).

In your class, to have a parameter be injected, you simply do this:

class Example {
    public constructor(@inject("%module_keyname.parameter_name%") private readonly parameterName: string) {}
}

How to specify a configuration for your module

Before we begin, there are two terms we use as a convention, and it's important to understand them before continuing.

ConfigurationDefinition

The ConfigurationDefinition is the configuration "model" that the module specifies it needs. By convention, we recommend creating a ConfigurationDefinitionInterface interface that specifies what you are expecting your users to pass. Here's an example:

interface ConfigurationDefinitionInterface {
    test1Parameter: string;
    test2Parameter?: string;
    test3Parameter?: string;
}

Then, you can create a ConfigurationDefinition class that specifies the default values for the required parameters.

    class ConfigurationDefinition implements ConfigurationDefinitionInterface {
        test1Parameter: string = "test1";
        test2Parameter?: string = "test2";
    }

Then, when you define and create your module, you pass the ConfigurationDefinition type object:

    const module: ModuleInterface = {
        keyname: "test",
        importServices: [],
        providerRegistrations: [
        ],
        configurationDefinition: ConfigurationDefinition
    };

That way, the ConfigurationModule now knows which values must be expected and will complain when you start the kernel if the user hasn't provided these values for your module.

Configuration

The Configuration represents the actual values that the configuration parameters will have. The configuration is passed in the init method of the kernel and Pristine evaluates if all the required configuration parameters are present.


NOTE

Now, you might be wondering why you need to create a class (on top of an interface) and define default values because you might want to force the user to pass those values. The problem is that interfaces in Typescript are lost during compile time, and we cannot use them (yet) to retrieve type or optionality information during runtime.

Therefore, that's why we recommend the creation of a ConfigurationDefinitionInterface so that this interface can act as a guide for your users, since this is the interface that you should expose.

When your users will be initiating the kernel, they will need to pass a configuration for each module keyname, and you ask them to use the configuration interface

This part is still pretty weak in our opinion and will need some interesting thinking to make it better, but for now, it works correctly, but is too opinionated for our liking.


##Instantiation and using the Configuration

When you instantiate the kernel, you need to specify the configuration for each module as follows:

    const kernel = new Kernel();

    const testModuleConfiguration: ConfigurationDefinitionInterface = {
        test1Parameter: "NotDefault",
    };

    await kernel.init(module, [{
        moduleKeyname: module.keyname,
        configuration: testModuleConfiguration
    }]);

This is also in the configuration that you can use resolvers to convert or retrieve parameters from the environment variables, your filesystem, AWS SSM, etc.

Using a configuration parameter in your class

This is how you would inject a parameter in the constructor of one of your services

@injectable()
class TestConfigurationParameterInjectedInConstructor {
    public constructor(@inject("%test.test1Parameter%") public readonly test1Parameter: string, @inject("%test.test2Parameter%") public readonly test2Parameter: string,) {
    }
}

Configuration Resolvers

We have made it very easy to create your own ConfigurationResolver. A ConfigurationResolver allows you to retrieve configuration from the configuration parameters from the current environment and inject it into the controller. For example, you can easily create a FileConfigurationResolver(to have configuration parameters retrieved from a file), an AwsSsmConfigurationResolver (to have configuration parameters retrieved from AWS SSM) or anything else you can come up with.

To create a ConfigurationResolver, you simply inherit the ResolverInterface. Then, your user can easily use it when they pass the configuration to the second argument of the init method in the Kernel.