Skip to content

Instantly share code, notes, and snippets.

View maslade's full-sized avatar
⚒️
Beating up GraphQL

Mark Slade maslade

⚒️
Beating up GraphQL
  • Peak 46 Development, LLC.
  • Syracuse, NY
View GitHub Profile
@maslade
maslade / test-playback.js
Created July 9, 2021 13:15
Loading and playing an MP3 in a headless browser
const puppeteer = require('puppeteer');
const testSrc = 'https://download.samplelib.com/mp3/sample-3s.mp3';
const audioEventTypes = [
'audioprocess',
'canplay',
'canplaythrough',
'complete',
'durationchange',
'emptied',
openapi: 3.0.0
info:
title: My microservice
# The double quotes are necessary to prevent version from being
# interpreted as a floating point number.
version: "1.0"
paths:
'/api/users/{userId}':
post:
// Imports types from the implementation.
import { User } from './api/models/user';
// ... others
// Exports types used in the REST layer - a request/response/both per API operation.
// This naming convention ensures that the name in OpenAPI won't change even if the
// underlying type does.
export type IUpdateUserRequestBody = Partial<User>;
export type IUpdateUserResponseBody = Omit<User, 'Password'>;
// ... others
// much better
interface IGenericRequest {
requestId: string;
}
interface IGetUserRequest extends IGenericRequest {
username: string;
}
function mustBeUserRequest(request: IGenericRequest): asserts request is IGetUserRequest {
// much better
interface IGenericRequest {
requestId: string;
}
interface IGetUserRequest extends IGenericRequest {
username: string;
}
function isUserRequest(request: IGenericRequest): request is IGetUserRequest {
// not great
interface IGenericRequest {
requestId: string;
}
interface IGetUserRequest extends IGenericRequest {
username: string;
}
function handler(request: IGenericRequest) {
const list: Array<number | string> = [1, 'two', 3, 'four'];
// the implicit type of both of these consts is Array<string | number> -- not helpful!
const numbers = list.filter(thing => typeof thing === 'number');
const strings = list.filter(thing => typeof thing === 'string');
@maslade
maslade / merging-event-emitters.js
Last active May 6, 2018 23:51
EventEmitter bubbling through composition.
/**
* Demonstrates a method for creating bubbling behavior through EventEmitter
* composition. With this method a parent class (ConsumerClass) is composed
* of instances of helper classes (HelpfulClass), and wants to bubble all of
* their events up to itself. The goal is that client code can listen at the
* parent level (to receive all events) or at the instance level.
*
* Limitations without a little more scaffolding:
* - no context for determining where an event originated from.
* - this implementation assumes that HelpfulClass.emitter will not have
@maslade
maslade / self-documenting.js
Last active April 22, 2018 11:47
Examples for writing self-documenting code.
// Awful - hard to parse the grammar with your eyes, easier to mix up parentheses
navigator.geolocation.getCurrentPosition((loc) => this.located(loc.coords.latitude, loc.coords.longitude), () => this.located(lat, lng))
// Better - easy to parse out what is happening, but not the meaning... what are the arrow functions for?
navigator.geolocation.getCurrentPosition(
(loc) => this.located(loc.coords.latitude, loc.coords.longitude),
() => this.located(lat, lng)
)
// Good - I can tell exactly what's going on now.