rafaelcascalho/keycloak-middleware-ts

createKeycloakCtx() - TypeError: this.request.post is not a function

hebertcisco opened this issue · 2 comments

When trying to inject an axios configuration in the request parameter in the createKeycloakCtx function, a typing error is returned, saying that the post method is not a function, as in the example below...

Steps to reproduce the behavior:

  1. Configure the project as explained in README.md under Usage.
  2. Add axios configuration following AxiosInstance typing
  3. Use the create user method
  4. See error

Will return a typing error showing that the post is not a function

Example:

[Nest] 16436  - 17/02/2022 16:35:44   ERROR [UserService] TypeError: this.request.post is not a function
TypeError: this.request.post is not a function
    at AccessToken.<anonymous> (C:\Users\example\www\nestjs-project\node_modules\keycloak-middleware-ts\lib\AccessToken.js:69:49)
    at Generator.next (<anonymous>)
    at C:\Users\example\www\nestjs-project\node_modules\keycloak-middleware-ts\lib\AccessToken.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\example\www\nestjs-project\node_modules\keycloak-middleware-ts\lib\AccessToken.js:4:12)
    at AccessToken.get (C:\Users\example\www\nestjs-project\node_modules\keycloak-middleware-ts\lib\AccessToken.js:55:16)
    at UserManager.<anonymous> (C:\Users\example\www\nestjs-project\node_modules\keycloak-middleware-ts\lib\UserManager.js:83:50)
    at Generator.next (<anonymous>)
    at C:\Users\example\www\nestjs-project\node_modules\keycloak-middleware-ts\lib\UserManager.js:8:71
    at new Promise (<anonymous>)

Versions:

  • OS: Windows NT
  • Node 16.13.1
  • NPM 8.2.0

Additional context
Below is an example of configuration and use of the library where this problem occurs

Configuration

import createKeycloakCtx from 'keycloak-middleware-ts';
import dotenv from 'dotenv';
import qs from 'qs';

dotenv.config();

export const {
  KEYCLOAK_REALM,
  KEYCLOAK_URL,
  KEYCLOAK_CLIENT_ID,
  KEYCLOAK_CLIENT_SECRET,
  KEYCLOAK_USERNAME,
  KEYCLOAK_PASSWORD,
  KEYCLOAK_JWT_KEY,
} = process.env;

const axiosConfig = {
  config: {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify({
      grant_type: 'password',
      clientId: KEYCLOAK_CLIENT_ID,
      clientSecret: KEYCLOAK_CLIENT_SECRET,
      username: KEYCLOAK_USERNAME,
      password: KEYCLOAK_PASSWORD,
    }),
  },
};
export const keycloak = createKeycloakCtx(
  {
    realm: KEYCLOAK_REALM,
    authServerUrl: KEYCLOAK_URL,
    clientId: KEYCLOAK_CLIENT_ID,
    clientSecret: KEYCLOAK_CLIENT_SECRET,
    username: KEYCLOAK_USERNAME,
    password: KEYCLOAK_PASSWORD,
    jwtKey: KEYCLOAK_JWT_KEY,
    jwtKeyAlgorithms: ['RS256'],
  },
  axiosConfig,
);

Usage

await keycloak.users
                    .create({
                      firstName: 'John',
                      lastName: 'Doe',
                      username: 'john_doe',
                      email: 'doe@example.com',
                      enabled: true,
                      password: 'str0ng-p4ss',
});

The thing is that you're passing the axios config and the function expects an axios instance. It was done this way, so that the helper that executes the requests would be injectable, enabling easily the unit tests - the main reason which it was created for. Try again with an axios instance and reply here case you're still having trouble.

Everything is ok, I passed the request parameter as an AxiosInstance, and it worked.
Now we can close #4