From 9f55ab3ae0c3d3f2d0775bf72ac3d8ca3eb1ae7d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 25 May 2021 10:47:17 +0200 Subject: [PATCH] fix(shell-api): allow ShellApi methods to be called with new MONGOSH-786 Use plain functions instead of arrow functions so that they can be used with new as well. --- packages/shell-api/src/shell-api.spec.ts | 5 +++++ packages/shell-api/src/shell-internal-state.ts | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/shell-api/src/shell-api.spec.ts b/packages/shell-api/src/shell-api.spec.ts index 312a2c475f..8eff7898b7 100644 --- a/packages/shell-api/src/shell-api.spec.ts +++ b/packages/shell-api/src/shell-api.spec.ts @@ -484,6 +484,11 @@ describe('ShellApi', () => { expect((await toShellResult(m)).type).to.equal('Mongo'); expect(m._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000'); }); + it('returns a new Mongo object with new', async() => { + const m = await new internalState.context.Mongo('mongodb://127.0.0.1:27017'); + expect((await toShellResult(m)).type).to.equal('Mongo'); + expect(m._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000'); + }); it('fails for non-CLI', async() => { serviceProvider.platform = ReplPlatform.Browser; try { diff --git a/packages/shell-api/src/shell-internal-state.ts b/packages/shell-api/src/shell-internal-state.ts index 65faa2c6e9..eb794650e3 100644 --- a/packages/shell-api/src/shell-internal-state.ts +++ b/packages/shell-api/src/shell-internal-state.ts @@ -181,14 +181,15 @@ export default class ShellInternalState { this.context = contextObject; Object.assign(contextObject, this.shellApi); for (const name of Object.getOwnPropertyNames(ShellApi.prototype)) { + const { shellApi } = this; if (toIgnore.concat(['help']).includes(name) || - typeof (this.shellApi as any)[name] !== 'function') { + typeof (shellApi as any)[name] !== 'function') { continue; } - contextObject[name] = (...args: any[]): any => { - return (this.shellApi as any)[name](...args); + contextObject[name] = function(...args: any[]): any { + return (shellApi as any)[name](...args); }; - contextObject[name].help = (this.shellApi as any)[name].help; + contextObject[name].help = (shellApi as any)[name].help; } contextObject.help = this.shellApi.help; Object.assign(contextObject, this.shellBson);