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

I am trying to followtutorialI am using typescript and I like the type annotation and auto-plition f

I am trying to follow tutorial I am using typescript and I like the type annotation and auto-plition 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-mon 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 pletion 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 ing from other OO languages

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

I am trying to follow https://developers.google./sheets/api/quickstart/nodejs tutorial I am using typescript and I like the type annotation and auto-plition 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-mon 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 pletion 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 ing from other OO languages

  • TLDR: What's the prefered way to access OAuth2Client and maybe other types for code pletion in typescript?
Share asked Jun 2, 2020 at 21:53 Yorai LeviYorai Levi 7708 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

As of Jun 19 a merge request adding this feature has been approved. https://github./googleapis/google-api-nodejs-client/issues/2208

import { google, Auth } from 'googleapis';
const oauthClient: Auth.OAuth2Client = new google.auth.OAuth2();

Thanks for angelxmoreno for the example

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741625476a4357250.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信