diff --git a/packages/data-connect/src/api/Mutation.ts b/packages/data-connect/src/api/Mutation.ts index ca2efdb7a30..570b28b0eab 100644 --- a/packages/data-connect/src/api/Mutation.ts +++ b/packages/data-connect/src/api/Mutation.ts @@ -76,7 +76,7 @@ export function mutationRef( * @internal */ export class MutationManager { - private _inflight: Array> = []; + private _inflight: Array> = []; constructor(private _transport: DataConnectTransport) {} executeMutation( mutationRef: MutationRef @@ -95,7 +95,7 @@ export class MutationManager { return obj; }); this._inflight.push(result); - const removePromise = (): Array> => + const removePromise = (): Array> => (this._inflight = this._inflight.filter(promise => promise !== result)); result.then(removePromise, removePromise); return withRefPromise; @@ -113,7 +113,7 @@ export interface MutationResult * Mutation return value from `executeMutation` */ export interface MutationPromise - extends PromiseLike> { + extends Promise> { // reserved for special actions like cancellation } diff --git a/packages/data-connect/src/api/query.ts b/packages/data-connect/src/api/query.ts index a4ab17b7ceb..00382686dab 100644 --- a/packages/data-connect/src/api/query.ts +++ b/packages/data-connect/src/api/query.ts @@ -67,7 +67,7 @@ export interface QueryResult * Promise returned from `executeQuery` */ export interface QueryPromise - extends PromiseLike> { + extends Promise> { // reserved for special actions like cancellation } @@ -124,7 +124,7 @@ export function queryRef( dataConnect: dcInstance, refType: QUERY_STR, name: queryName, - variables: variables as Variables + variables: variables }; } /** diff --git a/packages/data-connect/src/core/QueryManager.ts b/packages/data-connect/src/core/QueryManager.ts index 8b7c59aea85..f20dfaa44e9 100644 --- a/packages/data-connect/src/core/QueryManager.ts +++ b/packages/data-connect/src/core/QueryManager.ts @@ -105,10 +105,9 @@ export class QueryManager { variables: queryRef.variables, refType: QUERY_STR }); - const trackedQuery = this._queries.get(key) as TrackedQuery< - Data, - Variables - >; + const trackedQuery: TrackedQuery = this._queries.get( + key + ) as TrackedQuery; const subscription = { userCallback: onResultCallback, errCallback: onErrorCallback diff --git a/packages/data-connect/src/network/fetch.ts b/packages/data-connect/src/network/fetch.ts index 06ef6dd8285..d5d2a439432 100644 --- a/packages/data-connect/src/network/fetch.ts +++ b/packages/data-connect/src/network/fetch.ts @@ -30,9 +30,14 @@ function getGoogApiClientValue(_isUsingGen: boolean): string { } return str; } +export interface DataConnectFetchBody { + name: string; + operationName: string; + variables: T; +} export function dcFetch( url: string, - body: U, + body: DataConnectFetchBody, { signal }: AbortController, appId: string | null, accessToken: string | null, @@ -95,7 +100,7 @@ export function dcFetch( logError('DataConnect error while performing request: ' + stringified); throw new DataConnectError(Code.OTHER, stringified); } - return res as { data: T; errors: Error[] }; + return res; }); } interface MessageObject { diff --git a/packages/data-connect/src/network/transport/index.ts b/packages/data-connect/src/network/transport/index.ts index 5518faa0f95..bd7ce604ac5 100644 --- a/packages/data-connect/src/network/transport/index.ts +++ b/packages/data-connect/src/network/transport/index.ts @@ -26,16 +26,16 @@ export interface DataConnectTransport { invokeQuery( queryName: string, body?: U - ): PromiseLike<{ data: T; errors: Error[] }>; + ): Promise<{ data: T; errors: Error[] }>; invokeMutation( queryName: string, body?: U - ): PromiseLike<{ data: T; errors: Error[] }>; + ): Promise<{ data: T; errors: Error[] }>; useEmulator(host: string, port?: number, sslEnabled?: boolean): void; onTokenChanged: (token: string | null) => void; } -export interface CancellableOperation extends PromiseLike<{ data: T }> { +export interface CancellableOperation extends Promise<{ data: T }> { cancel: () => void; } diff --git a/packages/data-connect/src/network/transport/rest.ts b/packages/data-connect/src/network/transport/rest.ts index 85847868c5d..0a49fc9e269 100644 --- a/packages/data-connect/src/network/transport/rest.ts +++ b/packages/data-connect/src/network/transport/rest.ts @@ -161,7 +161,7 @@ export class RESTTransport implements DataConnectTransport { invokeQuery: ( queryName: string, body?: U - ) => PromiseLike<{ data: T; errors: Error[] }> = ( + ) => Promise<{ data: T; errors: Error[] }> = ( queryName: string, body: U ) => { @@ -174,7 +174,7 @@ export class RESTTransport implements DataConnectTransport { name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`, operationName: queryName, variables: body - } as unknown as U, // TODO(mtewani): This is a patch, fix this. + }, abortController, this.appId, this._accessToken, @@ -182,16 +182,12 @@ export class RESTTransport implements DataConnectTransport { this._isUsingGen ) ); - - return { - then: withAuth.then.bind(withAuth), - catch: withAuth.catch.bind(withAuth) - }; + return withAuth; }; invokeMutation: ( queryName: string, body?: U - ) => PromiseLike<{ data: T; errors: Error[] }> = ( + ) => Promise<{ data: T; errors: Error[] }> = ( mutationName: string, body: U ) => { @@ -203,7 +199,7 @@ export class RESTTransport implements DataConnectTransport { name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`, operationName: mutationName, variables: body - } as unknown as U, + }, abortController, this.appId, this._accessToken, @@ -211,12 +207,6 @@ export class RESTTransport implements DataConnectTransport { this._isUsingGen ); }); - - return { - then: taskResult.then.bind(taskResult), - // catch: taskResult.catch.bind(taskResult), - // finally: taskResult.finally.bind(taskResult), - cancel: () => abortController.abort() - }; + return taskResult; }; } diff --git a/packages/data-connect/test/unit/fetch.test.ts b/packages/data-connect/test/unit/fetch.test.ts index a50ac188724..3d9a9b04523 100644 --- a/packages/data-connect/test/unit/fetch.test.ts +++ b/packages/data-connect/test/unit/fetch.test.ts @@ -42,7 +42,11 @@ describe('fetch', () => { await expect( dcFetch( 'http://localhost', - {}, + { + name: 'n', + operationName: 'n', + variables: {} + }, {} as AbortController, null, null, @@ -61,7 +65,11 @@ describe('fetch', () => { await expect( dcFetch( 'http://localhost', - {}, + { + name: 'n', + operationName: 'n', + variables: {} + }, {} as AbortController, null, null,