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

ssm-config-loader

v1.2.0

Published

Load configuration from SSM Parameter Store with local fallback

Downloads

27

Readme

SSM Config Loader

Node.js CI codecov

This library helps simplifies the use of the SSM parameter store by loading all values of a prefix into a tree structure. It additionally supports loading the config from a local file, which aids in development.

Installation

  • Install the npm package:

    npm install ssm-config-loader

Quick Start

To load a configuration from SSM, you only have to create an SSM client (v3) and supply it to the loader together with a prefix:

import {SSM} from '@aws-sdk/client-ssm';
import {loadSsmConfig} from 'ssm-config-loader';

const config = await loadSsmConfig(new SSM({}), process.env.SSM_PREFIX);

If process.env.SSM_PREFIX is set, likely in your production environment, it will use this to load all parameters under that prefix and put it into a tree structure. This means it will strip the prefix (plus the next character, which should be a slash) from the name, split the remaining parts by a slash and create a tree structure out of this.

If process.env.SSM_PREFIX is undefined, the loader will try to load an ssm-config.json file from your CWD. It is up to you to create that JSON file in the same structure as it will be loaded from SSM, e.g. with the correct tree structure.

Validation and type safety

The config loader only returns a generic config object without much type safety. You most likely want to validate that you get the correct values back from it. An easy way to accomplish this is by utilizing e.g. the Zod library:

import {SSM} from '@aws-sdk/client-ssm';
import {loadSsmConfig} from 'ssm-config-loader';
import {z} from 'zod';

const configSchema = z.object({
    endpoint: z.string().url(),
});

const config = configSchema.parse(
    await loadSsmConfig(new SSM({}), process.env.SSM_PREFIX)
);

Now you can be certain that the config is complete and all values are in the format you expect. Additionally, your compiler can check for typos.

Complete recipe

As you probably don't want to load the config every time some component needs access to a value, it would make sense to create a singleton function to always return the same value once it was retrieved. This could look something like this:

import {SSM} from '@aws-sdk/client-ssm';
import {loadSsmConfig} from 'ssm-config-loader';
import {z} from 'zod';

const configSchema = z.object({
    endpoint: z.string().url(),
});

export type Config = z.infer<typeof configSchema>;

let configPromise : Promise<Config>;

export const getConfig = async () : Promise<Config> => {
    if (configPromise) {
        return configPromise;
    }
    
    return configPromise = (async () => {
        return configSchema.parse(await loadSsmConfig(
            new SSM({}), process.env.SSM_PREFIX)
        );  
    })();
};

Convenience helper

This library also comes with an opinionated convenience helper which will automatically create a singleton config getter with built-in schema validation via zod. This removes a lot of boilerplate code you have to write in your projects:

import {SSM} from '@aws-sdk/client-ssm';
import {createSingletonConfigGetter} from 'ssm-config-loader/lib/singleton-config-getter';
import {z} from 'zod';

const configSchema = z.object({
    endpoint: z.string().url(),
});

export type Config = z.infer<typeof configSchema>;

export const getConfig = createSingletonConfigGetter(
    new SSM({}),
    configSchema,
    process.env.SSM_PREFIX
);

In case either loading the config or the validation fails, the getter will throw a ConfigError from the same file.