Skip to content

Instantly share code, notes, and snippets.

💭
I may be slow to respond.

Alfon alfonmga

Block or report user

Report or block alfonmga

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@alfonmga
alfonmga / formWithFormik.js
Created May 12, 2019
Example enhanced form with Formik
View formWithFormik.js
import React from 'react';
import { withFormik } from 'formik';
const MyForm = props => {
const {
values,
touched,
errors,
handleChange,
handleBlur,
@alfonmga
alfonmga / saas-plan-based-volume-limits.md
Created May 6, 2019
SaaS plan based volume limits example
View saas-plan-based-volume-limits.md

Plan based volume limits

Checking plan features is pretty neatly handled by a fairly generic way of asserting if a thing is either "on" or "off".

Checking volumes is a bit different. Why is it different? It is different because we need to include the state of specific resources we are offering our customers, not just flags on the account record.

This means you have to actively poll your database and count stuff on each request. Yes, you can cache a bit and being off by one might not be the end of the world.

In the pricing page example above you can see Checkly offers 5 API checks for one plan and 15 for the other. This is how we assert this volume limit in our backend API

@alfonmga
alfonmga / example.js
Created Apr 7, 2019
JavaScript Abstract Factory Pattern
View example.js
class AudioDevice {
constructor () {
this.isPlaying = false;
this.currentTrack = null;
}
play (track) {
this.currentTrack = track;
this.isPlaying = true;
this.handlePlayCurrentAudioTrack();
View mongoose-list-method.js
/**
* List users in descending order of 'createdAt' timestamp.
* @param {number} skip - Number of users to be skipped.
* @param {number} limit - Limit number of users to be returned.
* @returns {Promise<User[]>}
*/
list({ skip = 0, limit = 50 } = {}) {
return this.find()
.sort({ createdAt: -1 })
.skip(+skip)
@alfonmga
alfonmga / scheduler.js
Created Mar 12, 2019
JS Interval Scheduler
View scheduler.js
const uuid = require('uuid');
const _idn = (id, name) => `${name}-${id}`;
class IntervalScheduler {
constructor({ log }) {
this._log = log.create('intervalScheduler');
this._jobs = {};
}
View build-changes-detector.sh
if git diff --name-only HEAD^...HEAD | grep "^src/${PROJECT}"; then
echo "Changes here, run the build"
npm run test
docker build --tag elasticio/${PROJECT}:${REVISION}
docker push elasticio/${PROJECT}:${REVISION}
else
echo "No changes detected"
exit 0
fi
@alfonmga
alfonmga / .bashrc
Last active Jan 25, 2019
Lighting Network Full Node aliases
View .bashrc
alias lndstatus='sudo journalctl -f -u lnd'
alias bitcoindstatus='sudo tail -f /home/bitcoin/.bitcoin/testnet3/debug.log'
alias unlock='lncli unlock'
alias newaddress='lncli --network=testnet newaddress np2wkh'
alias txns='lncli --network=testnet listchaintxns'
alias getinfo='lncli --network=testnet getinfo'
alias walletbalance='lncli --network=testnet walletbalance'
alias peers='lncli --network=testnet listpeers'
alias channels='lncli --network=testnet listchannels'
alias channelbalance='lncli --network=testnet channelbalance'
@alfonmga
alfonmga / coinbase-timezones.html
Last active May 21, 2019
Coinbase.com TZ list
View coinbase-timezones.html
<select name="user[time_zone]" id="user_time_zone">
<option value="American Samoa">(GMT-11:00) American Samoa</option>
<option value="International Date Line West">(GMT-11:00) International Date Line West</option>
<option value="Midway Island">(GMT-11:00) Midway Island</option>
<option value="Hawaii">(GMT-10:00) Hawaii</option>
<option value="Alaska">(GMT-09:00) Alaska</option>
<option value="Pacific Time (US &amp; Canada)">(GMT-08:00) Pacific Time (US &amp; Canada)</option>
<option value="Tijuana">(GMT-08:00) Tijuana</option>
<option value="Arizona">(GMT-07:00) Arizona</option>
<option value="Chihuahua">(GMT-07:00) Chihuahua</option>
@alfonmga
alfonmga / apollo-link-error.js
Last active May 21, 2019
Apollo link error handler. It tries to refresh user access token on the fly when API throws out an UNAUTHENTICATED error. If multiple requests fails at the same time it queues them to re-try them later if we are able to get a new access token, otherwise we logout the user and redirect him to the login page.
View apollo-link-error.js
@alfonmga
alfonmga / install-dependencies-monorepo.js
Created Dec 1, 2018
We use this to install all dependencies when we have a huge monorepo aka multiple directories with his own package.json
View install-dependencies-monorepo.js
// @flow
const { readdirSync, statSync } = require('fs');
const { join } = require('path');
const { spawn } = require('child_process');
const getDirs = p =>
readdirSync(p).filter(f => statSync(join(p, f)).isDirectory());
// Get a list of all non-build directories in the root folder
const rootDirs = getDirs(join(__dirname, '..')).filter(
You can’t perform that action at this time.