diff --git a/package.json b/package.json index 87b2151..6578ba8 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ }, "devDependencies": { "@babel/plugin-proposal-private-methods": "^7.18.6", - "@compodoc/compodoc": "^1.1.9", + "@compodoc/compodoc": "1.1.23", "@types/execa": "^0.9.0", "@types/extend": "^3.0.1", "@types/mocha": "^9.0.0", diff --git a/src/api.ts b/src/api.ts index 4562d3a..6b20bc9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -50,6 +50,7 @@ export interface GlobalOptions extends MethodOptions { } export interface MethodOptions extends GaxiosOptions { + apiVersion?: string; rootUrl?: string; http2?: boolean; userAgentDirectives?: UserAgentDirective[]; diff --git a/src/apirequest.ts b/src/apirequest.ts index 1387be2..cdcc60f 100644 --- a/src/apirequest.ts +++ b/src/apirequest.ts @@ -21,7 +21,7 @@ import * as extend from 'extend'; import {APIRequestParams, BodyResponseCallback} from './api'; import {isBrowser} from './isbrowser'; -import {SchemaParameters} from './schema'; +import {SchemaParameters, SchemaMethod} from './schema'; import * as h2 from './http2'; // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -138,7 +138,7 @@ async function createAPIRequestAsync(parameters: APIRequestParams) { // Grab headers from user provided options const headers = params.headers || {}; - populateAPIHeader(headers); + populateAPIHeader(headers, options.apiVersion); delete params.headers; // Un-alias parameters that were modified due to conflicts with reserved names @@ -390,13 +390,16 @@ class ProgressStream extends stream.Transform { } } -function populateAPIHeader(headers: Headers) { +function populateAPIHeader(headers: Headers, apiVersion: string | undefined) { // TODO: we should eventually think about adding browser support for this // populating the gl-web header (web support should also be added to // google-auth-library-nodejs). if (!isBrowser()) { - headers[ - 'x-goog-api-client' - ] = `gdcl/${pkg.version} gl-node/${process.versions.node}`; + headers['x-goog-api-client'] = + `gdcl/${pkg.version} gl-node/${process.versions.node}`; + } + + if (apiVersion) { + headers['x-goog-api-version'] = apiVersion; } } diff --git a/src/endpoint.ts b/src/endpoint.ts index dad86cb..8051576 100644 --- a/src/endpoint.ts +++ b/src/endpoint.ts @@ -118,6 +118,7 @@ export class Endpoint implements Target, APIRequestContext { options: { url: schemaUrl.substring(1, schemaUrl.length - 1), method: method.httpMethod, + apiVersion: method.apiVersion, }, params, requiredParams: method.parameterOrder || [], diff --git a/src/schema.ts b/src/schema.ts index e7080a4..ba60cb8 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -107,6 +107,7 @@ export interface SchemaMethod { fragment: string; mediaUpload: {protocols: {simple: {path: string}}}; supportsMediaDownload?: boolean; + apiVersion?: string; } export interface FragmentResponse { diff --git a/test/test.apirequest.ts b/test/test.apirequest.ts index 31a0f66..b52feed 100644 --- a/test/test.apirequest.ts +++ b/test/test.apirequest.ts @@ -287,6 +287,23 @@ describe('createAPIRequest', () => { scope.done(); }); + it('should populate x-goog-api-version', async () => { + const scope = nock(url) + .get('/') + .reply(function () { + assert.ok(/1234/.test(this.req.headers['x-goog-api-version'][0])); + return [200, '']; + }); + await createAPIRequest({ + options: {url, apiVersion: '1234'}, + params: {}, + requiredParams: [], + pathParams: [], + context: fakeContext, + }); + scope.done(); + }); + it('should rewrite url to match default rootUrl', async () => { const rootUrl = 'http://www.googleapis.com/'; const path = '/api/service';