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 / download-file.js
Last active August 31, 2022 16:45 — forked from javilobo8/download-file.js
Download files from Lambda with AWS Amplify
await API.get('myCloudApi', '/items', {
responseType: 'blob',
response: true
})
.then((response) => {
const blob = new Blob([response.data], { type: 'application/octet-stream' })
const filename = response.headers['content-disposition'].split('"')[1]
if (typeof window.navigator.msSaveBlob !== 'undefined') {
@Sleavely
Sleavely / api.js
Last active August 30, 2022 09:31
A helper for running a local webserver against lambda-api
// Require the framework and instantiate it
const api = require('lambda-api')()
// Define a route
api.get('/status', async (req, res) => {
return { status: 'ok' }
})
api.get('/README.md', async (req, res) => {
res.sendFile('./README.md')
@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 / routes.php
Created November 6, 2016 17:15
List all Laravel routes
<?php
// List all routes as a prettyprinted JSON array.
// Compatible with Laravel 4 and 5.
Route::get('routes', function(){
$routes = Route::getRoutes();
$results = array();
foreach ($routes as $route)
{
$path = $route->getPath();
@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