Skip to content

Commit 9f1ec56

Browse files
committed
Add generics to httpsCallable
1 parent 938dc1a commit 9f1ec56

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

common/api-review/functions-exp.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function getFunctions(app: FirebaseApp, regionOrCustomDomain?: string): F
2424
export { HttpsCallable }
2525

2626
// @public
27-
export function httpsCallable(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable;
27+
export function httpsCallable<RequestParams = unknown, ResponseData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable;
2828

2929
export { HttpsCallableOptions }
3030

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export function getFunctions(
7171
*
7272
* Note: this must be called before this instance has been used to do any operations.
7373
*
74-
* @param host The emulator host (ex: localhost)
75-
* @param port The emulator port (ex: 5001)
74+
* @param host - The emulator host (ex: localhost)
75+
* @param port - The emulator port (ex: 5001)
7676
* @public
7777
*/
7878
export function useFunctionsEmulator(
@@ -88,10 +88,14 @@ export function useFunctionsEmulator(
8888
* @param name - The name of the trigger.
8989
* @public
9090
*/
91-
export function httpsCallable(
91+
export function httpsCallable<RequestParams = unknown, ResponseData = unknown>(
9292
functionsInstance: Functions,
9393
name: string,
9494
options?: HttpsCallableOptions
9595
): HttpsCallable {
96-
return _httpsCallable(functionsInstance as FunctionsService, name, options);
96+
return _httpsCallable<RequestParams, ResponseData>(
97+
functionsInstance as FunctionsService,
98+
name,
99+
options
100+
);
97101
}

packages-exp/functions-exp/src/callable.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ describe('Firebase Functions > Call', () => {
8686
null: null
8787
};
8888

89-
const func = httpsCallable(functions, 'dataTest');
89+
const func = httpsCallable<
90+
Record<string, any>,
91+
{ message: string; code: number; long: number }
92+
>(functions, 'dataTest');
9093
const result = await func(data);
9194

9295
expect(result.data).to.deep.equal({
@@ -98,7 +101,7 @@ describe('Firebase Functions > Call', () => {
98101

99102
it('scalars', async () => {
100103
const functions = createTestService(app, region);
101-
const func = httpsCallable(functions, 'scalarTest');
104+
const func = httpsCallable<number, number>(functions, 'scalarTest');
102105
const result = await func(17);
103106
expect(result.data).to.equal(76);
104107
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ export function useFunctionsEmulator(
154154
* @param name - The name of the trigger.
155155
* @public
156156
*/
157-
export function httpsCallable(
157+
export function httpsCallable<RequestParams, ResponseData>(
158158
functionsInstance: FunctionsService,
159159
name: string,
160160
options?: HttpsCallableOptions
161-
): HttpsCallable {
161+
): HttpsCallable<RequestParams, ResponseData> {
162162
return data => {
163163
return call(functionsInstance, name, data, options || {});
164164
};

packages-exp/functions-types-exp/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import { FirebaseError } from '@firebase/util';
2020
/**
2121
* An HttpsCallableResult wraps a single result from a function call.
2222
*/
23-
export interface HttpsCallableResult {
24-
readonly data: any;
23+
export interface HttpsCallableResult<ResponseData = any> {
24+
readonly data: ResponseData;
2525
}
2626

2727
/**
2828
* An HttpsCallable is a reference to a "callable" http trigger in
2929
* Google Cloud Functions.
3030
*/
31-
export interface HttpsCallable {
32-
(data?: {} | null): Promise<HttpsCallableResult>;
31+
export interface HttpsCallable<RequestParams = any, ResponseData = any> {
32+
(data?: RequestParams | null): Promise<HttpsCallableResult<ResponseData>>;
3333
}
3434

3535
/**

0 commit comments

Comments
 (0)