Skip to content

Commit

Permalink
Add overloads to functions returning void or Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Loghorn authored and pierreca committed Nov 29, 2018
1 parent fcf3c0f commit 80f14df
Show file tree
Hide file tree
Showing 28 changed files with 276 additions and 24 deletions.
3 changes: 2 additions & 1 deletion common/core/src/authentication_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export enum AuthenticationType {
*/
export interface AuthenticationProvider {
type: AuthenticationType;
getDeviceCredentials(callback?: Callback<TransportConfig>): Promise<TransportConfig> | void;
getDeviceCredentials(callback: Callback<TransportConfig>): void;
getDeviceCredentials(): Promise<TransportConfig>;
}
24 changes: 24 additions & 0 deletions common/core/src/promise_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export type HttpResponseCallback<TResult> = TripleValueCallback<TResult, any>;
* const param = 42;
* callbackToPromise((_callback) => foo(param, _callback)).then(result => { console.log(result); }, error => { console.error(error); });
*/
export function callbackToPromise<TResult>(callBackOperation: (callback: Callback<TResult>) => void, userCallback: Callback<TResult>): void;
export function callbackToPromise<TResult>(callBackOperation: (callback: Callback<TResult>) => void): Promise<TResult>;
export function callbackToPromise<TResult>(callBackOperation: (callback: Callback<TResult>) => void, userCallback?: Callback<TResult>): Promise<TResult> | void {
if (userCallback) {
if (!(typeof userCallback === 'function')) {
Expand Down Expand Up @@ -115,6 +117,8 @@ export function callbackToPromise<TResult>(callBackOperation: (callback: Callbac
* const param = 42;
* errorCallbackToPromise((_callback) => foo(param, _callback)).then(_ => { }, err => { console.log(err); });
*/
export function errorCallbackToPromise(callBackOperation: (callback: ErrorCallback) => void, userCallback: ErrorCallback): void;
export function errorCallbackToPromise(callBackOperation: (callback: ErrorCallback) => void): Promise<void>;
export function errorCallbackToPromise(callBackOperation: (callback: ErrorCallback) => void, userCallback?: ErrorCallback): Promise<void> | void {
return callbackToPromise(callBackOperation, userCallback);
}
Expand Down Expand Up @@ -142,6 +146,8 @@ export function errorCallbackToPromise(callBackOperation: (callback: ErrorCallba
* const param = 42;
* noErrorCallbackToPromise((_callback) => foo(param, _callback)).then(result => { console.log(result); }, err => { console.log("it never rejects"); });
*/
export function noErrorCallbackToPromise<TResult>(callBackOperation: (callback: NoErrorCallback<TResult>) => void, userCallback: NoErrorCallback<TResult>): void;
export function noErrorCallbackToPromise<TResult>(callBackOperation: (callback: NoErrorCallback<TResult>) => void): Promise<TResult>;
export function noErrorCallbackToPromise<TResult>(callBackOperation: (callback: NoErrorCallback<TResult>) => void, userCallback?: NoErrorCallback<TResult>): Promise<TResult> | void {
if (userCallback) {
if (!(typeof userCallback === 'function')) {
Expand Down Expand Up @@ -188,6 +194,13 @@ export function noErrorCallbackToPromise<TResult>(callBackOperation: (callback:
* const param = 42;
* doubleValueCallbackToPromise((_callback) => foo(param, _callback), pack).then(result => { console.log(result); }, err => { console.error(error); });
*/
export function doubleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>(
callBackOperation: (callback: DoubleValueCallback<TResult1, TResult2>) => void,
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult,
userCallback: DoubleValueCallback<TResult1, TResult2>): void;
export function doubleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>(
callBackOperation: (callback: DoubleValueCallback<TResult1, TResult2>) => void,
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult): Promise<TPromiseResult>;
export function doubleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>(
callBackOperation: (callback: DoubleValueCallback<TResult1, TResult2>) => void,
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult,
Expand Down Expand Up @@ -249,6 +262,13 @@ export function doubleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>
* const param = 42;
* tripleValueCallbackToPromise((_callback) => foo(param, _callback), pack).then(result => { console.log(result); }, err => { console.error(error); });
*/
export function tripleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>(
callbackOperation: (callback: TripleValueCallback<TResult1, TResult2>) => void,
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult,
userCallback: TripleValueCallback<TResult1, TResult2>): void;
export function tripleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>(
callbackOperation: (callback: TripleValueCallback<TResult1, TResult2>) => void,
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult, ): Promise<TPromiseResult>;
export function tripleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>(
callbackOperation: (callback: TripleValueCallback<TResult1, TResult2>) => void,
packResults: (result1: TResult1, result2: TResult2) => TPromiseResult,
Expand Down Expand Up @@ -288,6 +308,10 @@ export function tripleValueCallbackToPromise<TResult1, TResult2, TPromiseResult>
* @returns {Promise<TResult> | void} Promise with result of TResult type or void if user's callback provided
* @template TResult - Type of the response body result.
*/
export function httpCallbackToPromise<TResult>(
callbackOperation: (callback: HttpResponseCallback<TResult>) => void, callback: HttpResponseCallback<TResult>): void;
export function httpCallbackToPromise<TResult>(
callbackOperation: (callback: HttpResponseCallback<TResult>) => void): Promise<ResultWithHttpResponse<TResult>>;
export function httpCallbackToPromise<TResult>(
callbackOperation: (callback: HttpResponseCallback<TResult>) => void,
callback?: HttpResponseCallback<TResult>): Promise<ResultWithHttpResponse<TResult>> | void {
Expand Down
10 changes: 8 additions & 2 deletions device/core/src/blob_upload/blob_upload_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@ export interface UploadParams {
* @private
*/
export interface FileUpload {
getBlobSharedAccessSignature(blobName: string, done?: Callback<UploadParams>): void;
getBlobSharedAccessSignature(blobName: string, done: Callback<UploadParams>): void;
getBlobSharedAccessSignature(blobName: string): Promise<UploadParams>;
notifyUploadComplete(correlationId: string, uploadResult: BlobUploadResult, done: (err?: Error) => void): void;
notifyUploadComplete(correlationId: string, uploadResult: BlobUploadResult): Promise<void>;
}

/**
* @private
*/
export interface BlobUploader {
uploadToBlob(uploadParams: UploadParams, stream: Stream, streamLength: number, done?: TripleValueCallback<any, BlobResponse>): void;
uploadToBlob(uploadParams: UploadParams, stream: Stream, streamLength: number, done: TripleValueCallback<any, BlobResponse>): void;
uploadToBlob(uploadParams: UploadParams, stream: Stream, streamLength: number): Promise<any>;
}

/**
* @private
*/
export interface BlobUpload {
uploadToBlob(blobName: string, stream: Stream, streamLength: number, done: (err?: Error) => void): void;
uploadToBlob(blobName: string, stream: Stream, streamLength: number): Promise<void>;
}

/**
Expand All @@ -65,6 +69,8 @@ export class BlobUploadClient implements BlobUpload {
this._blobUploader = blobUploader ? blobUploader : new DefaultBlobUploader();
}

uploadToBlob(blobName: string, stream: Stream, streamLength: number, done: ErrorCallback): void;
uploadToBlob(blobName: string, stream: Stream, streamLength: number): Promise<void>;
uploadToBlob(blobName: string, stream: Stream, streamLength: number, done?: ErrorCallback): Promise<void> | void {
return errorCallbackToPromise((_callback) => {
/*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_CLIENT_16_004: [`uploadToBlob` shall obtain a blob SAS token using the IoT Hub service file upload API endpoint.]*/
Expand Down
2 changes: 2 additions & 0 deletions device/core/src/blob_upload/blob_uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class BlobUploader implements BlobUploaderInterface {
}
}

uploadToBlob(blobInfo: UploadParams, stream: Stream, streamLength: number, done: TripleValueCallback<any, BlobResponse>): void;
uploadToBlob(blobInfo: UploadParams, stream: Stream, streamLength: number): Promise<{ body: any, result: BlobResponse }>;
uploadToBlob(blobInfo: UploadParams, stream: Stream, streamLength: number, done?: TripleValueCallback<any, BlobResponse>): Promise<{ body: any, result: BlobResponse }> | void {
tripleValueCallbackToPromise((_callback) => {
/*Codes_SRS_NODE_DEVICE_BLOB_UPLOAD_16_001: [`uploadToBlob` shall throw a `ReferenceError` if `blobInfo` is falsy.]*/
Expand Down
4 changes: 4 additions & 0 deletions device/core/src/blob_upload/file_upload_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class FileUploadApi implements FileUploadInterface {
this.http = httpTransport ? httpTransport : new DefaultHttpTransport();
}

getBlobSharedAccessSignature(blobName: string, done: Callback<UploadParams>): void;
getBlobSharedAccessSignature(blobName: string): Promise<UploadParams>;
getBlobSharedAccessSignature(blobName: string, done?: Callback<UploadParams>): Promise<UploadParams> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_FILE_UPLOAD_ENDPOINT_16_004: [`getBlobSharedAccessSignature` shall throw a `ReferenceError` if `blobName` is falsy.]*/
Expand Down Expand Up @@ -93,6 +95,8 @@ export class FileUploadApi implements FileUploadInterface {
}, done);
}

notifyUploadComplete(correlationId: string, uploadResult: BlobUploadResult, done: ErrorCallback): void;
notifyUploadComplete(correlationId: string, uploadResult: BlobUploadResult): Promise<void>;
notifyUploadComplete(correlationId: string, uploadResult: BlobUploadResult, done?: ErrorCallback): Promise<void> | void {
return errorCallbackToPromise((_callback) => {
/*Codes_SRS_NODE_FILE_UPLOAD_ENDPOINT_16_010: [`notifyUploadComplete` shall throw a `ReferenceError` if `correlationId` is falsy.]*/
Expand Down
10 changes: 7 additions & 3 deletions device/core/src/device_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export class Client extends InternalClient {
* @param {Callback<results.Disconnected>} [closeCallback] Optional function to call once the transport is disconnected and the client closed.
* @returns {Promise<results.Disconnected> | void} Promise if no callback function was passed, void otherwise.
*/
close(closeCallback: Callback<results.Disconnected>): void;
close(): Promise<results.Disconnected>;
close(closeCallback?: Callback<results.Disconnected>): Promise<results.Disconnected> | void {
return callbackToPromise((_callback) => {
this._transport.removeListener('disconnect', this._deviceDisconnectHandler);
Expand Down Expand Up @@ -127,6 +129,8 @@ export class Client extends InternalClient {
*
* @throws {ReferenceException} If blobName or stream or streamLength is falsy.
*/
uploadToBlob(blobName: string, stream: Stream, streamLength: number, callback: ErrorCallback): void;
uploadToBlob(blobName: string, stream: Stream, streamLength: number): Promise<void>;
uploadToBlob(blobName: string, stream: Stream, streamLength: number, callback?: ErrorCallback): Promise<void> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_DEVICE_CLIENT_16_037: [The `uploadToBlob` method shall throw a `ReferenceError` if `blobName` is falsy.]*/
Expand Down Expand Up @@ -186,7 +190,7 @@ export class Client extends InternalClient {
*
* @returns {module:azure-iot-device.Client}
*/
static fromConnectionString(connStr: string, transportCtor: any): any {
static fromConnectionString(connStr: string, transportCtor: any): Client {
/*Codes_SRS_NODE_DEVICE_CLIENT_05_003: [The fromConnectionString method shall throw ReferenceError if the connStr argument is falsy.]*/
if (!connStr) throw new ReferenceError('connStr is \'' + connStr + '\'');

Expand Down Expand Up @@ -225,7 +229,7 @@ export class Client extends InternalClient {
*
* @returns {module:azure-iothub.Client}
*/
static fromSharedAccessSignature(sharedAccessSignature: string, transportCtor: any): any {
static fromSharedAccessSignature(sharedAccessSignature: string, transportCtor: any): Client {
/*Codes_SRS_NODE_DEVICE_CLIENT_16_029: [The fromSharedAccessSignature method shall throw a ReferenceError if the sharedAccessSignature argument is falsy.] */
if (!sharedAccessSignature) throw new ReferenceError('sharedAccessSignature is \'' + sharedAccessSignature + '\'');

Expand All @@ -242,7 +246,7 @@ export class Client extends InternalClient {
* @param authenticationProvider Object used to obtain the authentication parameters for the IoT hub.
* @param transportCtor Transport protocol used to connect to IoT hub.
*/
static fromAuthenticationProvider(authenticationProvider: AuthenticationProvider, transportCtor: any): any {
static fromAuthenticationProvider(authenticationProvider: AuthenticationProvider, transportCtor: any): Client {
/*Codes_SRS_NODE_DEVICE_CLIENT_16_089: [The `fromAuthenticationProvider` method shall throw a `ReferenceError` if the `authenticationProvider` argument is falsy.]*/
if (!authenticationProvider) {
throw new ReferenceError('authenticationMethod cannot be \'' + authenticationProvider + '\'');
Expand Down
3 changes: 3 additions & 0 deletions device/core/src/device_method/device_method_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export class DeviceMethodResponse {
* service in a previous call to it. This method
* should be called only once.
*/
send(status: number, payload: any, done: ErrorCallback): void;
send(status: number, done: ErrorCallback): void;
send(status: number, payload?: any): Promise<void>;
send(status: number, payload?: any | ErrorCallback, done?: ErrorCallback): Promise<void> | void {
if (typeof (payload) === 'function') {
if (done !== undefined) {
Expand Down
2 changes: 2 additions & 0 deletions device/core/src/device_method/method_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class MethodClient {
};
}

invokeMethod(deviceId: string, moduleId: string, methodParams: MethodParams, callback: MethodCallback): void;
invokeMethod(deviceId: string, moduleId: string, methodParams: MethodParams): Promise<MethodResult>;
invokeMethod(deviceId: string, moduleId: string, methodParams: MethodParams, callback?: MethodCallback): Promise<MethodResult> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_DEVICE_METHOD_CLIENT_16_006: [The `invokeMethod` method shall get the latest credentials by calling `getDeviceCredentials` on the `AuthenticationProvider` object.]*/
Expand Down
24 changes: 22 additions & 2 deletions device/core/src/internal_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export abstract class InternalClient extends EventEmitter {
err - null
response - a transport-specific response object]*/

updateSharedAccessSignature(sharedAccessSignature: string, updateSasCallback?: (err?: Error, result?: results.SharedAccessSignatureUpdated) => void): void {
updateSharedAccessSignature(sharedAccessSignature: string, updateSasCallback?: Callback<results.SharedAccessSignatureUpdated>): void {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_031: [The updateSharedAccessSignature method shall throw a ReferenceError if the sharedAccessSignature parameter is falsy.]*/
if (!sharedAccessSignature) throw new ReferenceError('sharedAccessSignature is falsy');

Expand All @@ -137,6 +137,8 @@ export abstract class InternalClient extends EventEmitter {
});
}

open(openCallback: Callback<results.Connected>): void;
open(): Promise<results.Connected>;
open(openCallback?: Callback<results.Connected>): Promise<results.Connected> | void {
return callbackToPromise((_callback) => {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
Expand All @@ -149,6 +151,8 @@ export abstract class InternalClient extends EventEmitter {
}, openCallback);
}

sendEvent(message: Message, sendEventCallback: Callback<results.MessageEnqueued>): void;
sendEvent(message: Message): Promise<results.MessageEnqueued>;
sendEvent(message: Message, sendEventCallback?: Callback<results.MessageEnqueued>): Promise<results.MessageEnqueued> | void {
return callbackToPromise((_callback) => {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
Expand All @@ -161,6 +165,8 @@ export abstract class InternalClient extends EventEmitter {
}, sendEventCallback);
}

sendEventBatch(messages: Message[], sendEventBatchCallback: Callback<results.MessageEnqueued>): void;
sendEventBatch(messages: Message[]): Promise<results.MessageEnqueued>;
sendEventBatch(messages: Message[], sendEventBatchCallback?: Callback<results.MessageEnqueued>): Promise<results.MessageEnqueued> | void {
return callbackToPromise((_callback) => {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
Expand All @@ -173,6 +179,8 @@ export abstract class InternalClient extends EventEmitter {
}, sendEventBatchCallback);
}

close(closeCallback: Callback<results.Disconnected>): void;
close(): Promise<results.Disconnected>;
close(closeCallback?: Callback<results.Disconnected>): Promise<results.Disconnected> | void {
return callbackToPromise((_callback) => {
this._closeTransport((err, result) => {
Expand All @@ -181,6 +189,8 @@ export abstract class InternalClient extends EventEmitter {
}, closeCallback);
}

setTransportOptions(options: any, done: Callback<results.TransportConfigured>): void;
setTransportOptions(options: any): Promise<results.TransportConfigured>;
setTransportOptions(options: any, done?: Callback<results.TransportConfigured>): Promise<results.TransportConfigured> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_024: [The ‘setTransportOptions’ method shall throw a ‘ReferenceError’ if the options object is falsy] */
Expand Down Expand Up @@ -214,6 +224,8 @@ export abstract class InternalClient extends EventEmitter {
* @param [done] The optional callback to call once the options have been set.
* @returns {Promise<results.TransportConfigured> | void} Promise if no callback function was passed, void otherwise.
*/
setOptions(options: DeviceClientOptions, done: Callback<results.TransportConfigured>): void;
setOptions(options: DeviceClientOptions): Promise<results.TransportConfigured>;
setOptions(options: DeviceClientOptions, done?: Callback<results.TransportConfigured>): Promise<results.TransportConfigured> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_042: [The `setOptions` method shall throw a `ReferenceError` if the options object is falsy.]*/
Expand All @@ -239,6 +251,8 @@ export abstract class InternalClient extends EventEmitter {
}, done);
}

complete(message: Message, completeCallback: Callback<results.MessageCompleted>): void;
complete(message: Message): Promise<results.MessageCompleted>;
complete(message: Message, completeCallback?: Callback<results.MessageCompleted>): Promise<results.MessageCompleted> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_016: [The ‘complete’ method shall throw a ReferenceError if the ‘message’ parameter is falsy.] */
Expand All @@ -253,6 +267,8 @@ export abstract class InternalClient extends EventEmitter {
}, completeCallback);
}

reject(message: Message, rejectCallback: Callback<results.MessageRejected>): void;
reject(message: Message): Promise<results.MessageRejected>;
reject(message: Message, rejectCallback?: Callback<results.MessageRejected>): Promise<results.MessageRejected> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_018: [The reject method shall throw a ReferenceError if the ‘message’ parameter is falsy.] */
Expand All @@ -266,6 +282,8 @@ export abstract class InternalClient extends EventEmitter {
}, rejectCallback);
}

abandon(message: Message, abandonCallback: Callback<results.MessageAbandoned>): void;
abandon(message: Message): Promise<results.MessageAbandoned>;
abandon(message: Message, abandonCallback?: Callback<results.MessageAbandoned>): Promise<results.MessageAbandoned> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_017: [The abandon method shall throw a ReferenceError if the ‘message’ parameter is falsy.] */
Expand All @@ -280,7 +298,9 @@ export abstract class InternalClient extends EventEmitter {
}, abandonCallback);
}

getTwin(done: Callback<Twin>): Promise<Twin> | void {
getTwin(done: Callback<Twin>): void;
getTwin(): Promise<Twin>;
getTwin(done?: Callback<Twin>): Promise<Twin> | void {
return callbackToPromise((_callback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_094: [If this is the first call to `getTwin` the method shall instantiate a new `Twin` object and pass it the transport currently in use.]*/
if (!this._twin) {
Expand Down
Loading

0 comments on commit 80f14df

Please sign in to comment.