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

construct-typed-parameters

v4.0.2

Published

Type-safe parameter construction library. Useful for managing environment variables, aws parameter stores and more.

Downloads

19

Readme

construct-typed-parameters

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

Type-safe parameters construction library. Useful for managing Query Parameters, Environment Variables, AWS System Manager Parameter Store, and more.

Install

npm install construct-typed-parameters

Usage

Basic

import { TypedParameters } from 'construct-typed-parameters';

const parameters = new TypedParameters(parameterType => ({
  TOKEN: parameterType.string({ required: true }),
  FIREBASE_CONFIG: parameterType.json<{ apiKey: string }>({ required: true }),
}));

const stringifiedParameters = parameters.stringify({
  TOKEN: 'xxxx',
  FIREBASE_CONFIG: { apiKey: 'xxxx' },
});
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: '{"apiKey":"xxxx"}'}

const parsedParameters = parameters.parse({
  TOKEN: 'xxxx',
  FIREBASE_CONFIG: '{"apiKey":"xxxx"}',
});
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: { apiKey: 'xxxx' }}

AutoCompletion

AutoCompletion

with Query Parameters

const queryString = new URLSearchParams(
  parameters.stringify({
    TOKEN: 'xxxx',
    FIREBASE_CONFIG: { apiKey: 'xxxx' },
  })
).toString();
//=> 'TOKEN=xxxx&FIREBASE_CONFIG=%7B%22apiKey%22%3A%22xxxx%22%7D'

const parsedParameters = parameters.parse(
  Object.fromEntries(
    new URLSearchParams(
      'TOKEN=xxxx&FIREBASE_CONFIG=%7B%22apiKey%22%3A%22xxxx%22%7D'
    ).entries()
  )
);
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: { apiKey: 'xxxx' } }

with Environment Variables

Object.entries(
  parameters.stringify({
    TOKEN: 'xxxx',
    FIREBASE_CONFIG: { apiKey: 'xxxx' },
  })
).forEach(([parameterName, stringifiedValue]) => {
  process.env[parameterName] = stringifiedValue;
});
//=>
// process.env.TOKEN: 'xxxx'
// process.env.FIREBASE_CONFIG: '{"apiKey":"xxxx"}'

const parsedParameters = parameters.parse({
  TOKEN: process.env.TOKEN,
  FIREBASE_CONFIG: process.env.FIREBASE_CONFIG,
});
//=> { TOKEN: 'xxxx', FIREBASE_CONFIG: { apiKey: 'xxxx' } }

with AWS SSM Parameter Store

see https://github.com/masahirompp/ssm-parameters-boot

API

see test/index.spec.ts.

TypedParameters

Constructor

import { TypedParameters } from 'construct-typed-parameters';

const parameters = new TypedParameters(pt => ({
  stringValue: pt.string({
    // required: boolean
    required: true,
    // defaultValue?: T1
    defaultValue: 'xxxx',
    // validate?: (value: T1) => string | string[] | null;
    validate: v => (v.includes('x') ? null : 'the value must contain x'),
  }),
  unionStringValue: pt.unionString<'v1' | 'v2'>({
    required: true,
    defaultValue: 'v1',
    validate: v =>
      ['v1', 'v2'].includes(v) ? null : 'the value must be v1 or v2',
  }),
  numberValue: pt.number({
    required: true,
    defaultValue: 1,
    validate: v => (v === 0 ? 'value must not be 0' : null),
  }),
  unionNumberValue: pt.unionNumber<0 | 1>({
    required: true,
    defaultValue: 0,
    validate: v => ([0, 1].includes(v) ? null : 'the value must be 0 or 1'),
  }),
  booleanValue: pt.boolean({
    required: true,
    defaultValue: true,
    validate: v => (v ? null : 'the value must be true'),
  }),
  jsonValue: pt.json<{ apiKey: string }>({
    required: true,
    defaultValue: { apiKey: 'xxxx' },
    validate: v => (v.apiKey.length ? null : 'apiKey must be specified'),
  }),
  arrayValue: pt.json<string[]>({
    required: true,
    defaultValue: ['main', 'sub'],
    validate: v => (v.length ? null : 'array must not empty'),
  }),
}));

Method

parameters.parse(
  stringifiedParameters: Partial<StringifiedParameters<T>>,
  shouldValidate = true
) : ParsedParameters<T>

parameters.stringify(
  parsedParameters: Partial<ParsedParameters<T>>,
  shouldValidate = true
) : StringifiedParameters<T>