Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export of type definitions, depending on whether it is ES Module or CommonJS #250

Merged
merged 2 commits into from Nov 14, 2023

Conversation

yutak23
Copy link
Contributor

@yutak23 yutak23 commented Nov 14, 2023

This ought to fix #159.

The intent of this change is detailed in the issue.
#159 (comment)

The following tests were performed to verify that axios-retry is available for all patterns.

  • JS
    • CommonJS PJ
    • ES Module PJ
  • TS
    • CommonJS PJ
    • ES Module PJ

environment

  • Node.js v18.17.0
  • TypeScript ^5.2.2
  • tsconfig.json is below
{
	"extends": "@tsconfig/node16/tsconfig.json", // @tsconfig/node16: 16.1.1
	"compilerOptions": {
		"strict": true,
		"allowUnusedLabels": false,
		"allowUnreachableCode": false,
		"exactOptionalPropertyTypes": true,
		"noFallthroughCasesInSwitch": true,
		"noImplicitOverride": true,
		"noImplicitReturns": true,
		"noPropertyAccessFromIndexSignature": true,
		"noUncheckedIndexedAccess": true,
		"noUnusedLocals": true,
		"noUnusedParameters": true,
		"checkJs": true,
		"esModuleInterop": true,
		"skipLibCheck": true,
		"forceConsistentCasingInFileNames": true,
		"declaration": true
	},
	"include": ["srv/**/*.ts"],
	"exclude": ["node_modules", "build"]
}

"require": {
"types": "./lib/cjs/index.d.ts",
"default": "./index.js"
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using Conditional exports, the type definition of the imported axios will be switched within the type definition in each case of CommonJS and ES Module. This eliminates compilation errors.

Incidentally, I don't think there will be any impact on the existing use of JavaScript. Because Conditional exports was already in use.

@@ -1,6 +1,6 @@
import * as axios from 'axios';

export = IAxiosRetry;
export default IAxiosRetry;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when use =, below error occur. So, i change to default.

error TS1192: Module '"/home/study/workspace/ts-node-oidc/node_modules/axios-retry/lib/esm/index"' has no default export.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the reason for why an import like:

import { IAxiosRetryConfig } from 'axios-retry'

no longer works.

Copy link

@rchl rchl Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yutak23 how/when did you get that error? Did it work with version of axios-retry prior to your change?

I'm thinking that we could just revert to the old code if your error was not introduced by these changes.

Copy link
Contributor Author

@yutak23 yutak23 Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rchl

When compiling to CommonJS, export = is fine, but when compiling to ES Module, export = will result in the following error Therefore, we changed it to default export.
#250 (comment)

However, the modification to export IAxiosRetryConfig etc. for name import was omitted. Sorry about that. I have uploaded the fix in the #252.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did export = really create an issue and in what circumstances? I haven't seen issues after changing it in this project and building.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen issues after changing it in this project and building.

I would be very grateful if you could provide me with details of the configuration information for your TypeScript project.
The problem I am talking about happens when compiling to ES Module with ES Module configuration (set type: module in package.json)

When export =, the following compile error occurs. When export default is used, no error occurs.
image
image

My environment is below.

// package.json
	"type": "module",
	"dependencies": {
		"axios": "^1.6.2",
		"axios-retry": "^3.9.0",
	},
	"devDependencies": {
		"@tsconfig/node16": "^16.1.1",
		"@tsconfig/strictest": "^2.0.2",
		"typescript": "~5.2.2",
	}
// tsconfig.json
{
	"extends": ["@tsconfig/node16/tsconfig.json", "@tsconfig/strictest/tsconfig.json"],
	"compilerOptions": {
		"outDir": "dist",
		"declaration": true,
		"sourceMap": true
	},
	"include": ["srv/**/*.ts"],
	"exclude": ["node_modules", "dist"]
}

Copy link

@rchl rchl Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question is: did this work before (before v3.9.0)?

Because if not then there is not really a need to change that now. Could do that in next major version if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is already becoming Revert's policy, but I think it would be rude not to answer your question, so I want to answer.

The type definition used to be export default and it worked except for the ES Module. . However, this had a problem with issue and has been modified. But then another problem arose and more changes were made.

export default axiosRetry;

@mindhells
Copy link
Member

Looks great, I'm publishing this as a minor

@mindhells mindhells merged commit 8681aca into softonic:master Nov 14, 2023
8 checks passed
@mindhells
Copy link
Member

mindhells commented Nov 14, 2023

Thanks a lot! published as 3.9.0

@rchl
Copy link

rchl commented Nov 14, 2023

I haven't had time to look into those changes yet but it appears to break the import I had

import { IAxiosRetryConfig } from 'axios-retry'

@mindhells
Copy link
Member

I haven't had time to look into those changes yet but it appears to break the import I had

import { IAxiosRetryConfig } from 'axios-retry'

@rchl can you please open an Issue with all the details?
If you already know where the problem is and needs a fix in this lib, please don't hesitate to create a PR.

@yutak23 yutak23 deleted the feature/condetion-export-for-type branch November 15, 2023 00:41
@rchl rchl mentioned this pull request Nov 15, 2023
mindhells added a commit that referenced this pull request Nov 16, 2023
…-for-type"

This reverts commit 8681aca, reversing
changes made to c3de348.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Argument of type 'AxiosInstance' is not assignable to parameter of type 'AxiosStatic | AxiosInstance'
3 participants