Skip to content

Instantly share code, notes, and snippets.

View tengla's full-sized avatar

Thomas Eng tengla

  • Oslo, Norway
View GitHub Profile
import { Type } from '@sinclair/typebox';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import { z } from 'zod';
const ajv = new Ajv({ allErrors: true, strict: false });
addFormats(ajv);
// TypeBox/AJV Schemas
const personSchemaTypeBox = Type.Object({
@tengla
tengla / deco.ts
Last active May 8, 2025 14:14
Audit logging w decorators
import "reflect-metadata/lite";
import EventEmitter from "events";
import { container, inject, injectable } from "tsyringe";
const em = new EventEmitter();
em.on("audit", (data) => {
console.log(data);
// I can maybe write to a audit log here (db, stdout, or file?)...
});
@tengla
tengla / main.ts
Created February 26, 2025 09:57
Separation of concerns
// src/index.test.ts
import { describe, expect, it } from "bun:test";
import { AppointmentAccessService } from "./index";
describe('AppointmentAccessService', () => {
it('should allow user to find own appointments', () => {
const service = AppointmentAccessService.withUser({
id: 'jane', roles: ['user']
});
interface User {
id: string;
name: string;
roles: string[];
}
function withRoles(roles: string[]) {
return function (_target: Selector, _propertyName: string, propertyDescriptor: PropertyDescriptor) {
let originalMethod = propertyDescriptor.value!;
@tengla
tengla / sockets.ts
Created October 28, 2024 21:20
ws message observable
import {
Observable, retry, map, throwError,
catchError, of, mergeMap, filter
} from "rxjs";
type Message<T> = {
status: number;
data: null;
} | {
@tengla
tengla / node-typescript-esm.md
Created November 21, 2023 22:16 — forked from khalidx/node-typescript-esm.md
A Node + TypeScript + ts-node + ESM experience that works.

The experience of using Node.JS with TypeScript, ts-node, and ESM is horrible.

There are countless guides of how to integrate them, but none of them seem to work.

Here's what worked for me.

Just add the following files and run npm run dev. You'll be good to go!

package.json

const target = {
warn(...args){
console.log('tick')
console.log(this);
console.log(...args)
}
};
const logger = new Proxy(target, {
get (target, prop) {
@tengla
tengla / keybase.md
Last active November 3, 2020 11:08

Keybase proof

I hereby claim:

  • I am tengla on github.
  • I am fjosgut (https://keybase.io/fjosgut) on keybase.
  • I have a public key ASDVQIvPio9Qr3oW6lx6tIpE8xiqEr_Q7k5t4ojV2h_WNgo

To claim this, I am signing this object:

@tengla
tengla / upload.js
Last active January 23, 2019 12:41
Hapi file upload handler.
const Joi = require('joi');
module.exports = {
method: 'post',
path: '/upload',
config: {
validate: {
payload: {
files: Joi.array().items(
Joi.any()
@tengla
tengla / clock.js
Created November 13, 2018 22:10
An analog clock in React.
import React, { Component } from 'react';
const style = {
container: {
position: 'relative',
height: '420px',
width: '420px',
margin: '0 auto',
border: '1px solid #aaa'
},