Skip to content

Instantly share code, notes, and snippets.

@saleebm
saleebm / upload-image.ts
Created February 17, 2021 19:34
handle image upload using next.js API routes
import { NextApiRequest } from 'next'
import formidable from 'formidable'
import { promises as fs } from 'fs'
import path from 'path'
import sanitize from 'sanitize-filename'
import axios from 'axios'
import { handler } from '@Lib/api'
import {
UnauthenticatedError,
UnsupportedMethodError,
@saleebm
saleebm / auth-links.ts
Last active August 1, 2021 19:58
Auth Links for Apollo for WP-GraphQL
@saleebm
saleebm / get-client-ip-address.ts
Created May 2, 2020 06:12
Get a public ip address from client on server
const LOCAL_IPV4_REGEX = /(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/
const LOCAL_IPV6_REGEX = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/
export function getClientIPAddress() {
let addrInfo, ifaceDetails
let localIPInfo: string = ''
//Get the network interfaces
const networkInterfaces = require('os').networkInterfaces()
//Iterate o
@saleebm
saleebm / PostList.js
Last active February 16, 2020 20:55
Pagination woes
import { useQuery } from "@apollo/react-hooks";
import { NetworkStatus } from "@apollo/client";
import gql from "graphql-tag";
import ErrorMessage from "./ErrorMessage";
export const ALL_POSTS_QUERY = gql`
query GET_ALL_PRODUCTS_PAGES(
$first: Int
$after: String
$last: Int
@saleebm
saleebm / domain.org.conf
Created January 11, 2020 21:51
headless wordpress w/Nginx and Bedrocks setup (many thanks to https://www.digitalocean.com/community/tools/nginx)
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css 30d;
application/javascript 30d;
~image/ max;
font/woff2 max;
font/woff max;
image/x-icon max;
<?php
use GraphQL\Error\UserError;
use WPGraphQL\Data\UserMutation;
use WPGraphQL\JWT_Authentication\Auth;
use WPGraphQL\Model\User;
use WPGraphQL\Mutation\UserRegister;
use WPGraphQL\Types;
use WPGraphQL\WooCommerce\Data\Mutation\Customer_Mutation;
use WPGraphQL\WooCommerce\Model\Customer;
@saleebm
saleebm / Main.java
Last active November 29, 2019 18:30
Tree maps with dates
import java.util.*;
import java.text.*;
public class TreeMapDemo {
public static void main(String[] args) {
// creating maps
TreeMap<Date, String> treemap = new TreeMap<Date, String>();
SortedMap<Date, String> treemaphead = new TreeMap<Date, String>();
SortedMap<Date, String> treemaptail = new TreeMap<Date, String>();
@saleebm
saleebm / life.ts
Last active November 26, 2019 01:15
typeof Life
type Eternal = unknown;
type Dust<T extends any[], R> = (...args: T) => R;
class Sin extends Error {
source: InnerLife;
}
class Choices implements Sweat {
Labor: any[][];
Toil: any[][];
@saleebm
saleebm / App.java
Created October 17, 2019 14:49
Not static example
/*
3. Program stores student response in a double precision floating-point variable
4. Program uses floating-point comparison to determine if the students answer is correct
9. Program shall display the number of correct and incorrect responses at the end of a session
@saleebm
saleebm / react-datetime-custom-appearance-demo.markdown
Created February 27, 2019 18:43
react-datetime custom appearance demo