Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CorieW
Copy link
Member

@CorieW CorieW commented May 13, 2025

Resolves #260
Resolves #257

@cabljac cabljac requested a review from Copilot May 19, 2025 08:58
Copy link

@Copilot Copilot AI left a 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 full CallableRequest to a DeepPartial<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>

Comment on lines 37 to +38
export type WrappedV2CallableFunction<T> = (
data: CallableRequest
data: DeepPartial<CallableRequest>
Copy link
Preview

Copilot AI May 19, 2025

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.

Copy link
Contributor

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

Copy link
Contributor

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.

Comment on lines 37 to +38
export type WrappedV2CallableFunction<T> = (
data: CallableRequest
data: DeepPartial<CallableRequest>
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants