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

emmis

v1.1.0

Published

Create a chaining API for your application

Downloads

5

Readme

proxychain

Create a chaining API for your application

Writing readable code requires a lot of discipline or helpful tools. This is such a helpful tool. With a chaining API you are forced into a declarative pattern for your application logic and that will improve the readability of your code. Proxychain is a tiny library, less than 50 lines of Typescript code. It allows you to express and run logic in your application that has not even been defined yet.

API

import { ProxyChain } from 'proxychain'

const chain = ProxyChain()

const appLoaded = chain()
  .state(state => state.isLoading = true)
  .http('get', '/user', {
    success: chain().state((state, user) => state.user = user),
    error: chain().state((state, error) => state.error = error)
  })
  .state(state => state.isLoading = false)

You can call appLoaded if you want to, though nothing will really happen. We have to define a reducer function for our chains and implement how these chain operators should be managed.

import { ProxyChain } from 'proxychain'
import myStateStore from './myStateStore'

const chain = ProxyChain((payload, operator) => {
  return payload
})

The chain reducer is responsible for firing side effects and updating the payload of the chain. By default it should always return the current payload. Let us now add our first operator:

import { ProxyChain } from 'proxychain'
import myStateStore from './myStateStore'

const chain = ProxyChain((payload, operator) => {
  switch(operator.type) {
    case 'state':
      const stateCallback = operator.args[0]
      stateCallback(myStateStore, payload)
  }
  return payload
})

Whenever a state operator runs we will grab the callback and call it with our state store and the current payload of the chain. That is it... now any usage of state in your chains will allow you to do a state change.

Lets us add our http operator.

import { ProxyChain } from 'proxychain'
import myStateStore from './myStateStore'
import axios from 'axios'

const chain = ProxyChain((payload, operator) => {
  switch(operator.type) {
    case 'state': ...
    case 'http':
      const method = operator.args[0]
      const url = operator.args[1]
      const paths = operator.args[2]

      return axios[method](url).then((response) => paths.success(response)).catch((error) => paths.error(error))

  }
  return payload
})

As you can see, we are calling success or error depending on the success of the request. These are also chains that will be merged into the existing execution. That means that last operator will not run until http is done.

We can now improve the declarativeness of our code even more:

import { ProxyChain } from 'proxychain'

const chain = ProxyChain()

const setLoading = isLoading => state => state.isLoading = isLoading
const setUser = user => state => state.user = user
const setError = error => state => state.error = error

const success = chain().state(setUser)
const error = chain().state(setError)

const appLoaded = chain()
  .state(setLoading(true))
  .http('get', '/user', { success, error })
  .state(setLoading(false))

As you can see, chaining allows us to split up our code a lot more. Focus on what is actually happening when we express logic and then create reusable parts of our code. Proxychain takes this even further as you can just write out thea chain you want and then implement it as a second step.