Skip to content

Commit 3566aa2

Browse files
authored
style(event-handler): apply stricter linting (#4578)
1 parent 0124e1f commit 3566aa2

21 files changed

+216
-261
lines changed

packages/event-handler/src/appsync-events/Router.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
22
import { isRecord } from '@aws-lambda-powertools/commons/typeutils';
3-
import { getStringFromEnv, isDevMode } from '@aws-lambda-powertools/commons/utils/env';
3+
import {
4+
getStringFromEnv,
5+
isDevMode,
6+
} from '@aws-lambda-powertools/commons/utils/env';
47
import type {
58
OnPublishHandler,
69
OnSubscribeHandler,
@@ -31,7 +34,7 @@ class Router {
3134
* Whether the router is running in development mode.
3235
*/
3336
protected readonly isDev: boolean = false;
34-
37+
3538
public constructor(options?: RouterOptions) {
3639
const alcLogLevel = getStringFromEnv({
3740
key: 'AWS_LAMBDA_LOG_LEVEL',

packages/event-handler/src/appsync-graphql/AppSyncGraphQLResolver.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,7 @@ class AppSyncGraphQLResolver extends Router {
152152
* @param context - The AWS Lambda context object.
153153
* @param options - Optional parameters for the resolver, such as the scope of the handler.
154154
*/
155-
public async resolve(
156-
event: unknown,
157-
context: Context,
158-
options?: ResolveOptions
159-
): Promise<unknown> {
155+
public resolve(event: unknown, context: Context, options?: ResolveOptions) {
160156
if (Array.isArray(event)) {
161157
if (event.some((e) => !isAppSyncGraphQLEvent(e))) {
162158
this.logger.warn(
@@ -194,10 +190,10 @@ class AppSyncGraphQLResolver extends Router {
194190
* @param options - Optional resolve options for customizing resolver behavior.
195191
*/
196192
async #withErrorHandling(
197-
fn: () => Promise<unknown>,
193+
fn: () => unknown,
198194
event: AppSyncResolverEvent<Record<string, unknown>>,
199195
options?: ResolveOptions
200-
): Promise<unknown> {
196+
) {
201197
try {
202198
return await fn();
203199
} catch (error) {
@@ -377,11 +373,11 @@ class AppSyncGraphQLResolver extends Router {
377373
* @param options - Optional parameters for the resolver, such as the scope of the handler.
378374
* @throws {ResolverNotFoundException} If no resolver is registered for the given field and type.
379375
*/
380-
async #executeSingleResolver(
376+
#executeSingleResolver(
381377
event: AppSyncResolverEvent<Record<string, unknown>>,
382378
context: Context,
383379
options?: ResolveOptions
384-
): Promise<unknown> {
380+
): unknown {
385381
const { fieldName, parentTypeName: typeName } = event.info;
386382

387383
const resolverHandlerOptions = this.resolverRegistry.resolve(

packages/event-handler/src/appsync-graphql/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { AppSyncGraphQLResolver } from './AppSyncGraphQLResolver.js';
22
export {
3-
ResolverNotFoundException,
43
InvalidBatchResponseException,
4+
ResolverNotFoundException,
55
} from './errors.js';
66
export {
77
awsDate,

packages/event-handler/src/bedrock-agent/BedrockAgentFunctionResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class BedrockAgentFunctionResolver {
146146
public tool<TParams extends Record<string, ParameterValue>>(
147147
fn: ToolFunction<TParams>,
148148
config: Configuration
149-
): undefined {
149+
) {
150150
const { name } = config;
151151
if (this.#tools.has(name)) {
152152
this.#logger.warn(

packages/event-handler/src/rest/converters.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export const proxyEventToWebRequest = (
3636
const { domainName } = event.requestContext;
3737

3838
const headers = new Headers();
39-
for (const [name, value] of Object.entries(event.headers ?? {})) {
40-
if (value != null) headers.set(name, value);
39+
for (const [name, value] of Object.entries(event.headers)) {
40+
if (value !== undefined) headers.set(name, value);
4141
}
4242

43-
for (const [name, values] of Object.entries(event.multiValueHeaders ?? {})) {
43+
for (const [name, values] of Object.entries(event.multiValueHeaders)) {
4444
for (const value of values ?? []) {
4545
const headerValue = headers.get(name);
4646
if (!headerValue?.includes(value)) {

packages/event-handler/src/rest/middleware/cors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
* }));
3636
* ```
3737
*
38+
* @param options - Configuration options for CORS
3839
* @param options.origin - The origin to allow requests from
3940
* @param options.allowMethods - The HTTP methods to allow
4041
* @param options.allowHeaders - The headers to allow

packages/event-handler/src/rest/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const isAPIGatewayProxyResult = (
150150
* ```
151151
*/
152152
export const composeMiddleware = (middleware: Middleware[]): Middleware => {
153-
return async ({ reqCtx, next }): Promise<HandlerResponse | void> => {
153+
return async ({ reqCtx, next }) => {
154154
let index = -1;
155155
let result: HandlerResponse | undefined;
156156

packages/event-handler/src/types/bedrock-agent.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ type ToolFunction<TParams = Record<string, ParameterValue>> = (
5151
event: BedrockAgentFunctionEvent;
5252
context: Context;
5353
}
54-
) => Promise<JSONValue | BedrockFunctionResponse>;
54+
) =>
55+
| Promise<JSONValue | BedrockFunctionResponse>
56+
| JSONValue
57+
| BedrockFunctionResponse;
5558

5659
/**
5760
* Tool in the Bedrock Agent Function Resolver.
@@ -159,7 +162,7 @@ type ResolverOptions = {
159162
*
160163
* When no logger is provided, we'll only log warnings and errors using the global `console` object.
161164
*/
162-
logger?: GenericLogger;
165+
logger?: Pick<GenericLogger, 'debug' | 'warn' | 'error'>;
163166
};
164167

165168
export type {

packages/event-handler/src/types/rest.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ErrorHandler<T extends Error = Error> = (
2424
) => Promise<HandlerResponse>;
2525

2626
interface ErrorConstructor<T extends Error = Error> {
27+
// biome-ignore lint/suspicious/noExplicitAny: this is a generic type that is intentionally open
2728
new (...args: any[]): T;
2829
prototype: T;
2930
}
@@ -57,7 +58,7 @@ type HandlerResponse = Response | JSONObject;
5758

5859
type RouteHandler<TReturn = HandlerResponse> = (
5960
reqCtx: RequestContext
60-
) => Promise<TReturn>;
61+
) => Promise<TReturn> | TReturn;
6162

6263
type HttpMethod = keyof typeof HttpVerbs;
6364

@@ -78,12 +79,14 @@ type RestRouteOptions = {
7879
middleware?: Middleware[];
7980
};
8081

82+
// biome-ignore lint/suspicious/noConfusingVoidType: To ensure next function is awaited
8183
type NextFunction = () => Promise<HandlerResponse | void>;
8284

8385
type Middleware = (args: {
8486
reqCtx: RequestContext;
8587
next: NextFunction;
86-
}) => Promise<void | HandlerResponse>;
88+
// biome-ignore lint/suspicious/noConfusingVoidType: To ensure next function is awaited
89+
}) => Promise<HandlerResponse | void>;
8790

8891
type RouteRegistryOptions = {
8992
/**
@@ -176,4 +179,5 @@ export type {
176179
RouteRegistryOptions,
177180
ValidationResult,
178181
CompressionOptions,
182+
NextFunction,
179183
};

packages/event-handler/tests/unit/appsync-events/AppSyncEventsResolver.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('Class: AppSyncEventsResolver', () => {
8787
public scope = 'scoped';
8888

8989
@app.onPublish('/foo', { aggregate })
90-
public async handleFoo(payloads: OnPublishAggregatePayload) {
90+
public handleFoo(payloads: OnPublishAggregatePayload) {
9191
return payloads.map((payload) => {
9292
return {
9393
id: payload.id,
@@ -97,15 +97,15 @@ describe('Class: AppSyncEventsResolver', () => {
9797
}
9898

9999
@app.onPublish('/bar')
100-
public async handleBar(payload: string) {
100+
public handleBar(payload: string) {
101101
return `${this.scope} ${payload}`;
102102
}
103103

104-
public async handler(event: unknown, context: Context) {
104+
public handler(event: unknown, context: Context) {
105105
return this.stuff(event, context);
106106
}
107107

108-
async stuff(event: unknown, context: Context) {
108+
stuff(event: unknown, context: Context) {
109109
return app.resolve(event, context, { scope: this });
110110
}
111111
}
@@ -146,15 +146,15 @@ describe('Class: AppSyncEventsResolver', () => {
146146
public scope = 'scoped';
147147

148148
@app.onSubscribe('/foo')
149-
public async handleFoo(payload: AppSyncEventsSubscribeEvent) {
149+
public handleFoo(payload: AppSyncEventsSubscribeEvent) {
150150
console.debug(`${this.scope} ${payload.info.channel.path}`);
151151
}
152152

153-
public async handler(event: unknown, context: Context) {
153+
public handler(event: unknown, context: Context) {
154154
return this.stuff(event, context);
155155
}
156156

157-
async stuff(event: unknown, context: Context) {
157+
stuff(event: unknown, context: Context) {
158158
return app.resolve(event, context, { scope: this });
159159
}
160160
}
@@ -222,7 +222,7 @@ describe('Class: AppSyncEventsResolver', () => {
222222
async ({ error, message }) => {
223223
// Prepare
224224
const app = new AppSyncEventsResolver({ logger: console });
225-
app.onSubscribe('/foo', async () => {
225+
app.onSubscribe('/foo', () => {
226226
throw error;
227227
});
228228

@@ -242,7 +242,7 @@ describe('Class: AppSyncEventsResolver', () => {
242242
it('throws an UnauthorizedException when thrown by the handler', async () => {
243243
// Prepare
244244
const app = new AppSyncEventsResolver({ logger: console });
245-
app.onSubscribe('/foo', async () => {
245+
app.onSubscribe('/foo', () => {
246246
throw new UnauthorizedException('nah');
247247
});
248248

@@ -258,7 +258,7 @@ describe('Class: AppSyncEventsResolver', () => {
258258
it('returns the response of the onPublish handler', async () => {
259259
// Prepare
260260
const app = new AppSyncEventsResolver({ logger: console });
261-
app.onPublish('/foo', async (payload) => {
261+
app.onPublish('/foo', (payload) => {
262262
if (payload === 'foo') {
263263
return true;
264264
}
@@ -303,7 +303,7 @@ describe('Class: AppSyncEventsResolver', () => {
303303
const app = new AppSyncEventsResolver({ logger: console });
304304
app.onPublish(
305305
'/foo',
306-
async (payloads) => {
306+
(payloads) => {
307307
return payloads.map((payload) => ({
308308
id: payload.id,
309309
payload: true,
@@ -353,7 +353,7 @@ describe('Class: AppSyncEventsResolver', () => {
353353
const app = new AppSyncEventsResolver({ logger: console });
354354
app.onPublish(
355355
'/foo',
356-
async () => {
356+
() => {
357357
throw new Error('Error in handler');
358358
},
359359
{ aggregate: true }
@@ -379,7 +379,7 @@ describe('Class: AppSyncEventsResolver', () => {
379379
const app = new AppSyncEventsResolver({ logger: console });
380380
app.onPublish(
381381
'/foo',
382-
async () => {
382+
() => {
383383
throw new UnauthorizedException('nah');
384384
},
385385
{ aggregate: true }
@@ -400,7 +400,7 @@ describe('Class: AppSyncEventsResolver', () => {
400400
const app = new AppSyncEventsResolver();
401401
app.onPublish(
402402
'/foo',
403-
async () => {
403+
() => {
404404
throw new Error('Error in handler');
405405
},
406406
{ aggregate: true }

0 commit comments

Comments
 (0)