Skip to content

Instantly share code, notes, and snippets.

View adriangabardo's full-sized avatar
:octocat:

Adrian Gabardo adriangabardo

:octocat:
  • Australia
View GitHub Profile
@adriangabardo
adriangabardo / generic_spread.ts
Created February 21, 2022 14:23
A gist with examples of how to create a higher-order component that receives a method with a generic spread variable parameter, and how to type constrain said spread
/**
* Example of a higher-order component with type-safety for functions with generic spread variables.
* In this example, the HOC is a function to create AWS Lambda (for AWS API Gateway) function handlers.
* All AWS Lambda exported handlers receive an (event, context) set of parameters.
*
* @author Adrian Gabardo <contact@adriangabardo.com>
*/
type LambdaResponse = Promise<any | void>;
type BaseLambda = (event, context) => LambdaResponse;
@adriangabardo
adriangabardo / deep_nullable.ts
Created April 12, 2022 06:27
Example of a generic type alias that transforms a given type into a nullable type (including sub-properties in case of objects)
/**
* Type alias to recursively make all properties of an object optionally undefined or null.
* Adds | undefined | null to all objects and its properties.
*/
type DeepNullable<T> = T extends object
? { [k in keyof T]?: DeepNullable<T[k] | null> }
: T | null;
interface TestObj {
nothing: boolean;
@adriangabardo
adriangabardo / extract_env_vars.ts
Created July 27, 2022 06:43
Safely extract environment variables with proper generic typings
/**
* A utility method to safely extract environment variables.
* By default, if an environment variable is asked to be extracted but it is missing, this method will throw an error.
* @param options - Additional options for this method's behaviour, including deciding if it should throw when a variable is missing.
* @param environmentVariables - A spread of environment variable keys to be fetched from process.env.
* @returns A new object with each environmentVariables key in it. Unless throwOnError is false, all environmentVariables params will be present.
*/
export const extract_environment_variables = <T extends string[]>(
{ throwOnError = true }: { throwOnError?: boolean },
...environmentVariables: T
@adriangabardo
adriangabardo / array_injection.ts
Last active August 23, 2023 06:50
Inject extra properties into a native interface
// Defining a searchBy method on an array
interface SearchFn<T> {
(key: string, value: string): { index: number; value: T }[] | undefined;
}
// Extend the Array interface to include a searchBy method
interface SearchableArray<T> extends Array<T> {
searchBy: SearchFn<T>;
}
@adriangabardo
adriangabardo / db.sql
Last active August 27, 2023 02:55
Lynn DB
-- Create database
CREATE DATABASE "RENTALS";
CREATE TABLE CUSTOMER (
CID SERIAL PRIMARY KEY,
name TEXT NOT NULL,
address TEXT NOT NULL,
phone TEXT NOT NULL
);
services:
postgres:
image: postgres:14.1-alpine
restart: always
environment:
# You can set the value of environment variables
# in your docker-compose.yml file
# Our Node app will use these to connect
# to the database
- POSTGRES_USER=root