diff options
Diffstat (limited to 'src/client/apis/google_docs/GoogleApiClientUtils.ts')
-rw-r--r-- | src/client/apis/google_docs/GoogleApiClientUtils.ts | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/src/client/apis/google_docs/GoogleApiClientUtils.ts b/src/client/apis/google_docs/GoogleApiClientUtils.ts index 3c2f923ea..0b303eacf 100644 --- a/src/client/apis/google_docs/GoogleApiClientUtils.ts +++ b/src/client/apis/google_docs/GoogleApiClientUtils.ts @@ -1,4 +1,7 @@ -import { docs_v1 } from 'googleapis'; +/* 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'; @@ -15,12 +18,12 @@ export namespace GoogleApiClientUtils { } 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 { @@ -32,7 +35,7 @@ export namespace GoogleApiClientUtils { 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>; @@ -81,7 +84,7 @@ export namespace GoogleApiClientUtils { }, }; 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; @@ -90,13 +93,13 @@ 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 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) + .map(run => (run as docsV1.Schema$TextRun).content) .join('') ) .join(''); @@ -105,9 +108,9 @@ export namespace GoogleApiClientUtils { return { text, paragraphs }; }; - export type ContentArray = (docs_v1.Schema$TextRun | docs_v1.Schema$InlineObjectElement)[]; + export type ContentArray = (docsV1.Schema$TextRun | docsV1.Schema$InlineObjectElement)[]; export type DeconstructedParagraph = { contents: ContentArray; bullet: Opt<number> }; - const extractParagraphs = (document: docs_v1.Schema$Document, filterEmpty = true): DeconstructedParagraph[] => { + 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) { @@ -136,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) { @@ -150,6 +153,7 @@ export namespace GoogleApiClientUtils { } } } + return undefined; }; export const initialize = async (reference: Reference) => (typeof reference === 'string' ? reference : create(reference)); @@ -182,26 +186,26 @@ 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; + const { title } = document; let bodyLines = Utils.extractText(document).text.split('\n'); options.removeNewlines && (bodyLines = bodyLines.filter(line => line.length)); return { title: title ?? '', bodyLines }; } + return undefined; }); - }; export const setStyle = async (options: UpdateOptions) => { const replies: any = await update({ @@ -216,15 +220,16 @@ export namespace GoogleApiClientUtils { }; 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; } @@ -241,7 +246,7 @@ export namespace GoogleApiClientUtils { }); index = 1; } - const text = options.content.text; + const { text } = options.content; text.length && requests.push({ insertText: { |