Google APIs Common is a TypeScript npm library that provides essential tooling and utilities for Google APIs Node.js client libraries. It serves as a foundational layer that re-exports key dependencies (google-auth-library and gaxios) while providing standardized interfaces for API interactions, authentication, request handling, and HTTP/2 support.
npm install googleapis-commonimport {
GoogleAuth,
OAuth2Client,
createAPIRequest,
AuthPlus
} from "googleapis-common";For CommonJS:
const {
GoogleAuth,
OAuth2Client,
createAPIRequest,
AuthPlus
} = require("googleapis-common");import {
GoogleAuth,
createAPIRequest,
APIRequestContext,
MethodOptions
} from "googleapis-common";
// Set up authentication
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
// Create API request context
const context: APIRequestContext = {
_options: {
auth: auth
}
};
// Make an API request
const response = await createAPIRequest({
options: { url: 'https://example.googleapis.com/v1/resource' } as MethodOptions,
params: {},
requiredParams: [],
pathParams: [],
context: context
});Google APIs Common is built around several key components:
Enhanced authentication system built on google-auth-library with additional features for Google API client libraries.
class AuthPlus extends GoogleAuth {
JWT: typeof JWT;
Compute: typeof Compute;
OAuth2: typeof OAuth2Client;
GoogleAuth: typeof GoogleAuth;
AwsClient: typeof AwsClient;
IdentityPoolClient: typeof IdentityPoolClient;
ExternalAccountClient: typeof ExternalAccountClient;
getClient(options?: GoogleAuthOptions): Promise<Compute | JWT | UserRefreshClient | BaseExternalAccountClient | Impersonated>;
getProjectId(): Promise<string>;
getProjectId(callback: ProjectIdCallback): void;
}Core request creation functionality for Google APIs with built-in authentication, retry logic, and HTTP/2 support.
function createAPIRequest<T>(
parameters: APIRequestParams<T>,
callback?: BodyResponseCallback<T>
): GaxiosPromise<T>;
interface APIRequestParams<T = any> {
options: MethodOptions;
params: T;
requiredParams: string[];
pathParams: string[];
context: APIRequestContext;
mediaUrl?: string | null;
}Direct access to google-auth-library and gaxios functionality for authentication and HTTP operations.
// google-auth-library exports
const googleAuthLibrary: typeof import('google-auth-library');
class OAuth2Client extends import('google-auth-library').OAuth2Client {}
class JWT extends import('google-auth-library').JWT {}
class Compute extends import('google-auth-library').Compute {}
class GoogleAuth extends import('google-auth-library').GoogleAuth {}
// gaxios exports
const gaxios: typeof import('gaxios');
class Gaxios extends import('gaxios').Gaxios {}
class GaxiosError extends import('gaxios').GaxiosError {}
interface GaxiosOptions extends import('gaxios').GaxiosOptions {}
interface GaxiosResponse<T> extends import('gaxios').GaxiosResponse<T> {}API discovery system for dynamically generating client endpoints from Google API schemas.
class Discovery {
constructor(options: DiscoveryOptions);
discoverAPI(apiDiscoveryUrl: string | {url?: string}): Promise<EndpointCreator>;
discoverAllAPIs(discoveryUrl: string): Promise<{}>;
}
function getAPI<T>(
api: string,
options: ServiceOptions | string,
versions: {[index: string]: any},
context?: GoogleConfigurable
): T;Native HTTP/2 client implementation providing improved performance for Google API requests.
interface GaxiosResponseWithHTTP2<T = any> extends GaxiosResponse<T> {
headers: Record<string, string>;
}
const sessions: {[index: string]: SessionData};
interface SessionData {
session: http2.ClientHttp2Session;
timeoutHandle?: NodeJS.Timeout;
}Helper functions for header manipulation, response marshalling, and browser detection.
type HeadersInit = ConstructorParameters<typeof Headers>[0];
function headersToClassicHeaders<T extends Record<string, string>>(
headers: HeadersInit
): T;
function marshallGaxiosResponse<T extends GaxiosResponse>(
res?: T
): GaxiosResponseWithHTTP2;interface APIRequestContext {
google?: GoogleConfigurable;
_options: GlobalOptions;
}
interface GoogleConfigurable {
_options: GlobalOptions;
}
interface GlobalOptions extends MethodOptions {
auth?: GoogleAuth | OAuth2Client | BaseExternalAccountClient | string;
universeDomain?: string;
universe_domain?: string;
}
interface MethodOptions extends GaxiosOptions {
apiVersion?: string;
rootUrl?: string;
http2?: boolean;
userAgentDirectives?: UserAgentDirective[];
}
interface ServiceOptions extends GlobalOptions {
version?: string;
}type BodyResponseCallback<T> = (
err: Error | null,
res?: GaxiosResponseWithHTTP2<T> | null
) => void;
type APIEndpoint = Readonly<Endpoint & any>;
interface UserAgentDirective {
product: string;
version: string;
comment?: string;
}