Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / serverless-function-reference-formats.yml
Created Sep 19, 2020
Various ways to reference function name and ARN in serverless.yml
View serverless-function-reference-formats.yml
function:
foo:
handler: index.handler
# Ways to reference function in serverless.yml
FunctionName: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${self:service.name}-${self:provider.stage}-foo
FunctionName: !GetAtt FooLambdaFunction.Arn
FunctionName: !Sub "${AWS::StackName}-foo"
FunctionName: ${self:service.name}-${self:provider.stage}-foo
@DavidWells
DavidWells / using-cloudformation-metadata.yml
Created Sep 14, 2020
Example of using Metadata 'AWS::CloudFormation::Interface' to group stack params in CF deploy UI
View using-cloudformation-metadata.yml
# via https://mechanicalrock.github.io/2020/03/17/cloudformation-tricks-part-1.html
Parameters:
Name:
Type: String
Description: >-
The name of the application.
This should include a stage qualifier if this is a nonproduction instance.
Stage qualifiers should only include 3-5 characters
AllowedPattern: ^[a-z]{3,}(-[a-z]{3,5})?$
MinLength: 3
View locked-down-serverless-framework-policy.json
// From https://serverless-stack.com/chapters/customize-the-serverless-iam-policy.html
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudformation:Describe*",
"cloudformation:List*",
"cloudformation:Get*",
@DavidWells
DavidWells / cognito-lite.js
Created Sep 3, 2020
Add cognito auth with amplify without entire framework
View cognito-lite.js
// via https://github.com/aws-amplify/amplify-js/discussions/5554
import {Auth} from "@aws-amplify/auth";
....
Auth.configure({
// REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',
// REQUIRED - Amazon Cognito Region
region: 'XX-XXXX-X',
@DavidWells
DavidWells / parse-url-no-libs.js
Created Aug 23, 2020
Parse URL in browser / node with no dependancies
View parse-url-no-libs.js
function parseUrl(url) {
const match = url.match(/^(https?)?(?:[\:\/]*)([a-z0-9\.-]*)(?:\:([0-9]+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i)
return {
protocol: match[1] || '',
host: match[2] || '',
port: match[3] || '',
path: match[4] || '',
query: match[5] || '',
fragment: match[6] || '',
}
@DavidWells
DavidWells / refresh-token.js
Created Aug 23, 2020 — forked from pfulop/refresh-token.js
how to refresh amplify token
View refresh-token.js
const refreshToken = async () => {
const refreshPromise = new Promise(async (resolve) => {
const user = await Auth.currentAuthenticatedUser();
const session = await Auth.currentSession();
user.refreshSession(session.refreshToken, async (res, newSession) => {
if (newSession) {
await Auth.currentUserCredentials();
}
resolve('ok');
@DavidWells
DavidWells / cypress-tests-on-netlify-url.json
Last active Aug 13, 2020
Run Cypress tests on live netlify URL
View cypress-tests-on-netlify-url.json
{
"scripts": {
"installCLI": "npm install netlify-cli",
"build": "react-scripts build && npm run deployTestSite",
"deployTestSite": "netlify deploy --json | jq {siteURL} | cypress test {siteURL}",
"test": "react-scripts test",
"deploy": "netlify deploy --dir build --prod"
},
}
View async-forEach.js
var waitFor = (ms) => new Promise(r => setTimeout(r, ms))
async function asyncForEach(array, callback) { 
for (let index = 0; index < array.length; index++) {   
await callback(array[index], index, array) 
}
}
var start = async () => { 
let state = 0 
await asyncForEach([1, 2, 3], async (num) => {   
View get-global-context.js
function getGlobalThis() {
if (typeof globalThis !== 'undefined') return globalThis
if (typeof global !== 'undefined') return global
if (typeof self !== 'undefined') return self /* eslint-disable-line no-restricted-globals */
if (typeof window !== 'undefined') return window
if (typeof this !== 'undefined') return this
return {} // should never happen
}
View generate.js
// From https://github.com/sw-yx/swyxdotio/blob/60b088cea0439d3e2536a78dc922af3146ba40fd/screenshot-plugin/screenshot.js
const puppeteer = require('puppeteer')
const fs = require('fs')
const path = require('path')
module.exports = screenshot
async function screenshot(PostArray) {
const headless = true
// const headless = false // for debug
const browser = await puppeteer.launch({ headless })