Skip to content

Maintenance: Remove undefined From REST API Resolve Method Type Signature #4462

@svozza

Description

@svozza

Summary

The way the resolve method works now that we will always return a response:

public async resolve(
    event: unknown,
    context: Context,
    options?: ResolveOptions
  ): Promise<APIGatewayProxyResult | undefined> {
    // ...

    const requestContext: RequestContext = {
      event,
      context,
      request,
      // this response should be overwritten by the handler, if it isn't
      // it means somthing went wrong with the middleware chain
      res: new Response('', { status: 500 }),
    };

    try {
      // ...
      const middlewareResult = await middleware(
        route.params,
        requestContext,
        () => Promise.resolve()
      );

      // middleware result takes precedence to allow short-circuiting
      const result = middlewareResult ?? requestContext.res;

      return handlerResultToProxyResult(result);
    } catch (error) {
      this.logger.debug(`There was an error processing the request: ${error}`);
      const result = await this.handleError(error as Error, {
        ...requestContext,
        scope: options?.scope,
      });
      return await webResponseToProxyResult(result);
    }
  } 

requestContext.res is always initialised at the beginning of the function and we will fallback to that response assuming our middleware doesn't short-circuit.

Why is this needed?

Currently when customers use app.resolve they have to include unnecessary null checks:

export const handler = async (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> => {
  const result = app.resolve(event, context);
  console.log(result?.body);
  return result ?? someDefault;
};

Which area does this relate to?

Event Handler

Solution

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedinternalPRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)

Type

No type

Projects

Status

Shipped

Relationships

None yet

Development

No branches or pull requests

Issue actions