← Public packages
@kody/google
Call Gmail, Calendar, Tasks, Drive, Docs, Sheets, People, YouTube, and Analytics through saved Google OAuth.
src/docs.ts
70 lines · 2.2 KB · TypeScriptimport { requestGoogle } from './core.ts'
import type { GoogleAuthParams } from './accounts.ts'
import type { JsonObject } from './types.ts'
export type DocsGetParams = GoogleAuthParams & {
documentId: string
suggestionsViewMode?: string
}
export type DocsCreateParams = GoogleAuthParams & {
title?: string
dryRun?: boolean
}
export type DocsBatchUpdateParams = GoogleAuthParams & {
documentId: string
requests: unknown[]
dryRun?: boolean
}
/**
* Return the Google Docs helper namespace.
* Built-in Google includes the documents scope. Inbox/Drive-wide are unrelated.
* @returns Helper namespace or overview for this Google product.
* @example
* import docs from 'kody:@kody/google/docs'
* const doc = await docs().getDocument({ documentId: '...' })
*/
export default function docs() {
return { getDocument, createDocument, batchUpdateDocument }
}
export async function getDocument(params: DocsGetParams): Promise<JsonObject> {
if (!params.documentId) throw new Error('getDocument requires documentId.')
return (await requestGoogle({
...params,
api: 'docs',
path: '/v1/documents/' + encodeURIComponent(params.documentId),
query: { suggestionsViewMode: params.suggestionsViewMode },
})) as JsonObject
}
export async function createDocument(params: DocsCreateParams = {}): Promise<JsonObject> {
const { title = 'Untitled document', dryRun = false } = params
if (dryRun) return { dryRun: true, account: params.account, title }
return (await requestGoogle({
...params,
api: 'docs',
method: 'POST',
path: '/v1/documents',
body: { title },
})) as JsonObject
}
export async function batchUpdateDocument(params: DocsBatchUpdateParams): Promise<JsonObject> {
const { documentId, requests, dryRun = false } = params
if (!documentId) throw new Error('batchUpdateDocument requires documentId.')
if (!Array.isArray(requests) || requests.length === 0) {
throw new Error('batchUpdateDocument requires a non-empty requests array.')
}
if (dryRun) return { dryRun: true, account: params.account, documentId, requests }
return (await requestGoogle({
...params,
api: 'docs',
method: 'POST',
path: '/v1/documents/' + encodeURIComponent(documentId) + ':batchUpdate',
body: { requests },
})) as JsonObject
}