@faceteer/cdk
Version:
CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.
79 lines (78 loc) • 3.37 kB
TypeScript
import type { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2, Handler } from 'aws-lambda';
import { IFailedResponse, ISuccessResponse } from '../response';
import { HandlerDefinition, HandlerTypes } from './handler';
export type ApiPathParameters<T extends ReadonlyArray<string>> = Record<T[number], string>;
export interface ApiHandlerDefinition<B = never, Q = never, R = never, P extends ReadonlyArray<string> = ReadonlyArray<string>> extends HandlerDefinition {
/** The name of the function */
name: string;
/** HTTP method for which this function is invoked. */
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
/**
* Uri path for which this function is invoked. Must start with `/.`
*/
route: string;
/**
* Whether or not to disable authentication
* for a route
*/
disableAuth?: boolean;
/**
* Optional overrides of the scopes for a route
*/
scopes?: string[];
/**
* Parameters within the route. If specified, the route must
* contain `{parameter-name}`, ex: /users/{userId}
*/
pathParameters?: P;
/**
* The amount of time that Lambda allows a function to run before stopping it.
* The default is 3 seconds. The maximum allowed value is 30 seconds.
*
* Consider using a `QueueHandler` for a timeout larger than 30 seconds.
*/
timeout?: number;
validators?: {
body?: (body: unknown) => B;
query?: (query: unknown) => Q;
response?: (response: unknown) => R;
};
/**
* Overrides for the generated cloudformation resources. If you don't know what this is, check out [this doc](https://docs.aws.amazon.com/cdk/v2/guide/identifiers.html)
*/
cfnOverrides?: {
logicalIds?: {
function?: string;
route?: string;
integration?: string;
};
};
}
export type ApiHandlerAuthorizer<B, Q, P extends ReadonlyArray<string>, A, R> = (event: ParsedApiEvent<B, Q, P>, handlerOptions: ApiHandlerOptions<B, Q, A, P, R>) => A | false;
export interface ApiHandlerOptions<B, Q, A, P extends ReadonlyArray<string>, R> extends ApiHandlerDefinition<B, Q, R> {
authorizer?: ApiHandlerAuthorizer<B, Q, P, A, R>;
pathParameters?: P;
}
export type ValidatedApiEvent<B, Q, A, P extends ReadonlyArray<string>> = APIGatewayProxyEventV2 & {
input: {
body: B;
query: Q;
path: ApiPathParameters<P>;
auth: A;
};
};
export type ParsedApiEvent<B, Q, P extends ReadonlyArray<string>, A = never> = Omit<ValidatedApiEvent<B, Q, A, P>, 'input'> & {
input: Omit<ValidatedApiEvent<B, Q, A, P>['input'], 'auth'>;
};
export type ApiHandlerFunction<B, Q, A, R, P extends ReadonlyArray<string>> = Handler<ValidatedApiEvent<B, Q, A, P>, ISuccessResponse<R> | IFailedResponse>;
export type ApiHandlerWithDefinition<B = never, Q = never, R = never> = APIGatewayProxyHandlerV2 & {
type: HandlerTypes.API;
definition: ApiHandlerDefinition<B, Q, R>;
};
/**
* Creates a handler that will be attached to the service api
* @param options
* @param handler
* @returns
*/
export declare function ApiHandler<B = unknown, Q = unknown, A = unknown, P extends ReadonlyArray<string> = never, R = unknown>(options: ApiHandlerOptions<B, Q, A, P, R>, handler: ApiHandlerFunction<B, Q, A, R, P>): ApiHandlerWithDefinition<B, Q, R>;