Skip to content

TypeScript regression in 2.0.3 #713

Closed
@ryanelian

Description

@ryanelian

Checks

Describe the bug (be clear and concise)

import { createProxyMiddleware, Options } from 'http-proxy-middleware';
import { AppSettings } from '../../../functions/AppSettings';

// Great way to avoid using CORS and to transparently add Access Token to API requests
// Proxy requests from /api/demo/... to http://localhost:5000/...
const apiProxyOptions: Options = {
    target: AppSettings.current.backendHost,
    changeOrigin: true,
    // Set ws to true if proxying WebSocket
    // Recommendation: create a dedicated API gateway for WebSocket, don't mix!
    // If troublesome, just hit the WebSocket endpoint directly (CORS)
    ws: false,
    xfwd: true,
    pathRewrite: {
        '^/api/demo': '/'
    },
    logLevel: 'warn'
};

const apiProxy = createProxyMiddleware(apiProxyOptions);

const apiGateway = async (req, res) => {
    apiProxy(req, res, (result: unknown) => {
        if (result instanceof Error) {
            throw result;
        }

        throw new Error(`Failed to proxy request: '${req.url}'`);
    });
}

export default apiGateway;

export const config = {
    api: {
        externalResolver: true,
    },
}

This is the entire code.

In version 2.0.2, that code can be does not emit compile error in TypeScript.

But when upgrading to version 2.0.3, an error appeared:

[2:24:03 PM] Starting compilation in watch mode...

pages/api/demo/[...apiGateway].ts:23:5 - error TS2349: This expression is not callable.
  Type 'RequestHandler' has no call signatures.

23     apiProxy(req, res, (result: unknown) => {
       ~~~~~~~~

[2:24:05 PM] Found 1 error. Watching for file changes.

Step-by-step reproduction instructions

Place the above code in a TypeScript project.

If require a reproduction repository, run these commands:

git clone https://github.com/accelist/nextjs-starter
npm install [email protected] -E
npm update
npm run typescript

Expected behavior (be clear and concise)

No TypeScript type error in version 2..0.3

How is http-proxy-middleware used in your project?

PS D:\VS\accelist-nextjs-starter> npm ls http-proxy-middleware
[email protected] D:\VS\accelist-nextjs-starter
`-- [email protected]

What http-proxy-middleware configuration are you using?

const apiProxyOptions: Options = {
    target: AppSettings.current.backendHost,
    changeOrigin: true,
    // Set ws to true if proxying WebSocket
    // Recommendation: create a dedicated API gateway for WebSocket, don't mix!
    // If troublesome, just hit the WebSocket endpoint directly (CORS)
    ws: false,
    xfwd: true,
    pathRewrite: {
        '^/api/demo': '/'
    },
    logLevel: 'warn'
};

What OS/version and node/version are you seeing the problem?

System:
    OS: Windows 10 10.0.19044
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 22.21 GB / 31.85 GB
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Managers:
    pip3: 21.1.3 - ~\AppData\Local\Programs\Python\Python39\Scripts\pip3.EXE
  Utilities:
    Git: 2.34.0.
  Virtualization:
    Docker: 20.10.12 - C:\Program Files\Docker\Docker\resources\bin\docker.EXE
  SDKs:
    Windows SDK:
      AllowDevelopmentWithoutDevLicense: Enabled
      AllowAllTrustedApps: Enabled
  IDEs:
    Android Studio: Version  4.2.0.0 AI-202.7660.26.42.7351085
    VSCode: 1.64.2 - C:\Users\Ryan\AppData\Local\Programs\Microsoft VS Code\bin\code.CMD
    Visual Studio: 17.1.32210.238 (Visual Studio Community 2022)
  Languages:
    Bash: 5.0.17 - C:\Windows\system32\bash.EXE
    Java: 11.0.8
    Python: 3.9.6
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (98.0.1108.55)
    Internet Explorer: 11.0.19041.1566

Additional context (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions