Skip to content

Instantly share code, notes, and snippets.

View mykeels's full-sized avatar
😄
faffing!

Michael Ikechi mykeels

😄
faffing!
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mykeels
mykeels / transformWebsocketMessage.helper.ts
Created May 30, 2024 14:26
Intercept and transform websocket messages
import { Page } from '@playwright/test';
export async function transformWebsocketMessage(
page: Page,
fn: <TMessage extends Record<string, unknown>>(
url: string,
message: TMessage
) => TMessage
) {
fn = fn || ((url, message) => message);
@mykeels
mykeels / 0_Program.cs
Last active April 13, 2024 07:34
Code for extracting OpenAPI schema from ASP.NET Core projects
// Use a conditional, because You may not want to provide swagger documentation in public environments
if (_configuration.GetValue<bool>("SwaggerConfiguration:EnableSwagger"))
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options => {
options.AddCustomIds();
options.AddMetadata(typeof(Program));
options.SchemaFilter<NullableEnumSchemaFilter>();
options.SchemaFilter<RequiredPropertiesSchemaFilter>();
@mykeels
mykeels / change-casing.ts
Last active January 8, 2024 21:23
Change cashing
import { camelCase, pascalCase, snakeCase } from "change-case";
type CamelToPascalCase<S extends string> = S extends `${infer F}${infer R}`
? `${Capitalize<F>}${R}`
: S;
type PascalToCamelCase<S extends string> = S extends `${infer F}${infer R}`
? `${Uncapitalize<F>}${R}`
: S;
type SnakeToCamelCase<S extends string> = S extends `${infer F}_${infer R}`
? `${Uncapitalize<F>}${Capitalize<SnakeToCamelCase<R>>}`
@mykeels
mykeels / controller-error-handling.ts
Created August 19, 2023 01:03
controller-error-handling.ts
import httpStatus from "http-status";
export const makeError = <TErrorName extends string>(
name: TErrorName,
message?: string
) => ({
name,
message,
});
@mykeels
mykeels / repository.interface.ts
Created August 5, 2023 21:05
Generic repository interface, that just might work across DBs
type Prettify<T> = {
[K in keyof T]: T[K];
};
export type Model<T> = Prettify<
T & {
_id: string;
createdAt: Date | string;
updatedAt: Date | string;
deletedAt?: Date | string | null;
@mykeels
mykeels / snake-camel.d.ts
Created May 30, 2023 23:25
Typings for the snake-camel npm package https://www.npmjs.com/package/snake-camel
type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}`
? `${T extends Capitalize<T> ? '_' : ''}${Lowercase<T>}${CamelToSnakeCase<U>}`
: S
type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}`
? `${T}${Capitalize<SnakeToCamelCase<U>>}`
: S
type StringKeys<TEntity extends {}> = {
[key in keyof TEntity]: key extends string ? key : never
}[keyof TEntity]
type IsLiteral<TValue> = TValue extends string | number | boolean | symbol
@mykeels
mykeels / USE_FETCH_USERS_HOOK.md
Last active April 14, 2022 14:39
A simple hook, showing dependency inversion

Here, we'll explore loosely coupling react hooks with dependency inversion as a way to manage side effects.

NB: The hooks code given here, uses react-query hooks for simplicity cos I'm rushing to write this. I hope that's not a problem. Perhaps, someone will write the equivalent implementations using out-of-the-box react hooks.

We'll consider a simple react hook, that fetches a list of users in two ways:

1. No dependency inversion

const useFetchUsers = () =&gt; {
/**
* @typedef {object} AWSCloudfrontMessageEvent
* @property {AWSCloudfrontEventRecord[]} Records
*/
/**
* @typedef {object} AWSCloudfrontEventRecord
* @property {object} cf
* @property {{ distributionId: string, eventType: string, requestId: string, distributionDomainName: string }} cf.config
* @property {AWSCloudfrontRequest} cf.request