Skip to content

Commit

Permalink
feat: add Api Version to header for apiary (#550)
Browse files Browse the repository at this point in the history
* feat: add Api Version to header for apiary
  • Loading branch information
sofisl authored Apr 30, 2024
1 parent 2bf4e5e commit fba0837
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface GlobalOptions extends MethodOptions {
}

export interface MethodOptions extends GaxiosOptions {
apiVersion?: string;
rootUrl?: string;
http2?: boolean;
userAgentDirectives?: UserAgentDirective[];
Expand Down
15 changes: 9 additions & 6 deletions src/apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check warning on line 24 in src/apirequest.ts

View workflow job for this annotation

GitHub Actions / lint

'SchemaMethod' is defined but never used
import * as h2 from './http2';

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -138,7 +138,7 @@ async function createAPIRequestAsync<T>(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
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [],
Expand Down
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface SchemaMethod {
fragment: string;
mediaUpload: {protocols: {simple: {path: string}}};
supportsMediaDownload?: boolean;
apiVersion?: string;
}

export interface FragmentResponse {
Expand Down
17 changes: 17 additions & 0 deletions test/test.apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FakeParams>({
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';
Expand Down

0 comments on commit fba0837

Please sign in to comment.