Persist googlesheet refs in context
This commit is contained in:
parent
a705fbd497
commit
7807b734bb
|
@ -1,4 +1,6 @@
|
|||
import { IdentityContext, Snippet, VM } from "@budibase/types"
|
||||
import { OAuth2Client } from "google-auth-library"
|
||||
import { GoogleSpreadsheet } from "google-spreadsheet"
|
||||
|
||||
// keep this out of Budibase types, don't want to expose context info
|
||||
export type ContextMap = {
|
||||
|
@ -12,4 +14,8 @@ export type ContextMap = {
|
|||
vm?: VM
|
||||
cleanup?: (() => void | Promise<void>)[]
|
||||
snippets?: Snippet[]
|
||||
googleSheets?: {
|
||||
oauthClient: OAuth2Client
|
||||
clients: Record<string, GoogleSpreadsheet>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -228,6 +228,10 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
|||
|
||||
private async connect() {
|
||||
try {
|
||||
const bbCtx = context.getCurrentContext()
|
||||
let oauthClient = bbCtx?.googleSheets?.oauthClient
|
||||
|
||||
if (!oauthClient) {
|
||||
await setupCreationAuth(this.config)
|
||||
|
||||
// Initialise oAuth client
|
||||
|
@ -236,7 +240,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
|||
throw new HTTPError("Google config not found", 400)
|
||||
}
|
||||
|
||||
const oauthClient = new OAuth2Client({
|
||||
oauthClient = new OAuth2Client({
|
||||
clientId: googleConfig.clientID,
|
||||
clientSecret: googleConfig.clientSecret,
|
||||
})
|
||||
|
@ -251,9 +255,29 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
|||
refresh_token: this.config.auth.refreshToken,
|
||||
access_token: tokenResponse.access_token,
|
||||
})
|
||||
if (bbCtx && !bbCtx.googleSheets) {
|
||||
bbCtx.googleSheets = {
|
||||
oauthClient,
|
||||
clients: {},
|
||||
}
|
||||
bbCtx.cleanup = bbCtx.cleanup || []
|
||||
|
||||
this.client = new GoogleSpreadsheet(this.spreadsheetId, oauthClient)
|
||||
await this.client.loadInfo()
|
||||
}
|
||||
}
|
||||
|
||||
let client = bbCtx?.googleSheets?.clients[this.spreadsheetId]
|
||||
if (!client) {
|
||||
client = new GoogleSpreadsheet(this.spreadsheetId, oauthClient)
|
||||
await client.loadInfo()
|
||||
|
||||
if (bbCtx?.googleSheets?.clients) {
|
||||
bbCtx.googleSheets.clients[this.spreadsheetId] = client
|
||||
}
|
||||
}
|
||||
|
||||
this.client = client
|
||||
} catch (err: any) {
|
||||
// this happens for xlsx imports
|
||||
if (err.message?.includes("operation is not supported")) {
|
||||
|
|
Loading…
Reference in New Issue