Skip to content

Instantly share code, notes, and snippets.

@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
@Sleavely
Sleavely / attributeResolution.js
Created March 22, 2020 13:55
Product attribute schema POC
const attributes = {
// Attribute Keys are always localized
'color': {
name: {
'en-US': 'Color',
'sv-SE': 'Färg',
},
},
'size': {
@Sleavely
Sleavely / process.env.js
Last active March 22, 2020 14:14
AWS Lambda environment variables when running with a default configuration
// The original values have been redacted, but their general format remains for display purposes.
{
AWS_ACCESS_KEY_ID: 'ASIA5USITABHEEWHMCOL',
AWS_DEFAULT_REGION: 'eu-west-1',
AWS_EXECUTION_ENV: 'AWS_Lambda_nodejs12.x',
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '128',
AWS_LAMBDA_FUNCTION_NAME: 'tmp1337',
AWS_LAMBDA_FUNCTION_VERSION: '$LATEST',
AWS_LAMBDA_LOG_GROUP_NAME: '/aws/lambda/tmp1337',
AWS_LAMBDA_LOG_STREAM_NAME: '2020/01/14/[$LATEST]425d2a3419de4db38f86fc1896cc1cc1',