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

@thirdshot/contract

v0.8.1

Published

Write JavaScript by following design-by-contract (DBC) principles

Downloads

4

Readme

Design by Contract

Example: Async Contract

import { contractAsync } from '@thirdshot/contract'

async function getFileByKey(fileKey) {
  const file = await contractAsync({
    // The contracts optional identifier. Used within error messages.
    id: 'getFileByKey',

    // The contracts context is the internal state of the contract. It's important
    // that the state the contract reads is immutable: its the same at the end of
    // the contract as it is when the contract is invoked. This internal context
    // is passed to each of the other properties on this contract configuration
    // object and is checked once more at the end of execution.
    ctx: { fileKey },

    // If for whatever reason the contracts preconditions or postconditions fail,
    // this is what will get returned in its place. This allows you to overwrite
    // the default thrown error. It expects a function thats given the internal
    // context, the result if it exists, and the errors collected during the
    // process of running the contract.
    fallback: ({ errors }) => {
      return 'Something went wrong finding this file by key: ' + errors.map((e) => e.msg).join(', ')
    },

    // The contracts require (or preconditions) are conditions that must be met
    // in order for the contract to be invoked. If the preconditions are not met
    // the contract will throw an error or return whatever fallback error you
    // provide within the requirement. Its important that to understand that
    // whenever a contracts requirements fail it is considered a *bug*. Contract
    // requirements are not meant to be used for non-bug, unwanted behavior such
    // as form validation, for example.
    require: [
      { that: (ctx) => typeof ctx.fileKey === 'string', msg: 'fileKey not string' },
      { that: (ctx) => ctx.fileKey.length > 0, msg: 'fileKey not provided' },
    ],

    // The contracts invokation is the main purpose for the contract. When the
    // contracts requirements are met this invokation is called with the context
    // passed to it. Whatever this invokation returns is then passed to the
    // contracts postrequirements. This invokation accepts both sync and async
    // return types.
    invoke: (ctx) => api.getFileByKey(ctx.fileKey),

    // The contracts ensure (postconditions) are conditions that must be true
    // about what the invokation returns. If the postconditions are not met the
    // contract will throw an error or whatever fallback error you provide within
    // the ensurement. It's important to understand that whenever a contracts
    // ensurements fail it is considered a *bug* in your program. Contract
    // ensurements are not meant to be used for non-bug, unwanted behavior.
    ensure: [
      { that: (res) => res.meta.status === 200, msg: 'result was not 200' },
      { that: (res, ctx) => res.data.key === ctx.fileKey, msg: 'result was not same file' },
    ],
  })

  return file.data
}