feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Using API key authenticaion as per Feathers documentation causes TS compilation errors

Open JPStrydom opened this issue 5 months ago • 1 comments

Steps to reproduce

  • Create a new Feathers project configured with TS
  • Set up API key (machine key) authentication as instructed by the feathers documentation here
  • You'll get a TS compilation error on the line app.use('authentication', authentication); stating:
      src/authentication.ts:25:51 - error TS2339: Property 'apiKey' does not exist on type '{ entity?: string | null | undefined; entityId?: string | und
      efined; service?: string | undefined; parseStrategies?: string[] | undefined; jwtOptions?: {} | undefined; jwt?: { header: string; schemes: string; } | undefined; local?: { ...; } | undefined; oauth?: { ...; } | undefined; secret: string; authStrategies: s...'.
    
      25     const config = this.app.get('authentication').apiKey;
                                                           ~~~~~~
      src/authentication.ts:43:29 - error TS2345: Argument of type 'AuthenticationService' is not assignable to parameter of type 'AuthenticationService & ServiceAddons<any, Service<any, Partial<any>, Params<Query>, Partial<Partial<any>>>>'.
        Type 'AuthenticationService' is missing the following properties from type 'ServiceAddons<any, Service<any, Partial<any>, Params<Query>, Partial<Partial<any>>>>': hooks, publish, registerPublisher, addListener, and 14 more.
    
      43   app.use('authentication', authentication);
                                     ~~~~~~~~~~~~~~
    
          at createTSError (path\\to\\project\\node_modules\\ts-node\\src\\index.ts:859:12)
          at reportTSError (path\\to\\project\\node_modules\\ts-node\\src\\index.ts:863:19)
          at getOutput (path\\to\\project\\node_modules\\ts-node\\src\\index.ts:1077:36)
          at Object.compile (path\\to\\project\\node_modules\\ts-node\\src\\index.ts:1433:41)
          at Module.m._compile (path\\to\\project\\node_modules\\ts-node\\src\\index.ts:1617:30)
          at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
          at Object.require.extensions.<computed> [as .ts] (path\\to\\project\\node_modules\\ts-node\\src\\index.ts:1621:12)
          at Module.load (node:internal/modules/cjs/loader:1207:32)
          at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
          at Module.require (node:internal/modules/cjs/loader:1235:19) {
        diagnosticCodes: [ 2532, 2339, 2345 ]
      }
      [nodemon] app crashed - waiting for file changes before starting...
    
    

Expected behavior

The API key authentication from the Feathers example should work and allow API key authentication.

Actual behavior

The provided example doesn't compile. I've also tried the following:

interface ServiceTypes {
  authentication: AuthenticationService; // & ServiceAddons<any>;
}

Which resolves the initial issue, but then the following error occures:

src/authentication.ts:25:51 - error TS2339: Property 'apiKey' does not exist on type '{ entity?: string | null | undefined; entityId?: string | und
efined; service?: string | undefined; parseStrategies?: string[] | undefined; jwtOptions?: {} | undefined; jwt?: { header: string; schemes: string; } | undefined; local?: { ...; } | undefined; oauth?: { ...; } | undefined; secret: string; authStrategies: s...'.

25     const config = this.app.get('authentication').apiKey;
                                                   ~~~~~~

  at createTSError (path\to\project\node_modules\ts-node\src\index.ts:859:12)
  at reportTSError (path\to\project\node_modules\ts-node\src\index.ts:863:19)
  at getOutput (path\to\project\node_modules\ts-node\src\index.ts:1077:36)
  at Object.compile (path\to\project\node_modules\ts-node\src\index.ts:1433:41)
  at Module.m._compile (path\to\project\node_modules\ts-node\src\index.ts:1617:30)
  at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
  at Object.require.extensions.<computed> [as .ts] (path\to\project\node_modules\ts-node\src\index.ts:1621:12)
  at Module.load (node:internal/modules/cjs/loader:1207:32)
  at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
  at Module.require (node:internal/modules/cjs/loader:1235:19) {
diagnosticCodes: [ 2532, 2339 ]
}
[nodemon] app crashed - waiting for file changes before starting...

I'm sure I'm just missing something wrong with the TS typing, but not sure where to start.

System configuration

Module versions:

  • "@feathersjs/feathers": "5.0.21"
  • "typescript": "^5.3.3"

NodeJS version: v20.11.0

JPStrydom avatar Feb 08 '24 14:02 JPStrydom

Have you tried adding that property to your configurationSchema in configuration.ts?

jordandenison avatar Mar 01 '24 00:03 jordandenison