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-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 December 25, 2025 14:05
Laravel Reverb & Pusher Channels Link for Dart/Flutter GQL libraries like Ferry.
@devhammed
devhammed / fetch.php
Last active May 22, 2025 11:18
PHP Fetch
<?php
if (! class_exists('FetchResponse')) {
class FetchResponse
{
private int $status;
private string $body;
private ?string $error;
private array $headers;
@devhammed
devhammed / async.php
Created May 20, 2025 17:01
Async/Await in PHP
<?php
function async(Closure $task): Closure
{
static $resolved = [];
if ( ! extension_loaded('pcntl') || ! extension_loaded('posix')) {
return $task;
}