google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

`GoogleAuth` type should be generic `GoogleAuth<T extends AuthClient>`

Open edosrecki opened this issue 2 years ago • 1 comments

I am trying to instantiate a Sqladmin class by passing a custom GoogleAuth object, which was in turn instantiated by passing a custom AuthClient object.

However, I get a TypeScript error shown below, which is due to the fact that Sqladmin class expects auth: GoogleAuth, which is defaulted to GoogleAuth<JSONClient>, where JSONClient = JWT | UserRefreshClient | BaseExternalAccountClient | ExternalAccountAuthorizedUserClient | Impersonated;

I believe that Sqladmin class should be parameterized with T extends AuthClient, and then it should accept GoogleAuth<T> in the auth property.

Code

import {AuthClient, GoogleAuth} from 'google-auth-library';
import {sqladmin_v1beta4} from '@googleapis/sqladmin';
const {Sqladmin} = sqladmin_v1beta4;

function buildSqlAdmin (authClient: AuthClient) {
  const auth = new GoogleAuth<AuthClient>({
    auth: authClient,
    scopes: ['https://www.googleapis.com/auth/sqlservice.admin'],
  });

  return new Sqladmin({
    auth
  });
}

TypeScript Error

Screenshot 2023-09-28 at 09 59 09

Type Definitions

Sqladmin:

https://github.com/googleapis/google-api-nodejs-client/blob/261170592cc5cec6a6991a356f8d6180366cb675/src/apis/sqladmin/v1beta4.ts#L54

GoogleAuth:

https://github.com/googleapis/google-auth-library-nodejs/blob/7030314c55c369acbda1873f649579b395ad07c8/src/auth/googleauth.ts#L85

edosrecki avatar Sep 28 '23 08:09 edosrecki

Related:

  • https://github.com/GoogleCloudPlatform/cloud-sql-nodejs-connector/pull/238

The root of this request will be resolved here (for all googleapis libraries):

  • https://github.com/googleapis/google-auth-library-nodejs/issues/1677

danielbankhead avatar Oct 23 '23 20:10 danielbankhead