Skip to content

Commit

Permalink
fix: support thenCall on promise returned by request for backward com…
Browse files Browse the repository at this point in the history
…patibility
  • Loading branch information
Codeneos committed Aug 4, 2023
1 parent a017ad6 commit 9cd3afc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 2 additions & 6 deletions packages/salesforce/src/connection/salesforceConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Connection, ConnectionOptions, Metadata } from 'jsforce';

import { Logger, LogLevel, LogManager } from '@vlocode/core';
import { resumeOnce, CustomError, wait, asArray, formatString, DeferredPromise, Timer, encodeRFC3986URI } from '@vlocode/util';
import { resumeOnce, CustomError, wait, asArray, formatString, DeferredPromise, Timer, encodeRFC3986URI, thenablePromise } from '@vlocode/util';
import { HttpMethod, HttpRequestInfo, HttpResponse, HttpTransport, Transport } from './httpTransport';
import { EventEmitter } from 'events';
import { SalesforceOAuth2 } from './oath2';
Expand Down Expand Up @@ -284,15 +284,12 @@ export class SalesforceConnection extends Connection {
request: HttpRequestInfo,
options: RequestOptions & { responseType: 'raw' }): Promise<HttpResponse>;

@thenablePromise()
public async request<T = any>(
info: string | HttpRequestInfo,
options?: RequestOptions | any,
callback?: any): Promise<T> {

if (callback || typeof options === 'function') {
throw new Error('Use promises instead of using a callback parameter');
}

const request = this.prepareRequest(info);
let attempts = 0;

Expand All @@ -305,7 +302,6 @@ export class SalesforceConnection extends Connection {
const canRetry = attempts++ < SalesforceConnection.maxRetries || SalesforceConnection.maxRetries < 0;

if (!canRetry || !this.isRetryableError(err)) {
//callback?.(err, undefined as any);
this.emit('error', err);
this.logger.error(err);
throw err;
Expand Down
2 changes: 2 additions & 0 deletions packages/salesforce/src/registry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.json
*.ts
31 changes: 31 additions & 0 deletions packages/util/src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,34 @@ export function preventParallel<T extends (...args: TArgs[]) => Promise<TReturn>
};
};
}

/**
* Make any Async function that returns a promise support the old NodeJS callback style.
* And return the promise that supports `thenCall` method.
* @returns
*/
export function thenablePromise<T extends (...args: TArgs[]) => Promise<TReturn>, TArgs = any, TReturn = any>(): MethodDecorator {
return function<K = T>(target: any, name: string | symbol, descriptor: TypedPropertyDescriptor<K>): TypedPropertyDescriptor<K> | void {
const value = descriptor.value;
if (typeof value !== 'function') {
return;
}

const decoratedMethod = function(...args: TArgs[]) {
const callback = args.length && typeof args[args.length - 1] === 'function'
? args.pop() as (...args: any[]) => any
: undefined;

const result = Object.assign(value.apply(this, args), { thenCall: cb => thenCall(result, cb) }) ;
if (callback) {
return result.thenCall(result);
}
return result;
}

return {
...descriptor,
value: decoratedMethod as K
};
};
}

0 comments on commit 9cd3afc

Please sign in to comment.