Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mykeels's full-sized avatar
😄
faffing!

Ikechi Michael mykeels

😄
faffing!
View GitHub Profile
@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
@mykeels
mykeels / inline-email-templates.script.ts
Created April 21, 2021 23:48
A script to inline styles on *.hbs files in a directory
import { promises as fs } from 'fs';
import path from 'path';
import inlineCss from 'inline-css';
import env from '../common/config/env';
/**
* So there are multiple *.hbs files, in a directory, and one of them, contains a link reference to tailwind cdn like:
* <link rel="stylesheet" href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css">
* In this script, we inline all their styles by
* - reading each file's content
- name: President Muhammadu Buhari
ministry: Minister of Petroleum
email: info@petroleumresources.gov.ng
phone: "0952335368"
- name: Mohammed Musa Bello
ministry: Minister of Federal Capital Territory, FCT
email: ""
phone: ""