@theholocron/google-client 1.58.0-alpha.4 → 1.58.0-alpha.41

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 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,10 +1,19 @@
1
+ import { EnvLookup } from "@theholocron/env-utils/lookup";
1
2
  import { JWT, OAuth2Client } from "google-auth-library";
2
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. */
3
4
  type Return<T> = [Error, null, number] | [null, T, number];
4
- /** Signs in with Google OAuth: opens the consent page, waits on a local callback and resolves with the client holding the tokens. */
5
- export declare const oauth: (scopes: string[]) => Promise<OAuth2Client | null>;
6
- /** Authenticates as the service account from `loadServiceAccountKey()` for the given scopes and resolves with its JWT client. */
7
- export declare function googleAuth(scopes: string[]): Promise<JWT>;
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>;
8
17
  /** Read-only Google Docs and Sheets helpers, each authenticated with the service account. */
9
18
  export declare const google: {
10
19
  /** Google Docs helpers. */
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 = process.env.GOOGLE_SERVICE_ACCOUNT_JSON;
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,33 +23,37 @@ function loadServiceAccountKey() {
19
23
  "GOOGLE_PRIVATE_KEY",
20
24
  "GOOGLE_CLIENT_EMAIL",
21
25
  "GOOGLE_CLIENT_ID"
22
- ]) if (!process.env[key]) throw new Error(`Missing required environment variable: ${key}`);
26
+ ]) if (!env.get(key)) throw new Error(`Missing required environment variable: ${key}`);
23
27
  return {
24
- type: process.env.GOOGLE_TYPE,
25
- project_id: process.env.GOOGLE_PROJECT_ID,
26
- private_key_id: process.env.GOOGLE_PRIVATE_KEY_ID,
27
- private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"),
28
- client_email: process.env.GOOGLE_CLIENT_EMAIL,
29
- client_id: process.env.GOOGLE_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(process.env.GOOGLE_CLIENT_EMAIL)}`,
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 = process.env.GOOGLE_CLIENT_ID;
40
- const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
41
- const redirectURI = process.env.GOOGLE_REDIRECT_URI ?? "http://localhost:4000/oauth2callback";
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
- /** Signs in with Google OAuth: opens the consent page, waits on a local callback and resolves with the client holding the tokens. */
46
- const oauth = async (scopes) => {
47
- const oauth2Client = buildOAuth2Client();
48
- const port = parseInt(process.env.GOOGLE_REDIRECT_PORT ?? "4000", 10);
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);
49
57
  return new Promise((resolve, reject) => {
50
58
  const authorizeUrl = oauth2Client.generateAuthUrl({
51
59
  access_type: "offline",
@@ -62,18 +70,22 @@ const oauth = async (scopes) => {
62
70
  resolve(oauth2Client);
63
71
  }
64
72
  } catch (e) {
65
- reject(e);
73
+ reject(e instanceof Error ? e : new Error(String(e)));
66
74
  }
67
75
  }).listen(port, () => {
68
- open(authorizeUrl, { wait: false }).then((cp) => cp.unref());
76
+ open(authorizeUrl, { wait: false }).then((cp) => cp.unref()).catch(reject);
69
77
  });
70
78
  destroyer(server);
71
79
  });
72
80
  };
73
- /** Authenticates as the service account from `loadServiceAccountKey()` for the given scopes and resolves with its JWT client. */
74
- async function googleAuth(scopes) {
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()) {
75
87
  return await new google$1.auth.GoogleAuth({
76
- credentials: loadServiceAccountKey(),
88
+ credentials: loadServiceAccountKey(env),
77
89
  scopes
78
90
  }).getClient();
79
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/google-client",
3
- "version": "1.58.0-alpha.4",
3
+ "version": "1.58.0-alpha.41",
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.2",
35
+ "google-auth-library": "^11.3.4",
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.5"
39
40
  },
40
41
  "devDependencies": {
41
42
  "@types/node": "^26",
42
43
  "@types/server-destroy": "^1.0.4",
43
- "@vitest/coverage-v8": "^5.0.5",
44
- "@vitest/eslint-plugin": "^1.6.27",
45
- "eslint": "^10.8.1",
46
- "eslint-plugin-n": "^18.3.0",
47
- "globals": "^17.11.0",
48
- "tsdown": "^0.23.3",
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.7",
49
50
  "typescript": "^5.9.3",
50
- "vitest": "^5.0.5",
51
- "@theholocron/eslint-config": "^8.9.0-alpha.5",
52
- "@theholocron/tsdown-config": "^8.9.0-alpha.5",
53
- "@theholocron/vitest-config": "^8.9.0-alpha.5",
54
- "@theholocron/tsconfig": "^8.9.0-alpha.5"
51
+ "vitest": "^5.1.0",
52
+ "@theholocron/tsdown-config": "^8.9.0-alpha.21",
53
+ "@theholocron/tsconfig": "^8.9.0-alpha.21",
54
+ "@theholocron/vitest-config": "^8.9.0-alpha.21",
55
+ "@theholocron/eslint-config": "^8.9.0-alpha.21"
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
- "delivery.build": "tsdown && node ../scripts/check-bundle-size.mjs",
64
- "sourceQuality.staticAnalysis": "eslint .",
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
- "verification.typeSafety": "tsc --noEmit",
67
- "verification.unitTests": "vitest run"
68
+ "typecheck": "tsc --noEmit"
68
69
  }
69
70
  }