Skip to content

Commit fd61f1a

Browse files
committed
add invalidateAdminArgs to handle variadic JS executeOperation arguments
1 parent 5721657 commit fd61f1a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/data-connect/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export {
3939
export {
4040
DataConnect,
4141
} from './data-connect'
42+
export {
43+
validateAdminArgs
44+
} from './validateAdminArgs'
4245

4346
/**
4447
* Gets the {@link DataConnect} service with the provided connector configuration
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*!
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { getDataConnect } from './index';
19+
import { DataConnect } from './data-connect';
20+
import { ConnectorConfig, OperationOptions } from './data-connect-api';
21+
import { DATA_CONNECT_ERROR_CODE_MAPPING, FirebaseDataConnectError } from './data-connect-api-client-internal';
22+
23+
24+
interface ParsedAdminArgs<Variables> {
25+
dc: DataConnect;
26+
vars: Variables;
27+
options: OperationOptions
28+
}
29+
30+
/**
31+
* The generated Admin SDK will allow the user to pass in variables, a Data Connect
32+
* instance, or operation options. The only required argument is the variables,
33+
* which are only required when the operation has at least one required variable.
34+
* Otherwise, all arguments are optional. This function validates the variables
35+
* and returns back the DataConnect instance, variables, and options based on the
36+
* arguments passed in.
37+
* @param connectorConfig DataConnect connector config
38+
* @param dcOrVarsOrOptions the first argument provided to a generated admin function
39+
* @param varsOrOptions the second argument provided to a generated admin function
40+
* @param options the third argument provided to a generated admin function
41+
* @param hasVars boolean parameter indicating whether the operation has variables
42+
* @param validateVars boolean parameter indicating whether we should expect to find a value for realVars
43+
* @returns parsed DataConnect, Variables, and Options for the operation
44+
* @internal
45+
*/
46+
export function validateAdminArgs<Variables extends object>(
47+
connectorConfig: ConnectorConfig,
48+
dcOrVarsOrOptions?: DataConnect | Variables | OperationOptions,
49+
varsOrOptions?: Variables | OperationOptions,
50+
options?: OperationOptions,
51+
hasVars?: boolean,
52+
validateVars?: boolean,
53+
): ParsedAdminArgs<Variables> {
54+
let dcInstance: DataConnect;
55+
let realVars: Variables;
56+
let realOptions: OperationOptions;
57+
58+
if (dcOrVarsOrOptions && 'connectorConfig' in dcOrVarsOrOptions) {
59+
dcInstance = dcOrVarsOrOptions as DataConnect;
60+
if (hasVars) {
61+
realVars = varsOrOptions as Variables;
62+
realOptions = options as OperationOptions;
63+
} else {
64+
realVars = undefined as unknown as Variables;
65+
realOptions = varsOrOptions as OperationOptions;
66+
}
67+
} else {
68+
dcInstance = getDataConnect(connectorConfig);
69+
if (hasVars) {
70+
realVars = dcOrVarsOrOptions as Variables;
71+
realOptions = varsOrOptions as OperationOptions;
72+
} else {
73+
realVars = undefined as unknown as Variables;
74+
realOptions = dcOrVarsOrOptions as OperationOptions;
75+
}
76+
}
77+
78+
if (!dcInstance || (!realVars && validateVars)) {
79+
throw new FirebaseDataConnectError(DATA_CONNECT_ERROR_CODE_MAPPING.INVALID_ARGUMENT, 'Variables required.');
80+
}
81+
return { dc: dcInstance, vars: realVars, options: realOptions };
82+
}

0 commit comments

Comments
 (0)