Skip to content

Instantly share code, notes, and snippets.

View khalidx's full-sized avatar
💭
Engineering for the ☁️

Khalid Zoabi khalidx

💭
Engineering for the ☁️
View GitHub Profile
@khalidx
khalidx / typescript-decorator.md
Created June 7, 2021 01:57
Quickly create a TypeScript Decorator.

An example of a TypeScript Method Decorator, also known as an "annotation" in other languages.

You can use a decorator like @Decorator on a method inside of a class.

export function Decorator <Args extends any[], Result extends any> (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: Args) => Result>) {
  const originalMethod = descriptor.value // save a reference to the original method
  // NOTE: Do not use arrow syntax here. Use a function expression in 
  // order to use the correct value of `this` in this method (see notes below)
  if (originalMethod) {
@khalidx
khalidx / http.md
Created May 20, 2021 18:30
A list of HTTP methods and statuses, written as TypeScript types.

http

method

export type Method =
  | 'get'
  | 'options'
  | 'post'
 | 'put'
@khalidx
khalidx / a-simple-live-reload.md
Last active May 30, 2021 08:09
A simple live-reload implementation that refreshes the page when the source file changes on disk.

A simple live-reload implementation that refreshes the page when the source file changes on disk.

The server.js file just serves the files in the directory that it is in, including itself. It also ensures that a private Cache-Control header is set for GET requests, and caching disabled for other HTTP methods.

The index.html file contains a hand-rolled live-reload script that polls the server every 2 seconds and honors the HTTP 304 response code and the caching and etag directives specified by

@khalidx
khalidx / typed-string-splitter.md
Last active May 30, 2021 08:05
A typed string splitter for TypeScript that takes 'some cool string' => ['some', 'cool', 'string']

typed-string-splitter

What the built-in string 'some cool string'.split(' ') does:

--> converts 'some cool string' to ['some', 'cool', 'string'] with a resulting type of string[]

What you can do with TypedSplit('some cool string', ' ') from this gist:

--> converts 'some cool string' to ['some', 'cool', 'string'] with a resulting type of ['some', 'cool', 'string']

@khalidx
khalidx / message.js
Last active August 20, 2023 22:27
Quickly send an SMS message using the Twilio API.
// make sure you `npm install twilio` before you run
const config = {
// get your credentials from the twilio console: www.twilio.com/console
// can also be configured as environment variables instead
accountSid: '<twilio account sid>',
authToken: '<twilio auth token>',
// on a twilio trial account, you can only send from and to confirmed or purchased numbers
// upgrade your account to send to any number
from: '+10000000000',
@khalidx
khalidx / unzip-gz-file.md
Created November 12, 2020 04:57
For unzipping gz files (like for CloudFront logs stored in S3).

unzipping a .gz file

On Linux and MacOS, a .gz file can be decompressed using the gzip command.

For example:

gzip -d log.gz
@khalidx
khalidx / script-directory-name.sh
Created October 18, 2020 04:06
For cd'ing into the same directory as the script file, instead of running at cwd by default.
# https://stackoverflow.com/questions/3349105/how-to-set-current-working-directory-to-the-directory-of-the-script-in-bash
cd "${0%/*}"
@khalidx
khalidx / api.ts
Last active May 19, 2021 11:52
A quick api boilerplate file for express with hot-reloading and typescript support
#!/usr/bin/env node
/* The API routes and middlewares */
const server = () => import('express').then(({ default: express }) => {
const app = express()
app.get('/hello', (req, res) => {
res.json({ message: 'Hey there!' })
})
return app
})
@khalidx
khalidx / banner.md
Created June 25, 2020 16:34
Shows a banner on the CLI using a cool font

banner

Shows a banner on the CLI using a cool font.

For NodeJs & TypeScript. Depends on the amazing figlet and chalk libraries.

import figlet from 'figlet'
import chalk from 'chalk'
@khalidx
khalidx / typescript-config.md
Created June 18, 2020 03:08
Add a TypeScript config to your project easily

typescript config

Make sure to install the following dependencies for the best experience.

npm install --save tslib
npm install --save-dev typescript
npm install --save-dev ts-node