@divizend/scratch-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/basic/demo.ts +11 -0
  2. package/basic/index.ts +490 -0
  3. package/core/Auth.ts +63 -0
  4. package/core/Currency.ts +16 -0
  5. package/core/Env.ts +186 -0
  6. package/core/Fragment.ts +43 -0
  7. package/core/FragmentServingMode.ts +37 -0
  8. package/core/JsonSchemaValidator.ts +173 -0
  9. package/core/ProjectRoot.ts +76 -0
  10. package/core/Scratch.ts +44 -0
  11. package/core/URI.ts +203 -0
  12. package/core/Universe.ts +406 -0
  13. package/core/index.ts +27 -0
  14. package/gsuite/core/GSuite.ts +237 -0
  15. package/gsuite/core/GSuiteAdmin.ts +81 -0
  16. package/gsuite/core/GSuiteOrgConfig.ts +47 -0
  17. package/gsuite/core/GSuiteUser.ts +115 -0
  18. package/gsuite/core/index.ts +21 -0
  19. package/gsuite/documents/Document.ts +173 -0
  20. package/gsuite/documents/Documents.ts +52 -0
  21. package/gsuite/documents/index.ts +19 -0
  22. package/gsuite/drive/Drive.ts +118 -0
  23. package/gsuite/drive/DriveFile.ts +147 -0
  24. package/gsuite/drive/index.ts +19 -0
  25. package/gsuite/gmail/Gmail.ts +430 -0
  26. package/gsuite/gmail/GmailLabel.ts +55 -0
  27. package/gsuite/gmail/GmailMessage.ts +428 -0
  28. package/gsuite/gmail/GmailMessagePart.ts +298 -0
  29. package/gsuite/gmail/GmailThread.ts +97 -0
  30. package/gsuite/gmail/index.ts +5 -0
  31. package/gsuite/gmail/utils.ts +184 -0
  32. package/gsuite/index.ts +28 -0
  33. package/gsuite/spreadsheets/CellValue.ts +71 -0
  34. package/gsuite/spreadsheets/Sheet.ts +128 -0
  35. package/gsuite/spreadsheets/SheetValues.ts +12 -0
  36. package/gsuite/spreadsheets/Spreadsheet.ts +76 -0
  37. package/gsuite/spreadsheets/Spreadsheets.ts +52 -0
  38. package/gsuite/spreadsheets/index.ts +25 -0
  39. package/gsuite/spreadsheets/utils.ts +52 -0
  40. package/gsuite/utils.ts +104 -0
  41. package/http-server/HttpServer.ts +110 -0
  42. package/http-server/NativeHttpServer.ts +1084 -0
  43. package/http-server/index.ts +3 -0
  44. package/http-server/middlewares/01-cors.ts +33 -0
  45. package/http-server/middlewares/02-static.ts +67 -0
  46. package/http-server/middlewares/03-request-logger.ts +159 -0
  47. package/http-server/middlewares/04-body-parser.ts +54 -0
  48. package/http-server/middlewares/05-no-cache.ts +23 -0
  49. package/http-server/middlewares/06-response-handler.ts +39 -0
  50. package/http-server/middlewares/handler-wrapper.ts +250 -0
  51. package/http-server/middlewares/index.ts +37 -0
  52. package/http-server/middlewares/types.ts +27 -0
  53. package/index.ts +24 -0
  54. package/package.json +37 -0
  55. package/queue/EmailQueue.ts +228 -0
  56. package/queue/RateLimiter.ts +54 -0
  57. package/queue/index.ts +2 -0
  58. package/resend/Resend.ts +190 -0
  59. package/resend/index.ts +11 -0
  60. package/s2/S2.ts +335 -0
  61. package/s2/index.ts +11 -0
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Documents - Google Docs Service Integration
3
+ *
4
+ * The Documents class provides access to Google Docs functionality
5
+ * through the Google Docs API v1. It serves as the entry point for
6
+ * all document operations within the system.
7
+ *
8
+ * Key Features:
9
+ * - Document opening and access
10
+ * - API client management and authentication
11
+ * - Integration with other Google Workspace services
12
+ * - Support for document editing and manipulation
13
+ *
14
+ * This class enables automated workflows that require document
15
+ * generation, template processing, and content manipulation.
16
+ *
17
+ * @class Documents
18
+ * @version 1.0.0
19
+ * @author Divizend GmbH
20
+ */
21
+
22
+ import { google, docs_v1 } from "googleapis";
23
+ import { JWT } from "google-auth-library";
24
+ import { Universe, Document } from "../..";
25
+
26
+ export class Documents {
27
+ /** Google Docs API v1 client instance */
28
+ public readonly docs: docs_v1.Docs;
29
+
30
+ /**
31
+ * Creates a new Documents instance
32
+ *
33
+ * @param universe - Reference to the central Universe instance
34
+ * @param auth - JWT authentication for Docs access
35
+ */
36
+ constructor(public readonly universe: Universe, public readonly auth: JWT) {
37
+ this.docs = google.docs({ version: "v1", auth: this.auth });
38
+ }
39
+
40
+ /**
41
+ * Opens a document by ID for operations
42
+ *
43
+ * This method creates a Document instance that provides access
44
+ * to the document's content, structure, and editing capabilities.
45
+ *
46
+ * @param documentId - Google Docs document ID
47
+ * @returns Promise<Document> - Document instance for operations
48
+ */
49
+ async open(documentId: string): Promise<Document> {
50
+ return Document.construct(this, documentId);
51
+ }
52
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Google Docs Module - Document Management and Operations
3
+ *
4
+ * This module provides comprehensive Google Docs integration including:
5
+ * - Documents: Main service for document operations
6
+ * - Document: Individual document management and editing
7
+ * - Content manipulation and placeholder replacement
8
+ * - Document formatting and styling
9
+ *
10
+ * The Documents module enables automated document generation,
11
+ * template processing, and content manipulation workflows.
12
+ *
13
+ * @module GoogleDocs
14
+ * @version 1.0.0
15
+ * @author Divizend GmbH
16
+ */
17
+
18
+ export * from "./Documents";
19
+ export * from "./Document";
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Drive - Google Drive Service Integration
3
+ *
4
+ * The Drive class provides comprehensive access to Google Drive functionality
5
+ * including file operations, document copying, and file management. It serves
6
+ * as the primary interface for all Drive-related operations within the system.
7
+ *
8
+ * Key Features:
9
+ * - File opening and access
10
+ * - File copying and organization
11
+ * - Document template processing
12
+ * - Placeholder replacement and customization
13
+ * - Integration with other Google Workspace services
14
+ *
15
+ * This class enables automated workflows that require file manipulation,
16
+ * document generation, and template-based content creation.
17
+ *
18
+ * @class Drive
19
+ * @version 1.0.0
20
+ * @author Divizend GmbH
21
+ */
22
+
23
+ import { google, drive_v3 } from "googleapis";
24
+ import { JWT } from "google-auth-library";
25
+ import { Universe, DriveFile, DocumentPlaceholderReplacements } from "../..";
26
+
27
+ /**
28
+ * Parameters for copying files in Google Drive
29
+ */
30
+ export interface CopyFileParams {
31
+ /** ID of the source file to copy */
32
+ sourceFileId: string;
33
+ /** ID of the destination folder */
34
+ destFolderId: string;
35
+ /** Name for the new copied file */
36
+ name: string;
37
+ }
38
+
39
+ export class Drive {
40
+ /** Google Drive API v3 client instance */
41
+ public readonly drive: drive_v3.Drive;
42
+
43
+ /**
44
+ * Creates a new Drive instance
45
+ *
46
+ * @param universe - Reference to the central Universe instance
47
+ * @param auth - JWT authentication for Drive access
48
+ */
49
+ constructor(private readonly universe: Universe, private auth: JWT) {
50
+ this.drive = google.drive({ version: "v3", auth: this.auth });
51
+ }
52
+
53
+ /**
54
+ * Opens a file in Google Drive by ID
55
+ *
56
+ * This method creates a DriveFile instance that provides access
57
+ * to the file's content, metadata, and operations.
58
+ *
59
+ * @param fileId - Google Drive file ID
60
+ * @returns Promise<DriveFile> - File instance for operations
61
+ */
62
+ async open(fileId: string): Promise<DriveFile> {
63
+ return DriveFile.construct(this, fileId);
64
+ }
65
+
66
+ /**
67
+ * Copies a file to a new location in Google Drive
68
+ *
69
+ * This method creates a copy of the specified file in the destination
70
+ * folder with the new name. The original file remains unchanged.
71
+ *
72
+ * @param copyFileParams - Parameters for the copy operation
73
+ * @returns Promise<DriveFile> - Instance of the copied file
74
+ */
75
+ async copyFile({
76
+ sourceFileId,
77
+ destFolderId,
78
+ name,
79
+ }: CopyFileParams): Promise<DriveFile> {
80
+ const sourceFile = await this.open(sourceFileId);
81
+ return await sourceFile.copy(destFolderId, name);
82
+ }
83
+
84
+ /**
85
+ * Copies a document and optionally processes placeholder replacements
86
+ *
87
+ * This method combines file copying with document processing, making
88
+ * it ideal for template-based document generation workflows. It can
89
+ * automatically replace placeholders in the copied document.
90
+ *
91
+ * @param copyFileParams - Parameters for the copy operation
92
+ * @param replacements - Optional placeholder replacements for the document
93
+ * @returns Promise<DriveFile> - Instance of the processed copied file
94
+ */
95
+ async copyDocument(
96
+ copyFileParams: CopyFileParams,
97
+ replacements?: DocumentPlaceholderReplacements
98
+ ): Promise<DriveFile> {
99
+ const newFile = await this.copyFile(copyFileParams);
100
+ const newDocument = await newFile.asDocument();
101
+
102
+ if (replacements) {
103
+ await newDocument.replacePlaceholders(replacements);
104
+ }
105
+
106
+ return newFile;
107
+ }
108
+
109
+ /**
110
+ * Gets the GSuite user associated with this Drive instance
111
+ *
112
+ * This provides access to other Google Workspace services
113
+ * for the same authenticated user.
114
+ */
115
+ get user() {
116
+ return this.universe.gsuite.user(this.auth.subject!);
117
+ }
118
+ }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * DriveFile - Individual Google Drive File Operations
3
+ *
4
+ * The DriveFile class represents a single file in Google Drive and provides
5
+ * methods for file-specific operations including copying, conversion, and
6
+ * integration with other Google Workspace services.
7
+ *
8
+ * Key Features:
9
+ * - File metadata access and manipulation
10
+ * - File copying and organization
11
+ * - PDF export and conversion
12
+ * - Document integration and processing
13
+ * - Cross-service file operations
14
+ *
15
+ * This class serves as the primary interface for working with individual
16
+ * files in Google Drive, enabling complex file workflows and automation.
17
+ *
18
+ * @class DriveFile
19
+ * @version 1.0.0
20
+ * @author Divizend GmbH
21
+ */
22
+
23
+ import { drive_v3 } from "googleapis";
24
+ import { Drive, Document } from "../..";
25
+
26
+ export class DriveFile {
27
+ /**
28
+ * Creates a new DriveFile instance
29
+ *
30
+ * @param drive - Reference to the Drive service instance
31
+ * @param file - Raw Google Drive API file data
32
+ */
33
+ constructor(
34
+ private readonly drive: Drive,
35
+ public readonly file: drive_v3.Schema$File
36
+ ) {}
37
+
38
+ /**
39
+ * Constructs a DriveFile instance from a file ID
40
+ *
41
+ * This factory method fetches the file metadata from Google Drive
42
+ * and creates a DriveFile instance ready for operations.
43
+ *
44
+ * @param drive - Drive service instance
45
+ * @param fileId - Google Drive file ID
46
+ * @returns Promise<DriveFile> - Initialized file instance
47
+ */
48
+ static async construct(drive: Drive, fileId: string) {
49
+ const file = await drive.drive.files.get({
50
+ fileId,
51
+ supportsAllDrives: true,
52
+ });
53
+ return new DriveFile(drive, file.data);
54
+ }
55
+
56
+ /**
57
+ * Gets the unique identifier for this file
58
+ *
59
+ * The file ID is used for all Google Drive API operations
60
+ * and can be used to reference this file in other services.
61
+ */
62
+ get id(): string {
63
+ return this.file.id!;
64
+ }
65
+
66
+ /**
67
+ * Copies this file to a new location in Google Drive
68
+ *
69
+ * This method creates a copy of the current file in the specified
70
+ * destination folder with the new name. The original file remains
71
+ * unchanged and accessible.
72
+ *
73
+ * @param destFolderId - ID of the destination folder
74
+ * @param name - Name for the copied file
75
+ * @returns Promise<DriveFile> - Instance of the copied file
76
+ */
77
+ async copy(destFolderId: string, name: string) {
78
+ return DriveFile.construct(
79
+ this.drive,
80
+ (
81
+ await this.drive.drive.files.copy({
82
+ fileId: this.id,
83
+ supportsAllDrives: true,
84
+ requestBody: { name, parents: [destFolderId] },
85
+ })
86
+ ).data.id!
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Opens this file as a Google Document for editing
92
+ *
93
+ * This method provides access to the document editing capabilities
94
+ * of Google Docs, enabling content manipulation and placeholder
95
+ * replacement operations.
96
+ *
97
+ * @returns Promise<Document> - Document instance for editing
98
+ */
99
+ async asDocument(): Promise<Document> {
100
+ return this.drive.user.documents().open(this.id);
101
+ }
102
+
103
+ /**
104
+ * Exports this file as a PDF document
105
+ *
106
+ * This method converts the file to PDF format and returns the
107
+ * content as a Buffer. Useful for creating downloadable versions
108
+ * or email attachments.
109
+ *
110
+ * @returns Promise<Buffer> - PDF content as a buffer
111
+ */
112
+ async pdf(): Promise<Buffer> {
113
+ return Buffer.from(
114
+ (
115
+ await this.drive.drive.files.export(
116
+ {
117
+ fileId: this.id,
118
+ mimeType: "application/pdf",
119
+ },
120
+ { responseType: "arraybuffer" }
121
+ )
122
+ ).data as any
123
+ );
124
+ }
125
+
126
+ async download(): Promise<Buffer> {
127
+ const file = (
128
+ await this.drive.drive.files.get(
129
+ {
130
+ fileId: this.id,
131
+ alt: "media",
132
+ },
133
+ { responseType: "stream" }
134
+ )
135
+ ).data;
136
+ const chunks: Buffer[] = [];
137
+ for await (const chunk of file) {
138
+ chunks.push(chunk);
139
+ }
140
+ return Buffer.concat(chunks);
141
+ }
142
+
143
+ async text(): Promise<string> {
144
+ const download = await this.download();
145
+ return download.toString("utf8");
146
+ }
147
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Google Drive Module - File Management and Operations
3
+ *
4
+ * This module provides comprehensive Google Drive integration including:
5
+ * - Drive: Main Drive service and file operations
6
+ * - DriveFile: Individual file management and manipulation
7
+ * - File copying, moving, and organization
8
+ * - Folder management and file sharing
9
+ *
10
+ * The Drive module enables seamless file operations within Google Workspace,
11
+ * supporting automated workflows that require file management capabilities.
12
+ *
13
+ * @module GoogleDrive
14
+ * @version 1.0.0
15
+ * @author Divizend GmbH
16
+ */
17
+
18
+ export * from "./Drive";
19
+ export * from "./DriveFile";