googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.27k stars 1.91k forks source link

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

Open edosrecki opened 9 months ago

edosrecki commented 9 months ago

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

danielbankhead commented 8 months ago

Related:

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