Skip to content

Commit ca1aadf

Browse files
committed
.
1 parent 7668fe8 commit ca1aadf

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

index.d.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,66 @@ export declare function isAlbRequest<
653653
>(
654654
req: Request<any, TQuery, TParams, TBody>
655655
): req is Request<ALBContext, TQuery, TParams, TBody>;
656+
657+
/**
658+
* Source-agnostic request type that works with any AWS Lambda trigger
659+
*/
660+
export type SourceAgnosticRequest<
661+
TQuery extends Record<string, string | undefined> = Record<
662+
string,
663+
string | undefined
664+
>,
665+
TParams extends Record<string, string | undefined> = Record<
666+
string,
667+
string | undefined
668+
>,
669+
TBody = any
670+
> = Request<RequestContext, TQuery, TParams, TBody>;
671+
672+
/**
673+
* Source-agnostic middleware type that works with any AWS Lambda trigger
674+
*/
675+
export type SourceAgnosticMiddleware<
676+
TResponse = any,
677+
TQuery extends Record<string, string | undefined> = Record<
678+
string,
679+
string | undefined
680+
>,
681+
TParams extends Record<string, string | undefined> = Record<
682+
string,
683+
string | undefined
684+
>,
685+
TBody = any
686+
> = Middleware<TResponse, RequestContext, TQuery, TParams, TBody>;
687+
688+
/**
689+
* Source-agnostic handler function type that works with any AWS Lambda trigger
690+
*/
691+
export type SourceAgnosticHandler<
692+
TResponse = any,
693+
TQuery extends Record<string, string | undefined> = Record<
694+
string,
695+
string | undefined
696+
>,
697+
TParams extends Record<string, string | undefined> = Record<
698+
string,
699+
string | undefined
700+
>,
701+
TBody = any
702+
> = HandlerFunction<TResponse, RequestContext, TQuery, TParams, TBody>;
703+
704+
/**
705+
* Source-agnostic error handling middleware type that works with any AWS Lambda trigger
706+
*/
707+
export type SourceAgnosticErrorHandler<
708+
TResponse = any,
709+
TQuery extends Record<string, string | undefined> = Record<
710+
string,
711+
string | undefined
712+
>,
713+
TParams extends Record<string, string | undefined> = Record<
714+
string,
715+
string | undefined
716+
>,
717+
TBody = any
718+
> = ErrorHandlingMiddleware<TResponse, RequestContext, TQuery, TParams, TBody>;

index.test-d.ts

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import {
2222
isApiGatewayV2Event,
2323
isAlbEvent,
2424
App,
25+
SourceAgnosticMiddleware,
26+
SourceAgnosticHandler,
27+
SourceAgnosticErrorHandler,
2528
} from './index';
2629
import {
2730
APIGatewayProxyEvent,
@@ -99,11 +102,7 @@ const testEventTypeGuards = () => {
99102
}
100103
};
101104

102-
const sourceAgnosticMiddleware: Middleware<any, RequestContext> = (
103-
req,
104-
res,
105-
next
106-
) => {
105+
const sourceAgnosticMiddleware: SourceAgnosticMiddleware = (req, res, next) => {
107106
if (isApiGatewayContext(req.requestContext)) {
108107
expectType<string>(req.requestContext.requestId);
109108
const sourceIp = req.requestContext.identity.sourceIp;
@@ -190,7 +189,7 @@ const testRequestTypeGuards = () => {
190189
}
191190
};
192191

193-
const sourceAgnosticHandler: HandlerFunction<UserResponse, RequestContext> = (
192+
const sourceAgnosticHandler: SourceAgnosticHandler<UserResponse> = (
194193
req,
195194
res
196195
) => {
@@ -614,3 +613,53 @@ const testDefaultTypes = () => {
614613
);
615614
expectType<Promise<any>>(apiGwV2Result);
616615
};
616+
617+
const testSourceAgnosticTypes = () => {
618+
// Test source-agnostic handler with minimal type parameters
619+
const simpleSourceAgnosticHandler: SourceAgnosticHandler = (req, res) => {
620+
expectType<RequestContext>(req.requestContext);
621+
res.json({ message: 'ok' });
622+
};
623+
624+
// Test source-agnostic handler with response type
625+
const typedSourceAgnosticHandler: SourceAgnosticHandler<UserResponse> = (
626+
req,
627+
res
628+
) => {
629+
res.json({
630+
id: '1',
631+
name: 'John',
632+
633+
});
634+
};
635+
636+
// Test source-agnostic middleware with minimal type parameters
637+
const simpleSourceAgnosticMiddleware: SourceAgnosticMiddleware = (
638+
req,
639+
res,
640+
next
641+
) => {
642+
expectType<RequestContext>(req.requestContext);
643+
next();
644+
};
645+
646+
// Test source-agnostic error handler
647+
const sourceAgnosticErrorHandler: SourceAgnosticErrorHandler = (
648+
error,
649+
req,
650+
res,
651+
next
652+
) => {
653+
expectType<RequestContext>(req.requestContext);
654+
res.status(500).json({ error: error.message });
655+
};
656+
657+
// Test using source-agnostic types with API
658+
api.get('/source-agnostic', simpleSourceAgnosticHandler);
659+
api.post(
660+
'/source-agnostic',
661+
simpleSourceAgnosticMiddleware,
662+
typedSourceAgnosticHandler
663+
);
664+
api.use(sourceAgnosticErrorHandler);
665+
};

0 commit comments

Comments
 (0)