Skip to content

Commit 18ea9d9

Browse files
authored
fix: allow methodConfig to override request method and uri (#451)
* fix: allow methodConfig to override request method and uri * npm run fix
1 parent 5dd4084 commit 18ea9d9

2 files changed

Lines changed: 115 additions & 15 deletions

File tree

core/common/src/service-object.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,17 @@ class ServiceObject<T = any> extends EventEmitter {
271271
const methodConfig =
272272
(typeof this.methods.delete === 'object' && this.methods.delete) || {};
273273

274-
const reqOpts = extend(true, {}, methodConfig.reqOpts, {
275-
method: 'DELETE',
276-
uri: '',
277-
qs: options,
278-
});
274+
const reqOpts = extend(
275+
true,
276+
{
277+
method: 'DELETE',
278+
uri: '',
279+
},
280+
methodConfig.reqOpts,
281+
{
282+
qs: options,
283+
}
284+
);
279285

280286
// The `request` method may have been overridden to hold any special
281287
// behavior. Ensure we call the original `request` method.
@@ -402,10 +408,16 @@ class ServiceObject<T = any> extends EventEmitter {
402408
(typeof this.methods.getMetadata === 'object' &&
403409
this.methods.getMetadata) ||
404410
{};
405-
const reqOpts = extend(true, {}, methodConfig.reqOpts, {
406-
uri: '',
407-
qs: options,
408-
});
411+
const reqOpts = extend(
412+
true,
413+
{
414+
uri: '',
415+
},
416+
methodConfig.reqOpts,
417+
{
418+
qs: options,
419+
}
420+
);
409421

410422
// The `request` method may have been overridden to hold any special
411423
// behavior. Ensure we call the original `request` method.
@@ -452,12 +464,19 @@ class ServiceObject<T = any> extends EventEmitter {
452464
this.methods.setMetadata) ||
453465
{};
454466

455-
const reqOpts = extend(true, {}, methodConfig.reqOpts, {
456-
method: 'PATCH',
457-
uri: '',
458-
json: metadata,
459-
qs: options,
460-
});
467+
const reqOpts = extend(
468+
true,
469+
{},
470+
{
471+
method: 'PATCH',
472+
uri: '',
473+
},
474+
methodConfig.reqOpts,
475+
{
476+
json: metadata,
477+
qs: options,
478+
}
479+
);
461480

462481
// The `request` method may have been overridden to hold any special
463482
// behavior. Ensure we call the original `request` method.

core/common/test/service-object.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,34 @@ describe('ServiceObject', () => {
267267
serviceObject.delete(options, assert.ifError);
268268
});
269269

270+
it('should override method and uri field in request with methodConfig', done => {
271+
const methodConfig = {
272+
reqOpts: {
273+
uri: 'v2',
274+
method: 'PATCH',
275+
},
276+
};
277+
278+
const cachedMethodConfig = extend(true, {}, methodConfig);
279+
280+
sandbox
281+
.stub(ServiceObject.prototype, 'request')
282+
.callsFake((reqOpts_, callback) => {
283+
assert.deepStrictEqual(
284+
serviceObject.methods.delete,
285+
cachedMethodConfig
286+
);
287+
assert.deepStrictEqual(reqOpts_.uri, 'v2');
288+
assert.deepStrictEqual(reqOpts_.method, 'PATCH');
289+
done();
290+
callback(null, null, null!);
291+
});
292+
293+
const serviceObject = new ServiceObject(CONFIG) as FakeServiceObject;
294+
serviceObject.methods.delete = methodConfig;
295+
serviceObject.delete();
296+
});
297+
270298
it('should extend the defaults with request options', done => {
271299
const methodConfig = {
272300
reqOpts: {
@@ -561,6 +589,32 @@ describe('ServiceObject', () => {
561589
serviceObject.getMetadata(options, assert.ifError);
562590
});
563591

592+
it('should override uri field in request with methodConfig', done => {
593+
const methodConfig = {
594+
reqOpts: {
595+
uri: 'v2',
596+
},
597+
};
598+
599+
const cachedMethodConfig = extend(true, {}, methodConfig);
600+
601+
sandbox
602+
.stub(ServiceObject.prototype, 'request')
603+
.callsFake((reqOpts_, callback) => {
604+
assert.deepStrictEqual(
605+
serviceObject.methods.getMetadata,
606+
cachedMethodConfig
607+
);
608+
assert.deepStrictEqual(reqOpts_.uri, 'v2');
609+
done();
610+
callback(null, null, null!);
611+
});
612+
613+
const serviceObject = new ServiceObject(CONFIG) as FakeServiceObject;
614+
serviceObject.methods.getMetadata = methodConfig;
615+
serviceObject.getMetadata();
616+
});
617+
564618
it('should extend the defaults with request options', done => {
565619
const methodConfig = {
566620
reqOpts: {
@@ -662,6 +716,33 @@ describe('ServiceObject', () => {
662716
serviceObject.setMetadata(metadata, options, () => {});
663717
});
664718

719+
it('should override uri and method with methodConfig', done => {
720+
const methodConfig = {
721+
reqOpts: {
722+
uri: 'v2',
723+
method: 'PUT',
724+
},
725+
};
726+
const cachedMethodConfig = extend(true, {}, methodConfig);
727+
728+
sandbox
729+
.stub(ServiceObject.prototype, 'request')
730+
.callsFake((reqOpts_, callback) => {
731+
assert.deepStrictEqual(
732+
serviceObject.methods.setMetadata,
733+
cachedMethodConfig
734+
);
735+
assert.deepStrictEqual(reqOpts_.uri, 'v2');
736+
assert.deepStrictEqual(reqOpts_.method, 'PUT');
737+
done();
738+
callback(null, null, null!);
739+
});
740+
741+
const serviceObject = new ServiceObject(CONFIG) as FakeServiceObject;
742+
serviceObject.methods.setMetadata = methodConfig;
743+
serviceObject.setMetadata({});
744+
});
745+
665746
it('should extend the defaults with request options', done => {
666747
const methodConfig = {
667748
reqOpts: {

0 commit comments

Comments
 (0)