@theholocron/google-client 1.58.0-alpha.2 → 1.58.0-alpha.20
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/README.md +1 -1
- package/dist/index.d.mts +17 -2
- package/dist/index.mjs +42 -21
- package/package.json +19 -18
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ const [err, doc] = await google.documents.get("1BxiMVs0XRA...");
|
|
|
28
28
|
|
|
29
29
|
### Service Account
|
|
30
30
|
|
|
31
|
-
Set credentials via environment variables:
|
|
31
|
+
Set credentials via environment variables. Each is read as `HOLOCRON_<NAME>` first (`HOLOCRON_GOOGLE_PROJECT_ID`), then the bare name shown here, which keeps working:
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
34
|
# Option 1 — full JSON key file
|
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
|
+
import { EnvLookup } from "@theholocron/env-utils/lookup";
|
|
1
2
|
import { JWT, OAuth2Client } from "google-auth-library";
|
|
3
|
+
/** The result of a Google call as a tuple of error, value and elapsed milliseconds: `[Error, null, ms]` on failure, `[null, T, ms]` on success. */
|
|
2
4
|
type Return<T> = [Error, null, number] | [null, T, number];
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Signs in with Google OAuth: opens the consent page, waits on a local callback and resolves with the client holding the tokens.
|
|
7
|
+
*
|
|
8
|
+
* @param env - Where to read the GOOGLE_* variables from; defaults to the process environment.
|
|
9
|
+
*/
|
|
10
|
+
export declare const oauth: (scopes: string[], env?: EnvLookup) => Promise<OAuth2Client | null>;
|
|
11
|
+
/**
|
|
12
|
+
* Authenticates as the service account from `loadServiceAccountKey()` for the given scopes and resolves with its JWT client.
|
|
13
|
+
*
|
|
14
|
+
* @param env - Where to read the GOOGLE_* variables from; defaults to the process environment.
|
|
15
|
+
*/
|
|
16
|
+
export declare function googleAuth(scopes: string[], env?: EnvLookup): Promise<JWT>;
|
|
17
|
+
/** Read-only Google Docs and Sheets helpers, each authenticated with the service account. */
|
|
5
18
|
export declare const google: {
|
|
19
|
+
/** Google Docs helpers. */
|
|
6
20
|
documents: {
|
|
7
21
|
get: (id: string) => Promise<Return<import("googleapis").docs_v1.Schema$Document>>;
|
|
8
22
|
};
|
|
23
|
+
/** Google Sheets helpers. */
|
|
9
24
|
spreadsheets: {
|
|
10
25
|
get: (id: string) => Promise<Return<import("googleapis").sheets_v4.Schema$Spreadsheet>>;
|
|
11
26
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { google as google$1 } from "googleapis";
|
|
2
2
|
import http from "node:http";
|
|
3
3
|
import url from "node:url";
|
|
4
|
+
import { holocronEnv } from "@theholocron/env-utils/lookup";
|
|
4
5
|
import open from "open";
|
|
5
6
|
import destroyer from "server-destroy";
|
|
6
7
|
// src/key.ts
|
|
7
8
|
/**
|
|
8
9
|
* Load service account credentials from environment variables.
|
|
10
|
+
* Each variable is read as `HOLOCRON_<NAME>` first, then the bare `<NAME>`.
|
|
9
11
|
* Set GOOGLE_SERVICE_ACCOUNT_JSON to the full JSON key file content,
|
|
10
12
|
* or set individual GOOGLE_* variables for each field.
|
|
13
|
+
*
|
|
14
|
+
* @param env - Where to read the variables from; defaults to the process environment, pass a fake in tests.
|
|
11
15
|
*/
|
|
12
|
-
function loadServiceAccountKey() {
|
|
13
|
-
const raw =
|
|
16
|
+
function loadServiceAccountKey(env = holocronEnv()) {
|
|
17
|
+
const raw = env.get("GOOGLE_SERVICE_ACCOUNT_JSON");
|
|
14
18
|
if (raw) return JSON.parse(raw);
|
|
15
19
|
for (const key of [
|
|
16
20
|
"GOOGLE_TYPE",
|
|
@@ -19,32 +23,37 @@ function loadServiceAccountKey() {
|
|
|
19
23
|
"GOOGLE_PRIVATE_KEY",
|
|
20
24
|
"GOOGLE_CLIENT_EMAIL",
|
|
21
25
|
"GOOGLE_CLIENT_ID"
|
|
22
|
-
]) if (!
|
|
26
|
+
]) if (!env.get(key)) throw new Error(`Missing required environment variable: ${key}`);
|
|
23
27
|
return {
|
|
24
|
-
type:
|
|
25
|
-
project_id:
|
|
26
|
-
private_key_id:
|
|
27
|
-
private_key:
|
|
28
|
-
client_email:
|
|
29
|
-
client_id:
|
|
28
|
+
type: env.get("GOOGLE_TYPE"),
|
|
29
|
+
project_id: env.get("GOOGLE_PROJECT_ID"),
|
|
30
|
+
private_key_id: env.get("GOOGLE_PRIVATE_KEY_ID"),
|
|
31
|
+
private_key: env.get("GOOGLE_PRIVATE_KEY").replace(/\\n/g, "\n"),
|
|
32
|
+
client_email: env.get("GOOGLE_CLIENT_EMAIL"),
|
|
33
|
+
client_id: env.get("GOOGLE_CLIENT_ID"),
|
|
30
34
|
auth_uri: "https://accounts.google.com/o/oauth2/auth",
|
|
31
35
|
token_uri: "https://oauth2.googleapis.com/token",
|
|
32
36
|
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
|
|
33
|
-
client_x509_cert_url: `https://www.googleapis.com/robot/v1/metadata/x509/${encodeURIComponent(
|
|
37
|
+
client_x509_cert_url: `https://www.googleapis.com/robot/v1/metadata/x509/${encodeURIComponent(env.get("GOOGLE_CLIENT_EMAIL"))}`,
|
|
34
38
|
universe_domain: "googleapis.com"
|
|
35
39
|
};
|
|
36
40
|
}
|
|
37
41
|
// src/authentication.ts
|
|
38
|
-
function buildOAuth2Client() {
|
|
39
|
-
const clientID =
|
|
40
|
-
const clientSecret =
|
|
41
|
-
const redirectURI =
|
|
42
|
+
function buildOAuth2Client(env) {
|
|
43
|
+
const clientID = env.get("GOOGLE_CLIENT_ID");
|
|
44
|
+
const clientSecret = env.get("GOOGLE_CLIENT_SECRET");
|
|
45
|
+
const redirectURI = env.get("GOOGLE_REDIRECT_URI") ?? "http://localhost:4000/oauth2callback";
|
|
42
46
|
if (!clientID || !clientSecret) throw new Error("GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables are required for OAuth");
|
|
43
47
|
return new google$1.auth.OAuth2(clientID, clientSecret, redirectURI);
|
|
44
48
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Signs in with Google OAuth: opens the consent page, waits on a local callback and resolves with the client holding the tokens.
|
|
51
|
+
*
|
|
52
|
+
* @param env - Where to read the GOOGLE_* variables from; defaults to the process environment.
|
|
53
|
+
*/
|
|
54
|
+
const oauth = async (scopes, env = holocronEnv()) => {
|
|
55
|
+
const oauth2Client = buildOAuth2Client(env);
|
|
56
|
+
const port = parseInt(env.get("GOOGLE_REDIRECT_PORT") ?? "4000", 10);
|
|
48
57
|
return new Promise((resolve, reject) => {
|
|
49
58
|
const authorizeUrl = oauth2Client.generateAuthUrl({
|
|
50
59
|
access_type: "offline",
|
|
@@ -69,9 +78,14 @@ const oauth = async (scopes) => {
|
|
|
69
78
|
destroyer(server);
|
|
70
79
|
});
|
|
71
80
|
};
|
|
72
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Authenticates as the service account from `loadServiceAccountKey()` for the given scopes and resolves with its JWT client.
|
|
83
|
+
*
|
|
84
|
+
* @param env - Where to read the GOOGLE_* variables from; defaults to the process environment.
|
|
85
|
+
*/
|
|
86
|
+
async function googleAuth(scopes, env = holocronEnv()) {
|
|
73
87
|
return await new google$1.auth.GoogleAuth({
|
|
74
|
-
credentials: loadServiceAccountKey(),
|
|
88
|
+
credentials: loadServiceAccountKey(env),
|
|
75
89
|
scopes
|
|
76
90
|
}).getClient();
|
|
77
91
|
}
|
|
@@ -102,7 +116,9 @@ async function getDocument(id) {
|
|
|
102
116
|
];
|
|
103
117
|
}
|
|
104
118
|
}
|
|
105
|
-
const documents = {
|
|
119
|
+
const documents = {
|
|
120
|
+
/** Reads a Google Doc by id; resolves with a `Return` tuple of error, document and elapsed milliseconds. */
|
|
121
|
+
get: getDocument };
|
|
106
122
|
// src/spreadsheets.ts
|
|
107
123
|
const sheets = google$1.sheets("v4");
|
|
108
124
|
async function getSpreadsheet(id) {
|
|
@@ -132,8 +148,13 @@ async function getSpreadsheet(id) {
|
|
|
132
148
|
}
|
|
133
149
|
}
|
|
134
150
|
// src/index.ts
|
|
151
|
+
/** Read-only Google Docs and Sheets helpers, each authenticated with the service account. */
|
|
135
152
|
const google = {
|
|
153
|
+
/** Google Docs helpers. */
|
|
136
154
|
documents,
|
|
137
|
-
|
|
155
|
+
/** Google Sheets helpers. */
|
|
156
|
+
spreadsheets: {
|
|
157
|
+
/** Reads a spreadsheet by id, grid data included; resolves with a `Return` tuple of error, spreadsheet and elapsed ms. */
|
|
158
|
+
get: getSpreadsheet }
|
|
138
159
|
};
|
|
139
160
|
export { google, googleAuth, oauth };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/google-client",
|
|
3
|
-
"version": "1.58.0-alpha.
|
|
3
|
+
"version": "1.58.0-alpha.20",
|
|
4
4
|
"description": "A TypeScript client for Google Workspace APIs (Docs, Sheets)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"google",
|
|
@@ -32,26 +32,27 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"google-auth-library": "^11.3.
|
|
35
|
+
"google-auth-library": "^11.3.3",
|
|
36
36
|
"googleapis": "^188.0.0",
|
|
37
37
|
"open": "^11.0.0",
|
|
38
|
-
"server-destroy": "^1.0.1"
|
|
38
|
+
"server-destroy": "^1.0.1",
|
|
39
|
+
"@theholocron/env-utils": "^1.17.0-alpha.2"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@types/node": "^26",
|
|
42
43
|
"@types/server-destroy": "^1.0.4",
|
|
43
|
-
"@vitest/coverage-v8": "^5.0
|
|
44
|
-
"@vitest/eslint-plugin": "^1.6.
|
|
45
|
-
"eslint": "^10.
|
|
46
|
-
"eslint-plugin-n": "^18.
|
|
47
|
-
"globals": "^17.
|
|
48
|
-
"tsdown": "^0.23.
|
|
44
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
45
|
+
"@vitest/eslint-plugin": "^1.6.28",
|
|
46
|
+
"eslint": "^10.14.0",
|
|
47
|
+
"eslint-plugin-n": "^18.5.0",
|
|
48
|
+
"globals": "^17.14.0",
|
|
49
|
+
"tsdown": "^0.23.5",
|
|
49
50
|
"typescript": "^5.9.3",
|
|
50
|
-
"vitest": "^5.0
|
|
51
|
-
"@theholocron/
|
|
52
|
-
"@theholocron/
|
|
53
|
-
"@theholocron/
|
|
54
|
-
"@theholocron/
|
|
51
|
+
"vitest": "^5.1.0",
|
|
52
|
+
"@theholocron/eslint-config": "^8.9.0-alpha.12",
|
|
53
|
+
"@theholocron/tsconfig": "^8.9.0-alpha.12",
|
|
54
|
+
"@theholocron/vitest-config": "^8.9.0-alpha.12",
|
|
55
|
+
"@theholocron/tsdown-config": "^8.9.0-alpha.12"
|
|
55
56
|
},
|
|
56
57
|
"engines": {
|
|
57
58
|
"node": ">=22.0.0"
|
|
@@ -60,10 +61,10 @@
|
|
|
60
61
|
"access": "public"
|
|
61
62
|
},
|
|
62
63
|
"scripts": {
|
|
63
|
-
"
|
|
64
|
-
"
|
|
64
|
+
"build": "tsdown && node ../scripts/check-bundle-size.mjs",
|
|
65
|
+
"lint:code": "eslint .",
|
|
66
|
+
"test:unit": "vitest run --coverage",
|
|
65
67
|
"test:watch": "vitest",
|
|
66
|
-
"
|
|
67
|
-
"verification.unitTests": "vitest run"
|
|
68
|
+
"typecheck": "tsc --noEmit"
|
|
68
69
|
}
|
|
69
70
|
}
|