Skip to content

Commit 3019064

Browse files
committed
.
1 parent 3654a2f commit 3019064

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

index.d.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,25 @@ export declare type ErrorHandlingMiddleware<
9292
>,
9393
TBody = any
9494
> = (
95+
/**
96+
* The error that was thrown or passed to res.error()
97+
*/
9598
error: Error,
99+
/**
100+
* The request object
101+
*/
96102
req: Request<TContext, TQuery, TParams, TBody>,
103+
/**
104+
* The response object. Call res.send() or return a value to send a response.
105+
* The error will continue through the error middleware chain until a response is sent.
106+
*/
97107
res: Response<TResponse>,
108+
/**
109+
* Call next() to continue to the next error middleware.
110+
* Note: next(error) is not supported - the error parameter will be ignored.
111+
*/
98112
next: NextFunction
99-
) => void;
113+
) => void | Promise<void | TResponse> | TResponse;
100114

101115
export type METHODS =
102116
| 'GET'
@@ -142,23 +156,32 @@ export declare interface LoggerOptions {
142156
customKey?: string;
143157
errorLogging?: boolean;
144158
detail?: boolean;
145-
level?: string;
159+
level?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none';
146160
levels?: {
147-
[key: string]: string;
161+
[key: string]: number;
148162
};
149163
messageKey?: string;
150164
nested?: boolean;
151165
timestamp?: boolean | TimestampFunction;
152-
sampling?: {
153-
target?: number;
154-
rate?: number;
155-
period?: number;
156-
rules?: SamplingOptions[];
157-
};
166+
sampling?:
167+
| boolean
168+
| {
169+
target?: number;
170+
rate?: number;
171+
period?: number;
172+
rules?: SamplingOptions[];
173+
};
158174
serializers?: {
159-
[name: string]: (prop: any) => any;
175+
main?: (req: Request) => object;
176+
req?: (req: Request) => object;
177+
res?: (res: Response) => object;
178+
context?: (context: Context) => object;
179+
custom?: (custom: any) => object;
160180
};
161181
stack?: boolean;
182+
timer?: boolean;
183+
multiValue?: boolean;
184+
log?: (message: string) => void;
162185
}
163186

164187
export interface Options {
@@ -214,7 +237,7 @@ export declare class Request<
214237
};
215238
body: TBody;
216239
rawBody: string;
217-
route: '';
240+
route: string;
218241
requestContext: TContext;
219242
isBase64Encoded: boolean;
220243
pathParameters: { [name: string]: string } | null;
@@ -235,6 +258,12 @@ export declare class Request<
235258
clientType: 'desktop' | 'mobile' | 'tv' | 'tablet' | 'unknown';
236259
clientCountry: string;
237260
namespace: App;
261+
/**
262+
* Alias for namespace
263+
*/
264+
ns: App;
265+
interface: 'apigateway' | 'alb';
266+
payloadVersion?: string;
238267

239268
log: {
240269
trace: LoggerFunction;
@@ -249,6 +278,8 @@ export declare class Request<
249278
}
250279

251280
export declare class Response<TResponse = any> {
281+
app: API;
282+
252283
status(code: number): this;
253284
sendStatus(code: number): void;
254285
header(key: string, value?: string | Array<string>, append?: boolean): this;

index.test-d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
isApiGatewayEvent,
2525
isApiGatewayV2Event,
2626
isAlbEvent,
27+
App,
28+
SerializerFunction,
2729
} from './index';
2830
import {
2931
APIGatewayProxyEvent,
@@ -456,10 +458,42 @@ const testRequestProperties: HandlerFunction<any, RequestContext> = (
456458
expectType<string>(req.clientCountry);
457459
expectType<boolean>(req.coldStart);
458460
expectType<number>(req.requestCount);
461+
expectType<'apigateway' | 'alb'>(req.interface);
462+
expectType<string | undefined>(req.payloadVersion);
463+
expectType<App>(req.ns);
459464
req.log.trace('trace message');
460465
req.log.debug('debug message');
461466
req.log.info('info message');
462467
req.log.warn('warn message');
463468
req.log.error('error message');
464469
req.log.fatal('fatal message');
465470
};
471+
472+
const testErrorHandlingMiddleware: ErrorHandlingMiddleware = async (
473+
error,
474+
req,
475+
res,
476+
next
477+
) => {
478+
// Test that we can return different types
479+
if (error.message === 'sync') {
480+
return { message: 'handled synchronously' };
481+
}
482+
483+
if (error.message === 'async') {
484+
return Promise.resolve({ message: 'handled asynchronously' });
485+
}
486+
487+
if (error.message === 'void') {
488+
res.json({ message: 'handled with void' });
489+
return;
490+
}
491+
492+
if (error.message === 'promise-void') {
493+
await Promise.resolve();
494+
res.json({ message: 'handled with promise void' });
495+
return;
496+
}
497+
498+
next();
499+
};

0 commit comments

Comments
 (0)