Googledocsservice
Google Docs Service
Description
- The
GoogleDocsServiceclass is a lightweight client for interacting with a Google Apps Script web app endpoint, primarily used for creatingGoogle Docsvia a backend script.
Overview
This service provides a method to send document creation requests to a predefined Google Apps Script API. It wraps the AJAX request logic and encapsulates query parameter preparation.
Class : GoogleDocsService
- Properties
docApi: string: The URL of the Google Apps Script web app endpoint. This is where all document creation requests are sent.q: Record<string, any>: A generic object to hold query parameters that will be included in the request.
Methods
private builderQueryParams(payload: any): void- Description: Prepares query parameters for the request by assigning the given payload to the internal q object.
- Parameters:
- payload (any): The data to be sent in the request. It can be any structure expected by the backend API.
- Returns:
void
async createDocument(payload: any): Promise<any>
- Description:
- Sends a POST request to the Google Apps Script endpoint to initiate the creation of a Google Document.
- Parameters:
payload(any): The data to be sent to the Google Apps Script, such as document title, content, metadata, etc.
- Returns:
A
Promise<any>resolving with the response from the server. - Usage:
const service = new GoogleDocsService();
const response = await service.createDocument({
title: "Sample Document",
content: "This is the content of the document."
});
Dependencies
useAjaxServer: This function is expected to be an abstraction for making AJAX requests. It should support the parametersmethodandquery.
Example
const docService = new GoogleDocsService();
docService.createDocument({
title: "My First Doc",
body: "This is the body of the Google Doc."
}).then(response => {
console.log("Document created:", response);
}).catch(error => {
console.error("Error creating document:", error);
});