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 / 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.
@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;
}
@devhammed
devhammed / cloudfare-nginx.sh
Last active March 25, 2025 04:55
Script that automatically generates Cloudfare real-ips configuration for NGINX, you need to add `include /etc/nginx/cloudflare;` to the `http` block in `/etc/nginx/nginx.conf` file to activate.
#!/bin/bash
set -eu
CLOUDFLARE_FILE_PATH=/etc/nginx/cloudflare
echo "# Cloudflare" > $CLOUDFLARE_FILE_PATH;
echo "" >> $CLOUDFLARE_FILE_PATH;
@devhammed
devhammed / cloudfare-ufw.sh
Last active March 25, 2025 04:53
Script to reject TCP connections that are not coming from Cloudfare except if it is SSH.
#!/bin/bash
set -eu
# Get the Cloudflare IPs.
curl -s https://www.cloudflare.com/ips-v4 -o /tmp/cloudflare_ips
echo "" >> /tmp/cloudflare_ips
curl -s https://www.cloudflare.com/ips-v6 >> /tmp/cloudflare_ips
# Reset the firewall to clean stuff.
<?php
/**
* Class Duration
* Represents a time span with microsecond precision, similar to Dart's Duration class.
*/
class Duration
{
/**
* @var int Total duration in microseconds.