@processfocus/plugin-google-drive 0.1.0-next.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.
- package/LICENSE.md +28 -0
- package/README.md +161 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/browser.d.ts +2 -0
- package/dist/lib/browser.js +4 -0
- package/dist/lib/browser.js.map +1 -0
- package/dist/lib/drive-picker-element-loader.d.ts +3 -0
- package/dist/lib/drive-picker-element-loader.js +4 -0
- package/dist/lib/drive-picker-element-loader.js.map +1 -0
- package/dist/lib/google-drive-annotations.d.ts +10 -0
- package/dist/lib/google-drive-annotations.js +11 -0
- package/dist/lib/google-drive-annotations.js.map +1 -0
- package/dist/lib/google-drive-browser-export.d.ts +37 -0
- package/dist/lib/google-drive-browser-export.js +134 -0
- package/dist/lib/google-drive-browser-export.js.map +1 -0
- package/dist/lib/google-drive-download.d.ts +15 -0
- package/dist/lib/google-drive-download.js +9 -0
- package/dist/lib/google-drive-download.js.map +1 -0
- package/dist/lib/google-drive-field.d.ts +71 -0
- package/dist/lib/google-drive-field.js +89 -0
- package/dist/lib/google-drive-field.js.map +1 -0
- package/dist/lib/google-drive-oauth.d.ts +10 -0
- package/dist/lib/google-drive-oauth.js +13 -0
- package/dist/lib/google-drive-oauth.js.map +1 -0
- package/dist/lib/google-drive-picker-config.d.ts +12 -0
- package/dist/lib/google-drive-picker-config.js +28 -0
- package/dist/lib/google-drive-picker-config.js.map +1 -0
- package/dist/lib/google-drive-picker.d.ts +92 -0
- package/dist/lib/google-drive-picker.js +324 -0
- package/dist/lib/google-drive-picker.js.map +1 -0
- package/dist/lib/google-drive-provider.d.ts +5 -0
- package/dist/lib/google-drive-provider.js +6 -0
- package/dist/lib/google-drive-provider.js.map +1 -0
- package/dist/lib/google-drive.d.ts +16 -0
- package/dist/lib/google-drive.js +35 -0
- package/dist/lib/google-drive.js.map +1 -0
- package/dist/lib/picker-token-cache.d.ts +15 -0
- package/dist/lib/picker-token-cache.js +25 -0
- package/dist/lib/picker-token-cache.js.map +1 -0
- package/dist/lib/plugin-data.d.ts +8 -0
- package/dist/lib/plugin-data.js +1 -0
- package/dist/lib/plugin-data.js.map +1 -0
- package/dist/lib/plugin-identity.d.ts +2 -0
- package/dist/lib/plugin-identity.js +3 -0
- package/dist/lib/plugin-identity.js.map +1 -0
- package/dist/lib/prepare-client.d.ts +3 -0
- package/dist/lib/prepare-client.js +4 -0
- package/dist/lib/prepare-client.js.map +1 -0
- package/dist/lib/register-client.d.ts +10 -0
- package/dist/lib/register-client.js +83 -0
- package/dist/lib/register-client.js.map +1 -0
- package/dist/lib/register-walker.d.ts +16 -0
- package/dist/lib/register-walker.js +59 -0
- package/dist/lib/register-walker.js.map +1 -0
- package/dist/lib/types.d.ts +42 -0
- package/dist/lib/types.js +4 -0
- package/dist/lib/types.js.map +1 -0
- package/package.json +70 -0
- package/styles.css +16 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# O'Saasy License Agreement
|
|
2
|
+
|
|
3
|
+
Copyright © 2026, Xplain Technology Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
1. The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
2. No licensee or downstream recipient may use the Software (including
|
|
16
|
+
any modified or derivative versions) to directly compete with the
|
|
17
|
+
original Licensor by offering it to third parties as a hosted,
|
|
18
|
+
managed, or Software-as-a-Service (SaaS) product or cloud service
|
|
19
|
+
where the primary value of the service is the functionality of the
|
|
20
|
+
Software itself.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
23
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
24
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
25
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
26
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
27
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
28
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# @processfocus/plugin-google-drive
|
|
2
|
+
|
|
3
|
+
Google Drive file picker plugin for Process Focus forms.
|
|
4
|
+
|
|
5
|
+
## Environment Variables
|
|
6
|
+
|
|
7
|
+
The server-side form walker reads these public Google Picker values from the
|
|
8
|
+
runtime environment and sends them to the form renderer through field
|
|
9
|
+
`pluginData`. They are intentionally client-visible Picker configuration, not
|
|
10
|
+
secret credentials. Next.js does not embed them at build time.
|
|
11
|
+
|
|
12
|
+
| Variable | Description |
|
|
13
|
+
|---|---|
|
|
14
|
+
| `GOOGLE_DRIVE_PICKER_CLIENT_ID` | Google OAuth 2.0 client ID (public) |
|
|
15
|
+
| `GOOGLE_DRIVE_PICKER_APP_ID` | Google Cloud project number / app ID (public) |
|
|
16
|
+
| `GOOGLE_DRIVE_PICKER_DEVELOPER_KEY` | Browser-restricted API key for Google Picker (public) |
|
|
17
|
+
|
|
18
|
+
Environment configuration changes require a redeploy so the runtime receives
|
|
19
|
+
the new values.
|
|
20
|
+
|
|
21
|
+
Google Cloud must have the Google Drive API and Google Picker API enabled. The
|
|
22
|
+
OAuth client must allow the Dashboard origin. Restrict the developer key to the
|
|
23
|
+
Dashboard's browser origins and the Google Picker API. The picker requests only
|
|
24
|
+
`https://www.googleapis.com/auth/drive.file`.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Process model
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import {
|
|
32
|
+
GoogleDrive,
|
|
33
|
+
GoogleDriveField,
|
|
34
|
+
parseGoogleDriveGeneratedPdf,
|
|
35
|
+
} from "@processfocus/plugin-google-drive"
|
|
36
|
+
|
|
37
|
+
new GoogleDrive(org, "google-drive")
|
|
38
|
+
|
|
39
|
+
// In a form schema
|
|
40
|
+
const schema = {
|
|
41
|
+
document: GoogleDriveField({
|
|
42
|
+
label: "Tramping plan",
|
|
43
|
+
generatePdf: pdfStore,
|
|
44
|
+
}),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// In a later process consumer
|
|
48
|
+
const document = parseGoogleDriveGeneratedPdf(state.document)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This is the canonical pattern for process documents selected from Google Drive.
|
|
52
|
+
The browser exports the selected file as PDF with the user's in-memory OAuth
|
|
53
|
+
token, requests a short-lived signed upload URL, and uploads directly to the
|
|
54
|
+
configured `DocumentStore` before the form is submitted. The form value contains
|
|
55
|
+
only the stored PDF's non-empty `{ fileId, filename }` reference. Process models
|
|
56
|
+
use `parseGoogleDriveGeneratedPdf` to validate and consume that reference
|
|
57
|
+
directly; no additional system step is needed.
|
|
58
|
+
|
|
59
|
+
Without `generatePdf`, use `parseGoogleDriveValue` to validate and read the
|
|
60
|
+
selected file metadata in a later step.
|
|
61
|
+
|
|
62
|
+
### Organisation artifact registration
|
|
63
|
+
|
|
64
|
+
`new GoogleDrive(org, "google-drive")` declares the browser renderer and its
|
|
65
|
+
build-time Tailwind stylesheet as organisation artifact inputs. `pfcli build`
|
|
66
|
+
emits both as content-addressed, integrity-checked files. Dashboard and public
|
|
67
|
+
routes consume those emitted files without resolving this package from the
|
|
68
|
+
host runtime.
|
|
69
|
+
|
|
70
|
+
Importing `GoogleDriveField` registers the walker beside the organisation's
|
|
71
|
+
form model, so bundled form projection uses the registry in the organisation
|
|
72
|
+
bundle realm. Local and AWS hosts do not import this plugin.
|
|
73
|
+
|
|
74
|
+
### Low-level frontend registration
|
|
75
|
+
|
|
76
|
+
The Dashboard's organisation plugin composition activates the browser entrypoint
|
|
77
|
+
once before any form renders and injects its host capabilities:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { organisationFrontendPlugin } from "@processfocus/plugin-google-drive/register-client"
|
|
81
|
+
|
|
82
|
+
organisationFrontendPlugin.activate(host)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This registers the React renderer without importing Dashboard or form-registry
|
|
86
|
+
source. React and the frontend plugin host contract remain host-provided. The
|
|
87
|
+
picker is code-split and only loaded when a Google Drive field is rendered.
|
|
88
|
+
Schema walking remains a separate server entrypoint.
|
|
89
|
+
|
|
90
|
+
The Dashboard wraps authenticated form trees in its organisation plugin host
|
|
91
|
+
implementation so `generatePdf` fields can request signed document-store
|
|
92
|
+
upload URLs through authenticated GraphQL.
|
|
93
|
+
|
|
94
|
+
### Server-only (walker only)
|
|
95
|
+
|
|
96
|
+
If you only need schema walking without React (e.g. server-side):
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { registerGoogleDriveWalkerPlugin } from "@processfocus/plugin-google-drive/register-walker"
|
|
100
|
+
|
|
101
|
+
registerGoogleDriveWalkerPlugin()
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Content Security Policy
|
|
105
|
+
|
|
106
|
+
The plugin loads two external Google scripts at runtime. Your CSP must
|
|
107
|
+
allow at minimum:
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
script-src https://accounts.google.com https://apis.google.com;
|
|
111
|
+
frame-src https://accounts.google.com https://docs.google.com https://drive.google.com;
|
|
112
|
+
connect-src https://accounts.google.com https://www.googleapis.com https://docs.google.com https://*.googleusercontent.com;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Subresource Integrity (SRI) hashes are not used because both Google
|
|
116
|
+
endpoints serve version-less bundles whose content changes without notice.
|
|
117
|
+
|
|
118
|
+
## How it works
|
|
119
|
+
|
|
120
|
+
1. `GoogleDriveField` creates an Effect Schema string field annotated with
|
|
121
|
+
`FormGoogleDriveInput`.
|
|
122
|
+
2. The walker plugin recognises this annotation and emits a `PluginField`
|
|
123
|
+
node. Public Picker configuration is read from the server runtime and
|
|
124
|
+
sent to the renderer as field `pluginData`.
|
|
125
|
+
3. The renderer plugin lazy-loads `GoogleDrivePicker`, which handles its
|
|
126
|
+
own OAuth consent via Google Identity Services (the app's login token
|
|
127
|
+
only has profile/email scopes, not Drive). Scope is
|
|
128
|
+
`https://www.googleapis.com/auth/drive.file`. GIS is hinted with the
|
|
129
|
+
signed-in email and does not force consent after the user has granted
|
|
130
|
+
Drive access.
|
|
131
|
+
4. Without `generatePdf`, the selected file metadata is stored as a JSON
|
|
132
|
+
string in the form field.
|
|
133
|
+
5. With `generatePdf`, the browser uses the in-memory token to export the first
|
|
134
|
+
tab of a Google Doc through the Docs download endpoint, falling back to the
|
|
135
|
+
Drive export endpoint if needed. Sheets, Slides, and Drawings use Drive
|
|
136
|
+
export, while existing PDFs use the Drive media-download endpoint.
|
|
137
|
+
6. The browser requests a short-lived, step-authorized upload URL and uploads
|
|
138
|
+
the PDF bytes directly to the configured `DocumentStore`.
|
|
139
|
+
7. Form and process state receive only the stored PDF's `{ fileId, filename }`
|
|
140
|
+
reference. Process models parse this artifact where an attachment or other
|
|
141
|
+
consumer needs it.
|
|
142
|
+
8. `parseGoogleDriveGeneratedPdf` validates generated-PDF references, while
|
|
143
|
+
`parseGoogleDriveValue` validates and parses metadata-only values using
|
|
144
|
+
Effect Schema in downstream process steps.
|
|
145
|
+
|
|
146
|
+
## Credentials
|
|
147
|
+
|
|
148
|
+
OAuth bearer tokens are kept only in browser module memory for the active page.
|
|
149
|
+
An expiry-aware cache lets remounted picker fields reuse a token until one
|
|
150
|
+
minute before expiry, and plugin cleanup clears all cached tokens. Tokens must
|
|
151
|
+
not be written to browser storage, form values, process state, GraphQL
|
|
152
|
+
variables, queues, logs, or database rows. Google export URLs and browser blob
|
|
153
|
+
URLs must not be persisted either. Persist only the document-store `fileId` and
|
|
154
|
+
safe display metadata such as `filename`.
|
|
155
|
+
|
|
156
|
+
## Supported PDF sources
|
|
157
|
+
|
|
158
|
+
PDF generation accepts Google Docs, Sheets, Slides, Drawings, and existing PDF
|
|
159
|
+
files. The picker filters out other MIME types. If another Google Drive type is
|
|
160
|
+
needed, first confirm that Drive can export it as `application/pdf`; do not add
|
|
161
|
+
a server-side token transfer as a workaround.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { GoogleDrive } from "./lib/google-drive.js";
|
|
2
|
+
export type { DrivePickerFile, GoogleDriveFieldOptions, GoogleDriveGeneratedPdf, } from "./lib/google-drive-field.js";
|
|
3
|
+
export { DrivePickerFileSchema, FormGoogleDriveGeneratePdf, FormGoogleDriveInput, GoogleDriveField, GoogleDriveGeneratedPdfSchema, isGoogleDriveGeneratedPdf, parseGoogleDriveGeneratedPdf, parseGoogleDriveValue, } from "./lib/google-drive-field.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { GoogleDrive } from "./lib/google-drive.js";
|
|
2
|
+
export { DrivePickerFileSchema, FormGoogleDriveGeneratePdf, FormGoogleDriveInput, GoogleDriveField, GoogleDriveGeneratedPdfSchema, isGoogleDriveGeneratedPdf, parseGoogleDriveGeneratedPdf, parseGoogleDriveValue, } from "./lib/google-drive-field.js";
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAMhD,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC1B,oBAAoB,EACpB,gBAAgB,EAChB,6BAA6B,EAC7B,yBAAyB,EACzB,4BAA4B,EAC5B,qBAAqB,GACtB,MAAM,0BAA0B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/lib/browser.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,MAAM,CAAC,MAAM,cAAc,GAAG,GAAS,EAAE,CAAC,IAAI,CAAA;AAC9C,OAAO,EAAE,0BAA0B,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
const LOAD_ERROR_MESSAGE = "Failed to load Google Drive picker component";
|
|
2
|
+
const importDrivePickerElement = () => import("@googleworkspace/drive-picker-element");
|
|
3
|
+
export const loadDrivePickerElement = (onLoaded, onError, importElement = importDrivePickerElement) => importElement().then(onLoaded, () => onError(LOAD_ERROR_MESSAGE));
|
|
4
|
+
//# sourceMappingURL=drive-picker-element-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drive-picker-element-loader.js","sourceRoot":"","sources":["../../src/lib/drive-picker-element-loader.ts"],"names":[],"mappings":"AAAA,MAAM,kBAAkB,GAAG,8CAA8C,CAAA;AAIzE,MAAM,wBAAwB,GAA6B,GAAG,EAAE,CAC9D,MAAM,CAAC,uCAAuC,CAAC,CAAA;AAEjD,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,QAAoB,EACpB,OAAkC,EAClC,aAAa,GAA6B,wBAAwB,EACnD,EAAE,CACjB,aAAa,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin-owned annotation symbol for Google Drive file selection fields.
|
|
3
|
+
* This symbol is not in core — it belongs entirely to the plugin.
|
|
4
|
+
*/
|
|
5
|
+
export declare const FormGoogleDriveInput: unique symbol;
|
|
6
|
+
/**
|
|
7
|
+
* Annotation symbol indicating this field should generate a PDF export.
|
|
8
|
+
* Its metadata identifies the DocumentStore that receives the browser export.
|
|
9
|
+
*/
|
|
10
|
+
export declare const FormGoogleDriveGeneratePdf: unique symbol;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin-owned annotation symbol for Google Drive file selection fields.
|
|
3
|
+
* This symbol is not in core — it belongs entirely to the plugin.
|
|
4
|
+
*/
|
|
5
|
+
export const FormGoogleDriveInput = Symbol.for("pf/form/annotation/GoogleDriveInput");
|
|
6
|
+
/**
|
|
7
|
+
* Annotation symbol indicating this field should generate a PDF export.
|
|
8
|
+
* Its metadata identifies the DocumentStore that receives the browser export.
|
|
9
|
+
*/
|
|
10
|
+
export const FormGoogleDriveGeneratePdf = Symbol.for("pf/form/annotation/GoogleDriveGeneratePdf");
|
|
11
|
+
//# sourceMappingURL=google-drive-annotations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-drive-annotations.js","sourceRoot":"","sources":["../../src/lib/google-drive-annotations.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAC5C,qCAAqC,CACtC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC,GAAG,CAClD,2CAA2C,CAC5C,CAAA"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare const GOOGLE_DRIVE_PDF_MIME_TYPES: readonly ["application/vnd.google-apps.document", "application/vnd.google-apps.spreadsheet", "application/vnd.google-apps.presentation", "application/vnd.google-apps.drawing", "application/pdf"];
|
|
2
|
+
interface GoogleDriveSelectedFile {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly mimeType: string;
|
|
6
|
+
readonly url: string;
|
|
7
|
+
}
|
|
8
|
+
interface UploadUrlRequest {
|
|
9
|
+
readonly stepPath: string;
|
|
10
|
+
readonly documentStore: string;
|
|
11
|
+
readonly contentType: string;
|
|
12
|
+
readonly filename: string;
|
|
13
|
+
}
|
|
14
|
+
interface UploadUrlResult {
|
|
15
|
+
readonly fileId: string;
|
|
16
|
+
readonly uploadUrl: string;
|
|
17
|
+
}
|
|
18
|
+
interface ExportGoogleDriveFileInput {
|
|
19
|
+
readonly file: GoogleDriveSelectedFile;
|
|
20
|
+
readonly accessToken: string;
|
|
21
|
+
readonly stepPath: string;
|
|
22
|
+
readonly documentStore: string;
|
|
23
|
+
readonly requestUploadUrl: (input: UploadUrlRequest) => Promise<UploadUrlResult>;
|
|
24
|
+
readonly deleteFile: (fileId: string) => Promise<void>;
|
|
25
|
+
readonly fetch?: (url: string, init?: RequestInit | undefined) => Promise<Response>;
|
|
26
|
+
}
|
|
27
|
+
export interface GeneratedPdfArtifact {
|
|
28
|
+
readonly fileId: string;
|
|
29
|
+
readonly filename: string;
|
|
30
|
+
}
|
|
31
|
+
export type GoogleDriveBrowserExportErrorCode = "drive-network" | "drive-authorization" | "drive-access" | "drive-export" | "upload-preparation" | "upload-network" | "upload-rejected";
|
|
32
|
+
export declare class GoogleDriveBrowserExportError extends Error {
|
|
33
|
+
readonly code: GoogleDriveBrowserExportErrorCode;
|
|
34
|
+
constructor(code: GoogleDriveBrowserExportErrorCode, message: string);
|
|
35
|
+
}
|
|
36
|
+
export declare const exportGoogleDriveFileToDocumentStore: ({ file, accessToken, stepPath, documentStore, requestUploadUrl, deleteFile, fetch: fetchRequest, }: ExportGoogleDriveFileInput) => Promise<GeneratedPdfArtifact>;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
const GOOGLE_DRIVE_API_BASE = "https://www.googleapis.com/drive/v3/files";
|
|
2
|
+
const GOOGLE_DOCUMENT_MIME_TYPE = "application/vnd.google-apps.document";
|
|
3
|
+
const PDF_MIME_TYPE = "application/pdf";
|
|
4
|
+
export const GOOGLE_DRIVE_PDF_MIME_TYPES = [
|
|
5
|
+
GOOGLE_DOCUMENT_MIME_TYPE,
|
|
6
|
+
"application/vnd.google-apps.spreadsheet",
|
|
7
|
+
"application/vnd.google-apps.presentation",
|
|
8
|
+
"application/vnd.google-apps.drawing",
|
|
9
|
+
PDF_MIME_TYPE,
|
|
10
|
+
];
|
|
11
|
+
const googleDrivePdfMimeTypes = new Set(GOOGLE_DRIVE_PDF_MIME_TYPES);
|
|
12
|
+
export class GoogleDriveBrowserExportError extends Error {
|
|
13
|
+
code;
|
|
14
|
+
constructor(code, message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "GoogleDriveBrowserExportError";
|
|
17
|
+
this.code = code;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const pdfFilename = (name) => {
|
|
21
|
+
const baseName = name.replace(/\.pdf$/i, "");
|
|
22
|
+
return `${baseName || "document"}.pdf`;
|
|
23
|
+
};
|
|
24
|
+
const driveExportUrl = (file) => {
|
|
25
|
+
const fileUrl = `${GOOGLE_DRIVE_API_BASE}/${encodeURIComponent(file.id)}`;
|
|
26
|
+
if (file.mimeType === PDF_MIME_TYPE) {
|
|
27
|
+
return `${fileUrl}?alt=media`;
|
|
28
|
+
}
|
|
29
|
+
if (googleDrivePdfMimeTypes.has(file.mimeType)) {
|
|
30
|
+
return `${fileUrl}/export?mimeType=${encodeURIComponent("application/pdf")}`;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Select a Google document, spreadsheet, presentation, drawing, or PDF.");
|
|
33
|
+
};
|
|
34
|
+
const driveDownloadUrl = (file) => file.mimeType === GOOGLE_DOCUMENT_MIME_TYPE
|
|
35
|
+
? `https://docs.google.com/document/d/${encodeURIComponent(file.id)}/export?format=pdf&tab=t.0`
|
|
36
|
+
: driveExportUrl(file);
|
|
37
|
+
const isPdfResponse = (response) => response.ok &&
|
|
38
|
+
response.headers
|
|
39
|
+
.get("content-type")
|
|
40
|
+
?.split(";", 1)[0]
|
|
41
|
+
?.trim()
|
|
42
|
+
.toLowerCase() === PDF_MIME_TYPE;
|
|
43
|
+
const driveExportError = (status) => {
|
|
44
|
+
switch (status) {
|
|
45
|
+
case 401:
|
|
46
|
+
return new GoogleDriveBrowserExportError("drive-authorization", "Google Drive authorization expired. Select Try again to reauthorize.");
|
|
47
|
+
case 403:
|
|
48
|
+
return new GoogleDriveBrowserExportError("drive-access", "Google Drive did not grant access to export this file. Select Try again to reauthorize and choose it again.");
|
|
49
|
+
case 404:
|
|
50
|
+
return new GoogleDriveBrowserExportError("drive-export", "The selected Google Drive file is no longer available.");
|
|
51
|
+
case 429:
|
|
52
|
+
return new GoogleDriveBrowserExportError("drive-export", "Google Drive is temporarily rate-limited. Please try again shortly.");
|
|
53
|
+
default:
|
|
54
|
+
return new GoogleDriveBrowserExportError("drive-export", `Google Drive could not export the selected file (${status}).`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const downloadPdf = async ({ file, accessToken, fetchRequest, }) => {
|
|
58
|
+
const authorization = { Authorization: `Bearer ${accessToken}` };
|
|
59
|
+
if (file.mimeType === GOOGLE_DOCUMENT_MIME_TYPE) {
|
|
60
|
+
try {
|
|
61
|
+
const documentResponse = await fetchRequest(driveDownloadUrl(file), {
|
|
62
|
+
headers: authorization,
|
|
63
|
+
});
|
|
64
|
+
if (isPdfResponse(documentResponse)) {
|
|
65
|
+
try {
|
|
66
|
+
return await documentResponse.blob();
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// Fall back to the supported Drive export endpoint below.
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// A rejected request also means the undocumented endpoint failed. Let
|
|
75
|
+
// the supported Drive fallback determine any user-facing error.
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
let driveResponse;
|
|
79
|
+
try {
|
|
80
|
+
driveResponse = await fetchRequest(driveExportUrl(file), {
|
|
81
|
+
headers: authorization,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
throw new GoogleDriveBrowserExportError("drive-network", "Your browser could not connect to Google Drive. Check your connection and try again.");
|
|
86
|
+
}
|
|
87
|
+
if (!driveResponse.ok) {
|
|
88
|
+
throw driveExportError(driveResponse.status);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return await driveResponse.blob();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
throw new GoogleDriveBrowserExportError("drive-export", "The exported Google Drive file could not be read. Please try again.");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
export const exportGoogleDriveFileToDocumentStore = async ({ file, accessToken, stepPath, documentStore, requestUploadUrl, deleteFile, fetch: fetchRequest = fetch, }) => {
|
|
98
|
+
const filename = pdfFilename(file.name);
|
|
99
|
+
const pdf = await downloadPdf({ file, accessToken, fetchRequest });
|
|
100
|
+
let upload;
|
|
101
|
+
try {
|
|
102
|
+
upload = await requestUploadUrl({
|
|
103
|
+
stepPath,
|
|
104
|
+
documentStore,
|
|
105
|
+
contentType: "application/pdf",
|
|
106
|
+
filename,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
throw new GoogleDriveBrowserExportError("upload-preparation", "Process Focus could not prepare the PDF upload. Please try again.");
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
let uploadResponse;
|
|
114
|
+
try {
|
|
115
|
+
uploadResponse = await fetchRequest(upload.uploadUrl, {
|
|
116
|
+
method: "PUT",
|
|
117
|
+
headers: { "Content-Type": "application/pdf" },
|
|
118
|
+
body: pdf,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
throw new GoogleDriveBrowserExportError("upload-network", "Your browser could not upload the exported PDF. Check your connection and try again.");
|
|
123
|
+
}
|
|
124
|
+
if (!uploadResponse.ok) {
|
|
125
|
+
throw new GoogleDriveBrowserExportError("upload-rejected", `PDF upload failed (${uploadResponse.status}). Please try again.`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
await deleteFile(upload.fileId).catch(() => undefined);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
return { fileId: upload.fileId, filename };
|
|
133
|
+
};
|
|
134
|
+
//# sourceMappingURL=google-drive-browser-export.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-drive-browser-export.js","sourceRoot":"","sources":["../../src/lib/google-drive-browser-export.ts"],"names":[],"mappings":"AAAA,MAAM,qBAAqB,GAAG,2CAA2C,CAAA;AACzE,MAAM,yBAAyB,GAAG,sCAAsC,CAAA;AACxE,MAAM,aAAa,GAAG,iBAAiB,CAAA;AAEvC,MAAM,CAAC,MAAM,2BAA2B,GAAG;IACzC,yBAAyB;IACzB,yCAAyC;IACzC,0CAA0C;IAC1C,qCAAqC;IACrC,aAAa;CACL,CAAA;AAEV,MAAM,uBAAuB,GAAwB,IAAI,GAAG,CAC1D,2BAA2B,CAC5B,CAAA;AAkDD,MAAM,OAAO,6BAA8B,SAAQ,KAAK;IAC7C,IAAI,CAAmC;IAEhD,YAAY,IAAuC,EAAE,OAAe;QAClE,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,+BAA+B,CAAA;QAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF;AAED,MAAM,WAAW,GAAG,CAAC,IAAY,EAAU,EAAE;IAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAA;IAC5C,OAAO,GAAG,QAAQ,IAAI,UAAU,MAAM,CAAA;AACxC,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,CAAC,IAA6B,EAAU,EAAE;IA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;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QACtD,MAAM,KAAK,CAAA;IACb,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAA;AAC5C,CAAC,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface OpenStoredGoogleDrivePdfInput {
|
|
2
|
+
readonly stepPath: string;
|
|
3
|
+
readonly documentStore: string;
|
|
4
|
+
readonly fileId: string;
|
|
5
|
+
readonly requestDownloadUrl: (input: {
|
|
6
|
+
readonly stepPath: string;
|
|
7
|
+
readonly documentStore: string;
|
|
8
|
+
readonly fileId: string;
|
|
9
|
+
}) => Promise<{
|
|
10
|
+
readonly downloadUrl: string;
|
|
11
|
+
}>;
|
|
12
|
+
readonly openUrl: (url: string) => void;
|
|
13
|
+
}
|
|
14
|
+
export declare const openStoredGoogleDrivePdf: ({ stepPath, documentStore, fileId, requestDownloadUrl, openUrl, }: OpenStoredGoogleDrivePdfInput) => Promise<void>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const openStoredGoogleDrivePdf = async ({ stepPath, documentStore, fileId, requestDownloadUrl, openUrl, }) => {
|
|
2
|
+
const { downloadUrl } = await requestDownloadUrl({
|
|
3
|
+
stepPath,
|
|
4
|
+
documentStore,
|
|
5
|
+
fileId,
|
|
6
|
+
});
|
|
7
|
+
openUrl(downloadUrl);
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=google-drive-download.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-drive-download.js","sourceRoot":"","sources":["../../src/lib/google-drive-download.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,EAAE,EAC7C,QAAQ,EACR,aAAa,EACb,MAAM,EACN,kBAAkB,EAClB,OAAO,GACuB,EAAiB,EAAE;IACjD,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,kBAAkB,CAAC;QAC/C,QAAQ;QACR,aAAa;QACb,MAAM;KACP,CAAC,CAAA;IACF,OAAO,CAAC,WAAW,CAAC,CAAA;AACtB,CAAC,CAAA"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { type FieldOptions } from "processfocus/forms";
|
|
3
|
+
import { FormGoogleDriveGeneratePdf, FormGoogleDriveInput } from "./google-drive-annotations.js";
|
|
4
|
+
export { FormGoogleDriveGeneratePdf, FormGoogleDriveInput };
|
|
5
|
+
/**
|
|
6
|
+
* Extended options for GoogleDriveField.
|
|
7
|
+
*
|
|
8
|
+
* When `generatePdf` is set to a DocumentStore construct, the browser exports
|
|
9
|
+
* the selected Drive file and uploads the PDF directly to that store. The
|
|
10
|
+
* OAuth token remains in browser memory.
|
|
11
|
+
*/
|
|
12
|
+
export interface GoogleDriveFieldOptions extends FieldOptions<string> {
|
|
13
|
+
/**
|
|
14
|
+
* Pass the DocumentStore that should receive the browser-generated PDF.
|
|
15
|
+
* The field value stores the resulting file id and filename, never the
|
|
16
|
+
* Google OAuth token.
|
|
17
|
+
*/
|
|
18
|
+
readonly generatePdf?: {
|
|
19
|
+
readonly node: {
|
|
20
|
+
readonly path: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Stored document reference produced by the browser PDF upload flow. */
|
|
25
|
+
export declare const GoogleDriveGeneratedPdfSchema: Schema.Struct<{
|
|
26
|
+
fileId: typeof Schema.NonEmptyString;
|
|
27
|
+
filename: typeof Schema.NonEmptyString;
|
|
28
|
+
}>;
|
|
29
|
+
export type GoogleDriveGeneratedPdf = typeof GoogleDriveGeneratedPdfSchema.Type;
|
|
30
|
+
/** Parse a generated-PDF form value into its stored document reference. */
|
|
31
|
+
export declare const parseGoogleDriveGeneratedPdf: (value: string) => GoogleDriveGeneratedPdf;
|
|
32
|
+
/** Return whether a form value is exactly a generated-PDF document reference. */
|
|
33
|
+
export declare const isGoogleDriveGeneratedPdf: (value: string) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Create a Google Drive file selection field.
|
|
36
|
+
*
|
|
37
|
+
* Stores a JSON-encoded string of file metadata or, when generatePdf is set,
|
|
38
|
+
* only the generated PDF's document-store reference. It never stores a bearer
|
|
39
|
+
* token.
|
|
40
|
+
* Use `parseGoogleDriveValue` in process models to extract structured data.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const schema = Schema.Struct({
|
|
45
|
+
* document: GoogleDriveField({ label: "Select document" })
|
|
46
|
+
* })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare const GoogleDriveField: (options?: GoogleDriveFieldOptions) => Schema.Schema<string, string, never>;
|
|
50
|
+
/**
|
|
51
|
+
* Schema for validated Google Drive file metadata.
|
|
52
|
+
* Public process-state values never include bearer tokens.
|
|
53
|
+
*/
|
|
54
|
+
export declare const DrivePickerFileSchema: Schema.Struct<{
|
|
55
|
+
id: typeof Schema.String;
|
|
56
|
+
name: typeof Schema.String;
|
|
57
|
+
mimeType: typeof Schema.String;
|
|
58
|
+
url: typeof Schema.String;
|
|
59
|
+
}>;
|
|
60
|
+
export type DrivePickerFile = typeof DrivePickerFileSchema.Type;
|
|
61
|
+
/**
|
|
62
|
+
* Parse a Google Drive value string into validated file metadata.
|
|
63
|
+
* Throws a ParseError if the JSON is malformed or missing required fields.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const file = parseGoogleDriveValue(formValue)
|
|
68
|
+
* console.log(file.name, file.url)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare const parseGoogleDriveValue: (value: string) => DrivePickerFile;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { FormComponentPluginType, FormFileInput, applyFieldAnnotations, } from "processfocus/forms";
|
|
3
|
+
import { FormGoogleDriveGeneratePdf, FormGoogleDriveInput, } from "./google-drive-annotations.js";
|
|
4
|
+
import { GOOGLE_DRIVE_CLIENT_MODULE, GOOGLE_DRIVE_PLUGIN_TYPE, } from "./plugin-identity.js";
|
|
5
|
+
import { registerGoogleDriveWalkerPlugin } from "./register-walker.js";
|
|
6
|
+
export { FormGoogleDriveGeneratePdf, FormGoogleDriveInput };
|
|
7
|
+
// Organisation bundles contain their own runtime plugin registry. Registering
|
|
8
|
+
// while the field module loads keeps the walker and its registry in that same
|
|
9
|
+
// JavaScript realm, even when the host runtime has a separate registry copy.
|
|
10
|
+
registerGoogleDriveWalkerPlugin();
|
|
11
|
+
/** Stored document reference produced by the browser PDF upload flow. */
|
|
12
|
+
export const GoogleDriveGeneratedPdfSchema = Schema.Struct({
|
|
13
|
+
fileId: Schema.NonEmptyString,
|
|
14
|
+
filename: Schema.NonEmptyString,
|
|
15
|
+
});
|
|
16
|
+
const GoogleDriveGeneratedPdfJsonSchema = Schema.parseJson(GoogleDriveGeneratedPdfSchema);
|
|
17
|
+
/** Parse a generated-PDF form value into its stored document reference. */
|
|
18
|
+
export const parseGoogleDriveGeneratedPdf = (value) => Schema.decodeUnknownSync(GoogleDriveGeneratedPdfJsonSchema, {
|
|
19
|
+
onExcessProperty: "error",
|
|
20
|
+
})(value);
|
|
21
|
+
/** Return whether a form value is exactly a generated-PDF document reference. */
|
|
22
|
+
export const isGoogleDriveGeneratedPdf = (value) => Option.isSome(Schema.decodeUnknownOption(GoogleDriveGeneratedPdfJsonSchema, {
|
|
23
|
+
onExcessProperty: "error",
|
|
24
|
+
})(value));
|
|
25
|
+
/**
|
|
26
|
+
* Create a Google Drive file selection field.
|
|
27
|
+
*
|
|
28
|
+
* Stores a JSON-encoded string of file metadata or, when generatePdf is set,
|
|
29
|
+
* only the generated PDF's document-store reference. It never stores a bearer
|
|
30
|
+
* token.
|
|
31
|
+
* Use `parseGoogleDriveValue` in process models to extract structured data.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const schema = Schema.Struct({
|
|
36
|
+
* document: GoogleDriveField({ label: "Select document" })
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export const GoogleDriveField = (options) => {
|
|
41
|
+
const documentStore = options?.generatePdf?.node.path;
|
|
42
|
+
const normalizedDocumentStore = documentStore?.startsWith("/")
|
|
43
|
+
? documentStore
|
|
44
|
+
: documentStore === undefined
|
|
45
|
+
? undefined
|
|
46
|
+
: `/${documentStore}`;
|
|
47
|
+
const valueSchema = normalizedDocumentStore === undefined
|
|
48
|
+
? Schema.String
|
|
49
|
+
: Schema.String.pipe(Schema.filter(isGoogleDriveGeneratedPdf, {
|
|
50
|
+
message: () => "Select a Google Drive file and wait for its PDF upload to finish",
|
|
51
|
+
}));
|
|
52
|
+
return applyFieldAnnotations(valueSchema.annotations({
|
|
53
|
+
[FormComponentPluginType]: {
|
|
54
|
+
module: GOOGLE_DRIVE_CLIENT_MODULE,
|
|
55
|
+
type: GOOGLE_DRIVE_PLUGIN_TYPE,
|
|
56
|
+
},
|
|
57
|
+
[FormGoogleDriveInput]: true,
|
|
58
|
+
...(normalizedDocumentStore !== undefined && {
|
|
59
|
+
[FormGoogleDriveGeneratePdf]: {
|
|
60
|
+
documentStore: normalizedDocumentStore,
|
|
61
|
+
},
|
|
62
|
+
[FormFileInput]: { documentStore: normalizedDocumentStore },
|
|
63
|
+
}),
|
|
64
|
+
}), options);
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Schema for validated Google Drive file metadata.
|
|
68
|
+
* Public process-state values never include bearer tokens.
|
|
69
|
+
*/
|
|
70
|
+
export const DrivePickerFileSchema = Schema.Struct({
|
|
71
|
+
id: Schema.String,
|
|
72
|
+
name: Schema.String,
|
|
73
|
+
mimeType: Schema.String,
|
|
74
|
+
url: Schema.String,
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* Parse a Google Drive value string into validated file metadata.
|
|
78
|
+
* Throws a ParseError if the JSON is malformed or missing required fields.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const file = parseGoogleDriveValue(formValue)
|
|
83
|
+
* console.log(file.name, file.url)
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export const parseGoogleDriveValue = (value) => Schema.decodeUnknownSync(Schema.parseJson(DrivePickerFileSchema), {
|
|
87
|
+
onExcessProperty: "error",
|
|
88
|
+
})(value);
|
|
89
|
+
//# sourceMappingURL=google-drive-field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-drive-field.js","sourceRoot":"","sources":["../../src/lib/google-drive-field.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACvC,OAAO,EAEL,uBAAuB,EACvB,aAAa,EACb,qBAAqB,GACtB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,0BAA0B,EAC1B,oBAAoB,GACrB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EACL,0BAA0B,EAC1B,wBAAwB,GACzB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,+BAA+B,EAAE,MAAM,mBAAmB,CAAA;AAEnE,OAAO,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,CAAA;AAE3D,8EAA8E;AAC9E,8EAA8E;AAC9E,6EAA6E;AAC7E,+BAA+B,EAAE,CAAA;AAkBjC,yEAAyE;AACzE,MAAM,CAAC,MAAM,6BAA6B,GAAG,MAAM,CAAC,MAAM,CAAC;IACzD,MAAM,EAAE,MAAM,CAAC,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC,cAAc;CAChC,CAAC,CAAA;AAIF,MAAM,iCAAiC,GAAG,MAAM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,CAAC,uBAAuB,KAAK,SAAS,IAAI;YAC3C,CAAC,0BAA0B,CAAC,EAAE;gBAC5B,aAAa,EAAE,uBAAuB;aACvC;YACD,CAAC,aAAa,CAAC,EAAE,EAAE,aAAa,EAAE,uBAAuB,EAAE;SAC5D,CAAC;KACH,CAAC,EACF,OAAO,CACR,CAAA;AACH,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,CAAC;IACjD,EAAE,EAAE,MAAM,CAAC,MAAM;IACjB,IAAI,EAAE,MAAM,CAAC,MAAM;IACnB,QAAQ,EAAE,MAAM,CAAC,MAAM;IACvB,GAAG,EAAE,MAAM,CAAC,MAAM;CACnB,CAAC,CAAA;AAIF;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAAa,EAAmB,EAAE,CACtE,MAAM,CAAC,iBAAiB,CAAC,MAAM,CAAC,SAAS,CAAC,qBAAqB,CAAC,EAAE;IAChE,gBAAgB,EAAE,OAAO;CAC1B,CAAC,CAAC,KAAK,CAAC,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Google, TokenResponse } from "./types.js";
|
|
2
|
+
interface RequestGoogleDriveAccessTokenInput {
|
|
3
|
+
readonly google: Google;
|
|
4
|
+
readonly clientId: string;
|
|
5
|
+
readonly hint?: string;
|
|
6
|
+
readonly onResponse: (response: TokenResponse) => void;
|
|
7
|
+
readonly onError: () => void;
|
|
8
|
+
}
|
|
9
|
+
export declare const requestGoogleDriveAccessToken: ({ google, clientId, hint, onResponse, onError, }: RequestGoogleDriveAccessTokenInput) => void;
|
|
10
|
+
export {};
|