Skip to content

Instantly share code, notes, and snippets.

@swport
swport / file.md
Last active March 14, 2025 17:27
file
const { S3Client, CreateBucketCommand, ListObjectsV2Command, CopyObjectCommand, DeleteObjectCommand, PutBucketPolicyCommand } = require('@aws-sdk/client-s3');
const { CloudFrontClient, CreateDistributionCommand, GetDistributionCommand, UpdateDistributionCommand, CreateCloudFrontOriginAccessIdentityCommand } = require('@aws-sdk/client-cloudfront');
const { Upload } = require('@aws-sdk/lib-storage');
const fs = require('fs');
const path = require('path');

// Load environment variables
const bucketName = process.env.S3_BUCKET_NAME;
const region = process.env.AWS_REGION;
@swport
swport / react-app-to-s3-v3.md
Last active March 13, 2025 19:01
React app to s3

Updated Deployment Script for AWS SDK v3

This script uses the modular AWS SDK v3 to:

  1. Create a timestamped backup directory.
  2. Move existing files to the backup directory.
  3. Upload new files to the root of the S3 bucket.
  4. Enable static website hosting and log the publicly accessible URL.

@swport
swport / discount_offers.csv
Last active March 11, 2025 10:56
Discount offers fake testing data (Generated using GenAI)
ID MerchantName DiscountDescription Offer
1 Lenovo Back-to-school special 5% off on laptops
2 HP Exclusive holiday deal 10% off on printers
3 HomeChef Weekly subscription discount Flat 20 USD off on first order
4 Disney Magical savings event 15% off on theme park tickets
5 Apple New Year bonanza Free AirPods with MacBook purchase
6 Dell Work from home essentials Flat 50 USD off on monitors
7 Sony Ultimate gaming sale 10% off on PlayStation accessories
8 Amazon Prime Day exclusive 20% off on electronics
9 Walmart Weekend mega sale Flat 30 USD off on purchases above 200 USD
@swport
swport / react-app-to-s3.md
Last active March 13, 2025 15:13
Deployment steps to deploy simple react app to s3 bucket on aws

To create a React-TypeScript application, deploy it to an S3 bucket, and make it publicly accessible using the AWS SDK, follow these steps:


1. Set Up a React-TypeScript Application

  1. Create a React-TypeScript app using create-react-app:
    npx create-react-app my-react-ts-app --template typescript
    cd my-react-ts-app
@swport
swport / trpc-mock.tsx
Last active October 13, 2023 06:01
Mock tRPC query and mutation calls using msw (works well with both vitest and jest)
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, type RenderOptions } from "@testing-library/react";
import { createTRPCReact, httpLink } from "@trpc/react-query";
import { createTRPCMsw } from "msw-trpc";
import { type ReactElement } from "react";
import superjson from "superjson";
import { type AppRouter } from "@/server/api/root";
const mockedTRPC = createTRPCReact<AppRouter>({
unstable_overrides: {
@swport
swport / .gitconfig
Last active February 1, 2023 06:03 — forked from digitaljhelms/.gitconfig
My personal ~/.gitconfig file
[user]
name = Sumit Wadhwa
email = [email protected]
[alias]
aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /' | sort # list all aliases
cb = !git branch | grep ^* | cut -d' ' -f2
branch-name = !git cb # alias to "cb" alias
st = status
ci = commit
cie = commit --allow-empty-message -m ''
@swport
swport / useAxiosLoader.ts
Last active February 25, 2023 19:21
Axios react-hook to know how many network requests are currently running
import axios from 'axios';
const addToast = (message: string, type: 'success' | 'error') => {
/// add logic to show toast
};
// hook to know if there are network requests currently running
// and how many are running
export const useAxiosLoader = () => {
const [counter, setCounter] = React.useState(0);
@swport
swport / cart_context_reactjs.js
Last active June 6, 2021 07:53
Cart Context in ReactJS
import React, {
createContext,
useEffect,
useReducer
} from 'react';
const lStorage = require('store'); // store.js library
export const CartContext = createContext();
@swport
swport / states_regions_provinces.php
Created January 21, 2021 13:57
States / Regions / Provinces List PHP Array
<?php
return array (
0 =>
array (
'id' => 3901,
'name' => 'Badakhshan',
'country_id' => 1,
'state_code' => 'BDS',
),
1 =>
@swport
swport / countries_list.php
Created January 21, 2021 13:53
Countries List PHP Array
<?php
return array (
0 =>
array (
'id' => 1,
'name' => 'Afghanistan',
'iso3' => 'AFG',
'iso2' => 'AF',
'phone_code' => '93',
'capital' => 'Kabul',