-
Notifications
You must be signed in to change notification settings - Fork 71
feat(react/fdc) add validateReactArgs for parsing arguments to variadic generated SDK query hook function signatures
#174
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
Merged
Ehesp
merged 12 commits into
invertase:main
from
stephenarosaj:stephenarosaj/validateargs
Mar 25, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
75f723d
feat(react/fdc) add for parsing arguments to variadic generated SDK …
stephenarosaj 744afef
add unit tests for validateReactArgs
stephenarosaj 7b57607
update unit tests for validateReactArgs
stephenarosaj 9b23229
remove some changes unintentionally added to this PR, fix formatting
stephenarosaj 7381716
remove some changes unintentionally added to this PR
stephenarosaj 1b579a3
remove some changes unintentionally added to this PR
stephenarosaj 414a219
remove changes to pnpm-lock.yaml unintentionally added to this PR
stephenarosaj 8064916
remove changes to pnpm-lock.yaml unintentionally added to this PR
stephenarosaj 4e89404
address review comments - collapse expect checks in testing, create n…
stephenarosaj f5e5670
run VSCode file formatter
stephenarosaj 3c5a5da
Merge branch 'main' into stephenarosaj/validateargs
stephenarosaj f546c8b
Merge branch 'main' into stephenarosaj/validateargs
Ehesp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
packages/react/src/data-connect/validateReactArgs.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { validateReactArgs } from "./validateReactArgs"; | ||
| import { connectorConfig } from "@/dataconnect/default-connector"; | ||
| import { getDataConnect } from "firebase/data-connect"; | ||
| import { firebaseApp } from "~/testing-utils"; | ||
|
|
||
| // initialize firebase app | ||
| firebaseApp; | ||
|
|
||
| describe("validateReactArgs", () => { | ||
| const dataConnect = getDataConnect(connectorConfig); | ||
|
|
||
| const emptyObjectVars = {}; | ||
| const nonEmptyVars = { limit: 5 }; | ||
|
|
||
| const options = { meta: { hasOptions: true } }; | ||
|
|
||
| test.each([ | ||
| { | ||
| argsDescription: "no args are provided", | ||
| dcOrOptions: undefined, | ||
| options: undefined, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only dataconnect is provided", | ||
| dcOrOptions: dataConnect, | ||
| options: undefined, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only options are provided", | ||
| dcOrOptions: options, | ||
| options: undefined, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: options, | ||
| }, | ||
| { | ||
| argsDescription: "dataconnect and options are provided", | ||
| dcOrOptions: dataConnect, | ||
| options: options, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: options, | ||
| }, | ||
| ])( | ||
| "parses args correctly when $argsDescription for an operation with no variables", | ||
| ({ dcOrOptions, options, expectedInputVars, expectedInputOpts }) => { | ||
| const { | ||
| dc: dcInstance, | ||
| vars: inputVars, | ||
| options: inputOpts, | ||
| } = validateReactArgs( | ||
| connectorConfig, | ||
| dcOrOptions, | ||
| options | ||
| // hasVars = undefined (false-y) | ||
| // validateArgs = undefined (false-y) | ||
| ); | ||
|
|
||
| expect(dcInstance).toBe(dataConnect); | ||
| expect(inputVars).toBe(expectedInputVars); | ||
| expect(inputOpts).toBe(expectedInputOpts); | ||
| } | ||
| ); | ||
|
|
||
| test.each([ | ||
| { | ||
| argsDescription: "no args are provided", | ||
| dcOrVarsOrOptions: undefined, | ||
| varsOrOptions: undefined, | ||
| options: undefined, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only dataconnect is provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: undefined, | ||
| options: undefined, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only an empty vars object is provided", | ||
| dcOrVarsOrOptions: emptyObjectVars, | ||
| varsOrOptions: undefined, | ||
| options: undefined, | ||
| expectedInputVars: emptyObjectVars, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only vars are provided", | ||
| dcOrVarsOrOptions: nonEmptyVars, | ||
| varsOrOptions: undefined, | ||
| options: undefined, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only options are provided", | ||
| dcOrVarsOrOptions: undefined, | ||
| varsOrOptions: options, | ||
| options: undefined, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: options, | ||
| }, | ||
| { | ||
| argsDescription: "dataconnect and vars are provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: nonEmptyVars, | ||
| options: undefined, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "dataconnect and options are provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: undefined, | ||
| options: options, | ||
| expectedInputVars: undefined, | ||
| expectedInputOpts: options, | ||
| }, | ||
| { | ||
| argsDescription: "dataconnect and vars and options are provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: nonEmptyVars, | ||
| options: options, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: options, | ||
| }, | ||
| ])( | ||
| "parses args correctly when $argsDescription for an operation with all optional variables", | ||
| ({ | ||
| dcOrVarsOrOptions, | ||
| varsOrOptions, | ||
| options, | ||
| expectedInputVars, | ||
| expectedInputOpts, | ||
| }) => { | ||
| const { | ||
| dc: dcInstance, | ||
| vars: inputVars, | ||
| options: inputOpts, | ||
| } = validateReactArgs( | ||
| connectorConfig, | ||
| dcOrVarsOrOptions, | ||
| varsOrOptions, | ||
| options, | ||
| true, // hasVars = true | ||
| false // validateArgs = false | ||
| ); | ||
|
|
||
| expect(dcInstance).toBe(dataConnect); | ||
| expect(inputVars).toBe(expectedInputVars); | ||
| expect(inputOpts).toBe(expectedInputOpts); | ||
| } | ||
| ); | ||
|
|
||
| test.each([ | ||
| { | ||
| argsDescription: "only vars are provided", | ||
| dcOrVarsOrOptions: nonEmptyVars, | ||
| varsOrOptions: undefined, | ||
| options: undefined, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "dataconnect and vars are provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: nonEmptyVars, | ||
| options: undefined, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "vars and options are provided", | ||
| dcOrVarsOrOptions: nonEmptyVars, | ||
| varsOrOptions: options, | ||
| options: undefined, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: options, | ||
| }, | ||
| { | ||
| argsDescription: "dataconnect and vars and options are provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: nonEmptyVars, | ||
| options: options, | ||
| expectedInputVars: nonEmptyVars, | ||
| expectedInputOpts: options, | ||
| }, | ||
| ])( | ||
| "parses args correctly when $argsDescription for an operation with any required variables", | ||
| ({ | ||
| dcOrVarsOrOptions, | ||
| varsOrOptions, | ||
| options, | ||
| expectedInputVars, | ||
| expectedInputOpts, | ||
| }) => { | ||
| const { | ||
| dc: dcInstance, | ||
| vars: inputVars, | ||
| options: inputOpts, | ||
| } = validateReactArgs( | ||
| connectorConfig, | ||
| dcOrVarsOrOptions, | ||
| varsOrOptions, | ||
| options, | ||
| true, // hasVars = true | ||
| true // validateArgs = true | ||
| ); | ||
|
|
||
| expect(dcInstance).toBe(dataConnect); | ||
| expect(inputVars).toBe(expectedInputVars); | ||
| expect(inputOpts).toBe(expectedInputOpts); | ||
| } | ||
| ); | ||
|
|
||
| test.each([ | ||
| { | ||
| argsDescription: "only dataconnect is provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: undefined, | ||
| options: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only options are provided", | ||
| dcOrVarsOrOptions: undefined, | ||
| varsOrOptions: options, | ||
| options: undefined, | ||
| }, | ||
| { | ||
| argsDescription: "only dataconnect and options are provided", | ||
| dcOrVarsOrOptions: dataConnect, | ||
| varsOrOptions: undefined, | ||
| options: options, | ||
| }, | ||
| ])( | ||
| "throws error when $argsDescription for an operation with any required variables", | ||
| ({ dcOrVarsOrOptions, varsOrOptions, options }) => { | ||
| expect(() => { | ||
| validateReactArgs( | ||
| connectorConfig, | ||
| dcOrVarsOrOptions, | ||
| varsOrOptions, | ||
| options, | ||
| true, // hasVars = true | ||
| true // validateArgs = true | ||
| ); | ||
| }).toThrowError("invalid-argument: Variables required."); | ||
| } | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { | ||
| ConnectorConfig, | ||
| DataConnect, | ||
| getDataConnect, | ||
| } from "firebase/data-connect"; | ||
| import { useDataConnectQueryOptions } from "./useDataConnectQuery"; | ||
|
|
||
| type DataConnectOptions = | ||
| | useDataConnectQueryOptions | ||
| | useDataConnectQueryOptions; | ||
|
|
||
| interface ParsedReactArgs<Variables> { | ||
| dc: DataConnect; | ||
| vars: Variables; | ||
| options: DataConnectOptions; | ||
| } | ||
|
|
||
| /** | ||
| * The generated React SDK will allow the user to pass in variables, a Data Connect instance, or operation options. | ||
| * The only required argument is the variables, which are only required when the operation has at least one required | ||
| * variable. Otherwise, all arguments are optional. This function validates the variables and returns back the DataConnect | ||
| * instance, variables, and options based on the arguments passed in. | ||
| * @param connectorConfig DataConnect connector config | ||
| * @param dcOrVarsOrOptions the first argument provided to a generated react function | ||
| * @param varsOrOptions the second argument provided to a generated react function | ||
| * @param options the third argument provided to a generated react function | ||
| * @param hasVars boolean parameter indicating whether the operation has variables | ||
| * @param validateVars boolean parameter indicating whether we should expect to find a value for realVars | ||
| * @returns parsed DataConnect, Variables, and Options for the operation | ||
| * @internal | ||
| */ | ||
| export function validateReactArgs<Variables extends object>( | ||
| connectorConfig: ConnectorConfig, | ||
| dcOrVarsOrOptions?: DataConnect | Variables | DataConnectOptions, | ||
| varsOrOptions?: Variables | DataConnectOptions, | ||
| options?: DataConnectOptions, | ||
| hasVars?: boolean, | ||
| validateVars?: boolean | ||
| ): ParsedReactArgs<Variables> { | ||
| let dcInstance: DataConnect; | ||
| let realVars: Variables; | ||
| let realOptions: DataConnectOptions; | ||
|
|
||
| if (dcOrVarsOrOptions && "enableEmulator" in dcOrVarsOrOptions) { | ||
| dcInstance = dcOrVarsOrOptions as DataConnect; | ||
| if (hasVars) { | ||
| realVars = varsOrOptions as Variables; | ||
| realOptions = options as DataConnectOptions; | ||
| } else { | ||
| realVars = undefined as unknown as Variables; | ||
| realOptions = varsOrOptions as DataConnectOptions; | ||
| } | ||
| } else { | ||
| dcInstance = getDataConnect(connectorConfig); | ||
| if (hasVars) { | ||
| realVars = dcOrVarsOrOptions as Variables; | ||
| realOptions = varsOrOptions as DataConnectOptions; | ||
| } else { | ||
| realVars = undefined as unknown as Variables; | ||
| realOptions = dcOrVarsOrOptions as DataConnectOptions; | ||
| } | ||
| } | ||
|
|
||
| if (!dcInstance || (!realVars && validateVars)) { | ||
| throw new Error("invalid-argument: Variables required."); // copied from firebase error codes | ||
| } | ||
| return { dc: dcInstance, vars: realVars, options: realOptions }; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.