Skip to content

Instantly share code, notes, and snippets.

@timoschinkel
timoschinkel / middleware.ts
Created August 30, 2022 07:40
LambdaMiddleware
import { Context } from 'aws-lambda'; /* npm install @types/aws-lambda */
export interface IMiddleware<E, R> {
process(event: E, context: Context, next: IHandler<E, R>): Promise<R>;
}
export interface IHandler<E, R> {
handle(event: E, context: Context): Promise<R>;
}
@timoschinkel
timoschinkel / APIGatewayHandler.ts
Last active March 17, 2023 12:22
AWS lambda generic middleware enabled handlers
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
export class ApiGatewayCorrelationContextMiddleware implements IMiddleware<APIGatewayProxyEvent, APIGatewayProxyResult> {
public async process(event: APIGatewayProxyEvent, next: Handler<APIGatewayProxyEvent, APIGatewayProxyResult>): Promise<APIGatewayProxyResult> {
return next(event);
}
}
class SomeMiddleware implements IMiddleware<APIGatewayProxyEvent, APIGatewayProxyResult> {
public async process(event: APIGatewayProxyEvent, next: EventHandler<APIGatewayProxyEvent, APIGatewayProxyResult>): Promise<APIGatewayProxyResult> {
@timoschinkel
timoschinkel / README.md
Last active September 22, 2023 07:08
Accolade matching regular expressions

Assume a template syntax where you can use {x}, {{x}}, and {{{x}}}. How can we leverage regular expressions for this?

Example snippet:

{b} c {{d}} e {{{f}}} g

Option 1 - using JavaScript

@timoschinkel
timoschinkel / entry.ts
Created January 11, 2024 15:51
Generalized HTTP handling for TypeScript
/* eslint-disable max-classes-per-file */
/*
* An experiment about how we can structure this codebase
*/
/*
* Request handling infrastructure
*/
@timoschinkel
timoschinkel / Cognito.ts
Last active June 28, 2024 15:36
Validating an email address using different methods
// Using AWS Cognito via TypeScript
import { AdminCreateUserCommand, AdminDeleteUserCommand, CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
const addresses = [
// valid
'[email protected]',
'[email protected]',
'"name..name"@example.com',
'name@localhost',
@timoschinkel
timoschinkel / server.js
Last active March 21, 2025 14:01
Minimal HTTP server using vanilla JavaScript
'use strict';
import { createServer } from 'node:http';
/*
* Usage:
* node server.js
*
* This will pick a random available port on your local machine. You can steer the port and host using environment variables:
* PORT=8080 HOST=localhost node server.js