Navigation Menu

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 / 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 / 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 / MDXProvider.tsx
Last active January 25, 2023 16:26
RECMA MDX Frontmatter plugin to transform props generated from remark-mdx-frontmatter and feed into wrapper component
import React from 'react';
import { MDXProvider as StockMDXProvider } from '@mdx-js/react';
import type { PropsWithChildren } from 'react';
const wrapper = ({ children, frontmatter }) => {
console.log('mdx page props', { props: { frontmatter } });
return <Page meta={frontmatter}>{children}</Page>;
};
const shortcodes = {
const regex = /<(?<tag>[^>]*)>/;
/**
* Verifies whether the string is an HTML string
* @param str string to check
*/
export const isHtmlString = (str: string): boolean => {
// <code>hello world</code> -> true
// hello <code>world</code> -> true
// hello world -> false
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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',