1
\$\begingroup\$

I have made a class which wraps the JSForce Salesforce API Client:

import jsforce = require('jsforce');

export class SalesforcePortalClient {
  salesforceConsumerKey: string;
  salesforceConsumerSecret: string;
  salesforceUsername: string;
  salesforcePassword: string;
  constructor(salesforceConsumerKey: string, salesforceConsumerSecret: string, salesforceUsername: string, salesforcePassword: string) {
    this.salesforceConsumerKey = salesforceConsumerKey,
      this.salesforceConsumerSecret = salesforceConsumerSecret,
      this.salesforceUsername = salesforceUsername,
      this.salesforcePassword = salesforcePassword
  }


  private getConnection(): jsforce.Connection {
    return new jsforce.Connection({
      oauth2: {
        clientId: this.salesforceConsumerKey,
        clientSecret: this.salesforceConsumerSecret,
        redirectUri: 'http://localhost:3000/callback',
        loginUrl: 'https://test.salesforce.com'
      }
    })
  }

  private async login(): Promise<jsforce.Connection> {
    let conn = this.getConnection()
    await conn.login(this.salesforceUsername!, this.salesforcePassword!)
    return conn
  }

  async getInstanceId(): Promise<string> {
    return this.login().then(l => l.instanceUrl)
  }

  async postApexRest(projectTitle: string, organisationName: string): Promise<any> {
    return await this.login().then(l => l.apex.post('/loadData/', this.buildPayload(organisationName, projectTitle)))
  }

  private buildPayload = (organisationName: string, projectName: string) => {
    return {
      "projectName": projectName,
      "organisationName": organisationName,
    }
  }
}

I have some ideas about how this could be improved but not sure how to implement them, for example:

  • Is there a way to perform login() either when the class is instantiated or when it is first used and make it available at class level, so the token can be re-used rather than re-created on every request?

  • It seems like there is a fair amount of boilerplate in the arguments and constructor, is there anyway to reduce this whilst maintaining the same behaviour?

Also, I would welcome any suggestions on ways to improve this, as someone new to TypeScript.

\$\endgroup\$
1
  • \$\begingroup\$ Why did you feel the need for a wrapper? \$\endgroup\$ – Mast Sep 25 '19 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.