Skip to content

Commit

Permalink
add custom headers to track web frameworks usage
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenarosaj committed Jan 28, 2025
1 parent dafae52 commit cdf7b39
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
26 changes: 25 additions & 1 deletion packages/data-connect/src/api/DataConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export class DataConnect {
private _transportOptions?: TransportOptions;
private _authTokenProvider?: AuthTokenProvider;
_isUsingGeneratedSdk: boolean = false;
_isUsingTanStackSdk: boolean = false;

This comment has been minimized.

Copy link
@stephenarosaj

stephenarosaj Jan 29, 2025

Author Contributor

i split TanStack from the frameworks because i was trying to follow the bug's description closely, but based on conversation with Maneesh, it seems like when using the AngularFire SDK, users will pretty much always be using the TanStack bindings - that's already the case with the React SDK, so maybe this shouldn't be tracked separately?

_isUsingReactSdk: boolean = false;
_isUsingAngularFireSdk: boolean = false;
private _appCheckTokenProvider?: AppCheckTokenProvider;
// @internal
constructor(
Expand All @@ -116,6 +119,24 @@ export class DataConnect {
this._isUsingGeneratedSdk = true;
}
}
// @internal
_useTanStackSdk(): void {
if (!this._isUsingTanStackSdk) {
this._isUsingTanStackSdk = true;
}
}
// @internal
_useReactSdk(): void {
if (!this._isUsingReactSdk) {
this._isUsingReactSdk = true;
}
}
// @internal
_useAngularFireSdk(): void {
if (!this._isUsingAngularFireSdk) {
this._isUsingAngularFireSdk = true;
}
}
_delete(): Promise<void> {
_removeServiceInstance(
this.app,
Expand Down Expand Up @@ -164,7 +185,10 @@ export class DataConnect {
this._authTokenProvider,
this._appCheckTokenProvider,
undefined,
this._isUsingGeneratedSdk
this._isUsingGeneratedSdk,
this._isUsingTanStackSdk,
this._isUsingReactSdk,
this._isUsingAngularFireSdk
);
if (this._transportOptions) {
this._transport.useEmulator(
Expand Down
33 changes: 31 additions & 2 deletions packages/data-connect/src/network/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ function getGoogApiClientValue(_isUsingGen: boolean): string {
}
return str;
}
function getWebFrameworkValue(

This comment has been minimized.

Copy link
@stephenarosaj

stephenarosaj Jan 29, 2025

Author Contributor

I'm not sure if these header values are proper/useful - looking for feedback. The values would end up being something like:

  • "vanilla/"
  • "tanstack/ react/"
  • "angularfire/"
  • "tanstack/ react/ angularfire/"
_isUsingTanStack: boolean,
_isUsingReact: boolean,
_isUsingAngularFire: boolean
): string {
let str = '';
if (_isUsingTanStack) {
str += ' tanstack/';
}
if (_isUsingReact) {
str += ' react/';
}
if (_isUsingAngularFire) {
str += ' angularfire/';
}
// no framework SDK used
if (str === '') {
str = 'vanilla/';
}
return str.trim();
}
export interface DataConnectFetchBody<T> {
name: string;
operationName: string;
Expand All @@ -42,14 +63,22 @@ export function dcFetch<T, U>(
appId: string | null,
accessToken: string | null,
appCheckToken: string | null,
_isUsingGen: boolean
_isUsingGen: boolean,
_isUsingTanStack: boolean,
_isUsingReact: boolean,
_isUsingAngularFire: boolean
): Promise<{ data: T; errors: Error[] }> {
if (!connectFetch) {
throw new DataConnectError(Code.OTHER, 'No Fetch Implementation detected!');
}
const headers: HeadersInit = {
'Content-Type': 'application/json',
'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen)
'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen),
'X-Firebase-DataConnect-Web-Frameworks': getWebFrameworkValue(

This comment has been minimized.

Copy link
@stephenarosaj

stephenarosaj Jan 29, 2025

Author Contributor

I'm not sure about naming conventions for custom headers - looking for feedback here

_isUsingTanStack,
_isUsingReact,
_isUsingAngularFire
)
};
if (accessToken) {
headers['X-Firebase-Auth-Token'] = accessToken;
Expand Down
5 changes: 4 additions & 1 deletion packages/data-connect/src/network/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ export type TransportClass = new (
authProvider?: AuthTokenProvider,
appCheckProvider?: AppCheckTokenProvider,
transportOptions?: TransportOptions,
_isUsingGen?: boolean
_isUsingGen?: boolean,
_isUsingTanStack?: boolean,
_isUsingReact?: boolean,
_isUsingAngularFire?: boolean
) => DataConnectTransport;
15 changes: 12 additions & 3 deletions packages/data-connect/src/network/transport/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export class RESTTransport implements DataConnectTransport {
private authProvider?: AuthTokenProvider | undefined,
private appCheckProvider?: AppCheckTokenProvider | undefined,
transportOptions?: TransportOptions | undefined,
private _isUsingGen = false
private _isUsingGen = false,
private _isUsingReact = false,
private _isUsingAngularFire = false,
private _isUsingTanStack = false
) {
if (transportOptions) {
if (typeof transportOptions.port === 'number') {
Expand Down Expand Up @@ -180,7 +183,10 @@ export class RESTTransport implements DataConnectTransport {
this.appId,
this._accessToken,
this._appCheckToken,
this._isUsingGen
this._isUsingGen,
this._isUsingTanStack,
this._isUsingReact,
this._isUsingAngularFire
)
);
return withAuth;
Expand All @@ -205,7 +211,10 @@ export class RESTTransport implements DataConnectTransport {
this.appId,
this._accessToken,
this._appCheckToken,
this._isUsingGen
this._isUsingGen,
this._isUsingTanStack,
this._isUsingReact,
this._isUsingAngularFire
);
});
return taskResult;
Expand Down
6 changes: 6 additions & 0 deletions packages/data-connect/test/unit/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ describe('fetch', () => {
null,
null,
null,
false,
false,
false,
false
)
).to.eventually.be.rejectedWith(message);
Expand All @@ -74,6 +77,9 @@ describe('fetch', () => {
null,
null,
null,
false,
false,
false,
false
)
).to.eventually.be.rejectedWith(JSON.stringify(json));
Expand Down

0 comments on commit cdf7b39

Please sign in to comment.