Skip to content

Instantly share code, notes, and snippets.

View tomfa's full-sized avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / file.utils.ts
Last active July 20, 2023 21:21
TypeScript: fs.readDir recursive implementation
/*
* Walks a directory (fs.readdir but recursively)
* Context:
* - fs does not have support for walk/readdir recursively
* - fs-extra moved "walk" functionality to https://github.com/jprichardson/node-klaw
*/
export const listDirectory = async (
currentPath: string,
includeDirectories = false,
@tomfa
tomfa / generate-story.js
Created March 3, 2023 21:56
Script to generate basic storybook stories for
/**
* Usage node generate-story.js [path]
*
* Example (generate stories for all components in folder "components":
* node generate-story.js ./src/components
*/
const fs = require('fs');
const SUPPORTED_FILE_EXT = ['tsx', 'jsx'];
const EXCLUDED_FILES = [];
@tomfa
tomfa / csvDownload.ts
Created February 14, 2023 07:46
React hook for downloading csv files (typescript)
import { useCallback } from 'react';
export function useCsvDownload(
data: Record<string, any>[],
title = 'data.csv',
) {
return useCallback(() => {
const headerKeys = data
.map((d) => Object.keys(d))
.reduce((a, b) => {
@tomfa
tomfa / custom_js_in_footer.js
Last active February 8, 2023 08:08
Forwarding UTM data in Webflow, when using Weglot as translation tool
@tomfa
tomfa / index.html
Created February 6, 2023 20:47
A link, but a form that'll forward utm values
<html>
<form action="https://google.com/search" method="get">
<input type="hidden" name="utm_source" />
<input type="hidden" name="utm_medium" />
<input type="hidden" name="utm_campaign" />
<input type="hidden" name="utm_term" />
<input type="hidden" name="utm_content" />
<input type="submit" value="I pretend to be link" />
</form>
<script>
@tomfa
tomfa / client.ts
Last active April 7, 2023 22:15
graphql-request with automatic parsing of DateTime
import { GraphQLClient } from 'graphql-request';
import { gqlSerializer } from './serializer.ts'
const gqlClient = new GraphQLClient(url, {
credentials: 'include',
mode: 'cors',
jsonSerializer: gqlSerializer,
});
@tomfa
tomfa / main.rhai
Last active December 5, 2022 18:37
Demo desired ability in Apollo Router Rhai plugin
fn process_response(response) {
response.headers["set-cookie"] = "foo=bar; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None";
// This overwrites the first header set above
response.headers["set-cookie"] = "foo2=bar2; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None";
// This does not work
response.headers["set-cookie"] = [
"foo=bar; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None",
"foo2=bar2; Domain=localhost; Path=/; Expires=Wed, 04 Jan 2023 17:25:27 GMT; HttpOnly; Secure; SameSite=None",
];
@tomfa
tomfa / forward_set_cookie.rhai
Last active December 5, 2022 18:13
Example of Apollo Router multiple set-cookie header issue
@tomfa
tomfa / Records.csv
Created October 13, 2022 22:58
Parse T-SQL binary field with Python and save as file
id data
1 0x2550
@tomfa
tomfa / redis.ts
Created August 23, 2022 18:00
Connecting to AWS Elasticache (with ioredis + Bull)
import Redis from 'ioredis';
const requiresTLS = process.env.NODE_ENV === 'production';
const client = new Redis({
host: process.env.REDIS_HOST as string,
password: process.env.REDIS_PASSWORD as string,
port: parseInt(process.env.REDIS_PORT, 10),
// {} prop enables TLS (typical for production, not while developing locally)