@theholocron/google-client 1.58.0-alpha.13 → 1.58.0-alpha.15

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/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,6 +1,7 @@
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 { createEnvLookup } from "@theholocron/env-utils/lookup";
4
5
  import open from "open";
5
6
  import destroyer from "server-destroy";
6
7
  // src/key.ts
@@ -8,9 +9,11 @@ import destroyer from "server-destroy";
8
9
  * Load service account credentials from environment variables.
9
10
  * Set GOOGLE_SERVICE_ACCOUNT_JSON to the full JSON key file content,
10
11
  * or set individual GOOGLE_* variables for each field.
12
+ *
13
+ * @param env - Where to read the variables from; defaults to the process environment, pass a fake in tests.
11
14
  */
12
- function loadServiceAccountKey() {
13
- const raw = process.env.GOOGLE_SERVICE_ACCOUNT_JSON;
15
+ function loadServiceAccountKey(env = createEnvLookup()) {
16
+ const raw = env.get("GOOGLE_SERVICE_ACCOUNT_JSON");
14
17
  if (raw) return JSON.parse(raw);
15
18
  for (const key of [
16
19
  "GOOGLE_TYPE",
@@ -19,33 +22,37 @@ function loadServiceAccountKey() {
19
22
  "GOOGLE_PRIVATE_KEY",
20
23
  "GOOGLE_CLIENT_EMAIL",
21
24
  "GOOGLE_CLIENT_ID"
22
- ]) if (!process.env[key]) throw new Error(`Missing required environment variable: ${key}`);
25
+ ]) if (!env.get(key)) throw new Error(`Missing required environment variable: ${key}`);
23
26
  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,
27
+ type: env.get("GOOGLE_TYPE"),
28
+ project_id: env.get("GOOGLE_PROJECT_ID"),
29
+ private_key_id: env.get("GOOGLE_PRIVATE_KEY_ID"),
30
+ private_key: env.get("GOOGLE_PRIVATE_KEY").replace(/\\n/g, "\n"),
31
+ client_email: env.get("GOOGLE_CLIENT_EMAIL"),
32
+ client_id: env.get("GOOGLE_CLIENT_ID"),
30
33
  auth_uri: "https://accounts.google.com/o/oauth2/auth",
31
34
  token_uri: "https://oauth2.googleapis.com/token",
32
35
  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)}`,
36
+ client_x509_cert_url: `https://www.googleapis.com/robot/v1/metadata/x509/${encodeURIComponent(env.get("GOOGLE_CLIENT_EMAIL"))}`,
34
37
  universe_domain: "googleapis.com"
35
38
  };
36
39
  }
37
40
  // 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";
41
+ function buildOAuth2Client(env) {
42
+ const clientID = env.get("GOOGLE_CLIENT_ID");
43
+ const clientSecret = env.get("GOOGLE_CLIENT_SECRET");
44
+ const redirectURI = env.get("GOOGLE_REDIRECT_URI") ?? "http://localhost:4000/oauth2callback";
42
45
  if (!clientID || !clientSecret) throw new Error("GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables are required for OAuth");
43
46
  return new google$1.auth.OAuth2(clientID, clientSecret, redirectURI);
44
47
  }
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);
48
+ /**
49
+ * Signs in with Google OAuth: opens the consent page, waits on a local callback and resolves with the client holding the tokens.
50
+ *
51
+ * @param env - Where to read the GOOGLE_* variables from; defaults to the process environment.
52
+ */
53
+ const oauth = async (scopes, env = createEnvLookup()) => {
54
+ const oauth2Client = buildOAuth2Client(env);
55
+ const port = parseInt(env.get("GOOGLE_REDIRECT_PORT") ?? "4000", 10);
49
56
  return new Promise((resolve, reject) => {
50
57
  const authorizeUrl = oauth2Client.generateAuthUrl({
51
58
  access_type: "offline",
@@ -70,10 +77,14 @@ const oauth = async (scopes) => {
70
77
  destroyer(server);
71
78
  });
72
79
  };
73
- /** Authenticates as the service account from `loadServiceAccountKey()` for the given scopes and resolves with its JWT client. */
74
- async function googleAuth(scopes) {
80
+ /**
81
+ * Authenticates as the service account from `loadServiceAccountKey()` for the given scopes and resolves with its JWT client.
82
+ *
83
+ * @param env - Where to read the GOOGLE_* variables from; defaults to the process environment.
84
+ */
85
+ async function googleAuth(scopes, env = createEnvLookup()) {
75
86
  return await new google$1.auth.GoogleAuth({
76
- credentials: loadServiceAccountKey(),
87
+ credentials: loadServiceAccountKey(env),
77
88
  scopes
78
89
  }).getClient();
79
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/google-client",
3
- "version": "1.58.0-alpha.13",
3
+ "version": "1.58.0-alpha.15",
4
4
  "description": "A TypeScript client for Google Workspace APIs (Docs, Sheets)",
5
5
  "keywords": [
6
6
  "google",
@@ -35,7 +35,8 @@
35
35
  "google-auth-library": "^11.3.2",
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.1"
39
40
  },
40
41
  "devDependencies": {
41
42
  "@types/node": "^26",
@@ -49,8 +50,8 @@
49
50
  "typescript": "^5.9.3",
50
51
  "vitest": "^5.0.5",
51
52
  "@theholocron/eslint-config": "^8.9.0-alpha.9",
52
- "@theholocron/tsdown-config": "^8.9.0-alpha.9",
53
53
  "@theholocron/tsconfig": "^8.9.0-alpha.9",
54
+ "@theholocron/tsdown-config": "^8.9.0-alpha.9",
54
55
  "@theholocron/vitest-config": "^8.9.0-alpha.9"
55
56
  },
56
57
  "engines": {