Skip to content

Commit

Permalink
feat: add optional projectId override in service object (#722)
Browse files Browse the repository at this point in the history
* feat: add optional projectId override in service object

* add test

* update test
  • Loading branch information
steffnay authored Jan 18, 2022
1 parent 96b0e6d commit 4908d85
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/service-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export interface ServiceObjectConfig {
* for completion.
*/
pollIntervalMs?: number;

/**
* Override of projectId, used to allow access to resources in another project.
* For example, a BigQuery dataset in another project to which the user has been
* granted permission.
*/
projectId?: string;
}

export interface Methods {
Expand Down Expand Up @@ -157,6 +164,7 @@ class ServiceObject<T = any> extends EventEmitter {
private createMethod?: Function;
protected methods: Methods;
interceptors: Interceptor[];
projectId?: string;

/*
* @constructor
Expand Down Expand Up @@ -186,6 +194,7 @@ class ServiceObject<T = any> extends EventEmitter {
this.methods = config.methods || {};
this.interceptors = [];
this.pollIntervalMs = config.pollIntervalMs;
this.projectId = config.projectId;

if (config.methods) {
// This filters the ServiceObject instance (e.g. a "File") to only have
Expand Down Expand Up @@ -546,6 +555,10 @@ class ServiceObject<T = any> extends EventEmitter {
): void | r.Request {
reqOpts = extend(true, {}, reqOpts);

if (this.projectId) {
reqOpts.projectId = this.projectId;
}

const isAbsoluteUrl = reqOpts.uri.indexOf('http') === 0;
const uriComponents = [this.baseUrl, this.id || '', reqOpts.uri];

Expand Down
9 changes: 7 additions & 2 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ export class Service {
const uriComponents = [this.baseUrl];

if (this.projectIdRequired) {
uriComponents.push('projects');
uriComponents.push(this.projectId);
if (reqOpts.projectId) {
uriComponents.push('projects');
uriComponents.push(reqOpts.projectId);
} else {
uriComponents.push('projects');
uriComponents.push(this.projectId);
}
}

uriComponents.push(reqOpts.uri);
Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export interface DecorateRequestOptions extends r.CoreOptions {
uri: string;
interceptors_?: Interceptor[];
shouldReturnStream?: boolean;
projectId?: string;
}

export interface ParsedHttpResponseBody {
Expand Down
25 changes: 25 additions & 0 deletions test/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,31 @@ describe('Service', () => {

service.request_(reqOpts, assert.ifError);
});

it('should use projectId override', done => {
const config = extend({}, CONFIG, {projectIdRequired: true});
const service = new Service(config, OPTIONS);
const projectOverride = 'turing';

reqOpts.projectId = projectOverride;

const expectedUri = [
service.baseUrl,
'projects',
projectOverride,
reqOpts.uri,
].join('/');

service.makeAuthenticatedRequest = (
reqOpts_: DecorateRequestOptions
) => {
assert.strictEqual(reqOpts_.uri, expectedUri);

done();
};

service.request_(reqOpts, assert.ifError);
});
});
});

Expand Down

0 comments on commit 4908d85

Please sign in to comment.