Middy AutoProxyResponse Middleware

raw JSON →
0.1.0 verified Thu Apr 23 auth: no javascript abandoned

The `middy-autoproxyresponse` middleware was designed to simplify AWS Lambda proxy responses by automatically wrapping plain JavaScript objects returned from a handler into a `statusCode: 200` response with `Content-Type: application/json`. If a `statusCode` is already present in the returned object, it passes through untouched. The package is currently at version 0.1.0, released in 2018. Given the rapid evolution and numerous breaking changes in the core `middy` framework (which is now in its 7.x series, released frequently), this middleware is highly likely incompatible with modern `middy` versions (v2.x and above), especially since `middy` v5+ is ESM-only. It provides a basic utility that is often reimplemented or achieved with other, more actively maintained `middy` middlewares like `@middy/http-response-serializer` for newer versions.

error SyntaxError: Cannot use import statement outside a module
cause Attempting to `import` a CommonJS module in an ESM context, or vice-versa, which is common when mixing older middleware with newer `middy` versions or Node.js runtimes.
fix
This specific middleware is CommonJS. If you are using a recent version of middy (v5.x+) which is ESM-only, middy-autoproxyresponse is fundamentally incompatible. Downgrade middy to a compatible version (v0.x or v1.x) or replace this middleware with an ESM-compatible alternative.
error TypeError: handler.use is not a function
cause Attempting to use `middy-autoproxyresponse` with an incompatible `middy` core version, where the `middy()` wrapper might have changed its API or is not correctly imported.
fix
Ensure that the middy core package version is compatible with this middleware (likely middy@^0.x or middy@^1.x). Given this middleware's age, it's safer to use an actively maintained middleware for current middy versions.
breaking This middleware (v0.1.0) was developed for an older version of the `middy` framework (likely v0.x or v1.x). It is highly unlikely to be compatible with `middy` v2.x, v3.x, v4.x, or newer due to significant internal breaking changes in `middy`'s core API and middleware lifecycle.
fix Consider using the actively maintained `@middy/http-response-serializer` (or similar core middy middlewares) for modern Middy versions, or implementing the response transformation manually.
breaking The `middy` framework, starting from v5.0.0, is ESM-only (ECMAScript Modules). This `middy-autoproxyresponse` package (v0.1.0) is a CommonJS module. Using a CommonJS middleware with an ESM-only core framework will result in module resolution errors unless specific build configurations are applied (which is not officially supported or recommended).
fix Avoid using this middleware with `middy` v5.0.0 or later. Migrate to `middy`'s native HTTP response handling or use a modern, ESM-compatible alternative.
deprecated The functionality provided by `middy-autoproxyresponse` (converting plain objects to API Gateway proxy responses) is now commonly handled by official `middy` core middlewares like `@middy/http-response-serializer` which offer more robust and configurable options.
fix For new projects or migrations, prefer `@middy/http-response-serializer` or similar from the official `@middy` ecosystem.
npm install middy-autoproxyresponse
yarn add middy-autoproxyresponse
pnpm add middy-autoproxyresponse

Demonstrates defining a simple Lambda handler that returns a plain object, which `autoProxyResponse` then converts into a standard API Gateway proxy response.

import middy from '@middy/core';
import { autoProxyResponse } from 'middy-autoproxyresponse';

interface MyEvent {
  name?: string;
}

interface MyContext extends Record<string, any> {}

async function handler(event: MyEvent, context: MyContext) {
  // Simulate some async operation
  await new Promise(resolve => setTimeout(resolve, 100));

  if (event.name) {
    return { message: `Hello, ${event.name}!` };
  } else {
    // This will be converted to a 200 OK JSON response by autoProxyResponse
    return { greeting: 'Hello, world!' };
  }
}

export const wrappedHandler = middy(handler)
  .use(autoProxyResponse());

// To simulate execution (e.g., for local testing)
// wrappedHandler({ name: 'Registry' }, {} as MyContext)
//   .then(res => console.log('Response:', JSON.stringify(res, null, 2)));