Skip to content

feat(event-handler): add base router class #3972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions packages/event-handler/src/rest/BaseRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { isRecord } from '@aws-lambda-powertools/commons/typeutils';
import {
getStringFromEnv,
isDevMode,
} from '@aws-lambda-powertools/commons/utils/env';
import type { GenericLogger } from '../types/appsync-events.js';
import type {
RouteHandler,
RouteOptions,
RouterOptions,
} from '../types/rest.js';

abstract class BaseRouter {
protected context: Record<string, unknown>; // TODO: should this be a map instead?
/**
* A logger instance to be used for logging debug, warning, and error messages.
*
* When no logger is provided, we'll only log warnings and errors using the global `console` object.
*/
protected readonly logger: Pick<GenericLogger, 'debug' | 'warn' | 'error'>;
/**
* Whether the router is running in development mode.
*/
protected readonly isDev: boolean = false;

public constructor(options?: RouterOptions) {
this.context = {};
const alcLogLevel = getStringFromEnv({
key: 'AWS_LAMBDA_LOG_LEVEL',
defaultValue: '',
});
this.logger = options?.logger ?? {
debug: alcLogLevel === 'DEBUG' ? console.debug : () => undefined,
error: console.error,
warn: console.warn,
};
this.isDev = isDevMode();
}

public abstract route(handler: RouteHandler, options: RouteOptions): void;

public get(path: string, handler: RouteHandler, options?: RouteOptions): void;
public get(path: string, options?: RouteOptions): MethodDecorator;
public get(
path: string,
handler?: RouteHandler | RouteOptions,
options?: RouteOptions
): MethodDecorator | undefined {
if (handler && typeof handler === 'function') {
this.route(handler, {
...(options || {}),
method: 'GET',
path,
});
return;
}

return (_target, _propertyKey, descriptor: PropertyDescriptor) => {
const routeOptions = isRecord(handler) ? handler : options;
this.route(descriptor.value, {
...(routeOptions || {}),
method: 'GET',
path,
});
return descriptor;
};
}
}

export { BaseRouter };
24 changes: 24 additions & 0 deletions packages/event-handler/src/types/rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { BaseRouter } from '../rest/BaseRouter.js';
import type { GenericLogger } from './appsync-events.js';

/**
* Options for the {@link BaseRouter} class
*/
type RouterOptions = {
/**
* A logger instance to be used for logging debug, warning, and error messages.
*
* When no logger is provided, we'll only log warnings and errors using the global `console` object.
*/
logger?: GenericLogger;
};

// biome-ignore lint/suspicious/noExplicitAny: we want to keep arguments and return types as any to accept any type of function
type RouteHandler<T = any, R = any> = (...args: T[]) => R;

type RouteOptions = {
method?: string;
path?: string;
};

export type { RouterOptions, RouteHandler, RouteOptions };
Loading