Using platformClient with typescript

Hello,

We are trying to use the platformClient with typescript for our application, but we need to use the Code Authorization Grant for authentication, and it appears not to be included.

We are importing and initializing the client like so:

import {ApiClientClass} from "purecloud-platform-client-v2";
private _authClient: ApiClientClass;
...
this._authClient = new ApiClientClass().instance;

This all seems to work fine, but the loginCodeAuthorizationGrant method specified in the documentation does not seem to exist. The methods for the other authentication types appear, however.

We require the code authorization grant for our purposes, how can we accomplish this?

Thank you.

1 Like

It's there, it's just missing from the typescript definition. I'll file a bug to get that fixed. You should be able to do something like (this._authClient as any).loginCodeAuthorizationGrant(...) until the definition is updated.

2 Likes

How will this workaround affect unit testing with Jest?

It shouldn't affect the function of your tests. This is only a workaround for typescript's type linting; there's nothing wrong with the package itself. Are you having a specific problem running your tests?

I am trying to use a manual mock for the purecloud-platform-client-v2 module and experiencing issues trying to mock the loginCodeAuthorizationGrant method

Here is an example of the manual mock:

const mockApiClientClass = {
    loginCodeAuthorizationGrant: jest.fn().mockResolvedValue({})
};

const mockUsersApiInstance = {
    getUsersMe: jest.fn().mockResolvedValue({})
};

module.exports = {
        ApiClientClass: jest.fn().mockReturnValue(mockApiClientClass),
        UsersApi: jest.fn().mockReturnValue(mockUsersApiInstance)
};

I have no issue using the getUsersMe mocked method, but loginCodeAuthorizationGrant gives me the following error: "Property 'loginCodeAuthorizationGrant' does not exist on type 'ApiClientClass'"

It sounds like you're trying to use the TypeScript definition to mock the function using reflection. That won't work in this case because there is no TypeScript definition for the function. You can either create your own mocked object based on the source code of the actual function or you can skip this test until the SDK update is released that does contain the TypeScript definition.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.