From 79e3167ece6e2d174ff68b840c5543bfd0188f61 Mon Sep 17 00:00:00 2001 From: pauloh-fm Date: Sat, 13 Jul 2024 03:28:02 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20uma=20fun=C3=A7=C3=A3o=20que=20permite?= =?UTF-8?q?=20realizar=20a=20conex=C3=A3o=20ao=20google=20sheets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Conexão com o Google Sheets #26 --- .env.example | 4 ++++ src/utils/googleSheetsConexion.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .env.example create mode 100644 src/utils/googleSheetsConexion.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6f7d395 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +GOOGLE_CLIENT_EMAIL= +GOOGLE_PRIVATE_KEY= +GOOGLE_SHEET_ID= +SHEET_NAME= \ No newline at end of file diff --git a/src/utils/googleSheetsConexion.ts b/src/utils/googleSheetsConexion.ts new file mode 100644 index 0000000..f697498 --- /dev/null +++ b/src/utils/googleSheetsConexion.ts @@ -0,0 +1,21 @@ +import { google } from "googleapis"; + +export const getGoogleSheetsClient = () => { + const { GOOGLE_CLIENT_EMAIL, GOOGLE_PRIVATE_KEY } = process.env; + + if (!GOOGLE_CLIENT_EMAIL || !GOOGLE_PRIVATE_KEY) { + throw new Error( + "Não foi possivel carregar as variaveis de ambiente da Google Cloud", + ); + } + + const auth = new google.auth.GoogleAuth({ + credentials: { + client_email: GOOGLE_CLIENT_EMAIL, + private_key: GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"), + }, + scopes: ["https://www.googleapis.com/auth/spreadsheets"], + }); + + return google.sheets({ version: "v4", auth }); +};