Skip to content

Instantly share code, notes, and snippets.

@Sleavely
Sleavely / URLPatternRouter.test.ts
Last active March 22, 2026 11:50
A simple Express-style router using URLPattern API introduced in NodeJS 24
import { describe, it, expect, vi } from 'vitest';
import { URLPatternRouter } from './URLPatternRouter.js';
describe('URLPatternRouter', () => {
it('should match a simple route', async () => {
const router = new URLPatternRouter();
router.get('/hello', (): any => 'Hello World');
const route = router.match('GET', '/hello');
expect(route).not.toBeNull();
@Sleavely
Sleavely / Thenable.ts
Last active March 20, 2026 07:37
Singleton promises
/**
* A base class that only executes once, once you `await` it.
* You could call it a Lazy Singleton Promise.
*/
export abstract class Thenable {
#promise: Promise<unknown> | null = null;
abstract execute(): Promise<unknown>;
async then(
@Sleavely
Sleavely / testable-env-instance.test.ts
Created December 11, 2023 19:18
Example of API client instance module that can be replaced and refreshed with fake environment variables on the fly during testing
import { expect, test } from 'vitest'
import myModule from './testable-env-instance'
test('defaults to env', () => {
expect(myModule.token).toBe('INITIAL_TEST_VALUE')
})
test('env can be changed ondemand', () => {
expect(myModule.token).toBe('INITIAL_TEST_VALUE')
@Sleavely
Sleavely / UserProfile.vue
Last active December 12, 2022 18:06
@tanstack/query-core, with Vue's Options API
<template>
<div v-if="user.data">
{{ user.data.name }}
</div>
</template>
<script>
import getQuery from './queryManager'
export default {
@Sleavely
Sleavely / sqsClient.js
Last active September 13, 2022 07:24
A brief example of how interacting with SQS works in a Lambda environment
const SQS = require('aws-sdk/clients/sqs')
const {
AWS_REGION = 'eu-west-1',
MY_QUEUE_URL,
} = process.env
const sqs = new SQS({ region: AWS_REGION })
/**
@Sleavely
Sleavely / a Typescript Lambda problem.md
Last active December 3, 2021 09:39
Solving the AWS Lambda payload-might-be-undefined problem in Typescript with interface overloads

I ran in to some trouble when I wanted to use more explicit types than the APIGatewayProxyEventQueryStringParameters or APIGatewayProxyEventPathParameters ones tied to the base event, so I figured I'd give it a shot and jot it down for my own future reference.

Please leave a comment if you've got a cleaner idea 💌

@Sleavely
Sleavely / orchestrate-apidocs.js
Created November 2, 2020 23:28
A Serverless extension for orchestrating API->Lambda setups from your Swagger/OpenAPI definitions. This workflow encourages that the documentation is kept up-to-date.
const ServerlessAWSCloudFormationSubVariables = require('serverless-cloudformation-sub-variables')
class ApiOrchestrator {
constructor(serverless) {
this.serverless = serverless
// Register ${lambda:myFunctionKey} so that we can
// refer to it from our Swagger/OpenAPI definition
this.variableResolvers = {
@Sleavely
Sleavely / README.md
Created June 30, 2020 18:48
ACL Management API prototype

Open it in a fancy viewer here or locally with swagger-local:

$ swagger-local swagger.yaml
const Gpio = require('onoff').Gpio;
const sleep = require('./sleep')
const motor = new Gpio(17, 'out')
// Run the motor connected to GPIO17 every 1000ms
let beltIsShuttingDown = false
const runBelt = async () => {
if (beltIsShuttingDown) return
@Sleavely
Sleavely / README.md
Last active April 21, 2020 11:32
Restricted-access environment variables for AWS Lambda

This Lambda showcases how you can prohibit certain users from seeing or interacting with the environment variables of a Lambda.

Deployment assumes that you use aws cloudformation package followed by aws cloudformation deploy. Here's a suggested Makefile for allowing you to type make deploy. Note that the S3DEPLOYBUCKET and SECRET_PASSWORD variables need to be changed:

SECRET_PASSWORD    ?= pancakes
S3DEPLOYBUCKET      = my-s3-bucket