//native
/**
* Find Documents
* Search the Google Docs the account can see (shared drives included), newest-modified first. `query` matches the title or full text; leave it empty to list all docs. Pass the returned `nextPageToken` as `page_token` for the next page. Requires drive.readonly or drive on the connection (drive.file only sees docs this connection created).
*/
export async function main(
auth: RT.Gdocs,
query: string | undefined,
page_size: number = 50,
page_token: string | undefined
) {
let q = "mimeType = 'application/vnd.google-apps.document' and trashed = false"
if (query) {
const escaped = query.replace(/\\/g, "\\\\").replace(/'/g, "\\'")
q += ` and (name contains '${escaped}' or fullText contains '${escaped}')`
}
const url = new URL("https://www.googleapis.com/drive/v3/files")
url.searchParams.append("q", q)
url.searchParams.append(
"fields",
"nextPageToken,files(id,name,modifiedTime,webViewLink,owners(emailAddress))"
)
url.searchParams.append("orderBy", "modifiedTime desc")
url.searchParams.append("pageSize", String(page_size || 50))
url.searchParams.append("supportsAllDrives", "true")
url.searchParams.append("includeItemsFromAllDrives", "true")
if (page_token != null && page_token !== "")
url.searchParams.append("pageToken", page_token)
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 6 hours ago