-
Notifications
You must be signed in to change notification settings - Fork 59
chore: improve V2 callable typing #266
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR loosens the typing of V2 callable functions by allowing callers to pass a deep partial of the CallableRequest
, which can help in test scenarios or incremental data construction.
- Changed the
WrappedV2CallableFunction
parameter from a fullCallableRequest
to aDeepPartial<CallableRequest>
. - Adjusts type safety to be more flexible for users constructing request objects.
Comments suppressed due to low confidence (1)
src/v2.ts:38
- The change to accept partial requests should be covered by new or updated unit tests to ensure that missing properties are handled as expected by wrapped functions.
data: DeepPartial<CallableRequest>
export type WrappedV2CallableFunction<T> = ( | ||
data: CallableRequest | ||
data: DeepPartial<CallableRequest> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using DeepPartial<CallableRequest>
may allow invalid or incomplete payloads at runtime. Consider documenting which fields remain required or introduce a narrower helper type for testing vs. production usage.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this either, see for example:
import { wrapV2 } from 'firebase-functions-test/lib/v2';
import { onCall } from 'firebase-functions/v2/https';
interface HelloResult { greeting: string; }
const stub = onCall<{}, HelloResult>((req) => {
console.log('HTTP method:', req.rawRequest.method);
return { greeting: `Hello!` };
});
// 2) Wrap it
const invoke = wrapV2(stub);
// 3) This should be a compile‐time error, but compiles fine under the PR:
invoke({
data: {}, // TS should complain: “Property 'rawRequest' is missing” etc
});
The change makes this fine according to the typescript compilation, but at runtime of the test we'll get an error.
I feel like the library should build some defaults?
like here in v1 for event context:
https://github.com/firebase/firebase-functions-test/blob/master/src/v1.ts#L141
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great call. Instead of loosening the type, maybe we just need to provide sensible default so that user doesn't have to manually construct and pass callable context when invoking their v2 callable fn.
export type WrappedV2CallableFunction<T> = ( | ||
data: CallableRequest | ||
data: DeepPartial<CallableRequest> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this either, see for example:
import { wrapV2 } from 'firebase-functions-test/lib/v2';
import { onCall } from 'firebase-functions/v2/https';
interface HelloResult { greeting: string; }
const stub = onCall<{}, HelloResult>((req) => {
console.log('HTTP method:', req.rawRequest.method);
return { greeting: `Hello!` };
});
// 2) Wrap it
const invoke = wrapV2(stub);
// 3) This should be a compile‐time error, but compiles fine under the PR:
invoke({
data: {}, // TS should complain: “Property 'rawRequest' is missing” etc
});
The change makes this fine according to the typescript compilation, but at runtime of the test we'll get an error.
I feel like the library should build some defaults?
like here in v1 for event context:
https://github.com/firebase/firebase-functions-test/blob/master/src/v1.ts#L141
Resolves #260
Resolves #257