-
Notifications
You must be signed in to change notification settings - Fork 441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(integration-templates): improve google-drive templates #2727
Merged
khaliqgant
merged 3 commits into
master
from
wari/ext-145-refractor-google-drive-integrations
Sep 17, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
integration-templates/google-drive/actions/fetch-document.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { NangoAction, ProxyConfiguration } from '../../models'; | ||
import type { GoogleDriveFileResponse } from '../types.js'; | ||
import { mimeTypeMapping } from '../types.js'; | ||
|
||
/** | ||
* Retrieves and returns the content of a Google Drive file as a base64-encoded string. | ||
* | ||
* For detailed endpoint documentation, refer to: | ||
* | ||
* https://developers.google.com/drive/api/reference/rest/v3/files/get | ||
* https://developers.google.com/drive/api/reference/rest/v3/files/export | ||
* @param nango - An instance of NangoAction used for making API requests. | ||
* @param input - The ID of the file to be retrieved, provided as a string. | ||
* @returns The base64-encoded content of the file. | ||
* @throws Error if the input is invalid, or if the file metadata or content retrieval fails. | ||
*/ | ||
export default async function runAction(nango: NangoAction, input: string): Promise<string> { | ||
if (!input || typeof input !== 'string') { | ||
throw new Error('Missing or invalid input: a file ID is required and should be a string'); | ||
} | ||
|
||
// Fetch the file metadata first to get the MIME type | ||
const Config: ProxyConfiguration = { | ||
endpoint: `drive/v3/files/${input}`, | ||
params: { | ||
fields: 'id, name, mimeType' | ||
} | ||
}; | ||
const fileMetadataResponse = await nango.get<GoogleDriveFileResponse>(Config); | ||
|
||
if (fileMetadataResponse.status !== 200 || !fileMetadataResponse.data) { | ||
throw new Error(`Failed to retrieve file metadata: Status Code ${fileMetadataResponse.status}`); | ||
} | ||
|
||
const file = fileMetadataResponse.data; | ||
const mimeTypeDetails = mimeTypeMapping[file.mimeType]; | ||
|
||
if (!mimeTypeDetails) { | ||
throw new Error(`Unsupported MIME type: ${file.mimeType}`); | ||
} | ||
|
||
const { mimeType: exportMimeType, responseType } = mimeTypeDetails; | ||
|
||
await nango.log('Fetching document of ', { exportMimeType }); | ||
|
||
const endpoint = responseType === 'text' ? `drive/v3/files/${file.id}/export` : `drive/v3/files/${file.id}`; | ||
const params = responseType === 'text' ? { mimeType: exportMimeType } : { alt: 'media' }; | ||
|
||
const config: ProxyConfiguration = { | ||
endpoint, | ||
params, | ||
responseType | ||
}; | ||
const response = await nango.get(config); | ||
|
||
if (response.status !== 200) { | ||
throw new Error(`Failed to retrieve file content: Status Code ${response.status}`); | ||
} | ||
|
||
if (responseType === 'text') { | ||
return response.data ?? ''; | ||
} else { | ||
const chunks: Buffer[] = []; | ||
for await (const chunk of response.data) { | ||
chunks.push(chunk); | ||
} | ||
const buffer = Buffer.concat(chunks); | ||
return buffer.toString('base64'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export interface GoogleDriveFileResponse { | ||
id: string; | ||
name: string; | ||
mimeType: string; | ||
webViewLink: string; | ||
} | ||
|
||
export interface Metadata { | ||
files?: string[]; | ||
folders?: string[]; | ||
} | ||
|
||
interface MimeTypeMapping { | ||
mimeType: string; | ||
responseType: 'text' | 'stream'; | ||
} | ||
|
||
export const mimeTypeMapping: Record<string, MimeTypeMapping> = { | ||
// Documents | ||
'application/vnd.google-apps.document': { mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', responseType: 'text' }, | ||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': { | ||
mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | ||
responseType: 'stream' | ||
}, | ||
'application/vnd.oasis.opendocument.text': { mimeType: 'application/vnd.oasis.opendocument.text', responseType: 'stream' }, | ||
'application/rtf': { mimeType: 'application/rtf', responseType: 'stream' }, | ||
'text/plain': { mimeType: 'text/plain', responseType: 'stream' }, | ||
// Spreadsheets | ||
'application/vnd.google-apps.spreadsheet': { mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', responseType: 'text' }, | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': { | ||
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
responseType: 'stream' | ||
}, | ||
'application/vnd.oasis.opendocument.spreadsheet': { mimeType: 'application/vnd.oasis.opendocument.spreadsheet', responseType: 'stream' }, | ||
// PDFs | ||
'application/pdf': { mimeType: 'application/pdf', responseType: 'stream' }, | ||
// Text Files | ||
'text/csv': { mimeType: 'text/csv', responseType: 'text' }, | ||
'text/tab-separated-values': { mimeType: 'text/tab-separated-values', responseType: 'text' }, | ||
// Presentations | ||
'application/vnd.google-apps.presentation': { mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', responseType: 'text' }, | ||
'application/vnd.openxmlformats-officedocument.presentationml.presentation': { | ||
mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', | ||
responseType: 'stream' | ||
}, | ||
'application/vnd.oasis.opendocument.presentation': { mimeType: 'application/vnd.oasis.opendocument.presentation', responseType: 'stream' }, | ||
// Drawings and Images | ||
'application/vnd.google-apps.drawing': { mimeType: 'image/jpeg', responseType: 'stream' }, | ||
'image/jpeg': { mimeType: 'image/jpeg', responseType: 'stream' }, | ||
'image/png': { mimeType: 'image/png', responseType: 'stream' }, | ||
'image/svg+xml': { mimeType: 'image/svg+xml', responseType: 'stream' } | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where does this mapping and the one below come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
application/vnd.google-apps.document
: This is a MIME type used by Google Drive to represent Google Docs documents.mimeType
:application/vnd.openxmlformats-officedocument.wordprocessingml.document
specifies the desired MIME type for the converted file, which is the standard MIME type for Microsoft Word documents in the Office Open XML format.