Skip to content

Instantly share code, notes, and snippets.

View tomfa's full-sized avatar

Tomas Fagerbekk tomfa

View GitHub Profile
@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)
@tomfa
tomfa / code.js
Created January 12, 2022 13:00
Connecting to multiple databases with Prisma
/*
* Source: https://github.com/prisma/prisma/issues/2443#issuecomment-630679118
* Client typings is generated with the cli commands below:
*
* prisma generate --schema prisma/schema1.prisma
* prisma generate --schema prisma/schema2.prisma
*/
import { PrismaClient as PrismaClient1 } from '../prisma/client1'
import { PrismaClient as PrismaClient2 } from '../prisma/client2'
@tomfa
tomfa / ImageIcon.stories.tsx
Created January 5, 2022 19:29
Example storybook
import React from 'react';
import { Meta } from '@storybook/react';
import Component from './ImageIcon';
export default {
title: 'components/ImageIcon/ImageIcon',
component: Component,
} as Meta;
const Template = (props: React.ComponentProps<typeof Component>) => <Component {...props} />;
@tomfa
tomfa / cache.ts
Last active December 29, 2021 11:43
Caching in Node with Redis and/or in-memory storage + Browser with localStorage
import { ICache } from './types';
import { MemoryClient } from './memoryClient';
import { RedisClient } from './redisClient';
export class CacheClient implements ICache {
private client: ICache;
constructor({ redisUrl }: { redisUrl?: string }) {
if (redisUrl) {
this.client = new RedisClient({ redisUrl });
@tomfa
tomfa / create-signed-urls.ts
Last active December 20, 2021 22:33
Upload to S3 from Typescript
// Demo of creating upload urls to AWS S3.
// See github.com/tomfa/nextjs-s3-upload for a complete app
import {
S3Client,
PutObjectCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client({
@tomfa
tomfa / types.ts
Last active January 30, 2023 21:05
LogStructure.ts
type ApplicationLog = {
level: 'debug' | 'info' | 'warning' | 'danger' | 'critical',
message: string,
createdAt: number,
userId?: string,
// a business metric, e.g. SIGNUP
action?: string,
monetaryValue?: number,
requestId?: string,
@tomfa
tomfa / bigquery_import.py
Last active October 21, 2021 20:13
Demo on how to upload to BigQuery with python3. See https://notes.webutvikling.org/starting-bigquery/
# 1. install dependencies
# 2. Set service account json in code
# or with env var "SA_ACCOUNT"
# 3. Run this file to import test data:
# "python3 bigquery_import.py"
import os
import json
from google.cloud import bigquery