Skip to content

Instantly share code, notes, and snippets.

View andywer's full-sized avatar

Andy Wermke andywer

View GitHub Profile
@andywer
andywer / router-1.ts
Last active August 6, 2020 14:57
Koa: Passing data from middleware to route handler
/*
* Description:
*
* There are times when we want to re-use some data created/derived in a middleware later in a route handler.
*
* The default Koa way of doing so would be using `context.state`. Unfortunately `context.state` is hard to type and
* quite implicit, since it's an object shared and potentially mutated by all middlewares and the route handler.
*/
/*

Keybase proof

I hereby claim:

  • I am andywer on github.
  • I am andywer (https://keybase.io/andywer) on keybase.
  • I have a public key ASAaseoxu8cFB_1LgQKirVmIAnK46TA20OoMraS114BRwAo

To claim this, I am signing this object:

@andywer
andywer / minimal.ts
Last active November 19, 2018 09:29
Better node server API - An attempt based on Koa
import { createApp, respond, route } from "koax"
import pkg from "../package.json"
const app = createApp()
app.get("/", async () => {
const body = {
name: pkg.name,
version: pkg.version
}
@andywer
andywer / client.ts
Last active November 26, 2018 14:58
Typing REST APIs with TypeScript
import { Responses as PublisherAPIResponses } from "@satoshipay/publisher-service-api"
import axios from "axios"
import { URL } from "url"
import config from "../config"
export async function getPublisherById (id: string) {
const url = new URL(`/${id}`, config.publisherServiceUrl).toString()
return await axios.get(url) as PublisherAPIResponses["GET /*"]
}
@andywer
andywer / Monorepo Setup.md
Last active April 8, 2022 03:50
Monorepo Setup

Overall Approach

This document is a short description of the monorepo setup that I came up for a few different smaller projects in the last 8 months or so.

Directory structure

/
  services/
 product/
@andywer
andywer / app.ts
Last active February 27, 2019 12:57
Sample usage - Better HAL package for node
import Koa from "koa"
import * as KoaHAL from "$new-hal-koa-package"
import { api, Users } from "./hal"
const app = new Koa()
const koaRouter = KoaHAL.Router(api, router => {
// GET /users
router.get(Users, async ctx => {
const users = await queryAllUsers()
@andywer
andywer / _readme.md
Last active January 8, 2025 04:42
React - Functional error boundaries

React - Functional error boundaries

Thanks to React hooks you have now happily turned all your classes into functional components.

Wait, all your components? Not quite. There is one thing that can still only be implemented using classes: Error boundaries.

There is just no functional equivalent for componentDidCatch and deriveStateFromError yet.

Proposed solution

@andywer
andywer / sharded-key-storage.md
Last active March 17, 2021 16:04
Sharded Key Storage

Sharded Secret Key Storage

  • Store secret keys on the internet
  • Authenticate with arbitrary authentication methods to retrieve secret
  • Trustless: None of the key storage server providers can read the secret
  • Recoverable funds: Even if authentication credentials are lost, the secret can be recovered
  • Secure: If a key storage server gets compromised, the attacker will not even know a fragment of the secret
  • Fault-tolerant: Redundancy fragment allows retrieving the secret even if a server is down
  • Versatile: Allows storage of arbitrary secrets
@andywer
andywer / preauthed-multisig.md
Created July 27, 2019 13:48
Stellar Pre-Authorized Multi-Signature

Stellar Pre-Authorized Multi-Signature

Abstract

I tried to use pre-authorizeded transactions on Stellar as a replacement for a co-signer signature, but it seems like they don't play terribly well with multi-signature setups.

Scenario

Given:

@andywer
andywer / hello-worker.js
Created September 7, 2019 16:15
Hello world threads.js worker (compiled)
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;