Skip to content

Instantly share code, notes, and snippets.

View devhammed's full-sized avatar
💭
Changing the world, one dollar sign in my PHP code at a time!

Hammed Oyedele devhammed

💭
Changing the world, one dollar sign in my PHP code at a time!
View GitHub Profile
@devhammed
devhammed / 01-audit.module.ts
Last active September 12, 2025 09:42
Nest.js Audit Module (request with all database operations that happened in the request-response flow)
import {
forwardRef,
MiddlewareConsumer,
Module,
NestModule,
} from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Audit } from '@/audit/entities/audit.entity';
import { AuditMiddleware } from '@/audit/middlewares/audit.middleware';
import { APP_GUARD } from '@nestjs/core';
@devhammed
devhammed / 01-UrlGenerator.php
Last active September 3, 2025 16:52
Laravel Locale-aware URL generator.
<?php
namespace App\Support\Routing;
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class UrlGenerator extends BaseUrlGenerator
{
public function to($path, $extra = [], $secure = null): string
@devhammed
devhammed / 01-comparison.validator.ts
Last active August 31, 2025 09:06
Nest.js Comparison Class Validator
import { Injectable } from '@nestjs/common';
import {
ValidationArguments,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
import { ComparisonValidatorType } from '@/app/enums/comparison-validator-type.enum';
export type ComparisonValidatorConstraint<T extends object> = [
keyof T,
@devhammed
devhammed / 01-presence.validator.ts
Last active August 31, 2025 09:11
Nest.js Database Presence Class Validator
import {
EntityManager,
EntityTarget,
ObjectLiteral,
SelectQueryBuilder,
} from 'typeorm';
import { Injectable } from '@nestjs/common';
import {
ValidationArguments,
ValidatorConstraint,
@devhammed
devhammed / typeorm-query-failed.filter.ts
Last active August 31, 2025 09:24
Nest.js TypeORM QueryFailedError Friendly Messages Filter
import {
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { QueryFailedError } from 'typeorm';
import { camelCase } from 'typeorm/util/StringUtils';
export enum PostgresErrorCode {
@devhammed
devhammed / custom-class-serializer.interceptor.ts
Last active August 22, 2025 15:55
Custom Class Serializer Interceptor (support for automatically adding user roles)
import {
Injectable,
ExecutionContext,
CallHandler,
ClassSerializerInterceptor,
PlainLiteralObject,
} from '@nestjs/common';
import { Request } from 'express';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@devhammed
devhammed / fetcher.ts
Created August 22, 2025 11:51
TypeScript API Fetcher (plus refresh token support even across multiple requests, the other requests will wait for the promise set by the first request to acquire the promise)
import { getJwtToken, removeJwtToken, setJwtToken } from '@/providers/auth-provider.ts';
import { backendUrl } from '@/utils/config.ts';
import type { JwtTokenModel } from '@/utils/models.ts';
export interface FetcherResponse<TData> {
message: string;
statusCode: number;
data?: TData;
error?: string;
page?: number;
@devhammed
devhammed / 01-notification.service.ts
Last active August 22, 2025 19:44
Laravel-like Notification Service for Nest.js
import nodemailer from 'nodemailer';
import { FindOptionsWhere, IsNull, Like, Not, Repository } from 'typeorm';
import { User } from '@/user/entities/user.entity';
import { Notification } from '@/common/entities/notification.entity';
import { InjectRepository } from '@nestjs/typeorm';
import {
forwardRef,
Inject,
Injectable,
NotFoundException,
@devhammed
devhammed / active-scope-repository.factory.ts
Last active August 19, 2025 18:38
Hide TypeORM Entities Marked As Inactive From All Queries With Support For Nested Relations (except for admin users that can specify to see them with active records by setting "includeInactive" to true or only them by setting "onlyInactive" to true)
// This must be in a top-level module as a provider (e.g. AppModule or a shared module marked as Global)
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { DataSource, EntityTarget, ObjectLiteral } from 'typeorm';
import { ActiveScopeRepository } from '@/common/repositories/active-scope.repository';
@Injectable({
scope: Scope.REQUEST,
@devhammed
devhammed / pusher_channels_ws_link.dart
Last active June 10, 2025 10:23
Pusher Channels Link for Flutter GraphQL/Ferry.