Helper functions for header manipulation and response marshalling. These utilities provide cross-platform compatibility and standardized data handling for Google API operations.
Functions for converting between different header formats and ensuring compatibility.
/**
* Type representing various header initialization formats
*/
type HeadersInit = ConstructorParameters<typeof Headers>[0];
/**
* Converts potential Headers objects to plain headers objects
* @param headers - Headers in any compatible format (Headers, array of pairs, or object)
* @returns Plain object with string key-value pairs
*/
function headersToClassicHeaders<T extends Record<string, string>>(
headers: HeadersInit
): T;Usage Examples:
import { headersToClassicHeaders, HeadersInit } from "googleapis-common";
// Convert Headers object to plain object
const webHeaders = new Headers({
'Content-Type': 'application/json',
'Authorization': 'Bearer token123'
});
const plainHeaders = headersToClassicHeaders(webHeaders);
console.log(plainHeaders);
// Output: { 'Content-Type': 'application/json', 'Authorization': 'Bearer token123' }
// Convert array format to plain object
const arrayHeaders: [string, string][] = [
['Content-Type', 'application/xml'],
['Accept', 'application/json']
];
const convertedHeaders = headersToClassicHeaders(arrayHeaders);
console.log(convertedHeaders);
// Output: { 'Content-Type': 'application/xml', 'Accept': 'application/json' }
// Plain object passthrough (no conversion needed)
const objectHeaders = {
'User-Agent': 'my-app/1.0',
'X-API-Key': 'secret-key'
};
const passedHeaders = headersToClassicHeaders(objectHeaders);
console.log(passedHeaders === objectHeaders); // true (same reference)Functions for converting and standardizing HTTP response objects.
/**
* Marshals a GaxiosResponse into a library-friendly type with HTTP/2 compatibility
* @param res - The Gaxios response to marshall
* @returns GaxiosResponse with HTTP/2-ready/compatible headers
*/
function marshallGaxiosResponse<T extends GaxiosResponse>(
res?: T
): GaxiosResponseWithHTTP2;Usage Examples:
import {
marshallGaxiosResponse,
GaxiosResponse,
GaxiosResponseWithHTTP2
} from "googleapis-common";
// Marshall a standard Gaxios response
async function makeRequest(): Promise<GaxiosResponseWithHTTP2> {
// Assume this returns a standard GaxiosResponse
const rawResponse: GaxiosResponse = await someGaxiosRequest();
// Convert to HTTP/2-compatible format
const marshalled = marshallGaxiosResponse(rawResponse);
// Now guaranteed to have plain object headers
console.log(typeof marshalled.headers); // 'object'
console.log(marshalled.headers['content-type']); // Accessible as plain object
return marshalled;
}
// Handle undefined responses gracefully
const emptyResponse = marshallGaxiosResponse(undefined);
console.log(emptyResponse.headers); // {}
// Type safety with generics
interface ApiData {
items: string[];
count: number;
}
const typedResponse = marshallGaxiosResponse<GaxiosResponse<ApiData>>(apiResponse);
console.log(typedResponse.data.items); // Type-safe access to response dataType definitions for utility functions and cross-platform compatibility.
/**
* Type for various header input formats supported by headersToClassicHeaders
*/
type HeadersInit =
| Headers
| Array<[string, string]>
| Record<string, string>
| undefined
| null;
/**
* Generic constraint for header objects
*/
type HeaderRecord = Record<string, string>;