Skip to content

Commit e58bf6b

Browse files
authored
Add logging capability to track whether the user is using Core or Gen SDK (#8385)
* Wrote tests for user agent * Added another test * Update API reports * Fixed linting errors * Fixed tests * Updated changelog * Updated changelog again * Updated formatting * Updated docgen
1 parent 6aae5f5 commit e58bf6b

File tree

12 files changed

+113
-256
lines changed

12 files changed

+113
-256
lines changed

common/api-review/data-connect.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ export function terminate(dataConnect: DataConnect): Promise<void>;
283283
export function toQueryRef<Data, Variables>(serializedRef: SerializedRef<Data, Variables>): QueryRef<Data, Variables>;
284284

285285
// @public (undocumented)
286-
export type TransportClass = new (options: DataConnectOptions, apiKey?: string, authProvider?: AuthTokenProvider, transportOptions?: TransportOptions) => DataConnectTransport;
286+
export type TransportClass = new (options: DataConnectOptions, apiKey?: string, authProvider?: AuthTokenProvider, transportOptions?: TransportOptions, _isUsingGen?: boolean) => DataConnectTransport;
287287

288288
// @public
289289
export interface TransportOptions {

docs-devsite/data-connect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,5 @@ export declare type ReferenceType = typeof QUERY_STR | typeof MUTATION_STR;
532532
<b>Signature:</b>
533533

534534
```typescript
535-
export declare type TransportClass = new (options: DataConnectOptions, apiKey?: string, authProvider?: AuthTokenProvider, transportOptions?: TransportOptions) => DataConnectTransport;
535+
export declare type TransportClass = new (options: DataConnectOptions, apiKey?: string, authProvider?: AuthTokenProvider, transportOptions?: TransportOptions, _isUsingGen?: boolean) => DataConnectTransport;
536536
```

packages/data-connect/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# @firebase/data-connect
22
## UNRELEASED
3-
* Updated reporting to use @firebase/data-connect instead of @firebase/connect
3+
* Updated reporting to use @firebase/data-connect instead of @firebase/connect.
44
* Added functionality to retry queries and mutations if the server responds with UNAUTHENTICATED.
5-
* Moved `validateArgs` to core SDK
5+
* Moved `validateArgs` to core SDK.
66
* Updated errors to only show relevant details to the user.
7+
* Added ability to track whether user is calling core sdk or generated sdk.

packages/data-connect/rollup.config.react.js

Lines changed: 0 additions & 238 deletions
This file was deleted.

packages/data-connect/src/api/DataConnect.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export class DataConnect {
8989
private _transportClass: TransportClass | undefined;
9090
private _transportOptions?: TransportOptions;
9191
private _authTokenProvider?: AuthTokenProvider;
92+
_isUsingGeneratedSdk: boolean = false;
9293
constructor(
9394
public readonly app: FirebaseApp,
9495
// TODO(mtewani): Replace with _dataConnectOptions in the future
@@ -104,6 +105,14 @@ export class DataConnect {
104105
}
105106
}
106107
}
108+
/*
109+
@internal
110+
*/
111+
_useGeneratedSdk(): void {
112+
if (!this._isUsingGeneratedSdk) {
113+
this._isUsingGeneratedSdk = true;
114+
}
115+
}
107116
_delete(): Promise<void> {
108117
_removeServiceInstance(
109118
this.app,
@@ -140,7 +149,9 @@ export class DataConnect {
140149
this._transport = new this._transportClass(
141150
this.dataConnectOptions,
142151
this.app.options.apiKey,
143-
this._authTokenProvider
152+
this._authTokenProvider,
153+
undefined,
154+
this._isUsingGeneratedSdk
144155
);
145156
if (this._transportOptions) {
146157
this._transport.useEmulator(

packages/data-connect/src/network/fetch.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,26 @@ let connectFetch: typeof fetch | null = globalThis.fetch;
2323
export function initializeFetch(fetchImpl: typeof fetch): void {
2424
connectFetch = fetchImpl;
2525
}
26-
function getGoogApiClientValue(): string {
27-
return 'gl-js/ fire/' + SDK_VERSION;
26+
function getGoogApiClientValue(_isUsingGen: boolean): string {
27+
let str = 'gl-js/ fire/' + SDK_VERSION;
28+
if (_isUsingGen) {
29+
str += ' web/gen';
30+
}
31+
return str;
2832
}
2933
export function dcFetch<T, U>(
3034
url: string,
3135
body: U,
3236
{ signal }: AbortController,
33-
accessToken: string | null
37+
accessToken: string | null,
38+
_isUsingGen: boolean
3439
): Promise<{ data: T; errors: Error[] }> {
3540
if (!connectFetch) {
3641
throw new DataConnectError(Code.OTHER, 'No Fetch Implementation detected!');
3742
}
3843
const headers: HeadersInit = {
3944
'Content-Type': 'application/json',
40-
'X-Goog-Api-Client': getGoogApiClientValue()
45+
'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen)
4146
};
4247
if (accessToken) {
4348
headers['X-Firebase-Auth-Token'] = accessToken;

packages/data-connect/src/network/transport/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export type TransportClass = new (
5252
options: DataConnectOptions,
5353
apiKey?: string,
5454
authProvider?: AuthTokenProvider,
55-
transportOptions?: TransportOptions
55+
transportOptions?: TransportOptions,
56+
_isUsingGen?: boolean
5657
) => DataConnectTransport;
5758
export * from '../../core/FirebaseAuthProvider';

packages/data-connect/src/network/transport/rest.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export class RESTTransport implements DataConnectTransport {
3939
options: DataConnectOptions,
4040
private apiKey?: string | undefined,
4141
private authProvider?: AuthTokenProvider | undefined,
42-
transportOptions?: TransportOptions | undefined
42+
transportOptions?: TransportOptions | undefined,
43+
private _isUsingGen = false
4344
) {
4445
if (transportOptions) {
4546
if (typeof transportOptions.port === 'number') {
@@ -166,12 +167,14 @@ export class RESTTransport implements DataConnectTransport {
166167
variables: body
167168
} as unknown as U, // TODO(mtewani): This is a patch, fix this.
168169
abortController,
169-
this._accessToken
170+
this._accessToken,
171+
this._isUsingGen
170172
)
171173
);
172174

173175
return {
174-
then: withAuth.then.bind(withAuth)
176+
then: withAuth.then.bind(withAuth),
177+
catch: withAuth.catch.bind(withAuth)
175178
};
176179
};
177180
invokeMutation: <T, U>(
@@ -191,7 +194,8 @@ export class RESTTransport implements DataConnectTransport {
191194
variables: body
192195
} as unknown as U,
193196
abortController,
194-
this._accessToken
197+
this._accessToken,
198+
this._isUsingGen
195199
);
196200
});
197201

packages/data-connect/test/unit/fetch.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('fetch', () => {
4040
message
4141
});
4242
await expect(
43-
dcFetch('http://localhost', {}, {} as AbortController, null)
43+
dcFetch('http://localhost', {}, {} as AbortController, null, false)
4444
).to.eventually.be.rejectedWith(message);
4545
});
4646
it('should throw a stringified message when the server responds with an error without a message property in the body', async () => {
@@ -51,7 +51,7 @@ describe('fetch', () => {
5151
};
5252
mockFetch(json);
5353
await expect(
54-
dcFetch('http://localhost', {}, {} as AbortController, null)
54+
dcFetch('http://localhost', {}, {} as AbortController, null, false)
5555
).to.eventually.be.rejectedWith(JSON.stringify(json));
5656
});
5757
});

0 commit comments

Comments
 (0)