Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-safe callable functions with generics #1360

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/v1/providers/https.spec.ts
Expand Up @@ -115,7 +115,7 @@ describe("#onCall", () => {
});

it("has a .run method", () => {
const cf = https.onCall((d, c) => {
const cf = https.onCall<string, { data: string; context: https.CallableContext }>((d, c) => {
return { data: d, context: c };
});

Expand Down
4 changes: 2 additions & 2 deletions spec/v2/providers/https.spec.ts
Expand Up @@ -454,8 +454,8 @@ describe("onCall", () => {
https.onCall<string, string>(
(request: https.CallableRequest<string>) => `hello, ${request.data}!`
);
https.onCall<string>((request: https.CallableRequest<string>) => `hello, ${request.data}!`);
https.onCall<string>((request: https.CallableRequest) => `hello, ${request.data}!`);
https.onCall<string, string>((request) => `hello, ${request.data}!`);
https.onCall<string, string>((request) => `hello, ${request.data}!`);
https.onCall((request: https.CallableRequest<string>) => `Hello, ${request.data}`);
https.onCall((request: https.CallableRequest) => `Hello, ${request.data}`);
});
Expand Down
5 changes: 3 additions & 2 deletions src/v1/function-builder.ts
Expand Up @@ -340,8 +340,9 @@ export class FunctionBuilder {
* Declares a callable method for clients to call using a Firebase SDK.
* @param handler A method that takes a data and context and returns a value.
*/
onCall: (handler: (data: any, context: https.CallableContext) => any | Promise<any>) =>
https._onCallWithOptions(handler, this.options),
onCall: <TData = never, TReturn = never>(
handler: (data: TData, context: https.CallableContext) => TReturn | Promise<TReturn>
) => https._onCallWithOptions(handler, this.options),
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/v1/providers/https.ts
Expand Up @@ -51,9 +51,9 @@ export function onRequest(
* Declares a callable method for clients to call using a Firebase SDK.
* @param handler A method that takes a data and context and returns a value.
*/
export function onCall(
handler: (data: any, context: CallableContext) => any | Promise<any>
): HttpsFunction & Runnable<any> {
export function onCall<TData = never, TReturn = never>(
handler: (data: TData, context: CallableContext) => TReturn | Promise<TReturn>
): HttpsFunction & Runnable<TData> {
return _onCallWithOptions(handler, {});
}

Expand Down
26 changes: 13 additions & 13 deletions src/v2/providers/https.ts
Expand Up @@ -187,12 +187,12 @@ export type HttpsFunction = ((
/**
* Creates a callable method for clients to call using a Firebase SDK.
*/
export interface CallableFunction<T, Return> extends HttpsFunction {
export interface CallableFunction<T, TReturn> extends HttpsFunction {
/** Executes the handler function with the provided data as input. Used for unit testing.
* @param data - An input for the handler function.
* @returns The output of the handler function.
*/
run(data: CallableRequest<T>): Return;
run(data: CallableRequest<T>): TReturn;
}
/**
* Handles HTTPS requests.
Expand Down Expand Up @@ -299,26 +299,26 @@ export function onRequest(
* @param handler - A function that takes a {@link https.CallableRequest}.
* @returns A function that you can export and deploy.
*/
export function onCall<T = any, Return = any | Promise<any>>(
export function onCall<T = never, TReturn = never>(
opts: CallableOptions,
handler: (request: CallableRequest<T>) => Return
): CallableFunction<T, Return>;
handler: (request: CallableRequest<T>) => TReturn | Promise<TReturn>
): CallableFunction<T, TReturn | Promise<TReturn>>;
/**
* Declares a callable method for clients to call using a Firebase SDK.
* @param handler - A function that takes a {@link https.CallableRequest}.
* @returns A function that you can export and deploy.
*/
export function onCall<T = any, Return = any | Promise<any>>(
handler: (request: CallableRequest<T>) => Return
): CallableFunction<T, Return>;
export function onCall<T = any, Return = any | Promise<any>>(
optsOrHandler: CallableOptions | ((request: CallableRequest<T>) => Return),
handler?: (request: CallableRequest<T>) => Return
): CallableFunction<T, Return> {
export function onCall<T = never, TReturn = never>(
handler: (request: CallableRequest<T>) => TReturn | Promise<TReturn>
): CallableFunction<T, TReturn | Promise<TReturn>>;
export function onCall<T = never, TReturn = never>(
optsOrHandler: CallableOptions | ((request: CallableRequest<T>) => TReturn | Promise<TReturn>),
handler?: (request: CallableRequest<T>) => TReturn | Promise<TReturn>
): CallableFunction<T, TReturn | Promise<TReturn>> {
let opts: CallableOptions;
if (arguments.length === 1) {
opts = {};
handler = optsOrHandler as (request: CallableRequest<T>) => Return;
handler = optsOrHandler as (request: CallableRequest<T>) => TReturn | Promise<TReturn>;
} else {
opts = optsOrHandler as CallableOptions;
}
Expand Down