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

Typescript definition is broken (at least for cjs) #21

Closed
orgads opened this issue Dec 7, 2022 · 16 comments
Closed

Typescript definition is broken (at least for cjs) #21

orgads opened this issue Dec 7, 2022 · 16 comments
Labels
bug Something isn't working

Comments

@orgads
Copy link

orgads commented Dec 7, 2022

import axios from 'axios';
import oauthClient from 'axios-oauth-client';
const axiosInstance = axios.create();
const func = oauthClient(axiosInstance, { url: '' });
const func = (0, axios_oauth_client_1.default)(axiosInstance, { url: '' });
                                              ^
TypeError: (0 , axios_oauth_client_1.default) is not a function
    at Object.<anonymous> (F:\Projects\jstest\dist\tstest.js:6:47)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47
orgads added a commit to orgads/axios-oauth-client that referenced this issue Dec 7, 2022
@compwright
Copy link
Owner

@orgads this is not a documented use case, you shouldn't rely on calling the default function directly. Instead, call one of the methods demonstrated in the README. There are methods for client credentials, authorization code, owner credentials, and refresh token grants. If there's another you need, I'm happy to add it or better yet submit a PR.

@orgads
Copy link
Author

orgads commented Dec 7, 2022

It's the same:

import axios from 'axios'
import oauth from 'axios-oauth-client'
const getAuthorizationCode = oauth.authorizationCode(
  axios.create(),
  'https://oauth.com/2.0/token', // OAuth 2.0 token endpoint
  'CLIENT_ID',
  'CLIENT_SECRET',
  'https://your-app.com/oauth-redirect' // Redirect URL for your app
)

getAuthorizationCode('AUTHORIZATION_CODE');
const getAuthorizationCode = axios_oauth_client_1.default.authorizationCode(axios_1.default.create(), 'https://oauth.com/2.0/token', // OAuth 2.0 token endpoint
                                                          ^

TypeError: Cannot read properties of undefined (reading 'authorizationCode')

@compwright compwright reopened this Dec 7, 2022
@compwright compwright added the bug Something isn't working label Dec 7, 2022
@orgads
Copy link
Author

orgads commented Dec 8, 2022

Same with 2.0.1. Please reopen.

@compwright
Copy link
Owner

compwright commented Dec 8, 2022

@orgads can you provide a complete code example that I can run that reproduces your issue? Preferably, a repository that demonstrates the problem. If so I'll be glad to reopen this and fix whatever the problem is. I need more than the code snippet above to be able to see what's going on.

@orgads
Copy link
Author

orgads commented Dec 8, 2022

I already did. Try this:

import axios from 'axios'
import oauth from 'axios-oauth-client'
const getAuthorizationCode = oauth.authorizationCode(
  axios.create(),
  'https://oauth.com/2.0/token', // OAuth 2.0 token endpoint
  'CLIENT_ID',
  'CLIENT_SECRET',
  'https://your-app.com/oauth-redirect' // Redirect URL for your app
)

getAuthorizationCode();

@orgads
Copy link
Author

orgads commented Dec 8, 2022

Or do you also need package.json and tsconfig.json?

@compwright
Copy link
Owner

@orgads Yes, I need those too.

@orgads
Copy link
Author

orgads commented Dec 8, 2022

package.json:

{
  "name": "jstest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "keywords": [],
  "author": "",
  "type": "commonjs",
  "license": "ISC",
  "dependencies": {
    "@tsconfig/node18": "^1.0.1",
    "@types/node": "^18.11.11",
    "axios": "^1.2.1",
    "axios-oauth-client": "^2.0.1",
    "typescript": "^4.9.4"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "skipLibCheck": true
  }
}

@orgads
Copy link
Author

orgads commented Dec 8, 2022

Notice that if esModuleInterop is set then it works, but unfortunately I can't use it in my project.

@compwright compwright reopened this Dec 8, 2022
@compwright
Copy link
Owner

Got it thanks. What command are you running that reproduces the problem?

@orgads
Copy link
Author

orgads commented Dec 8, 2022

npx tsc
node test.js

@compwright
Copy link
Owner

@orgads thanks a lot! Try it now in v2.0.2

@compwright
Copy link
Owner

@orgads also you will need to change "module": "Node16", in your tsconfig.json.

@orgads
Copy link
Author

orgads commented Dec 8, 2022

This also breaks my project. Is there a way to create definitions that are compatible with plain cjs?

@compwright
Copy link
Owner

compwright commented Dec 8, 2022

Could you elaborate on why you are unable to use "module": "Node16"? It seems a contradiction between "target": "ES2022" and "module": "CommonJS".

@compwright
Copy link
Owner

compwright commented Dec 8, 2022

This seems to be at the root of the problem: https://www.typescriptlang.org/docs/handbook/2/modules.html#commonjs-and-es-modules-interop

Unless someone can show me a build setting that makes this work, I'm going to just say that if you're using typescript you either need to set "module": "Node16" or enable esModuleInterop. That's not my problem as a library publisher/maintainer, as best as I can determine; it seems to be a Typescript problem.

Again, if you or anyone else can show me how to adjust my Typescript build settings to make this work with your settings, I'm open to making further changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants