Skip to content

@happ-integ/google-drive

Клиент для Google Drive API.

Установка

bash
pnpm add @happ-integ/google-drive

Использование

Инициализация

typescript
import { GoogleDriveClient } from "@happ-integ/google-drive";

const drive = new GoogleDriveClient({
	credentials: {
		type: "service_account",
		project_id: "...",
		private_key: env.GOOGLE_PRIVATE_KEY,
		client_email: env.GOOGLE_CLIENT_EMAIL,
		// ... другие поля
	},
	scopes: ["https://www.googleapis.com/auth/drive.readonly"], // опционально
});

Файлы

typescript
// Получить список файлов
const response = await drive.listFiles();
// => { files: [...], nextPageToken: "..." }

// Файлы в папке
const response = await drive.listFiles("folder-id");

// С пагинацией
const nextPage = await drive.listFiles("folder-id", "next-page-token");

// Получить файл
const file = await drive.getFile("file-id");

// Скачать текстовый файл
const text = await drive.downloadTextFile("file-id");

// Скачать Excel / CSV
const data = await drive.downloadSpreadsheet("file-id");
// => { rows: [...], columns: [...] }

// Скачать Word документ
const content = await drive.downloadWordDocument("file-id");

// Скачать PDF текст
const text = await drive.downloadPdfText("file-id");

Папки

typescript
// Создать папку
const folder = await drive.createFolder("folder-name");

// Получить папку
const folder = await drive.getFolder("folder-id");

// Список файлов в папке
const files = await drive.listFilesInFolder("folder-id");

Health Check

typescript
const isHealthy = await drive.healthCheck();

API

GoogleDriveClient

typescript
new GoogleDriveClient(config: IGoogleDriveConfig)

Config:

typescript
{
  credentials: object; // Google service account credentials
  scopes?: string[];   // опционально
}

Методы Список Файлов

listFiles(folderId?, pageToken?): Promise<IListFilesResponse>

Получить список файлов с пагинацией.

listFilesInFolder(folderId): Promise<IGoogleDriveFile[]>

Все файлы в папке.

Методы Скачивание

getFile(fileId): Promise<IGoogleDriveFile>

Метаданные файла.

downloadTextFile(fileId): Promise<string>

Скачать текстовый файл (text, markdown).

downloadSpreadsheet(fileId): Promise<{ rows, columns }>

Скачать Google Sheet или Excel.

downloadWordDocument(fileId): Promise<string>

Скачать Word документ.

downloadPdfText(fileId): Promise<string>

Скачать PDF текст.

downloadFile(fileId, mimeType?): Promise<Blob>

Скачать файл.

Методы Папки

createFolder(name, parentId?): Promise<IGoogleDriveFile>

getFolder(folderId): Promise<IGoogleDriveFile>

Другое

healthCheck(): Promise<boolean>

Google Service Account

  1. https://console.cloud.google.com/iam-admin/serviceaccounts
  2. Create Service Account
  3. Create Key (JSON)
  4. Enable Google Drive API
  5. Добавить credentials в Doppler / env
GOOGLE_PRIVATE_KEY=-----BEGIN RSA...
GOOGLE_CLIENT_EMAIL=xxx@xxx.iam.gserviceaccount.com

Когда использовать

  • ✅ Чтение файлов с Google Drive
  • ✅ Парсинг документов
  • ✅ Интеграция с Google Workspace
  • ❌ Загрузка файлов — используйте другой сервис