diff options
author | bobzel <zzzman@gmail.com> | 2024-05-17 14:55:36 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2024-05-17 14:55:36 -0400 |
commit | 0b451af28e5aef6b749da61e8a9fcd0a840789ac (patch) | |
tree | bdee4e28ee4715b69299a8da1b615c70b6adc445 /src/client/apis/google_docs/GoogleApiClientUtils.ts | |
parent | 8c1b420a143e4b72ec551277887c211ca6ca003b (diff) | |
parent | 38a382a03675d6a50ec7de75f05025efd093f570 (diff) |
merged with new master
Diffstat (limited to 'src/client/apis/google_docs/GoogleApiClientUtils.ts')
-rw-r--r-- | src/client/apis/google_docs/GoogleApiClientUtils.ts | 164 |
1 files changed, 86 insertions, 78 deletions
diff --git a/src/client/apis/google_docs/GoogleApiClientUtils.ts b/src/client/apis/google_docs/GoogleApiClientUtils.ts index c8f381cc0..0b303eacf 100644 --- a/src/client/apis/google_docs/GoogleApiClientUtils.ts +++ b/src/client/apis/google_docs/GoogleApiClientUtils.ts @@ -1,45 +1,46 @@ -import { docs_v1 } from "googleapis"; -import { Opt } from "../../../fields/Doc"; -import { isArray } from "util"; -import { EditorState } from "prosemirror-state"; -import { Networking } from "../../Network"; +/* eslint-disable no-restricted-syntax */ +/* eslint-disable no-use-before-define */ +import { docs_v1 as docsV1 } from 'googleapis'; +// eslint-disable-next-line node/no-deprecated-api +import { isArray } from 'util'; +import { EditorState } from 'prosemirror-state'; +import { Opt } from '../../../fields/Doc'; +import { Networking } from '../../Network'; -export const Pulls = "googleDocsPullCount"; -export const Pushes = "googleDocsPushCount"; +export const Pulls = 'googleDocsPullCount'; +export const Pushes = 'googleDocsPushCount'; export namespace GoogleApiClientUtils { - export enum Actions { - Create = "create", - Retrieve = "retrieve", - Update = "update" + Create = 'create', + Retrieve = 'retrieve', + Update = 'update', } export namespace Docs { - - export type RetrievalResult = Opt<docs_v1.Schema$Document>; - export type UpdateResult = Opt<docs_v1.Schema$BatchUpdateDocumentResponse>; + export type RetrievalResult = Opt<docsV1.Schema$Document>; + export type UpdateResult = Opt<docsV1.Schema$BatchUpdateDocumentResponse>; export interface UpdateOptions { documentId: DocumentId; - requests: docs_v1.Schema$Request[]; + requests: docsV1.Schema$Request[]; } export enum WriteMode { Insert, - Replace + Replace, } export type DocumentId = string; export type Reference = DocumentId | CreateOptions; export interface Content { text: string | string[]; - requests: docs_v1.Schema$Request[]; + requests: docsV1.Schema$Request[]; } export type IdHandler = (id: DocumentId) => any; export type CreationResult = Opt<DocumentId>; - export type ReadLinesResult = Opt<{ title?: string, bodyLines?: string[] }>; - export type ReadResult = { title: string, body: string }; + export type ReadLinesResult = Opt<{ title?: string; bodyLines?: string[] }>; + export type ReadResult = { title: string; body: string }; export interface ImportResult { title: string; text: string; @@ -67,23 +68,23 @@ export namespace GoogleApiClientUtils { } /** - * After following the authentication routine, which connects this API call to the current signed in account - * and grants the appropriate permissions, this function programmatically creates an arbitrary Google Doc which - * should appear in the user's Google Doc library instantaneously. - * - * @param options the title to assign to the new document, and the information necessary - * to store the new documentId returned from the creation process - * @returns the documentId of the newly generated document, or undefined if the creation process fails. - */ + * After following the authentication routine, which connects this API call to the current signed in account + * and grants the appropriate permissions, this function programmatically creates an arbitrary Google Doc which + * should appear in the user's Google Doc library instantaneously. + * + * @param options the title to assign to the new document, and the information necessary + * to store the new documentId returned from the creation process + * @returns the documentId of the newly generated document, or undefined if the creation process fails. + */ export const create = async (options: CreateOptions): Promise<CreationResult> => { const path = `/googleDocs/Documents/${Actions.Create}`; const parameters = { requestBody: { - title: options.title || `Dash Export (${new Date().toDateString()})` - } + title: options.title || `Dash Export (${new Date().toDateString()})`, + }, }; try { - const schema: docs_v1.Schema$Document = await Networking.PostToServer(path, parameters); + const schema: docsV1.Schema$Document = await Networking.PostToServer(path, parameters); return schema.documentId === null ? undefined : schema.documentId; } catch { return undefined; @@ -91,19 +92,25 @@ export namespace GoogleApiClientUtils { }; export namespace Utils { - - export type ExtractResult = { text: string, paragraphs: DeconstructedParagraph[] }; - export const extractText = (document: docs_v1.Schema$Document, removeNewlines = false): ExtractResult => { + export type ExtractResult = { text: string; paragraphs: DeconstructedParagraph[] }; + export const extractText = (document: docsV1.Schema$Document, removeNewlines = false): ExtractResult => { const paragraphs = extractParagraphs(document); - let text = paragraphs.map(paragraph => paragraph.contents.filter(content => !("inlineObjectId" in content)).map(run => (run as docs_v1.Schema$TextRun).content).join("")).join(""); + let text = paragraphs + .map(paragraph => + paragraph.contents + .filter(content => !('inlineObjectId' in content)) + .map(run => (run as docsV1.Schema$TextRun).content) + .join('') + ) + .join(''); text = text.substring(0, text.length - 1); - removeNewlines && text.replace(/\n/g, ""); + removeNewlines && text.replace(/\n/g, ''); return { text, paragraphs }; }; - export type ContentArray = (docs_v1.Schema$TextRun | docs_v1.Schema$InlineObjectElement)[]; - export type DeconstructedParagraph = { contents: ContentArray, bullet: Opt<number> }; - const extractParagraphs = (document: docs_v1.Schema$Document, filterEmpty = true): DeconstructedParagraph[] => { + export type ContentArray = (docsV1.Schema$TextRun | docsV1.Schema$InlineObjectElement)[]; + export type DeconstructedParagraph = { contents: ContentArray; bullet: Opt<number> }; + const extractParagraphs = (document: docsV1.Schema$Document, filterEmpty = true): DeconstructedParagraph[] => { const fragments: DeconstructedParagraph[] = []; if (document.body && document.body.content) { for (const element of document.body.content) { @@ -132,7 +139,7 @@ export namespace GoogleApiClientUtils { return fragments; }; - export const endOf = (schema: docs_v1.Schema$Document): number | undefined => { + export const endOf = (schema: docsV1.Schema$Document): number | undefined => { if (schema.body && schema.body.content) { const paragraphs = schema.body.content.filter(el => el.paragraph); if (paragraphs.length) { @@ -146,10 +153,10 @@ export namespace GoogleApiClientUtils { } } } + return undefined; }; - export const initialize = async (reference: Reference) => typeof reference === "string" ? reference : create(reference); - + export const initialize = async (reference: Reference) => (typeof reference === 'string' ? reference : create(reference)); } export const retrieve = async (options: RetrieveOptions): Promise<RetrievalResult> => { @@ -168,8 +175,8 @@ export namespace GoogleApiClientUtils { const parameters = { documentId: options.documentId, requestBody: { - requests: options.requests - } + requests: options.requests, + }, }; try { const replies: UpdateResult = await Networking.PostToServer(path, parameters); @@ -179,83 +186,84 @@ export namespace GoogleApiClientUtils { } }; - export const read = async (options: ReadOptions): Promise<Opt<ReadResult>> => { - return retrieve({ documentId: options.documentId }).then(document => { + export const read = async (options: ReadOptions): Promise<Opt<ReadResult>> => + retrieve({ documentId: options.documentId }).then(document => { if (document) { const title = document.title!; const body = Utils.extractText(document, options.removeNewlines).text; return { title, body }; } + return undefined; }); - }; - export const readLines = async (options: ReadOptions): Promise<Opt<ReadLinesResult>> => { - return retrieve({ documentId: options.documentId }).then(document => { + export const readLines = async (options: ReadOptions): Promise<Opt<ReadLinesResult>> => + retrieve({ documentId: options.documentId }).then(document => { if (document) { - const title = document.title; - let bodyLines = Utils.extractText(document).text.split("\n"); + const { title } = document; + let bodyLines = Utils.extractText(document).text.split('\n'); options.removeNewlines && (bodyLines = bodyLines.filter(line => line.length)); - return { title: title ?? "", bodyLines }; + return { title: title ?? '', bodyLines }; } + return undefined; }); - }; export const setStyle = async (options: UpdateOptions) => { const replies: any = await update({ documentId: options.documentId, - requests: options.requests + requests: options.requests, }); - if ("errors" in replies) { - console.log("Write operation failed:"); + if ('errors' in replies) { + console.log('Write operation failed:'); console.log(replies.errors.map((error: any) => error.message)); } return replies; }; export const write = async (options: WriteOptions): Promise<UpdateResult> => { - const requests: docs_v1.Schema$Request[] = []; + const requests: docsV1.Schema$Request[] = []; const documentId = await Utils.initialize(options.reference); if (!documentId) { return undefined; } - let index = options.index; - const mode = options.mode; + let { index } = options; + const { mode } = options; if (!(index && mode === WriteMode.Insert)) { const schema = await retrieve({ documentId }); + // eslint-disable-next-line no-cond-assign if (!schema || !(index = Utils.endOf(schema))) { return undefined; } } if (mode === WriteMode.Replace) { - index > 1 && requests.push({ - deleteContentRange: { - range: { - startIndex: 1, - endIndex: index - } - } - }); + index > 1 && + requests.push({ + deleteContentRange: { + range: { + startIndex: 1, + endIndex: index, + }, + }, + }); index = 1; } - const text = options.content.text; - text.length && requests.push({ - insertText: { - text: isArray(text) ? text.join("\n") : text, - location: { index } - } - }); + const { text } = options.content; + text.length && + requests.push({ + insertText: { + text: isArray(text) ? text.join('\n') : text, + location: { index }, + }, + }); if (!requests.length) { return undefined; } requests.push(...options.content.requests); const replies: any = await update({ documentId, requests }); - if ("errors" in replies) { - console.log("Write operation failed:"); + if ('errors' in replies) { + console.log('Write operation failed:'); console.log(replies.errors.map((error: any) => error.message)); } return replies; }; - } - -}
\ No newline at end of file +} |