Skip to content

Commit 04b4986

Browse files
authored
Add generics to httpsCallable (#4466)
1 parent 43d7520 commit 04b4986

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface Functions {
1414
region: string;
1515
}
1616

17-
// @public (undocumented)
17+
// @public
1818
export interface FunctionsError extends FirebaseError {
1919
readonly code: FunctionsErrorCode;
2020
readonly details?: unknown;
@@ -27,13 +27,13 @@ export type FunctionsErrorCode = 'ok' | 'cancelled' | 'unknown' | 'invalid-argum
2727
export function getFunctions(app: FirebaseApp, regionOrCustomDomain?: string): Functions;
2828

2929
// @public
30-
export interface HttpsCallable {
30+
export interface HttpsCallable<RequestData = unknown, ResponseData = unknown> {
3131
// (undocumented)
32-
(data?: {} | null): Promise<HttpsCallableResult>;
32+
(data?: RequestData | null): Promise<HttpsCallableResult<ResponseData>>;
3333
}
3434

3535
// @public
36-
export function httpsCallable(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable;
36+
export function httpsCallable<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
3737

3838
// @public
3939
export interface HttpsCallableOptions {
@@ -42,9 +42,9 @@ export interface HttpsCallableOptions {
4242
}
4343

4444
// @public
45-
export interface HttpsCallableResult {
45+
export interface HttpsCallableResult<ResponseData = unknown> {
4646
// (undocumented)
47-
readonly data: unknown;
47+
readonly data: ResponseData;
4848
}
4949

5050
// @public

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export function getFunctions(
5757
*
5858
* Note: this must be called before this instance has been used to do any operations.
5959
*
60-
* @param host The emulator host (ex: localhost)
61-
* @param port The emulator port (ex: 5001)
60+
* @param host - The emulator host (ex: localhost)
61+
* @param port - The emulator port (ex: 5001)
6262
* @public
6363
*/
6464
export function useFunctionsEmulator(
@@ -74,10 +74,14 @@ export function useFunctionsEmulator(
7474
* @param name - The name of the trigger.
7575
* @public
7676
*/
77-
export function httpsCallable(
77+
export function httpsCallable<RequestData = unknown, ResponseData = unknown>(
7878
functionsInstance: Functions,
7979
name: string,
8080
options?: HttpsCallableOptions
81-
): HttpsCallable {
82-
return _httpsCallable(functionsInstance as FunctionsService, name, options);
81+
): HttpsCallable<RequestData, ResponseData> {
82+
return _httpsCallable<RequestData, ResponseData>(
83+
functionsInstance as FunctionsService,
84+
name,
85+
options
86+
);
8387
}

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/public-types.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@ import { FirebaseError } from '@firebase/util';
1919

2020
/**
2121
* An HttpsCallableResult wraps a single result from a function call.
22+
* @public
2223
*/
23-
export interface HttpsCallableResult {
24-
readonly data: unknown;
24+
export interface HttpsCallableResult<ResponseData = unknown> {
25+
readonly data: ResponseData;
2526
}
2627

2728
/**
2829
* An HttpsCallable is a reference to a "callable" http trigger in
2930
* Google Cloud Functions.
31+
* @public
3032
*/
31-
export interface HttpsCallable {
32-
(data?: {} | null): Promise<HttpsCallableResult>;
33+
export interface HttpsCallable<RequestData = unknown, ResponseData = unknown> {
34+
(data?: RequestData | null): Promise<HttpsCallableResult<ResponseData>>;
3335
}
3436

3537
/**
3638
* HttpsCallableOptions specify metadata about how calls should be executed.
39+
* @public
3740
*/
3841
export interface HttpsCallableOptions {
3942
timeout?: number; // in millis
@@ -42,6 +45,7 @@ export interface HttpsCallableOptions {
4245
/**
4346
* `Functions` represents a Functions instance, and is a required argument for
4447
* all Functions operations.
48+
* @public
4549
*/
4650
export interface Functions {
4751
/**
@@ -100,6 +104,7 @@ export interface Functions {
100104
* - 'data-loss': Unrecoverable data loss or corruption.
101105
* - 'unauthenticated': The request does not have valid authentication
102106
* credentials for the operation.
107+
* @public
103108
*/
104109
export type FunctionsErrorCode =
105110
| 'ok'
@@ -120,6 +125,10 @@ export type FunctionsErrorCode =
120125
| 'data-loss'
121126
| 'unauthenticated';
122127

128+
/**
129+
* An error returned by the Firebase Functions client SDK.
130+
* @public
131+
*/
123132
export interface FunctionsError extends FirebaseError {
124133
/**
125134
* A standard error code that will be returned to the client. This also

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

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

167167
/**

0 commit comments

Comments
 (0)