typescript, how to reference the unexported OAuth2Client? google-api-nodejs-client

Viewed 1511

I am trying to follow https://developers.google.com/sheets/api/quickstart/nodejs tutorial I am using typescript and I like the type annotation and auto-complition features it provides and I would like to re-write the example in typescript and async rather than callbacks. Sadly I am stuck, OAuth2Client. in my code I creating an oauth client inside a function like this:

async function setupClient() {
  const content: string = await fs.readFile("credentials.json", "utf8");
  const credentials = JSON.parse(content);
  // eslint-disable-next-line camelcase
  const { client_secret, client_id, redirect_uris } = credentials.installed;
  const oAuth2Client: o2 = new google.auth.OAuth2(
    client_id,
    client_secret,
    redirect_uris[0]
  );
  type OAuth2Client = typeof oAuth2Client;
  return oAuth2Client;
}

And I would like to delegate that client to two other setup functions:

function setupAuthUrl(oAuth2Client, scopes) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: "offline",
    scope: scopes,
  });
  return authUrl;
}
function setupToken(oAuth2Client, code) {
  //code
}

But I am struggling to annotate oauth2client type as it is not directly exposed by anything? One would guess that it is exposed under the 'oauth2_v2' namespace? but doesn't seem to be it.

I have been looking for a way to reference this type prior to instantiation, preferably via importing, sadly, when installing this libary via npm install googleapis it doesn't provide a dependency to niether of these google-auth-library, googleapis-common where it is exported and exposed for importing. eslint errors unless it adds the dependency and I struggle to see why It is required as the type should be accessable for code completion with ease.

As for type aliasing:

type OAuth2Client = typeof GoogleApis.prototype.auth.OAuth2.prototype;

This is the only option I could figure out that doesn't throw an error/warning(object as namespace) for aliasing. this is both a long line and very weird one at that too coming from other OO languages

  • TLDR: What's the prefered way to access OAuth2Client and maybe other types for code completion in typescript?
1 Answers
Related