Skip to content

Instantly share code, notes, and snippets.

View gameshler's full-sized avatar
🎯
Focused

Gameshler gameshler

🎯
Focused
View GitHub Profile
@gameshler
gameshler / http.ts
Created July 15, 2025 17:50
Minimal and readable HTTP status code constants with a type-safe union in TypeScript. Ideal for use in API responses, error handlers, and service layers.
export const OK = 200;
export const CREATED = 201;
export const BAD_REQUEST = 400;
export const UNAUTHORIZED = 401;
export const FORBIDDEN = 403;
export const NOT_FOUND = 404;
export const CONFLICT = 409;
export const UNPROCESSABLE_CONTENT = 422;
export const TOO_MANY_REQUESTS = 429;
export const INTERNAL_SERVER_ERROR = 500;
@gameshler
gameshler / getEnv.ts
Created July 15, 2025 17:47
A TypeScript utility to safely retrieve environment variables with optional fallbacks. Ensures critical environment variables are present and throws errors early when missing. Helps prevent runtime bugs due to undefined config.
/**
* Usage:
* create a .env file containing the correct env names
* export const PORT = getEnv("PORT");
* const DB_URL = getEnv("DATABASE_URL"); // throws if not defined
*/
const getEnv = (key: string, defaultValue?: string): string => {
const value = process.env[key] || defaultValue;
if (value === undefined) {
@gameshler
gameshler / bcrypt.ts
Created July 15, 2025 17:38
Reusable functions to hash and compare sensitive values using bcryptjs in TypeScript. Defaults to 12 salt rounds. Gracefully handles comparison errors. Ideal for a secure user authentication.
import bcrypt from "bcryptjs";
export const hashValue = async (value: string, saltRounds?: number) =>
bcrypt.hash(value, saltRounds || 12);
export const compareValue = async (value: string, hashedValue: string) =>
bcrypt.compare(value, hashedValue).catch(() => false);
@gameshler
gameshler / logger.ts
Created July 15, 2025 17:22
A clean, production-ready logger using Winston in TypeScript. Supports colorized console logging in development and daily log rotation in production.
/**
* Usage:
* import { logger } from './logger';
* logger.info("Server started");
* logger.error("Something went wrong");
*/
import { createLogger, format, transports } from "winston";
import "winston-daily-rotate-file";
import path from "path";