Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / netlify.toml
Last active February 7, 2024 08:50
All Netlify.toml & yml values
[Settings]
ID = "Your_Site_ID"
# Settings in the [build] context are global and are applied to all contexts unless otherwise overridden by more specific contexts.
[build]
# This is the directory to change to before starting a build.
base = "project/"
# NOTE: This is where we will look for package.json/.nvmrc/etc, not root.
# This is the directory that you are publishing from (relative to root of your repo)
@DavidWells
DavidWells / aws-lambda-redirect.js
Created June 28, 2018 20:48
How to do a 301 redirect from an AWS lambda function
exports.handler = (event, context, callback) => {
const response = {
statusCode: 301,
headers: {
Location: 'https://google.com',
}
};
return callback(null, response);
}
@DavidWells
DavidWells / add-wordpress-settings-page.php
Created January 28, 2013 05:59
WordPress :: Add Settings Page with All Fields
<?php
/*
Plugin Name: Homepage Settings for BigBang
Plugin URI: http://www.inboundnow.com/
Description: Adds additional functionality to the big bang theme.
Author: David Wells
Author URI: http://www.inboundnow.com
*/
// Specify Hooks/Filters
@DavidWells
DavidWells / javascript-proxy-as-rest-client.js
Last active October 6, 2023 18:39
Using a javascript proxy as low code REST client
/* Using a JavaScript proxy for a super low code REST client */
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3
// also see https://github.com/fastify/manifetch
// also see https://github.com/flash-oss/allserver
// and https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb
const createApi = (url) => {
return new Proxy({}, {
get(target, key) {
@DavidWells
DavidWells / clean-secret-from-url.js
Created October 24, 2018 21:43
Clean your access token from URL to guard against user accidentally copy + pasting url elsewhere
function removeAccessTokenFromUrl() {
const { history, location } = window
const { search } = location
if (search && search.indexOf('access_token') !== -1 && history && history.replaceState) {
// remove access_token from url
const cleanSearch = search.replace(/(\&|\?)access_token([_A-Za-z0-9=\.%]+)/g, '').replace(/^&/, '?')
// replace search params with clean params
const cleanURL = location.toString().replace(search, cleanSearch)
// use browser history API to clean the params
history.replaceState({}, '', cleanURL)
@DavidWells
DavidWells / stringify-object.js
Created September 13, 2023 23:23
stringify object into one line
const props = {
text: 'hello',
boolean: true,
array: ['hi', 'there', true],
object: {
cool: true,
nice: 'awesome'
},
func: () => {},
@DavidWells
DavidWells / toml-vs-yaml.md
Last active July 21, 2023 09:21
Yaml and Toml

Which is better? YOU DECIDE

# toml
[build]
  command = "npm run build"
/* Helper buddy for removing async/await try/catch litter 🗑 */
function O_o(promise) {
return promise.then(data => {
if (data instanceof Error) return [data]
return [null, data]
}).catch(err => [err])
}
/* Look ma, no try/catch */
async function usageExample(params) {
@DavidWells
DavidWells / async-await-node-style-promises.js
Last active June 30, 2023 14:08
Nicer business logic with async wait promises that won't swallow native JS errors. Update to https://gist.github.com/DavidWells/54f9dd1af4a489e5f1358f33ce59e8ad where we handle native JS errors instead of potentially missing them. Now a package here https://www.npmjs.com/package/safe-await
/* Native Error types https://mzl.la/2Veh3TR */
const nativeExceptions = [
EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError
].filter((except) => typeof except === 'function')
/* Throw native errors. ref: https://bit.ly/2VsoCGE */
function throwNative(error) {
for (const Exception of nativeExceptions) {
if (error instanceof Exception) throw error
}
@DavidWells
DavidWells / dynamic-html-lambda.js
Created January 26, 2019 01:37
Respond with dynamic HTML from a lambda function
module.exports = (event, context, callback) => {
let name
if (event.pathParameters && event.pathParameters.name) {
name = event.pathParameters.name
}
/* generate the hello paragraph */
const helloParagraph = greetPerson(name)
// callback is sending HTML back