Skip to content

Instantly share code, notes, and snippets.

View josefaidt's full-sized avatar
🦉

josef josefaidt

🦉
View GitHub Profile
@josefaidt
josefaidt / use-user-profile.tsx
Last active April 10, 2024 21:53
useUserProfile hook for Amplify auth
import type { PropsWithChildren } from "react"
import { useState, createContext, useEffect, useContext } from "react"
import { useAuthenticator } from "@aws-amplify/ui-react"
import { fetchUserAttributes } from "aws-amplify/auth"
type UserProfile = {
/**
* the sub
*/
id: string
@josefaidt
josefaidt / parse-url-encoded-form-event.ts
Created April 3, 2024 15:17
Parse a URL encoded form event in AWS Lambda
import type { APIGatewayProxyEventV2 } from "aws-lambda"
import busboy from "busboy"
// https://github.com/francismeynard/lambda-multipart-parser/blob/master/index.js
async function parseUrlEncodedFormEvent<T>(
event: APIGatewayProxyEventV2,
): Promise<T> {
return new Promise((resolve, reject) => {
const fields: Record<string, unknown> = {}
const bb = busboy({ headers: event.headers })
@josefaidt
josefaidt / create-store.ts
Created February 21, 2024 01:43
quick and simple store example
function createStore<T = unknown>(initial?: T) {
type Subscriber = (state: T) => void
let previous: T
let current: T
let _subscriber: Subscriber
if (initial) current = initial
const update = (state: T) => {
previous = current
@josefaidt
josefaidt / fetch-aws-credentials.ts
Created January 30, 2024 00:46
fetch-aws-credentials.ts
import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts'
export type fetchAwsCredentialsOptions = {
/**
* AWS Region to use
* @default {process.env.AWS_REGION}
*/
region: string
}
@josefaidt
josefaidt / ambient.d.ts
Last active March 22, 2023 21:11
Amplify CLI command hook helper to auto-commit changes
type HookData = {
amplify: {
environment: {
envName: string
projectPath: string
defaultEditor: string
}
command: string
subCommand: string
argv: string[]
@josefaidt
josefaidt / login.tsx
Last active March 2, 2023 22:42
Sample "login" and "logout" page with Next.js and Amplify UI
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import { useAuthenticator, Authenticator } from '@aws-amplify/ui-react'
export default function LoginPage() {
const { user, route } = useAuthenticator((context) => [
context.user,
context.route,
])
const router = useRouter()
@josefaidt
josefaidt / manually-remove-resource-from-amplify-project.md
Created May 13, 2022 19:29
Instructions for surgically removing non-existent resource from local Amplify project

Let's say we've removed a Lambda Layer from the Lambda Console and are no longer able to perform operations using the Amplify CLI.

An error occurred fetching the latest layer version metadata for ""

> amplify remove function
? Choose the resource you would want to remove 10263layer58c94806 (layer)
When you delete a layer version, you can no longer configure functions to use it.
However, any function that already uses the layer version continues to have access to it.
✖ Loading layer data from the cloud...
#!/usr/bin/env node
const { existsSync: exists, promises: fs } = require('fs');
const { spawn } = require('child_process');
const path = require('path');
const c = require('chalk');
const dotenv = require('dotenv');
const nodemon = require('nodemon');
// set default node_env
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
@josefaidt
josefaidt / post-checkout
Created November 9, 2021 21:16
Git post-checkout hook to change AWS Amplify environments upon branch checkout
#!/bin/bash
# .git/hooks/post-checkout
# Utility to change AWS Amplify environments on branch checkout
# helper to get path to aws-exports.js
function get_aws_exports_path {
local AMPLIFY_PROJECT_CONFIG_PATH="amplify/.config/project-config.json"
local AMPLIFY_EXPORTS_PATH=$(jq -r '.javascript.config.SourceDir' $AMPLIFY_PROJECT_CONFIG_PATH)
if [[ "$AMPLIFY_EXPORTS_PATH" == */ ]]
const fn = new lambda.NodejsFunction(this, 'MyFunction', {
entry: fileURLToPath(new URL('./handler.ts', import.meta.url)),
functionName: `my-function`,
bundling: {
minify: true, // minify code, defaults to false
sourceMap: true, // include source map, defaults to false
sourceMapMode: lambda.SourceMapMode.INLINE, // defaults to SourceMapMode.DEFAULT
target: 'esnext',
define: {
'import.meta.vitest': 'undefined',