Skip to content

Instantly share code, notes, and snippets.

View miguelrk's full-sized avatar

miguelrk

View GitHub Profile
@miguelrk
miguelrk / proxy-console.ts
Created April 2, 2024 15:26
Proxy the globalThis.console to silence selected messages by substring
/**
* Silence globalThis.console for selected messages by substrings
*
* @param substringsToSkip {string[]} - substrings to skip
* @returns {Proxy} - a proxied console object
*/
export const proxyConsole = (...substringsToSkip: string[]) => {
return new Proxy(console, {
get(target, prop, _receiver) {
const method = target[prop as keyof typeof console]; // intercept method calls
@miguelrk
miguelrk / ResponseError.ts
Created March 18, 2024 18:27
A custom Error class for HTTP Response errors with a toResponse() method for conversion to HTTP Responses.
const errors: Record<string, { status: number; message: string }> = {
BadRequest: { status: 400, message: "Bad Request" },
Unauthorized: { status: 401, message: "Unauthorized" },
PaymentRequired: { status: 402, message: "Payment Required" },
Forbidden: { status: 403, message: "Forbidden" },
NotFound: { status: 404, message: "Not Found" },
MethodNotAllowed: { status: 405, message: "Method Not Allowed" },
InternalServerError: { status: 500, message: "Internal Server Error" },
NotImplemented: { status: 501, message: "Not Implemented" },
BadGateway: { status: 502, message: "Bad Gateway" },
@miguelrk
miguelrk / fetch-form.tsx
Last active April 10, 2024 07:04
An isomorphic form component that overrides the default submit (GET/POST) with an HTTP Request using fetch to allow for GET/POST/PUT/PATCH/DELETE
// adapted from https://stackoverflow.com/a/74202858
import type { ComponentChildren, JSX } from "preact";
export type FormFetchProps = JSX.IntrinsicElements["form"] & {
id: string;
method: "get" | "post" | "put" | "patch" | "delete";
action: string;
children?: ComponentChildren;
};
@miguelrk
miguelrk / chart.tsx
Created March 7, 2024 15:01
Isomorphic Plot component for rendering charts on the server and hydrating them on the client for client-side interactivity.
export default () => {
const data = [...];
return (
<Plot.Figure
options={{
x: { tickFormat: Plot.formatMonth(), ticks: 12 },
marks: [
Plot.barY(data, { x: "month", y: "amount", tip: true }),
Plot.ruleY([0]),
],
@miguelrk
miguelrk / main.ts
Last active November 22, 2023 06:03
A Deno Fresh plugin for Preact Signals `bind:value` inspired by https://gist.github.com/marvinhagemeister/daada86f82466dff81d48840c8d5f27b
import { setup } from "./shared.ts";
export default function hydrate() {
setup();
}
@miguelrk
miguelrk / kv-proxy.js
Last active September 29, 2022 16:56 — forked from stelf/proxyftw.js
using ES6 proxies and async/await to dynamically, yet almost transparently connect to some remote data provider
const providerHandler = {
get: async(target, name) => {
console.log('load someting from remote...')
return new Promise( (res, rej) => {
setTimeout(() => res(42), 4200)
})
},
set: function (obj, prop, value) {
return new Promise((res, rej) => {
console.log('save someting remotely...')
@miguelrk
miguelrk / github-proxy-client.js
Created March 11, 2022 15:53 — forked from DavidWells/github-proxy-client.js
Full Github REST api in 34 lines of code
/* Ultra lightweight Github REST Client */
const token = 'github-token-here'
const githubClient = generateAPI('https://api.github.com', {
headers: {
'User-Agent': 'xyz',
'Authorization': `bearer ${token}`
}
})
async function getRepo() {
@miguelrk
miguelrk / javascript-proxy-as-rest-client.js
Created March 11, 2022 15:52 — forked from DavidWells/javascript-proxy-as-rest-client.js
Using a javascript proxy as low code REST client
/* Using a JavaScript proxy for a super low code REST client */
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3
// also see https://github.com/fastify/manifetch
// also see https://github.com/flash-oss/allserver
// and https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb
const createApi = (url) => {
return new Proxy({}, {
get(target, key) {