Skip to content

Commit cd84a1c

Browse files
committed
Implement
1 parent c2de05f commit cd84a1c

File tree

6 files changed

+57
-21
lines changed

6 files changed

+57
-21
lines changed

packages-exp/functions-compat/src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,33 @@
1818
import firebase from '@firebase/app-compat';
1919
import { name, version } from '../package.json';
2020
import { registerFunctions } from './register';
21-
import * as types from '@firebase/functions-types';
22-
import { Functions as FunctionsServiceExp } from '@firebase/functions-exp';
21+
import { FirebaseFunctions as FunctionsCompat } from '@firebase/functions-types';
22+
import { Functions as FunctionsExp } from '@firebase/functions-exp';
2323

2424
declare module '@firebase/functions-exp' {
2525
export function httpsCallable<RequestData = unknown, ResponseData = unknown>(
26-
functionsInstance: types.FirebaseFunctions | FunctionsServiceExp,
26+
functionsInstance: FunctionsCompat | FunctionsExp,
2727
name: string,
2828
options?: HttpsCallableOptions
2929
): HttpsCallable<RequestData, ResponseData>;
30+
31+
export function useFunctionsEmulator(
32+
functionsInstance: FunctionsCompat | FunctionsExp,
33+
host: string,
34+
port: number
35+
): void;
3036
}
3137
registerFunctions();
3238
firebase.registerVersion(name, version);
3339

3440
declare module '@firebase/app-compat' {
3541
interface FirebaseNamespace {
3642
functions?: {
37-
(app?: FirebaseApp): types.FirebaseFunctions;
38-
Functions: typeof types.FirebaseFunctions;
43+
(app?: FirebaseApp): FunctionsCompat;
44+
Functions: typeof FunctionsCompat;
3945
};
4046
}
4147
interface FirebaseApp {
42-
functions?(regionOrCustomDomain?: string): types.FirebaseFunctions;
48+
functions?(regionOrCustomDomain?: string): FunctionsCompat;
4349
}
4450
}

packages-exp/functions-compat/src/service.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@ export class FunctionsService implements FirebaseFunctions {
3737
*/
3838
_customDomain: string | null;
3939

40-
constructor(
41-
public app: FirebaseApp,
42-
private _functionsInstance: FunctionsServiceExp
43-
) {
44-
this._region = this._functionsInstance.region;
45-
this._customDomain = this._functionsInstance.customDomain;
40+
constructor(public app: FirebaseApp, private _delegate: FunctionsServiceExp) {
41+
this._region = this._delegate.region;
42+
this._customDomain = this._delegate.customDomain;
4643
}
4744
httpsCallable(name: string, options?: HttpsCallableOptions): HttpsCallable {
48-
return httpsCallableExp(this._functionsInstance, name, options);
45+
return httpsCallableExp(this._delegate, name, options);
4946
}
5047
/**
5148
* Deprecated in pre-modularized repo, does not exist in modularized
@@ -67,13 +64,9 @@ export class FunctionsService implements FirebaseFunctions {
6764
'Port missing in origin provided to useFunctionsEmulator()'
6865
);
6966
}
70-
return useFunctionsEmulatorExp(
71-
this._functionsInstance,
72-
match[1],
73-
Number(match[2])
74-
);
67+
return useFunctionsEmulatorExp(this._delegate, match[1], Number(match[2]));
7568
}
7669
useEmulator(host: string, port: number): void {
77-
return useFunctionsEmulatorExp(this._functionsInstance, host, port);
70+
return useFunctionsEmulatorExp(this._delegate, host, port);
7871
}
7972
}

packages-exp/functions-exp/src/api.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
useFunctionsEmulator as _useFunctionsEmulator,
2727
httpsCallable as _httpsCallable
2828
} from './service';
29+
import { getExpInstance } from '@firebase/util';
2930

3031
export * from './public-types';
3132

@@ -66,7 +67,11 @@ export function useFunctionsEmulator(
6667
host: string,
6768
port: number
6869
): void {
69-
_useFunctionsEmulator(functionsInstance as FunctionsService, host, port);
70+
_useFunctionsEmulator(
71+
getExpInstance<FunctionsService>(functionsInstance as FunctionsService),
72+
host,
73+
port
74+
);
7075
}
7176

7277
/**
@@ -80,7 +85,7 @@ export function httpsCallable<RequestData = unknown, ResponseData = unknown>(
8085
options?: HttpsCallableOptions
8186
): HttpsCallable<RequestData, ResponseData> {
8287
return _httpsCallable<RequestData, ResponseData>(
83-
functionsInstance as FunctionsService,
88+
getExpInstance<FunctionsService>(functionsInstance as FunctionsService),
8489
name,
8590
options
8691
);

packages/util/index.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ export * from './src/validation';
3737
export * from './src/utf8';
3838
export * from './src/exponential_backoff';
3939
export * from './src/formatters';
40+
export * from './src/exp';

packages/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export * from './src/validation';
3232
export * from './src/utf8';
3333
export * from './src/exponential_backoff';
3434
export * from './src/formatters';
35+
export * from './src/exp';

packages/util/src/exp.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
interface CompatService<T> {
19+
_delegate?: T;
20+
}
21+
22+
export function getExpInstance<ExpService>(
23+
service: CompatService<ExpService> | ExpService
24+
): ExpService {
25+
if ((service as { [key: string]: unknown })._delegate) {
26+
return (service as { [key: string]: unknown })._delegate as ExpService;
27+
} else {
28+
return service as ExpService;
29+
}
30+
}

0 commit comments

Comments
 (0)