Skip to content

Instantly share code, notes, and snippets.

@o-az
o-az / discord-bot-send-message.sh
Created October 21, 2023 06:37
Sends a message to a Discord channel via a Discord bot using cURL cli command
# replace CHANNEL_ID and BOT_SECRET with your values
curl --include \
--request POST "https://discord.com/api/channels/CHANNEL_ID/messages" \
--header "Content-Type: application/json" \
--header "Authorization: Bot BOT_SECRET" \
--data '{ "content": "lorem ipsum" }'
@o-az
o-az / suppress-warnings.cjs
Last active October 6, 2023 04:17
Suppress all Node.js warnings (including deprecation warnings)
const { emit: originalEmit } = process;
/**
* @param {string} event
* @param {{ name: string; }} error
*/
function suppresser(event, error) {
return event === "warning" && error.name === "ExperimentalWarning"
? false
: originalEmit.apply(process, arguments);
@o-az
o-az / curl-performance.sh
Created September 2, 2023 07:35
I have this in my ~/.zshrc and can run 'perf <url>' to get request performance
function perf {:
curl --silent \
--url "$1" \
--output /dev/null \
--write-out "\n%{time_total} sec\n%{size_download} bytes\n"
}
@o-az
o-az / sleep.ts
Last active June 23, 2023 23:58
Actual JavaScript 'sleep' function
/**
* @param ms milliseconds to sleep
* @returns void
*/
export function sleep(ms: number): void {
/**
* In some environments such as Cloudflare Workers, Atomics is not defined
* setTimeout is used as a fallback
*/
if (typeof Atomics === 'undefined') {
@o-az
o-az / wrangler.json
Last active November 27, 2023 13:28
Cloudflare Workers wrangler JSON schema (might not be complete)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the project"
},
"main": {
"type": "string",
@o-az
o-az / pretty.ts
Created May 23, 2023 11:33
Type helper to force TypeScript to simplify the type
/**
* Wrap any complex and hard to read type with this
* and it'll magically be a lot simpler on hover.
*/
export type Pretty<T> = { [K in keyof T]: T[K] } & {};
@o-az
o-az / promises.js
Last active May 2, 2023 23:25
Promise.all vs Promise.allSettled vs Promise.any vs Promise.race
const promise1 = Promise.resolve(1);
const promise2 = Promise.reject(2);
/**
* Promise.all() example
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
*/
Promise.all([promise1, promise2])
.then((value) => console.log({ value }))
.catch((error) => console.log({ error }));
@o-az
o-az / swagger-ui.html
Last active March 25, 2024 16:46
barebone index.html to use for swagger ui. Assumes you have a swagger.json file ready
<!doctype html>
<html lang='en'>
<head>
<meta charset='UTF-8' />
<title>_TITLE_</title>
<link rel='stylesheet' type='text/css' href='https://unpkg.com/swagger-ui-dist/swagger-ui.css' />
<style>
*,
*:before,
@o-az
o-az / Node.js-https-imports.md
Last active April 23, 2023 00:27
https imports in Node.js (aka 'import from url')
@o-az
o-az / graphql-introspect-via-curl.md
Last active July 28, 2023 05:38
Introspect GraphQL Schema via curl command
curl --silent --location \
  --request POST \
  --url 'https://spacex-production.up.railway.app' \
  --header 'Content-Type: application/json' \
  --data '{"query":"query IntrospectionQuery { __schema { queryType { name } mutationType { name } subscriptionType { name } types { ...FullType } directives { name description locations args { ...InputValue } } } } fragment FullType on __Type { kind name description fields(includeDeprecated: true) { name description args { ...InputValue } type { ...TypeRef } isDeprecated deprecationReason } inputFields { ...InputValue } interfaces { ...TypeRef } enumValues(includeDeprecated: true) { name description isDeprecated deprecationReason } possibleTypes { ...TypeRef } } fragment InputValue on __InputValue { name description type { ...TypeRef } defaultValue } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name } } } } } } } }","variable":{}}'
  • T