Skip to content

Instantly share code, notes, and snippets.

const asynchronousCall = async () => {
try {
const res = await fetch(...);
if (!res.ok) {
// Boomify the error response (See: Boom).
throw Boom(res.status, res.statusText);
}
} catch (err) {
throw err;
@james-gardner
james-gardner / email.js
Last active January 23, 2018 14:05
Example/Idea for rabbit emailer
const { map, isArray } = require('lodash');
module.exports = (exchange, key, options = {}, channel) => (messages) => {
const defaultOptions = {
priority: 0,
deliveryMode: 'persistent',
contentEncoding: 'utf-8',
contentType: 'application/json'
};
@james-gardner
james-gardner / acl.js
Created January 18, 2018 15:16
ACL with Ramda
const R = require('ramda');
const acl = [
{
roles: ['USER_ADMIN'],
allows: [
{ resource: '/test', permissions: ['get', 'post'] },
{ resource: '/plus', permissions: ['put'] }
]
@james-gardner
james-gardner / express_with_jwt.js
Created January 10, 2018 10:46 — forked from thebigredgeek/express_with_jwt.js
Express API with JWT
var express = require('express')
, jwtMiddleware = require('express-jwt')
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, cors = require('cors');
// We pass a secret token into the NodeJS process via an environment variable.
// We will use this token to sign cookies and JWTs
var SECRET_TOKEN = process.env.SECRET_TOKEN;
function factory() {
const config = {
data: []
};
return {
add(...nodes) {
nodes.forEach((item) => {
@james-gardner
james-gardner / buildStack.js
Created October 3, 2017 16:15
Route generation
function buildStack(routes) {
const router = express.Router();
each(routes, (config, path) => {
const { chain, method } = config;
router[method](path, chain);
});
return router;
@james-gardner
james-gardner / get-npm-package-version
Created July 6, 2017 10:08 — forked from DarrenN/get-npm-package-version
Extract version from package.json (NPM) using bash / shell
# Version key/value should be on his own line
PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g')
echo $PACKAGE_VERSION
@james-gardner
james-gardner / cps.js
Created June 2, 2017 13:36 — forked from trdarr/cps.js
"Thunks, Trampolines, and Continuation Passing" in JavaScript
/* "Thunks, Trampolines, and Continuation Passing"
* Python implementation from http://jtauber.com/blog/2008/03/30/
* JavaScript implementation by Thomas Darr <me@trdarr.com>.
*/
// thunk = lambda name: lambda *args: lambda: name(*args)
var thunk = function thunk(fn) {
return function _thunk() {
var splat = Array.prototype.slice.apply(arguments);
return function __thunk() { return fn.apply(this, splat); };
@james-gardner
james-gardner / Enhance.js
Created September 22, 2016 07:30 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
import whilst from 'async/whilst';
const buffer = {
data: [],
page: -1,
cursor: 0,