Skip to content

Instantly share code, notes, and snippets.

View Hoxtygen's full-sized avatar
🎯
Focusing

Wasiu Idowu Hoxtygen

🎯
Focusing
  • Nigeria
View GitHub Profile
//file: ./pages/api/checkHTTPMethod.ts
/* this code will allow only GET method requests on this route */
import { Middleware, use } from 'next-api-route-middleware';
import type { NextApiRequest, NextApiResponse } from 'next';
export const allowMethods = (allowedMethods: string[]): Middleware => {
return async function (req, res, next) {
@mhaecal
mhaecal / middleware.ts
Created July 19, 2022 08:25
Protected Routes Middleware NextJS 12
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(req: NextRequest) {
// get cookie token
const hasToken = req.cookies.get('token')
// protected routes (admin routes)
if (req.nextUrl.pathname.startsWith('/admin')) {
if (hasToken) {
@timosadchiy
timosadchiy / jest.config.js
Last active September 22, 2023 05:46
Jest + Typescript. Resolve tsconfig.json paths.
/**
* Converts paths defined in tsconfig.json to the format of
* moduleNameMapper in jest.config.js.
*
* For example, {'@alias/*': [ 'path/to/alias/*' ]}
* Becomes {'@alias/(.*)': [ '<rootDir>/path/to/alias/$1' ]}
*
* @param {string} srcPath
* @param {string} tsconfigPath
*/
@filiph
filiph / main.dart
Last active July 11, 2021 10:22
A functional way to capitalize each word in a sentence (a.k.a. Title Case). This is not efficient -- use something like string_scanner if you need to run this in a tight loop.
main() {
var city = "new york";
print(titleCase(city));
}
/// Inefficient way of capitalizing each word in a string.
String titleCase(String text) {
if (text.length <= 1) return text.toUpperCase();
var words = text.split(' ');
var capitalized = words.map((word) {
@adamelliotfields
adamelliotfields / docker-compose.yml
Created February 10, 2019 22:39
Docker Compose Mongo with Mongo Express
version: "3.5"
services:
mongo:
image: mongo:latest
container_name: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
ports:
@iamandrewluca
iamandrewluca / oss-tools.md
Last active May 20, 2022 07:18
Open source software tools
@paolocarrasco
paolocarrasco / README.md
Last active April 23, 2024 14:50
How to understand the `gpg failed to sign the data` problem in git

Problem

You have installed GPG, then tried to commit and suddenly you see this error message after it:

error: gpg failed to sign the data
fatal: failed to write commit object

Debug

@BilalBudhani
BilalBudhani / FileUpload.js
Last active September 17, 2023 07:21
Upload Multiple Files To Cloudinary With React & Axios
handleDrop = files => {
// Push all the axios request promise into a single array
const uploaders = files.map(file => {
// Initial FormData
const formData = new FormData();
formData.append("file", file);
formData.append("tags", `codeinfuse, medium, gist`);
formData.append("upload_preset", "pvhilzh7"); // Replace the preset name with your own
formData.append("api_key", "1234567"); // Replace API key with your own Cloudinary key
formData.append("timestamp", (Date.now() / 1000) | 0);
@zcaceres
zcaceres / Revealing-Module-Pattern.md
Last active April 23, 2024 22:02
Using the Revealing Module Pattern in Javascript

The Revealing Module Pattern in Javascript

Zach Caceres

Javascript does not have the typical 'private' and 'public' specifiers of more traditional object oriented languages like C# or Java. However, you can achieve the same effect through the clever application of Javascript's function-level scoping. The Revealing Module pattern is a design pattern for Javascript applications that elegantly solves this problem.

The central principle of the Revealing Module pattern is that all functionality and variables should be hidden unless deliberately exposed.

Let's imagine we have a music application where a musicPlayer.js file handles much of our user's experience. We need to access some methods, but shouldn't be able to mess with other methods or variables.

Using Function Scope to Create Public and Private Methods

@ca0v
ca0v / debounce.ts
Last active April 4, 2024 08:28
Typescript Debounce
// ts 3.6x
function debounce<T extends Function>(cb: T, wait = 20) {
let h = 0;
let callable = (...args: any) => {
clearTimeout(h);
h = setTimeout(() => cb(...args), wait);
};
return <T>(<any>callable);
}