google-drive-picker-plus
v1.1.0
Published
A modern, zero-dependency, TypeScript-friendly wrapper for Google Drive Picker API and OAuth2, enabling cross-platform file selection.
Maintainers
Readme
Google Drive Picker Plus
一个现代化、零依赖、支持 TypeScript 的 Google Drive Picker API 和 OAuth2 封装库,帮助你在跨平台应用中无缝集成 Google Drive 文件选择功能。
✨ 功能特性
- 现代 & 简单: 只需一个异步函数即可完成整个流程。
- TypeScript 优先: 100% 使用 TypeScript 编写,提供完善的类型定义。
- 可配置化: 可自定义 MIME 类型、文件大小限制和选择模式。
- 健壮的错误处理: 标准化错误对象,便于统一管理。
- 多模块格式支持: 兼容 UMD、CJS、ESM,满足不同环境需求。
🚀 安装
pnpm add google-drive-picker-plus💡 Usage
import { openGoogleDrivePicker, GooglePickerError } from 'google-drive-picker-plus';
async function selectFiles() {
const options = {
clientId: 'YOUR_GOOGLE_CLIENT_ID',
apiKey: 'YOUR_GOOGLE_API_KEY',
appId: 'YOUR_GOOGLE_APP_ID',
// Optional configurations
multiSelect: true,
maxFileSizeMB: 10,
mimeTypes: ['image/jpeg', 'image/png', 'application/pdf'],
};
try {
const files = await openGoogleDrivePicker(options);
console.log('Selected Files:', files);
// Now you have an array of File objects to work with!
} catch (error) {
if (error instanceof GooglePickerError) {
console.error('Picker Error:', error.code, error.message);
} else {
console.error('An unexpected error occurred:', error);
}
}
}
// Call the function when needed, e.g., on a button click
document.getElementById('my-button').addEventListener('click', selectFiles);API 参考
openGoogleDrivePicker(options: PickerOptions): Promise<File[]>
options: PickerOptions 对象。
clientId: string:必填。你的 Google API Client ID。
apiKey: string:必填。你的 Google API Key。
appId: string:必填。你的 Google App ID。
mimeTypes?: string[]:可选。允许的 MIME 类型数组。
maxFileSizeMB?: number:可选。最大文件大小(单位:MB)。
multiSelect?: boolean:可选。是否允许多文件选择,默认为 true。
Error Handling
The function throws GooglePickerError for predictable errors. Each error instance has code and message properties.
| Code | Message |
| ---------------------------- | ------------------------------------------------------ |
| MISSING_CONFIG | Required configuration options are missing. |
| SCRIPT_LOAD_FAILED | Failed to load Google API scripts. |
| AUTH_FAILED | Authorization failed or was denied. |
| USER_CANCELLED | User cancelled the file selection. |
| FILE_SIZE_LIMIT_EXCEEDED | A selected file exceeds the size limit. |
| ...and more | |
