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

@fewlines/ts-workflow

v1.0.0

Published

an implementation of the workflow pattern in typescript

Downloads

1

Readme

ts-workflow

Disclaimer: this package is made for our internal usage and is only open source for convenience so we might not consider Pull Requests or Issues.

A TypeScript implementation of the workflow pattern.

Usage

Using workflow provides a framework to add error handling and logging to your business logic.

The workflow module exports:

  • a Workflow<Input, Result> that represent the final function
  • a UnexpectedError error type. This error is thrown if your business logic throws an undeclared error.
  • a Logger interface with
    • info a function to handle usal logging (e.g. console.log, winston.info...)
    • error a function to handle error logging (e.g. console.error, winston.error...)
  • a Meta interface that allows you to put a logger and a set of adapters that will be passed to your business logic.
  • a Payload<Input> type that contains an input: Input and a meta: Meta field.
  • a makePayload(input: Input, meta: Meta) function that should be used to create payloads.
  • finally the default export is makeWorkflow function that use your business logic function to create a workflow. It accepts:
    • workflowName: string this should be unique per workflow and is used to identify your workflow in logs
    • handleRun: (input: Input, adapters?: object) => Promise<Result> your actual business logic functions. the input that will be passed is the input contained in the payload. the adapters are the adapters contained in the payload's meta.
    • acceptableErrors an Array of error classes that are accepted to be thrown by the workflow.

Exemple of a workflow declaration

// src/workflows/dummy-workflow.ts
import makeWorkflow from "workflow"

// A custom error class
export class WrongInput extends Error {}

// this is your actual business logic
function handleRun(input: Input, adapters: object): Promise<Result> {

  const adapter = adapters["dummyAdapter"] || realImplementation

  return someLogic(adapter(input))
}

// this is the errors you handle
const acceptableErrors = [WrongInput]

export default makeWorkflow("dummyWorkflow", handleRun, acceptableErrors)

Exemple of usage in an express handler

// src/handlers/some-module.ts
import {UnexpectedError} from "workflow"
import dummyWorkflow, {WrongInput} from "../worflows/dummy-workflow"

export function dummyHandle(req, res) {
  const payload = makePayload({myParam: req.params.myParam}, {logger: {info: console.log, error: console.error}})

  return dummyWorkflow(payload)
    .then(data => res.status(200).json(data))
    .catch(error => {
      if (error instanceof WrongInput) {
        return res.status(422).json({message: error.message})
      } else {
        return res.status(500).json({message: "An unexpected error happened"})
      }
    })
}

Contributing

See CONTRIBUTING.md.