Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to include promise instead of promiselike #8667

Merged
merged 14 commits into from
Jan 9, 2025
Merged
6 changes: 6 additions & 0 deletions .changeset/tame-tigers-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/data-connect": minor
"firebase": minor
---

Updated to include promise instead of promiselike
12 changes: 2 additions & 10 deletions common/api-review/data-connect.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import { FirebaseError } from '@firebase/util';
import { LogLevelString } from '@firebase/logger';
import { Provider } from '@firebase/component';

// @public (undocumented)
export interface CancellableOperation<T> extends PromiseLike<{
data: T;
}> {
// (undocumented)
cancel: () => void;
}

// @public
export function connectDataConnectEmulator(dc: DataConnect, host: string, port?: number, sslEnabled?: boolean): void;

Expand Down Expand Up @@ -88,7 +80,7 @@ export function getDataConnect(app: FirebaseApp, options: ConnectorConfig): Data
export const MUTATION_STR = "mutation";

// @public
export interface MutationPromise<Data, Variables> extends PromiseLike<MutationResult<Data, Variables>> {
export interface MutationPromise<Data, Variables> extends Promise<MutationResult<Data, Variables>> {
}

// @public (undocumented)
Expand Down Expand Up @@ -144,7 +136,7 @@ export interface OpResult<Data> {
export const QUERY_STR = "query";

// @public
export interface QueryPromise<Data, Variables> extends PromiseLike<QueryResult<Data, Variables>> {
export interface QueryPromise<Data, Variables> extends Promise<QueryResult<Data, Variables>> {
}

// @public
Expand Down
6 changes: 3 additions & 3 deletions packages/data-connect/src/api/Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function mutationRef<Data, Variables>(
* @internal
*/
export class MutationManager {
private _inflight: Array<PromiseLike<unknown>> = [];
private _inflight: Array<Promise<unknown>> = [];
constructor(private _transport: DataConnectTransport) {}
executeMutation<Data, Variables>(
mutationRef: MutationRef<Data, Variables>
Expand All @@ -95,7 +95,7 @@ export class MutationManager {
return obj;
});
this._inflight.push(result);
const removePromise = (): Array<PromiseLike<unknown>> =>
const removePromise = (): Array<Promise<unknown>> =>
(this._inflight = this._inflight.filter(promise => promise !== result));
result.then(removePromise, removePromise);
return withRefPromise;
Expand All @@ -113,7 +113,7 @@ export interface MutationResult<Data, Variables>
* Mutation return value from `executeMutation`
*/
export interface MutationPromise<Data, Variables>
extends PromiseLike<MutationResult<Data, Variables>> {
extends Promise<MutationResult<Data, Variables>> {
// reserved for special actions like cancellation
}

Expand Down
4 changes: 2 additions & 2 deletions packages/data-connect/src/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface QueryResult<Data, Variables>
* Promise returned from `executeQuery`
*/
export interface QueryPromise<Data, Variables>
extends PromiseLike<QueryResult<Data, Variables>> {
extends Promise<QueryResult<Data, Variables>> {
// reserved for special actions like cancellation
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export function queryRef<Data, Variables>(
dataConnect: dcInstance,
refType: QUERY_STR,
name: queryName,
variables: variables as Variables
variables: variables
};
}
/**
Expand Down
9 changes: 7 additions & 2 deletions packages/data-connect/src/network/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ function getGoogApiClientValue(_isUsingGen: boolean): string {
}
return str;
}
export interface DataConnectFetchBody<T> {
name: string;
operationName: string;
variables: T;
}
export function dcFetch<T, U>(
url: string,
body: U,
body: DataConnectFetchBody<U>,
{ signal }: AbortController,
appId: string | null,
accessToken: string | null,
Expand Down Expand Up @@ -95,7 +100,7 @@ export function dcFetch<T, U>(
logError('DataConnect error while performing request: ' + stringified);
throw new DataConnectError(Code.OTHER, stringified);
}
return res as { data: T; errors: Error[] };
return res;
});
}
interface MessageObject {
Expand Down
8 changes: 2 additions & 6 deletions packages/data-connect/src/network/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ export interface DataConnectTransport {
invokeQuery<T, U>(
queryName: string,
body?: U
): PromiseLike<{ data: T; errors: Error[] }>;
): Promise<{ data: T; errors: Error[] }>;
invokeMutation<T, U>(
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<T> extends PromiseLike<{ data: T }> {
cancel: () => void;
}

/**
* @internal
*/
Expand Down
22 changes: 6 additions & 16 deletions packages/data-connect/src/network/transport/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class RESTTransport implements DataConnectTransport {
invokeQuery: <T, U>(
queryName: string,
body?: U
) => PromiseLike<{ data: T; errors: Error[] }> = <T, U = unknown>(
) => Promise<{ data: T; errors: Error[] }> = <T, U = unknown>(
queryName: string,
body: U
) => {
Expand All @@ -174,24 +174,20 @@ 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,
this._appCheckToken,
this._isUsingGen
)
);

return {
then: withAuth.then.bind(withAuth),
catch: withAuth.catch.bind(withAuth)
};
return withAuth;
};
invokeMutation: <T, U>(
queryName: string,
body?: U
) => PromiseLike<{ data: T; errors: Error[] }> = <T, U = unknown>(
) => Promise<{ data: T; errors: Error[] }> = <T, U = unknown>(
mutationName: string,
body: U
) => {
Expand All @@ -203,20 +199,14 @@ 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,
this._appCheckToken,
this._isUsingGen
);
});

return {
then: taskResult.then.bind(taskResult),
// catch: taskResult.catch.bind(taskResult),
// finally: taskResult.finally.bind(taskResult),
cancel: () => abortController.abort()
};
return taskResult;
};
}
12 changes: 10 additions & 2 deletions packages/data-connect/test/unit/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ describe('fetch', () => {
await expect(
dcFetch(
'http://localhost',
{},
{
name: 'n',
operationName: 'n',
variables: {}
},
{} as AbortController,
null,
null,
Expand All @@ -61,7 +65,11 @@ describe('fetch', () => {
await expect(
dcFetch(
'http://localhost',
{},
{
name: 'n',
operationName: 'n',
variables: {}
},
{} as AbortController,
null,
null,
Expand Down
Loading