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