Skip to content

Instantly share code, notes, and snippets.

View joshtwist's full-sized avatar

Josh Twist joshtwist

View GitHub Profile
openapi: 3.0.0
info:
title: Chill Vibes CBD Drinks API
description: >
Welcome to the Chill Vibes CBD Drinks API!
We're here to quench your thirst and soothe your soul,
one API call at a time. Remember, our drinks are high
in CBD, but our server responses are always 200 OK!
version: 1.0.0
contact:
{
"openapi": "3.0.0",
"info": {
"title": "Slice of Heaven Pizza API",
"description": "Welcome to the Slice of Heaven Pizza API! We're serving up hot, fresh endpoints faster than you can say 'extra cheese'. Whether you're looking to order a pizza, track your delivery, or browse our menu, we've got you covered. So sit back, relax, and let our API do the heavy lifting (unlike our delivery drivers, who are strictly forbidden from lifting heavy objects).",
"version": "1.0.0",
"contact": {
"name": "Pizza Support",
"email": "[email protected]",
==== Custom Roles-Based Access Control (RBAC) ==== rbac.ts
// Check user is in correct role, or return 403
if (request.user.data.roles.includes(options.role)) {
return request;
}
else {
return new Response('Access denied', { status: 403 });
}
@joshtwist
joshtwist / fire-requests.js
Created April 21, 2024 19:23
Fire async requests and summarize results in table (node)
import fetch from "node-fetch";
import ProgressBar from "progress";
// Function to handle the individual fetch operation
async function sendRequest(url, index) {
try {
// console.log(`Sending request ${index + 1} to ${url}`);
const startTime = process.hrtime(); // Start timing here
const response = await fetch(url);
const duration = process.hrtime(startTime);
@joshtwist
joshtwist / extract-query-to-context.ts
Created March 5, 2024 12:34
Extract Query String (search params) to context.custom in Zuplo
import {ZuploContext, ZuploRequest} from "@zuplo/runtime";
export default async function policy(
request: ZuploRequest,
context: ZuploContext,
options: never,
policyName: string
) {
context.custom.originalQuery = request.query;
import {
ZuploContext,
ZuploRequest,
OpenIdJwtInboundPolicy,
InboundPolicyHandler,
OpenIdJwtInboundPolicyOptions,
Auth0JwtInboundPolicy,
Auth0JwtInboundPolicyOptions,
} from "@zuplo/runtime";
@joshtwist
joshtwist / graphql-logging.ts
Last active April 29, 2022 02:43
GraphQL Logging Policy
import { ZuploContext, ZuploRequest } from "@zuplo/runtime";
import environment from "@app/environment";
import { parse, visit } from "graphql";
import { BQWriter } from "./bg-writer";
interface LoggingEntry {
url: string;
method: string;
@joshtwist
joshtwist / BQWriter.ts
Created April 22, 2022 03:44
Writing to BigQuery from Zuplo
import { getTokenFromGCPServiceAccount } from "@sagi.io/workers-jwt";
import { Logger, ZuploContext } from "@zuplo/runtime";
const aud = "https://bigquery.googleapis.com/";
export class BQWriter<T> {
constructor(serviceAccountJson: string, projectId: string, datasetId: string, tableId: string) {
this.#serviceAccountJson = JSON.parse(serviceAccountJson);
this.#insertUrl = `${aud}bigquery/v2/projects/${projectId}/datasets/${datasetId}/tables/${tableId}/insertAll`;
export default async () => {
const response = await fetch('https://your-api.com');
const transformed = await transformBody(response);
return transformed;
}
@joshtwist
joshtwist / Fusion.ps1
Last active July 30, 2018 16:29
PowerShell to move and organize files from GoPro fusion
## GoPro Fusion Script to move files into grouped folders
# Where do you want your files moved to?
$targetFolder = "D:\GoPro Fusion\"
# For easiest workflow, just set the friendly/file system label for your front and back SD cards to "FusionSD"
$volumes = Get-Volume | Where FileSystemLabel -eq "fusionsd"
#### ==== support functions