Skip to content

Instantly share code, notes, and snippets.

View ojhaujjwal's full-sized avatar

Ujjwal Ojha ojhaujjwal

View GitHub Profile
// Companion code for "The Error Is a Value" (part 2 of the exceptions series).
// `npm run build` typechecks this file (prebuild runs `tsc --noEmit`), so the
// code printed in the post cannot drift from code that compiles.
//
// Trade-off vs the repo's coding standards, kept deliberately: this file
// mirrors the published post 1:1 for series continuity. Boolean `ok`
// discriminator (the exact type part 1 printed), plain tagged-object errors
// rather than `Error` subclasses, the `never` exhaustiveness idiom rather than
// `casesHandled`, and a hand-rolled `pipe` with `any` in its variadic runtime.
// No JSDoc: the post is the documentation.
type Address = {
street: string;
city: string;
country: string;
};
export type Order = {
address: Address;
};
@ojhaujjwal
ojhaujjwal / effect-v4-layer-cheatsheet.md
Created June 15, 2026 01:45
Effect V4 Layer CheatSheet

Layer Cheat Sheet — Effect v4

Type-safe dependency injection.

Service Tags

Function-style:

const Foo = Context.Service<{
  readonly bar: Effect.Effect<number>
@ojhaujjwal
ojhaujjwal / 1-await-try-catch.ts
Last active August 4, 2025 05:45
intro-to-effect
try {
const doc = await s3.getObject();
} catch (err) {
// error is any or unknown
if (err instanceof ObjectNotFoundError) {
// TODO: handle error
}
}
@ojhaujjwal
ojhaujjwal / reduce-map.test.ts
Created September 8, 2020 09:37
Javascript Reduce Map
import { reduceMap } from './reduce-map';
describe('reduceMap', () => {
const sumValueAndSubtractKey = (result: number, value: number, key: number) => result + value - key;
it('should return initial value for empty map', () => {
expect(
reduceMap(
new Map(),
sumValueAndSubtractKey,
import { Controller, ControllerOptions } from '@nestjs/common';
import { join } from 'path';
import { AuthenticationGuard, AccessControlGuard } from 'app/guards';
expose function AdminController(options: ControllerOptions) {
return function (target: Function) {
Controller(join('/admin', options.path))(target);
AuthenticationGuard()(target);
AccessControlGuard({ role: 'admin' })(target);
};
@AdminController(options) {
@Controller({ path: '/admin' + options.path })
@AuthenticationGuard()
@AccessControlGuard({ role: 'admin' })
}
import { Controller, Get } from '@nestjs/common';
import { AdminController, AccessControlGuard } from 'app/decorators';
@AdminController({ path: '/dashboard' })
export class AdminDashboardController {
@Get()
index(): string {
return 'dashboard data for admin';
}
}