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

@dotcom-tool-kit/schemas

v1.1.1

Published

> ⚠️ this README is out of date and describes the pre-Zod schema types. update plz

Downloads

967

Readme

Tool Kit Option Schemas

⚠️ this README is out of date and describes the pre-Zod schema types. update plz

Overview

Tool Kit allows plugins to take options from a .toolkitrc.yml file. Each plugin has a different set of options that it can expect to get, and this package defines the shape of these options. It does that via a schema object defined for each of the plugins that take options. This is currently being used primarily so that the user can be prompted for these options via the command line interface used by the create package, though they could also be used to verify options specified in a config file in the future.

Seeing as JavaScript does not have any runtime concept of types, we instead define the types by string literals which can then be parsed at runtime (allowances have been made to make parsing easier so some syntax forms might look a little odd.) For example, a schema could look like this:

const ExampleSchema = {
  files: 'string',
  retryCount: 'number?'
} as const

which would define a set of options where it is expected a files string will be specified, and a number field can be optionally specified for a retryCount.

Schema Type Reference

In this table, A can be substituted by any of the first four types (unfortunately TypeScript does not seem to support recursive conditional types right now.) B and C in the union type are string literals. T can be any type, with a ? suffix denoting an optional type.

| Syntax | Type | | ------------ | ------------------- | | "string" | string | | "number" | number | | "boolean" | boolean | | "unknown" | unknown | | "array.A" | A[] | | "record.A" | Record<string, A> | | "\|B,C" | B \| C | | "T?" | T \| undefined |

TypeScript Helpers

All schemas should use the TypeScript type Schema. This will make sure that there aren't any typos in the schema and that is structured correctly. It also allows you to make use of the SchemaOutput type, a special generic type that can be used to extract the type of the object that is successfully validated by the schema. For instance, you could use the type inferred by the ExampleSchema declaration above as the type parameter for SchemaOutput like so:

type ExampleOutput = SchemaOutput<typeof ExampleSchema>

which would create a type equivalent to

type ExampleOutput = {
  files: string
  retryCount?: number
}

This can be very useful when you don't want to have your options defined in two different forms which you have to remember to keep in sync with each other whenever you make any changes!

One gotcha to look out for is to make sure you cast any schema object you define as constant with the as const assertion. This restricts TypeScript from 'widening' the types, i.e., treating a literal field with value "number" as a string rather than as having type "number", which the SchemaOutput depends on for its conditional typing.

Integrating Into Code

The Task class, which all tasks must inherit, takes a Schema as a type parameter. It then uses SchemaOutput to ascertain the type of the options that are actually stored for the task. You should pass a Schema to Task if your plugin takes options, but it is safe to ignore this type parameter if your plugin does not.

You should also follow the convention of storing your schema at <tool kit root>/lib/types/schema/<package name>.ts, and export it from the module as Schema. This allows the create package to dynamically read the schema and prompt the user for options to set when they are initialising Tool Kit and its plugins.