softonic / axios-retry

Axios plugin that intercepts failed requests and retries them whenever possible
Other
1.9k stars 167 forks source link

fix: ESM default import `This expression is not callable` #230

Closed yutak23 closed 1 year ago

yutak23 commented 1 year ago

This ought to fix https://github.com/softonic/axios-retry/issues/228

ESM seems incompatible in CommonJS PJs with default export (see https://github.com/microsoft/TypeScript/issues/52086). I think there are two solutions to make TypeScript types compatible with both CommonJS and ESM.

  1. Create a new type definition for ESM reference: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing
  2. Change type definitions to support both CommonJS and ESM

Case 1, I would make a copy of the current index.d.ts, rename the file to index.d.mts, and change package.json as follows.

  "exports": {
    ".": {
      "import": {
        "types": "./index.d.mts",
        "default": "./lib/esm/index.js"
      },
      "require": {
        "types": "./index.d.ts",
        "default": "./index.js"
      }
    },
    "./package.json": "./package.json"
  }

However, I think this is redundant and low-maintainability because we have to manage two type definitions, so I tried to modify it in the second way(Case 2).

Explanation of this modification

For this change, I refer to the following

mindhells commented 1 year ago

@yutak23 1st of all: thanks a lot for your effort and patience

I'm not completely sure about the approach. Although it seems to solve the typings problem, IMO it increases the cost of maintenance. At this point, wouldn't it make sense to port the project to TS?, so we also get rid of having to synchronise the TS definitions, WDYT?

I'd like to get more feedback on this from other maintainers

yutak23 commented 1 year ago

@yutak23 1st of all: thanks a lot for your effort and patience

I'm not completely sure about the approach. Although it seems to solve the typings problem, IMO it increases the cost of maintenance. At this point, wouldn't it make sense to port the project to TS?, so we also get rid of having to synchronise the TS definitions, WDYT?

I'd like to get more feedback on this from other maintainers

@mindhells

Thank you for your comment.

I agree that it would require maintenance of both JavaScript and TypeScript types, and in that sense would increase maintenance costs.

I agree with you on the move to TypeScript.

yutak23 commented 1 year ago

I have changed my PR content and raised another PR. This one is closed.

https://github.com/softonic/axios-retry/pull/241