LionC / express-basic-auth

Plug & play basic auth middleware for express
325 stars 57 forks source link

Typescript example fails #30

Open Tyguy7 opened 4 years ago

Tyguy7 commented 4 years ago

The provided typescript usage produces a typescript error:

import * as basicAuth from 'express-basic-auth'
app.use(basicAuth(options), (req: basicAuth.IBasicAuthedRequest, res, next) => {
    res.end(`Welcome ${req.auth.user} (your password is ${req.auth.password})`)
    next()
})

Produces this error:

This expression is not callable. Type 'typeof expressBasicAuth' has no call signatures.ts(2349) service.ts(6, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

toverux commented 4 years ago

Huh, that should work. That always worked like this, not only for this library. Can you tell us what's your TypeScript version, configuration (tsconfig.json), and try with a default import (import basicAuth from 'express-basic-auth')?

nhopp commented 4 years ago

Check to see if you have "esModuelInterop":true enabled in your tsconfig.json file. I ran into the same error and disabling this setting resolved the error.

DarkLite1 commented 3 years ago

Same issue but for standard Exrpess:

app.use(
  passport.authenticate('oauth-bearer', { session: false }),
  (next) => {
    next()
  }
)

Throws the TS error: image

Tried the suggestion "esModuelInterop":false but no luck.

Current workaround:

app.use(
  passport.authenticate('oauth-bearer', { session: false }),
  (next: CallableFunction) => {
    next()
  }
)

Another option is to start the unused variables with an underscore:

app.use(
  passport.authenticate('oauth-bearer', { session: false }),
  (_req, _res, next) => {
    next()
  }
)

TypeScript version 3.9.7

ikarasz commented 3 years ago

The provided typescript usage produces a typescript error:

import * as basicAuth from 'express-basic-auth'
app.use(basicAuth(options), (req: basicAuth.IBasicAuthedRequest, res, next) => {
    res.end(`Welcome ${req.auth.user} (your password is ${req.auth.password})`)
    next()
})

Produces this error:

This expression is not callable. Type 'typeof expressBasicAuth' has no call signatures.ts(2349) service.ts(6, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

For me basicAuth.default has solved the issue:

import * as basicAuth from 'express-basic-auth';

...

app.use(basicAuth.default({
  users: {
    uname: 'secret',
  },
  challenge: true,
}));
NixBiks commented 2 years ago

Fails for me as well. Can't get basicAuth.IBasicAuthedRequest to work with requests

LionC commented 2 years ago

I did not maintain the package for a while (simply did not have time), but I recently returned to open source work. I will revive my TS rewrite branch next week and make sure to release a new version written in Typescript until the end of the month, as a lot of people still depend on this package.

Thank you for the patience and activity everyone :-)

sephentos commented 1 year ago

I did not maintain the package for a while (simply did not have time), but I recently returned to open source work. I will revive my TS rewrite branch next week and make sure to release a new version written in Typescript until the end of the month, as a lot of people still depend on this package.

Thank you for the patience and activity everyone :-)

Any update here, @LionC ?