Skip to content

Commit 40a8c0b

Browse files
committed
redesign framework/gen flags into CallerSdkType enum
1 parent 6f27cbd commit 40a8c0b

File tree

6 files changed

+61
-112
lines changed

6 files changed

+61
-112
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import { FirebaseError } from '@firebase/util';
1111
import { LogLevelString } from '@firebase/logger';
1212
import { Provider } from '@firebase/component';
1313

14+
// @public
15+
export type CallerSdkType = 'Base' | 'Generated' | 'TanstackReactCore' | 'GeneratedReact' | 'TanstackAngularCore' | 'GeneratedAngular';
16+
17+
// @public (undocumented)
18+
export const CallerSdkTypeEnum: Readonly<{
19+
readonly Base: "Base";
20+
readonly Generated: "Generated";
21+
readonly TanstackReactCore: "TanstackReactCore";
22+
readonly GeneratedReact: "GeneratedReact";
23+
readonly TanstackAngularCore: "TanstackAngularCore";
24+
readonly GeneratedAngular: "GeneratedAngular";
25+
}>;
26+
1427
// @public
1528
export function connectDataConnectEmulator(dc: DataConnect, host: string, port?: number, sslEnabled?: boolean): void;
1629

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

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from '../core/FirebaseAuthProvider';
3434
import { QueryManager } from '../core/QueryManager';
3535
import { logDebug, logError } from '../logger';
36-
import { DataConnectTransport, TransportClass } from '../network';
36+
import { CallerSdkType, CallerSdkTypeEnum, DataConnectTransport, TransportClass } from '../network';
3737
import { RESTTransport } from '../network/transport/rest';
3838

3939
import { MutationManager } from './Mutation';
@@ -91,10 +91,7 @@ export class DataConnect {
9191
private _transportClass: TransportClass | undefined;
9292
private _transportOptions?: TransportOptions;
9393
private _authTokenProvider?: AuthTokenProvider;
94-
_isUsingGeneratedSdk: boolean = false;
95-
_isUsingTanStackSdk: boolean = false;
96-
_isUsingReactSdk: boolean = false;
97-
_isUsingAngularSdk: boolean = false;
94+
_callerSdkType: CallerSdkType = CallerSdkTypeEnum.Base;
9895
private _appCheckTokenProvider?: AppCheckTokenProvider;
9996
// @internal
10097
constructor(
@@ -114,37 +111,8 @@ export class DataConnect {
114111
}
115112
}
116113
// @internal
117-
_useGeneratedSdk(): void {
118-
if (!this._isUsingGeneratedSdk) {
119-
this._isUsingGeneratedSdk = true;
120-
}
121-
}
122-
// @internal
123-
_useTanStackSdk(): void {
124-
if (!this._isUsingTanStackSdk) {
125-
this._isUsingTanStackSdk = true;
126-
}
127-
if (this._transport) {
128-
this._transport._useTanStack();
129-
}
130-
}
131-
// @internal
132-
_useReactSdk(): void {
133-
if (!this._isUsingReactSdk) {
134-
this._isUsingReactSdk = true;
135-
}
136-
if (this._transport) {
137-
this._transport._useReact();
138-
}
139-
}
140-
// @internal
141-
_useAngularSdk(): void {
142-
if (!this._isUsingAngularSdk) {
143-
this._isUsingAngularSdk = true;
144-
}
145-
if (this._transport) {
146-
this._transport._useAngular();
147-
}
114+
_setCallerSdkType(callerSdkType: CallerSdkType): void {
115+
this._callerSdkType = callerSdkType;
148116
}
149117
_delete(): Promise<void> {
150118
_removeServiceInstance(
@@ -194,10 +162,7 @@ export class DataConnect {
194162
this._authTokenProvider,
195163
this._appCheckTokenProvider,
196164
undefined,
197-
this._isUsingGeneratedSdk,
198-
this._isUsingTanStackSdk,
199-
this._isUsingReactSdk,
200-
this._isUsingAngularSdk
165+
this._callerSdkType
201166
);
202167
if (this._transportOptions) {
203168
this._transport.useEmulator(

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

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,23 @@ import { Code, DataConnectError } from '../core/error';
1919
import { SDK_VERSION } from '../core/version';
2020
import { logDebug, logError } from '../logger';
2121

22+
import { CallerSdkType, CallerSdkTypeEnum } from './transport';
23+
2224
let connectFetch: typeof fetch | null = globalThis.fetch;
2325
export function initializeFetch(fetchImpl: typeof fetch): void {
2426
connectFetch = fetchImpl;
2527
}
26-
function getGoogApiClientValue(_isUsingGen: boolean): string {
28+
function getGoogApiClientValue(_callerSdkType: CallerSdkType): string {
2729
let str = 'gl-js/ fire/' + SDK_VERSION;
28-
if (_isUsingGen) {
30+
if (_callerSdkType !== CallerSdkTypeEnum.Base) {
2931
str += ' js/gen';
3032
}
3133
return str;
3234
}
3335
function getWebFrameworkValue(
34-
_isUsingTanStack: boolean,
35-
_isUsingReact: boolean,
36-
_isUsingAngular: boolean
36+
_callerSdkType: CallerSdkType
3737
): string {
38-
let str = '';
39-
if (_isUsingTanStack) {
40-
str += ' tanstack/';
41-
}
42-
if (_isUsingReact) {
43-
str += ' react/';
44-
}
45-
if (_isUsingAngular) {
46-
str += ' angular/';
47-
}
48-
// no framework SDK used
49-
if (str === '') {
50-
str = 'vanilla/';
51-
}
52-
return str.trim();
38+
return _callerSdkType + "/";
5339
}
5440
export interface DataConnectFetchBody<T> {
5541
name: string;
@@ -63,22 +49,16 @@ export function dcFetch<T, U>(
6349
appId: string | null,
6450
accessToken: string | null,
6551
appCheckToken: string | null,
66-
_isUsingGen: boolean,
67-
_isUsingTanStack: boolean,
68-
_isUsingReact: boolean,
69-
_isUsingAngular: boolean
52+
_callerSdkType: CallerSdkType
7053
): Promise<{ data: T; errors: Error[] }> {
7154
if (!connectFetch) {
7255
throw new DataConnectError(Code.OTHER, 'No Fetch Implementation detected!');
7356
}
7457
const headers: HeadersInit = {
7558
'Content-Type': 'application/json',
76-
'X-Goog-Api-Client': getGoogApiClientValue(_isUsingGen),
77-
'X-Firebase-DataConnect-Web-Frameworks': getWebFrameworkValue(
78-
_isUsingTanStack,
79-
_isUsingReact,
80-
_isUsingAngular
81-
)
59+
'X-Goog-Api-Client': getGoogApiClientValue(_callerSdkType),
60+
'X-Firebase-DataConnect-Web-Frameworks':
61+
getWebFrameworkValue(_callerSdkType)
8262
};
8363
if (accessToken) {
8464
headers['X-Firebase-Auth-Token'] = accessToken;

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ import { DataConnectOptions, TransportOptions } from '../../api/DataConnect';
1919
import { AppCheckTokenProvider } from '../../core/AppCheckTokenProvider';
2020
import { AuthTokenProvider } from '../../core/FirebaseAuthProvider';
2121

22+
/**
23+
* enum representing different flavors of the SDK used by developers
24+
* use the CallerSdkType for type-checking, and the CallerSdkTypeEnum for value-checking/assigning
25+
*/
26+
export type CallerSdkType =
27+
| 'Base' // Core JS SDK
28+
| 'Generated' // Generated JS SDK
29+
| 'TanstackReactCore' // Tanstack non-generated React SDK
30+
| 'GeneratedReact' // Generated React SDK
31+
| 'TanstackAngularCore' // Tanstack non-generated Angular SDK
32+
| 'GeneratedAngular'; // Generated Angular SDK
33+
export const CallerSdkTypeEnum = Object.freeze({
34+
Base: 'Base', // Core JS SDK
35+
Generated: 'Generated', // Generated JS SDK
36+
TanstackReactCore: 'TanstackReactCore', // Tanstack non-generated React SDK
37+
GeneratedReact: 'GeneratedReact', // Tanstack non-generated Angular SDK
38+
TanstackAngularCore: 'TanstackAngularCore', // Tanstack non-generated Angular SDK
39+
GeneratedAngular: 'GeneratedAngular' // Generated Angular SDK
40+
} as const);
41+
2242
/**
2343
* @internal
2444
*/
@@ -33,9 +53,8 @@ export interface DataConnectTransport {
3353
): Promise<{ data: T; errors: Error[] }>;
3454
useEmulator(host: string, port?: number, sslEnabled?: boolean): void;
3555
onTokenChanged: (token: string | null) => void;
36-
_useTanStack(): void;
37-
_useReact(): void;
38-
_useAngular(): void;
56+
// @internal
57+
_setCallerSdkType(callerSdkType: CallerSdkType): void;
3958
}
4059

4160
/**
@@ -48,8 +67,5 @@ export type TransportClass = new (
4867
authProvider?: AuthTokenProvider,
4968
appCheckProvider?: AppCheckTokenProvider,
5069
transportOptions?: TransportOptions,
51-
_isUsingGen?: boolean,
52-
_isUsingTanStack?: boolean,
53-
_isUsingReact?: boolean,
54-
_isUsingAngular?: boolean
70+
_callerSdkType?: CallerSdkType
5571
) => DataConnectTransport;

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

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { logDebug } from '../../logger';
2323
import { addToken, urlBuilder } from '../../util/url';
2424
import { dcFetch } from '../fetch';
2525

26-
import { DataConnectTransport } from '.';
26+
import { CallerSdkType, CallerSdkTypeEnum, DataConnectTransport } from '.';
2727

2828
export class RESTTransport implements DataConnectTransport {
2929
private _host = '';
@@ -43,10 +43,7 @@ export class RESTTransport implements DataConnectTransport {
4343
private authProvider?: AuthTokenProvider | undefined,
4444
private appCheckProvider?: AppCheckTokenProvider | undefined,
4545
transportOptions?: TransportOptions | undefined,
46-
private _isUsingGen = false,
47-
private _isUsingReact = false,
48-
private _isUsingAngular = false,
49-
private _isUsingTanStack = false
46+
private _callerSdkType: CallerSdkType = CallerSdkTypeEnum.Base,
5047
) {
5148
if (transportOptions) {
5249
if (typeof transportOptions.port === 'number') {
@@ -183,10 +180,7 @@ export class RESTTransport implements DataConnectTransport {
183180
this.appId,
184181
this._accessToken,
185182
this._appCheckToken,
186-
this._isUsingGen,
187-
this._isUsingTanStack,
188-
this._isUsingReact,
189-
this._isUsingAngular
183+
this._callerSdkType
190184
)
191185
);
192186
return withAuth;
@@ -211,28 +205,14 @@ export class RESTTransport implements DataConnectTransport {
211205
this.appId,
212206
this._accessToken,
213207
this._appCheckToken,
214-
this._isUsingGen,
215-
this._isUsingTanStack,
216-
this._isUsingReact,
217-
this._isUsingAngular
208+
this._callerSdkType
218209
);
219210
});
220211
return taskResult;
221212
};
222213

223-
_useTanStack(): void {
224-
if (!this._isUsingTanStack) {
225-
this._isUsingTanStack = true;
226-
}
227-
}
228-
_useReact(): void {
229-
if (!this._isUsingReact) {
230-
this._isUsingReact = true;
231-
}
232-
}
233-
_useAngular(): void {
234-
if (!this._isUsingAngular) {
235-
this._isUsingAngular = true;
236-
}
214+
// @internal
215+
_setCallerSdkType(callerSdkType: CallerSdkType): void {
216+
this._callerSdkType = callerSdkType;
237217
}
238218
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import chaiAsPromised from 'chai-as-promised';
2020
import * as sinon from 'sinon';
2121

2222
import { dcFetch, initializeFetch } from '../../src/network/fetch';
23+
import { CallerSdkTypeEnum } from '../../src/network/transport';
2324
use(chaiAsPromised);
2425
function mockFetch(json: object): void {
2526
const fakeFetchImpl = sinon.stub().returns(
@@ -51,10 +52,7 @@ describe('fetch', () => {
5152
null,
5253
null,
5354
null,
54-
false,
55-
false,
56-
false,
57-
false
55+
CallerSdkTypeEnum.Base
5856
)
5957
).to.eventually.be.rejectedWith(message);
6058
});
@@ -77,10 +75,7 @@ describe('fetch', () => {
7775
null,
7876
null,
7977
null,
80-
false,
81-
false,
82-
false,
83-
false
78+
CallerSdkTypeEnum.Base
8479
)
8580
).to.eventually.be.rejectedWith(JSON.stringify(json));
8681
});

0 commit comments

Comments
 (0)